You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EFMessageStoreTest.cs 6.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Linq;
  3. using DotNetCore.CAP.Infrastructure;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Xunit;
  6. namespace DotNetCore.CAP.EntityFrameworkCore.Test
  7. {
  8. public class EFMessageStoreTest : DatabaseTestHost
  9. {
  10. [Fact]
  11. public void CanCreateSentMessageUsingEF()
  12. {
  13. using (var db = CreateContext())
  14. {
  15. var guid = Guid.NewGuid().ToString();
  16. var message = new CapSentMessage
  17. {
  18. Id = guid,
  19. Content = "this is message body",
  20. StatusName = StatusName.Enqueued
  21. };
  22. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  23. db.SaveChanges();
  24. Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
  25. Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
  26. }
  27. }
  28. [Fact]
  29. public void CanUpdateSentMessageUsingEF()
  30. {
  31. using (var db = CreateContext())
  32. {
  33. var guid = Guid.NewGuid().ToString();
  34. var message = new CapSentMessage
  35. {
  36. Id = guid,
  37. Content = "this is message body",
  38. StatusName = StatusName.Enqueued
  39. };
  40. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  41. db.SaveChanges();
  42. var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  43. Assert.NotNull(selectedMessage);
  44. selectedMessage.StatusName = StatusName.Succeeded;
  45. selectedMessage.Content = "Test";
  46. db.SaveChanges();
  47. selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
  48. Assert.NotNull(selectedMessage);
  49. Assert.True(selectedMessage.Content == "Test");
  50. }
  51. }
  52. [Fact]
  53. public void CanRemoveSentMessageUsingEF()
  54. {
  55. using (var db = CreateContext())
  56. {
  57. var guid = Guid.NewGuid().ToString();
  58. var message = new CapSentMessage
  59. {
  60. Id = guid,
  61. Content = "this is message body",
  62. StatusName = StatusName.Enqueued
  63. };
  64. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  65. db.SaveChanges();
  66. var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  67. Assert.NotNull(selectedMessage);
  68. db.CapSentMessages.Remove(selectedMessage);
  69. selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  70. Assert.Null(selectedMessage);
  71. }
  72. }
  73. [Fact]
  74. public void CanCreateReceivedMessageUsingEF()
  75. {
  76. using (var db = CreateContext())
  77. {
  78. var guid = Guid.NewGuid().ToString();
  79. var message = new CapReceivedMessage
  80. {
  81. Id = guid,
  82. Content = "this is message body",
  83. StatusName = StatusName.Enqueued
  84. };
  85. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  86. db.SaveChanges();
  87. Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
  88. Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
  89. }
  90. }
  91. [Fact]
  92. public void CanUpdateReceivedMessageUsingEF()
  93. {
  94. using (var db = CreateContext())
  95. {
  96. var guid = Guid.NewGuid().ToString();
  97. var message = new CapReceivedMessage
  98. {
  99. Id = guid,
  100. Content = "this is message body",
  101. StatusName = StatusName.Enqueued
  102. };
  103. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  104. db.SaveChanges();
  105. var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  106. Assert.NotNull(selectedMessage);
  107. selectedMessage.StatusName = StatusName.Succeeded;
  108. selectedMessage.Content = "Test";
  109. db.SaveChanges();
  110. selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
  111. Assert.NotNull(selectedMessage);
  112. Assert.True(selectedMessage.Content == "Test");
  113. }
  114. }
  115. [Fact]
  116. public void CanRemoveReceivedMessageUsingEF()
  117. {
  118. using (var db = CreateContext())
  119. {
  120. var guid = Guid.NewGuid().ToString();
  121. var message = new CapReceivedMessage
  122. {
  123. Id = guid,
  124. Content = "this is message body",
  125. StatusName = StatusName.Enqueued
  126. };
  127. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  128. db.SaveChanges();
  129. var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  130. Assert.NotNull(selectedMessage);
  131. db.CapSentMessages.Remove(selectedMessage);
  132. selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  133. Assert.Null(selectedMessage);
  134. }
  135. }
  136. public TestDbContext CreateContext(bool delete = false)
  137. {
  138. var db = Provider.GetRequiredService<TestDbContext>();
  139. if (delete)
  140. {
  141. db.Database.EnsureDeleted();
  142. }
  143. db.Database.EnsureCreated();
  144. return db;
  145. }
  146. }
  147. }