From fc0d6973ca81486641543f791da9654842a91644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Vin=C3=ADcius?= Date: Mon, 24 Jun 2024 21:50:15 -0300 Subject: [PATCH] Feat(Tests): Bringing back the old ef repository tests --- Tests/Unit.Tests/Fixture/TestDatabaseFixture.cs | 9 +++++++++ .../Repository.Layer/LaunchRepositoryTest.cs | 17 +++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Tests/Unit.Tests/Fixture/TestDatabaseFixture.cs b/Tests/Unit.Tests/Fixture/TestDatabaseFixture.cs index 8ff2bd2..8bd3f48 100644 --- a/Tests/Unit.Tests/Fixture/TestDatabaseFixture.cs +++ b/Tests/Unit.Tests/Fixture/TestDatabaseFixture.cs @@ -41,9 +41,18 @@ private void SeedDatabase() { Context.Launch.AddRange(TestLaunchInMemoryObjects.Test1(), TestLaunchInMemoryObjects.Test2(), TestLaunchInMemoryObjects.Test3()); Context.SaveChanges(); + + DetachEntitiesEfChangeTracker(); } } + public void DetachEntitiesEfChangeTracker() + { + var entries = Context.ChangeTracker.Entries().ToList(); + foreach (var entry in entries) + entry.State = EntityState.Detached; + } + public Launch NewObjectForSaveTests() { return new Launch() diff --git a/Tests/Unit.Tests/Repository.Layer/LaunchRepositoryTest.cs b/Tests/Unit.Tests/Repository.Layer/LaunchRepositoryTest.cs index 4dbcb20..8adcea6 100644 --- a/Tests/Unit.Tests/Repository.Layer/LaunchRepositoryTest.cs +++ b/Tests/Unit.Tests/Repository.Layer/LaunchRepositoryTest.cs @@ -1,5 +1,6 @@ using System.Linq.Expressions; using Domain.Interface; +using Infrastructure.Persistence.Context; using Tests.Test.Objects; using Tests.Unit.Tests.Fixture; @@ -7,7 +8,7 @@ namespace Tests.Unit.Tests.Repository.Layer { public class LaunchRepositoryTest : IClassFixture { - /*private readonly TestDatabaseFixture _fixture; + private readonly TestDatabaseFixture _fixture; public LaunchRepositoryTest(TestDatabaseFixture fixture) { _fixture = fixture; @@ -361,13 +362,13 @@ public async Task GenericRepository_GetSelected_GetOneLaunchWithIncludeAndBuildD public async Task GenericRepository_UpdateOnQuery_UpdateDatabaseObjectsWithoutBringToMemory() { //Arrange + _fixture.DetachEntitiesEfChangeTracker(); Expression> selectedLaunch = l => l.Id == new Guid("000ebc80-d782-4dee-8606-1199d9074039"); Expression> updateStatusColumn = l => new Launch() { EntityStatus = EStatus.TRASH.GetDisplayName() }; //Act await _fixture.Launch.UpdateOnQuery(selectedLaunch, updateStatusColumn); - _fixture.TransferUpdatedObjectsQueryContext(); //Assert var getAssertInMemoryObject = await _fixture.Launch.GetSelected( @@ -379,6 +380,7 @@ public async Task GenericRepository_UpdateOnQuery_UpdateDatabaseObjectsWithoutBr Assert.Equal(getAssertInMemoryObject, EStatus.TRASH.GetDisplayName()); //Rolling back the change for the other tests... + _fixture.DetachEntitiesEfChangeTracker(); Expression> rollbackUpdate = l => new Launch() { EntityStatus = EStatus.PUBLISHED.GetDisplayName() }; await _fixture.Launch.UpdateOnQuery(selectedLaunch, rollbackUpdate); @@ -428,7 +430,6 @@ public async Task GenericRepository_Save_SaveANewObjectInMemoryWithTransaction() launchRepository.Setup(l => l.Save(It.IsAny())); launchRepository.Setup(l => l.CommitTransaction()) .Callback(async () => await _fixture.Launch.Save(newLaunch)); - _fixture.TransferUpdatedObjectsQueryContext(); //Act await launchRepository.Object.BeginTransaction(); @@ -500,10 +501,9 @@ public async Task GenericRepository_Save_UpdateAEntityWithSaveMethod() { //Arrange Expression> qryTest = l => l.Id == TestLaunchInMemoryObjects.Test1().Id; - var entityToBeUpdated = await _fixture.Launch.Get(filter: qryTest); - entityToBeUpdated.EntityStatus = EStatus.TRASH.GetDisplayName(); + entityToBeUpdated.EntityStatus = EStatus.TRASH.GetDisplayName(); Assert.Equal(EStatus.PUBLISHED.GetDisplayName(), await _fixture.Launch.GetSelected( filter: qryTest, selectColumns:l => l.EntityStatus, @@ -514,11 +514,12 @@ public async Task GenericRepository_Save_UpdateAEntityWithSaveMethod() await _fixture.Launch.Save(entityToBeUpdated); //Assert - Assert.Equal(EStatus.TRASH.GetDisplayName(), await _fixture.Launch.GetSelected( + var newStatus = await _fixture.Launch.GetSelected( filter: qryTest, selectColumns:l => l.EntityStatus, buildObject: l => l - )); + ); + Assert.Equal(EStatus.TRASH.GetDisplayName(), newStatus); //Rollback the changes.. entityToBeUpdated.EntityStatus = EStatus.PUBLISHED.GetDisplayName(); @@ -555,6 +556,6 @@ public async Task GenericRepository_Delete_DeleteEntityById() //Assert Assert.Equal(3, await _fixture.Launch.EntityCount()); - }*/ + } } } \ No newline at end of file