GeneralRepositoryTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using Microsoft.EntityFrameworkCore;
  2. using qdr.fnd.core.test.Repositories.Fakes;
  3. using Quadarax.Foundation.Core.Data;
  4. using Quadarax.Foundation.Core.Data.Domain;
  5. namespace qdr.fnd.core.test.Repositories
  6. {
  7. [TestFixture(Category = "Dao")]
  8. internal class GeneralRepositoryTest : DbContextTest
  9. {
  10. private DomainContextResolver<TestDbContext>? _dbcResolver;
  11. private const string DaoName1 = "Name1";
  12. private const string DaoName2 = "Name2";
  13. private const string DaoValue1 = "Value1";
  14. private const string DaoValue2 = "Value2";
  15. [SetUp]
  16. public void Setup()
  17. {
  18. var context = new TestDbContext(new DbContextOptions<TestDbContext>());
  19. context.Database.EnsureCreated();
  20. _dbcResolver = new DomainContextResolver<TestDbContext>(context);
  21. }
  22. [TearDown]
  23. public void TearDown()
  24. {
  25. _dbcResolver?.GetCurrent<TestDbContext>().Dispose();
  26. _dbcResolver = null;
  27. }
  28. [Test]
  29. public void AddEntity()
  30. {
  31. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  32. {
  33. var repo = new TestRepo(uow.As<IDataDomain>());
  34. var dao = repo.New();
  35. dao.Name = DaoName1;
  36. dao.Value = DaoValue1;
  37. uow.Commit();
  38. // read check inside UOW
  39. dao = repo.Get(1);
  40. Assert.IsNotNull(dao, "Cannot obtain created dao inside UOW.");
  41. Assert.That(dao.Name, Is.EqualTo(DaoName1));
  42. Assert.That(dao.Value, Is.EqualTo(DaoValue1));
  43. }
  44. // read check outside UOW
  45. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  46. {
  47. var repo = new TestRepo(uow.As<IDataDomain>());
  48. var dao = repo.Get(1);
  49. Assert.IsNotNull(dao, "Cannot obtain created dao outside UOW.");
  50. Assert.That(dao.Name, Is.EqualTo(DaoName1));
  51. Assert.That(dao.Value, Is.EqualTo(DaoValue1));
  52. }
  53. }
  54. [Test]
  55. public void UpdateEntity()
  56. {
  57. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  58. {
  59. var repo = new TestRepo(uow.As<IDataDomain>());
  60. var dao = repo.New();
  61. dao.Name = DaoName1;
  62. dao.Value = DaoValue1;
  63. uow.Commit();
  64. dao.Value = DaoValue2;
  65. uow.Commit();
  66. }
  67. // read check outside UOW
  68. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  69. {
  70. var repo = new TestRepo(uow.As<IDataDomain>());
  71. var dao = repo.Get(1);
  72. Assert.IsNotNull(dao, "Cannot obtain created dao outside UOW.");
  73. Assert.That(dao.Name, Is.EqualTo(DaoName1));
  74. Assert.That(dao.Value, Is.EqualTo(DaoValue2));
  75. }
  76. }
  77. [Test]
  78. public void RemoveEntity()
  79. {
  80. var result = false;
  81. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  82. {
  83. var repo = new TestRepo(uow.As<IDataDomain>());
  84. var dao = repo.New();
  85. dao.Name = DaoName1;
  86. dao.Value = DaoValue1;
  87. uow.Commit();
  88. result = repo.Remove(1);
  89. uow.Commit();
  90. }
  91. // read check outside UOW
  92. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  93. {
  94. var repo = new TestRepo(uow.As<IDataDomain>());
  95. var dao = repo.Get(1);
  96. Assert.IsNull(dao, "Dao doesn't remove outside UOW.");
  97. result = repo.Remove(1);
  98. Assert.IsFalse(result);
  99. }
  100. }
  101. [Test]
  102. public void QueryEntity()
  103. {
  104. var context = new TestDbContext(new DbContextOptions<TestDbContext>());
  105. context.Database.EnsureCreated();
  106. var dbcResolver = new DomainContextResolver<TestDbContext>(context);
  107. var result = false;
  108. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  109. {
  110. var repo = new TestRepo(uow.As<IDataDomain>());
  111. var dao = repo.New();
  112. dao.Name = DaoName1;
  113. dao.Value = DaoValue1;
  114. dao = repo.New();
  115. dao.Name = DaoName2;
  116. dao.Value = DaoValue2;
  117. uow.Commit();
  118. // read check inside UOW
  119. var list = repo.Query(Paging.NoPaging());
  120. Assert.IsNotNull(list);
  121. Assert.That(list.Count(), Is.EqualTo(2));
  122. }
  123. // read check outside UOW
  124. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  125. {
  126. var repo = new TestRepo(uow.As<IDataDomain>());
  127. var list = repo.Query(Paging.NoPaging());
  128. Assert.IsNotNull(list);
  129. Assert.That(list.Count(), Is.EqualTo(2));
  130. }
  131. }
  132. [Test]
  133. public void QueryPagingEntity()
  134. {
  135. var result = false;
  136. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  137. {
  138. var repo = new TestRepo(uow.As<IDataDomain>());
  139. var dao = repo.New();
  140. dao.Name = DaoName1;
  141. dao.Value = DaoValue1;
  142. dao = repo.New();
  143. dao.Name = DaoName2;
  144. dao.Value = DaoValue2;
  145. uow.Commit();
  146. }
  147. // read check outside UOW
  148. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  149. {
  150. var repo = new TestRepo(uow.As<IDataDomain>());
  151. var list = repo.Query(new Paging(0,1))?.ToArray();
  152. Assert.IsNotNull(list);
  153. Assert.That(list.Count(), Is.EqualTo(1));
  154. Assert.That(list[0].Name, Is.EqualTo(DaoName1));
  155. list = repo.Query(new Paging(1,1))?.ToArray();
  156. Assert.IsNotNull(list);
  157. Assert.That(list[0].Name, Is.EqualTo(DaoName2));
  158. }
  159. }
  160. [Test]
  161. public void RollbackEntity()
  162. {
  163. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  164. {
  165. var repo = new TestRepo(uow.As<IDataDomain>());
  166. var dao = repo.New();
  167. dao.Name = DaoName1;
  168. dao.Value = DaoValue1;
  169. uow.Commit();
  170. dao = repo.Get(1);
  171. Assert.IsNotNull(dao);
  172. Assert.That(dao.Name, Is.EqualTo(DaoName1));
  173. dao.Name = DaoName2;
  174. }
  175. // read check outside UOW
  176. using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
  177. {
  178. var repo = new TestRepo(uow.As<IDataDomain>());
  179. var dao = repo.Get(1);
  180. Assert.IsNotNull(dao);
  181. Assert.That(dao.Name, Is.EqualTo(DaoName1));
  182. }
  183. }
  184. }
  185. }