diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs index 993e685..d6b21b6 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/DatabaseTestHost.cs @@ -5,58 +5,58 @@ using Microsoft.EntityFrameworkCore; namespace DotNetCore.CAP.EntityFrameworkCore.Test { - public abstract class DatabaseTestHost : TestHost - { - private static bool _sqlObjectInstalled; + //public abstract class DatabaseTestHost : TestHost + //{ + // private static bool _sqlObjectInstalled; - protected override void PostBuildServices() - { - base.PostBuildServices(); - InitializeDatabase(); - } + // protected override void PostBuildServices() + // { + // base.PostBuildServices(); + // InitializeDatabase(); + // } - public override void Dispose() - { - DeleteAllData(); - base.Dispose(); - } + // public override void Dispose() + // { + // DeleteAllData(); + // base.Dispose(); + // } - private void InitializeDatabase() - { - if (!_sqlObjectInstalled) - { - using (CreateScope()) - { - var context = GetService(); - context.Database.EnsureDeleted(); - context.Database.Migrate(); - _sqlObjectInstalled = true; - } - } - } + // private void InitializeDatabase() + // { + // if (!_sqlObjectInstalled) + // { + // using (CreateScope()) + // { + // var context = GetService(); + // context.Database.EnsureDeleted(); + // context.Database.Migrate(); + // _sqlObjectInstalled = true; + // } + // } + // } - private void DeleteAllData() - { - using (CreateScope()) - { - var context = GetService(); + // private void DeleteAllData() + // { + // using (CreateScope()) + // { + // var context = GetService(); - var commands = new[] - { - "DISABLE TRIGGER ALL ON ?", - "ALTER TABLE ? NOCHECK CONSTRAINT ALL", - "DELETE FROM ?", - "ALTER TABLE ? CHECK CONSTRAINT ALL", - "ENABLE TRIGGER ALL ON ?" - }; - foreach (var command in commands) - { - context.Database.GetDbConnection().Execute( - "sp_MSforeachtable", - new {command1 = command}, - commandType: CommandType.StoredProcedure); - } - } - } - } + // var commands = new[] + // { + // "DISABLE TRIGGER ALL ON ?", + // "ALTER TABLE ? NOCHECK CONSTRAINT ALL", + // "DELETE FROM ?", + // "ALTER TABLE ? CHECK CONSTRAINT ALL", + // "ENABLE TRIGGER ALL ON ?" + // }; + // foreach (var command in commands) + // { + // context.Database.GetDbConnection().Execute( + // "sp_MSforeachtable", + // new {command1 = command}, + // commandType: CommandType.StoredProcedure); + // } + // } + // } + //} } \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs index 2897482..e59f393 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs @@ -1,175 +1,168 @@ -using System; -using System.Linq; -using DotNetCore.CAP.Infrastructure; -using DotNetCore.CAP.Models; -using Microsoft.Extensions.DependencyInjection; -using Xunit; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test +namespace DotNetCore.CAP.EntityFrameworkCore.Test { - public class EFMessageStoreTest : DatabaseTestHost - { - [Fact] - public void CanCreateSentMessageUsingEF() - { - using (var db = CreateContext()) - { - var guid = Guid.NewGuid().ToString(); - var message = new CapSentMessage - { - Id = guid, - Content = "this is message body", - StatusName = StatusName.Enqueued - }; - db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - db.SaveChanges(); - - Assert.True(db.CapSentMessages.Any(u => u.Id == guid)); - Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); - } - } - - [Fact] - public void CanUpdateSentMessageUsingEF() - { - using (var db = CreateContext()) - { - var guid = Guid.NewGuid().ToString(); - var message = new CapSentMessage - { - Id = guid, - Content = "this is message body", - StatusName = StatusName.Enqueued - }; - db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - db.SaveChanges(); - - var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - Assert.NotNull(selectedMessage); - - selectedMessage.StatusName = StatusName.Succeeded; - selectedMessage.Content = "Test"; - db.SaveChanges(); - - selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); - Assert.NotNull(selectedMessage); - Assert.True(selectedMessage.Content == "Test"); - } - } - - [Fact] - public void CanRemoveSentMessageUsingEF() - { - using (var db = CreateContext()) - { - var guid = Guid.NewGuid().ToString(); - var message = new CapSentMessage - { - Id = guid, - Content = "this is message body", - StatusName = StatusName.Enqueued - }; - db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - db.SaveChanges(); - - var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - Assert.NotNull(selectedMessage); - - db.CapSentMessages.Remove(selectedMessage); - db.SaveChanges(); - selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - Assert.Null(selectedMessage); - } - } - - [Fact] - public void CanCreateReceivedMessageUsingEF() - { - using (var db = CreateContext()) - { - var guid = Guid.NewGuid().ToString(); - var message = new CapReceivedMessage - { - Id = guid, - Content = "this is message body", - StatusName = StatusName.Enqueued - }; - db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - db.SaveChanges(); - - Assert.True(db.CapReceivedMessages.Any(u => u.Id == guid)); - Assert.NotNull(db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); - } - } - - [Fact] - public void CanUpdateReceivedMessageUsingEF() - { - using (var db = CreateContext()) - { - var guid = Guid.NewGuid().ToString(); - var message = new CapReceivedMessage - { - Id = guid, - Content = "this is message body", - StatusName = StatusName.Enqueued - }; - db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - db.SaveChanges(); - - var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - Assert.NotNull(selectedMessage); - - selectedMessage.StatusName = StatusName.Succeeded; - selectedMessage.Content = "Test"; - db.SaveChanges(); - - selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); - Assert.NotNull(selectedMessage); - Assert.True(selectedMessage.Content == "Test"); - } - } - - [Fact] - public void CanRemoveReceivedMessageUsingEF() - { - using (var db = CreateContext()) - { - var guid = Guid.NewGuid().ToString(); - var message = new CapReceivedMessage - { - Id = guid, - Content = "this is message body", - StatusName = StatusName.Enqueued - }; - db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - db.SaveChanges(); - - var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - Assert.NotNull(selectedMessage); - - db.CapReceivedMessages.Remove(selectedMessage); - db.SaveChanges(); - selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - Assert.Null(selectedMessage); - } - } - - public TestDbContext CreateContext(bool delete = false) - { - var db = Provider.GetRequiredService(); - if (delete) - { - db.Database.EnsureDeleted(); - } - db.Database.EnsureCreated(); - return db; - } - } + //public class EFMessageStoreTest : DatabaseTestHost + //{ + // [Fact] + // public void CanCreateSentMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapPublishedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // Assert.True(db.CapSentMessages.Any(u => u.Id == guid)); + // Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); + // } + // } + + // [Fact] + // public void CanUpdateSentMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapPublishedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // selectedMessage.StatusName = StatusName.Succeeded; + // selectedMessage.Content = "Test"; + // db.SaveChanges(); + + // selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); + // Assert.NotNull(selectedMessage); + // Assert.True(selectedMessage.Content == "Test"); + // } + // } + + // [Fact] + // public void CanRemoveSentMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapPublishedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // db.CapSentMessages.Remove(selectedMessage); + // db.SaveChanges(); + // selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.Null(selectedMessage); + // } + // } + + // [Fact] + // public void CanCreateReceivedMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapReceivedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // Assert.True(db.CapReceivedMessages.Any(u => u.Id == guid)); + // Assert.NotNull(db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); + // } + // } + + // [Fact] + // public void CanUpdateReceivedMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapReceivedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // selectedMessage.StatusName = StatusName.Succeeded; + // selectedMessage.Content = "Test"; + // db.SaveChanges(); + + // selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); + // Assert.NotNull(selectedMessage); + // Assert.True(selectedMessage.Content == "Test"); + // } + // } + + // [Fact] + // public void CanRemoveReceivedMessageUsingEF() + // { + // using (var db = CreateContext()) + // { + // var guid = Guid.NewGuid().ToString(); + // var message = new CapReceivedMessage + // { + // Id = guid, + // Content = "this is message body", + // StatusName = StatusName.Enqueued + // }; + // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; + + // db.SaveChanges(); + + // var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.NotNull(selectedMessage); + + // db.CapReceivedMessages.Remove(selectedMessage); + // db.SaveChanges(); + // selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + // Assert.Null(selectedMessage); + // } + // } + + // public TestDbContext CreateContext(bool delete = false) + // { + // var db = Provider.GetRequiredService(); + // if (delete) + // { + // db.Database.EnsureDeleted(); + // } + // db.Database.EnsureCreated(); + // return db; + // } + //} } \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/20170708050416_InitDB.Designer.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/20170708050416_InitDB.Designer.cs deleted file mode 100644 index a018f8f..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/20170708050416_InitDB.Designer.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using DotNetCore.CAP.EntityFrameworkCore.Test; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test.Migrations -{ - [DbContext(typeof(TestDbContext))] - [Migration("20170708050416_InitDB")] - partial class InitDB - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { - modelBuilder - .HasAnnotation("ProductVersion", "1.1.2") - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("DotNetCore.CAP.Infrastructure.CapReceivedMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd(); - - b.Property("Added"); - - b.Property("Content"); - - b.Property("Group"); - - b.Property("KeyName"); - - b.Property("LastRun"); - - b.Property("Retries"); - - b.Property("StatusName") - .HasMaxLength(50); - - b.HasKey("Id"); - - b.ToTable("CapReceivedMessages"); - }); - - modelBuilder.Entity("DotNetCore.CAP.Infrastructure.CapSentMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd(); - - b.Property("Added"); - - b.Property("Content"); - - b.Property("KeyName"); - - b.Property("LastRun"); - - b.Property("Retries"); - - b.Property("StatusName") - .HasMaxLength(50); - - b.HasKey("Id"); - - b.ToTable("CapSentMessages"); - }); - } - } -} diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/20170708050416_InitDB.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/20170708050416_InitDB.cs deleted file mode 100644 index af02548..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/20170708050416_InitDB.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test.Migrations -{ - public partial class InitDB : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "CapReceivedMessages", - columns: table => new - { - Id = table.Column(nullable: false), - Added = table.Column(nullable: false), - Content = table.Column(nullable: true), - Group = table.Column(nullable: true), - KeyName = table.Column(nullable: true), - LastRun = table.Column(nullable: false), - Retries = table.Column(nullable: false), - StatusName = table.Column(maxLength: 50, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_CapReceivedMessages", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "CapSentMessages", - columns: table => new - { - Id = table.Column(nullable: false), - Added = table.Column(nullable: false), - Content = table.Column(nullable: true), - KeyName = table.Column(nullable: true), - LastRun = table.Column(nullable: false), - Retries = table.Column(nullable: false), - StatusName = table.Column(maxLength: 50, nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_CapSentMessages", x => x.Id); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "CapReceivedMessages"); - - migrationBuilder.DropTable( - name: "CapSentMessages"); - } - } -} diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/TestDbContextModelSnapshot.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/TestDbContextModelSnapshot.cs deleted file mode 100644 index a4e6e0a..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/Migrations/TestDbContextModelSnapshot.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using DotNetCore.CAP.EntityFrameworkCore.Test; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test.Migrations -{ - [DbContext(typeof(TestDbContext))] - partial class TestDbContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { - modelBuilder - .HasAnnotation("ProductVersion", "1.1.2") - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("DotNetCore.CAP.Infrastructure.CapReceivedMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd(); - - b.Property("Added"); - - b.Property("Content"); - - b.Property("Group"); - - b.Property("KeyName"); - - b.Property("LastRun"); - - b.Property("Retries"); - - b.Property("StatusName") - .HasMaxLength(50); - - b.HasKey("Id"); - - b.ToTable("CapReceivedMessages"); - }); - - modelBuilder.Entity("DotNetCore.CAP.Infrastructure.CapSentMessage", b => - { - b.Property("Id") - .ValueGeneratedOnAdd(); - - b.Property("Added"); - - b.Property("Content"); - - b.Property("KeyName"); - - b.Property("LastRun"); - - b.Property("Retries"); - - b.Property("StatusName") - .HasMaxLength(50); - - b.HasKey("Id"); - - b.ToTable("CapSentMessages"); - }); - } - } -} diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestDbContext.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestDbContext.cs deleted file mode 100644 index 4b7cbef..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestDbContext.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.EntityFrameworkCore; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class TestDbContext : CapDbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - var connectionString = ConnectionUtil.GetConnectionString(); - optionsBuilder.UseSqlServer(connectionString); - } - } -} \ No newline at end of file diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs index cc69061..fbc1a94 100644 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/TestHost.cs @@ -29,7 +29,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test var connectionString = ConnectionUtil.GetConnectionString(); //services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString }); - services.AddDbContext(options => options.UseSqlServer(connectionString)); + //services.AddDbContext(options => options.UseSqlServer(connectionString)); _services = services; }