|
|
@@ -0,0 +1,207 @@
|
|
|
+using Microsoft.EntityFrameworkCore;
|
|
|
+using qdr.fnd.core.test.Repositories.Fakes;
|
|
|
+using Quadarax.Foundation.Core.Data;
|
|
|
+using Quadarax.Foundation.Core.Data.Domain;
|
|
|
+
|
|
|
+namespace qdr.fnd.core.test.Repositories
|
|
|
+{
|
|
|
+
|
|
|
+ [TestFixture(Category = "Dao")]
|
|
|
+ internal class GeneralRepositoryTest : DbContextTest
|
|
|
+ {
|
|
|
+ private DomainContextResolver<TestDbContext>? _dbcResolver;
|
|
|
+
|
|
|
+
|
|
|
+ private const string DaoName1 = "Name1";
|
|
|
+ private const string DaoName2 = "Name2";
|
|
|
+ private const string DaoValue1 = "Value1";
|
|
|
+ private const string DaoValue2 = "Value2";
|
|
|
+
|
|
|
+
|
|
|
+ [SetUp]
|
|
|
+ public void Setup()
|
|
|
+ {
|
|
|
+ var context = new TestDbContext(new DbContextOptions<TestDbContext>());
|
|
|
+ context.Database.EnsureCreated();
|
|
|
+ _dbcResolver = new DomainContextResolver<TestDbContext>(context);
|
|
|
+ }
|
|
|
+ [TearDown]
|
|
|
+ public void TearDown()
|
|
|
+ {
|
|
|
+ _dbcResolver?.GetCurrent<TestDbContext>().Dispose();
|
|
|
+ _dbcResolver = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void AddEntity()
|
|
|
+ {
|
|
|
+
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.New();
|
|
|
+ dao.Name = DaoName1;
|
|
|
+ dao.Value = DaoValue1;
|
|
|
+ uow.Commit();
|
|
|
+
|
|
|
+ // read check inside UOW
|
|
|
+ dao = repo.Get(1);
|
|
|
+ Assert.IsNotNull(dao, "Cannot obtain created dao inside UOW.");
|
|
|
+ Assert.That(dao.Name, Is.EqualTo(DaoName1));
|
|
|
+ Assert.That(dao.Value, Is.EqualTo(DaoValue1));
|
|
|
+ }
|
|
|
+ // read check outside UOW
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.Get(1);
|
|
|
+ Assert.IsNotNull(dao, "Cannot obtain created dao outside UOW.");
|
|
|
+ Assert.That(dao.Name, Is.EqualTo(DaoName1));
|
|
|
+ Assert.That(dao.Value, Is.EqualTo(DaoValue1));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [Test]
|
|
|
+ public void UpdateEntity()
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.New();
|
|
|
+ dao.Name = DaoName1;
|
|
|
+ dao.Value = DaoValue1;
|
|
|
+ uow.Commit();
|
|
|
+
|
|
|
+ dao.Value = DaoValue2;
|
|
|
+ uow.Commit();
|
|
|
+ }
|
|
|
+ // read check outside UOW
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.Get(1);
|
|
|
+ Assert.IsNotNull(dao, "Cannot obtain created dao outside UOW.");
|
|
|
+ Assert.That(dao.Name, Is.EqualTo(DaoName1));
|
|
|
+ Assert.That(dao.Value, Is.EqualTo(DaoValue2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ [Test]
|
|
|
+ public void RemoveEntity()
|
|
|
+ {
|
|
|
+
|
|
|
+ var result = false;
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.New();
|
|
|
+ dao.Name = DaoName1;
|
|
|
+ dao.Value = DaoValue1;
|
|
|
+ uow.Commit();
|
|
|
+ result = repo.Remove(1);
|
|
|
+ uow.Commit();
|
|
|
+
|
|
|
+ }
|
|
|
+ // read check outside UOW
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.Get(1);
|
|
|
+ Assert.IsNull(dao, "Dao doesn't remove outside UOW.");
|
|
|
+ result = repo.Remove(1);
|
|
|
+ Assert.IsFalse(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ [Test]
|
|
|
+ public void QueryEntity()
|
|
|
+ {
|
|
|
+ var context = new TestDbContext(new DbContextOptions<TestDbContext>());
|
|
|
+ context.Database.EnsureCreated();
|
|
|
+ var dbcResolver = new DomainContextResolver<TestDbContext>(context);
|
|
|
+
|
|
|
+ var result = false;
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.New();
|
|
|
+ dao.Name = DaoName1;
|
|
|
+ dao.Value = DaoValue1;
|
|
|
+ dao = repo.New();
|
|
|
+ dao.Name = DaoName2;
|
|
|
+ dao.Value = DaoValue2;
|
|
|
+ uow.Commit();
|
|
|
+ // read check inside UOW
|
|
|
+ var list = repo.Query(Paging.NoPaging());
|
|
|
+ Assert.IsNotNull(list);
|
|
|
+ Assert.That(list.Count(), Is.EqualTo(2));
|
|
|
+
|
|
|
+ }
|
|
|
+ // read check outside UOW
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var list = repo.Query(Paging.NoPaging());
|
|
|
+ Assert.IsNotNull(list);
|
|
|
+ Assert.That(list.Count(), Is.EqualTo(2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ [Test]
|
|
|
+ public void QueryPagingEntity()
|
|
|
+ {
|
|
|
+
|
|
|
+ var result = false;
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.New();
|
|
|
+ dao.Name = DaoName1;
|
|
|
+ dao.Value = DaoValue1;
|
|
|
+ dao = repo.New();
|
|
|
+ dao.Name = DaoName2;
|
|
|
+ dao.Value = DaoValue2;
|
|
|
+ uow.Commit();
|
|
|
+
|
|
|
+ }
|
|
|
+ // read check outside UOW
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var list = repo.Query(new Paging(0,1))?.ToArray();
|
|
|
+ Assert.IsNotNull(list);
|
|
|
+ Assert.That(list.Count(), Is.EqualTo(1));
|
|
|
+ Assert.That(list[0].Name, Is.EqualTo(DaoName1));
|
|
|
+ list = repo.Query(new Paging(1,1))?.ToArray();
|
|
|
+ Assert.IsNotNull(list);
|
|
|
+ Assert.That(list[0].Name, Is.EqualTo(DaoName2));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ [Test]
|
|
|
+ public void RollbackEntity()
|
|
|
+ {
|
|
|
+
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.New();
|
|
|
+ dao.Name = DaoName1;
|
|
|
+ dao.Value = DaoValue1;
|
|
|
+ uow.Commit();
|
|
|
+ dao = repo.Get(1);
|
|
|
+ Assert.IsNotNull(dao);
|
|
|
+ Assert.That(dao.Name, Is.EqualTo(DaoName1));
|
|
|
+ dao.Name = DaoName2;
|
|
|
+ }
|
|
|
+ // read check outside UOW
|
|
|
+ using (var uow = new UnitOfWork<TestDbContext>(_dbcResolver))
|
|
|
+ {
|
|
|
+ var repo = new TestRepo(uow.As<IDataDomain>());
|
|
|
+ var dao = repo.Get(1);
|
|
|
+ Assert.IsNotNull(dao);
|
|
|
+ Assert.That(dao.Name, Is.EqualTo(DaoName1));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|