Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

175 linhas
6.2 KiB

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