From 71016d4ef36f589075150fae9027258c7bbba3da Mon Sep 17 00:00:00 2001 From: yangxiaodong Date: Thu, 29 Jun 2017 17:10:26 +0800 Subject: [PATCH] Rfining unit test. --- ...onsistencyDbContext.cs => CapDbContext.cs} | 0 .../CapMessageStoreTest.cs | 12 ++ .../EFMessageStoreTest.cs | 172 ++++++++++++++++++ .../MessageStoreTest.cs | 64 ------- 4 files changed, 184 insertions(+), 64 deletions(-) rename src/DotNetCore.CAP.EntityFrameworkCore/{ConsistencyDbContext.cs => CapDbContext.cs} (100%) create mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/CapMessageStoreTest.cs create mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs delete mode 100644 test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs diff --git a/src/DotNetCore.CAP.EntityFrameworkCore/ConsistencyDbContext.cs b/src/DotNetCore.CAP.EntityFrameworkCore/CapDbContext.cs similarity index 100% rename from src/DotNetCore.CAP.EntityFrameworkCore/ConsistencyDbContext.cs rename to src/DotNetCore.CAP.EntityFrameworkCore/CapDbContext.cs diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/CapMessageStoreTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/CapMessageStoreTest.cs new file mode 100644 index 0000000..4f7f88e --- /dev/null +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/CapMessageStoreTest.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Xunit; + +namespace DotNetCore.CAP.EntityFrameworkCore.Test +{ + public class CapMessageStoreTest + { + + } +} diff --git a/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs new file mode 100644 index 0000000..3c4bc77 --- /dev/null +++ b/test/DotNetCore.CAP.EntityFrameworkCore.Test/EFMessageStoreTest.cs @@ -0,0 +1,172 @@ +using System; +using System.Linq; +using DotNetCore.CAP.Infrastructure; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +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); + 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.CapSentMessages.Any(u => u.Id == guid)); + Assert.NotNull(db.CapSentMessages.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.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 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.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); + Assert.NotNull(selectedMessage); + + db.CapSentMessages.Remove(selectedMessage); + selectedMessage = db.CapSentMessages.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/MessageStoreTest.cs b/test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs deleted file mode 100644 index efaf03e..0000000 --- a/test/DotNetCore.CAP.EntityFrameworkCore.Test/MessageStoreTest.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Linq; -using DotNetCore.CAP.Infrastructure; -using Microsoft.Extensions.DependencyInjection; -using Xunit; - -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - public class MessageStoreTest : 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 async Task CanCreateUsingManager() - //{ - // var manager = CreateManager(); - // var guid = Guid.NewGuid().ToString(); - // var message = new CapSentMessage - // { - // Id = guid, - // Content = "this is message body", - // StateName = StateName.Enqueued, - // }; - - // var result = await manager.StoreSentMessageAsync(message); - // Assert.NotNull(result); - // Assert.True(result.Succeeded); - - // result = await manager.RemoveSentMessageAsync(message); - // Assert.NotNull(result); - // Assert.True(result.Succeeded); - //} - - 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