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.
 
 
 

174 lines
6.2 KiB

  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. db.SaveChanges();
  70. selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  71. Assert.Null(selectedMessage);
  72. }
  73. }
  74. [Fact]
  75. public void CanCreateReceivedMessageUsingEF()
  76. {
  77. using (var db = CreateContext())
  78. {
  79. var guid = Guid.NewGuid().ToString();
  80. var message = new CapReceivedMessage
  81. {
  82. Id = guid,
  83. Content = "this is message body",
  84. StatusName = StatusName.Enqueued
  85. };
  86. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  87. db.SaveChanges();
  88. Assert.True(db.CapReceivedMessages.Any(u => u.Id == guid));
  89. Assert.NotNull(db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued));
  90. }
  91. }
  92. [Fact]
  93. public void CanUpdateReceivedMessageUsingEF()
  94. {
  95. using (var db = CreateContext())
  96. {
  97. var guid = Guid.NewGuid().ToString();
  98. var message = new CapReceivedMessage
  99. {
  100. Id = guid,
  101. Content = "this is message body",
  102. StatusName = StatusName.Enqueued
  103. };
  104. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  105. db.SaveChanges();
  106. var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  107. Assert.NotNull(selectedMessage);
  108. selectedMessage.StatusName = StatusName.Succeeded;
  109. selectedMessage.Content = "Test";
  110. db.SaveChanges();
  111. selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded);
  112. Assert.NotNull(selectedMessage);
  113. Assert.True(selectedMessage.Content == "Test");
  114. }
  115. }
  116. [Fact]
  117. public void CanRemoveReceivedMessageUsingEF()
  118. {
  119. using (var db = CreateContext())
  120. {
  121. var guid = Guid.NewGuid().ToString();
  122. var message = new CapReceivedMessage
  123. {
  124. Id = guid,
  125. Content = "this is message body",
  126. StatusName = StatusName.Enqueued
  127. };
  128. db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added;
  129. db.SaveChanges();
  130. var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  131. Assert.NotNull(selectedMessage);
  132. db.CapReceivedMessages.Remove(selectedMessage);
  133. db.SaveChanges();
  134. selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued);
  135. Assert.Null(selectedMessage);
  136. }
  137. }
  138. public TestDbContext CreateContext(bool delete = false)
  139. {
  140. var db = Provider.GetRequiredService<TestDbContext>();
  141. if (delete)
  142. {
  143. db.Database.EnsureDeleted();
  144. }
  145. db.Database.EnsureCreated();
  146. return db;
  147. }
  148. }
  149. }