|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 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<TestDbContext>();
- if (delete)
- {
- db.Database.EnsureDeleted();
- }
- db.Database.EnsureCreated();
- return db;
- }
- }
- }
|