From 0adc61b14a3ef2370d1447f41a705f4a1528e29c Mon Sep 17 00:00:00 2001 From: Luciana Regina Lino Date: Tue, 18 Jun 2024 09:57:32 -0300 Subject: [PATCH] wip(Liquid.Repository.EntityFramework): Update Liquid framework for net8. --- .../DatabaseDoesNotExistException.cs | 6 - .../Liquid.Repository.EntityFramework.csproj | 12 +- .../EntityFrameworkDataContextTests.cs | 47 ++++---- .../EntityFrameworkRepositoryTest.cs | 112 ++++++++---------- ...id.Repository.EntityFramework.Tests.csproj | 20 ++-- 5 files changed, 89 insertions(+), 108 deletions(-) diff --git a/src/Liquid.Repository.EntityFramework/Exceptions/DatabaseDoesNotExistException.cs b/src/Liquid.Repository.EntityFramework/Exceptions/DatabaseDoesNotExistException.cs index 1eebcd66..de62d7d3 100644 --- a/src/Liquid.Repository.EntityFramework/Exceptions/DatabaseDoesNotExistException.cs +++ b/src/Liquid.Repository.EntityFramework/Exceptions/DatabaseDoesNotExistException.cs @@ -1,7 +1,6 @@ using Liquid.Core.Exceptions; using System; using System.Diagnostics.CodeAnalysis; -using System.Runtime.Serialization; namespace Liquid.Repository.EntityFramework.Exceptions { @@ -30,10 +29,5 @@ public DatabaseDoesNotExistException() public DatabaseDoesNotExistException(string message, Exception innerException) : base(message, innerException) { } - - /// - protected DatabaseDoesNotExistException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } } } \ No newline at end of file diff --git a/src/Liquid.Repository.EntityFramework/Liquid.Repository.EntityFramework.csproj b/src/Liquid.Repository.EntityFramework/Liquid.Repository.EntityFramework.csproj index 21f5d2f2..6136b5b5 100644 --- a/src/Liquid.Repository.EntityFramework/Liquid.Repository.EntityFramework.csproj +++ b/src/Liquid.Repository.EntityFramework/Liquid.Repository.EntityFramework.csproj @@ -1,9 +1,9 @@  - net6.0 + net8.0 Liquid.Repository.EntityFramework - 6.0.0 + 8.0.0-alpha-01 Avanade Brazil Avanade Inc. Liquid - Modern Application Framework @@ -18,7 +18,8 @@ - + + @@ -27,9 +28,4 @@ - - - - - diff --git a/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkDataContextTests.cs b/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkDataContextTests.cs index c1da4583..4c2f0ade 100644 --- a/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkDataContextTests.cs +++ b/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkDataContextTests.cs @@ -1,10 +1,11 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using NSubstitute; -using NUnit.Framework; + using System; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; +using Xunit; namespace Liquid.Repository.EntityFramework.Tests { @@ -15,8 +16,8 @@ public class EntityFrameworkDataContextTests private DbContext _client; private DatabaseFacade _database; - [SetUp] - protected void SetContext() + + public EntityFrameworkDataContextTests() { _client = Substitute.For(); @@ -28,7 +29,7 @@ protected void SetContext() _sut = new EntityFrameworkDataContext(_client); } - [Test] + [Fact] public async Task StartTransactionAsync_WhenClientExecutedSucessfuly_Success() { await _sut.StartTransactionAsync(); @@ -36,17 +37,17 @@ public async Task StartTransactionAsync_WhenClientExecutedSucessfuly_Success() await _client.Database.Received(1).BeginTransactionAsync(); } - [Test] - public void StartTransactionAsync_WhenClientThrow_ThrowException() + [Fact] + public async Task StartTransactionAsync_WhenClientThrow_ThrowException() { _database.When(o => o.BeginTransactionAsync()).Do((call) => throw new Exception()); var task = _sut.StartTransactionAsync(); - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Test] + [Fact] public async Task CommitAsync_WhenClientExecutedSucessfuly_Success() { await _sut.CommitAsync(); @@ -54,17 +55,17 @@ public async Task CommitAsync_WhenClientExecutedSucessfuly_Success() await _client.Database.Received(1).CommitTransactionAsync(); } - [Test] - public void CommitAsync_WhenClientExcept_ThrowException() + [Fact] + public async Task CommitAsync_WhenClientExcept_ThrowException() { _database.When(o => o.CommitTransactionAsync()).Do((call) => throw new Exception()); var task = _sut.CommitAsync(); - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Test] + [Fact] public async Task RollbackTransactionAsync_WhenClientExecutedSucessfuly_Success() { await _sut.RollbackTransactionAsync(); @@ -72,17 +73,17 @@ public async Task RollbackTransactionAsync_WhenClientExecutedSucessfuly_Success( await _client.Database.Received(1).RollbackTransactionAsync(); } - [Test] - public void RollbackTransactionAsync_WhenClientExcept_ThrowException() + [Fact] + public async Task RollbackTransactionAsync_WhenClientExcept_ThrowException() { _database.When(o => o.RollbackTransactionAsync()).Do((call) => throw new Exception()); var task = _sut.RollbackTransactionAsync(); - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Test] + [Fact] public void Verify_Dispose() { _sut.Dispose(); @@ -93,7 +94,7 @@ public void Verify_Dispose() _sut.Dispose(); } - [Test] + [Fact] public void Verify_Dispose_Except() { _client.When(o => o.Dispose()).Do((call) => throw new Exception()); @@ -101,23 +102,23 @@ public void Verify_Dispose_Except() Assert.Throws(() => _sut.Dispose()); } - [Test] + [Fact] public void EntityFrameworkDataContext_WhenCreated_DbContextIsValid() { - Assert.IsNotNull(_sut.DbClient); - Assert.IsInstanceOf(_sut.DbClient); + Assert.NotNull(_sut.DbClient); + Assert.IsAssignableFrom(_sut.DbClient); } - [Test] + [Fact] public void EntityFrameworkDataContext_WhenCreatedWithoutDbContext_ThrowException() { Assert.Throws(() => new EntityFrameworkDataContext(null)); } - [Test] + [Fact] public void EntityFrameworkDataContext_IdIsAlwaysNull() { - Assert.IsNull(_sut.Id); + Assert.Null(_sut.Id); } } } diff --git a/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkRepositoryTest.cs b/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkRepositoryTest.cs index 7ee3c73b..175cfc8b 100644 --- a/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkRepositoryTest.cs +++ b/test/Liquid.Repository.EntityFramework.Tests/EntityFrameworkRepositoryTest.cs @@ -4,22 +4,21 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using NSubstitute; -using NUnit.Framework; using System; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; +using Xunit; namespace Liquid.Repository.EntityFramework.Tests { [ExcludeFromCodeCoverage] - [TestFixture()] public class EntityFrameworkRepositoryTest { private IServiceProvider _serviceProvider; - [SetUp] - public async Task EstablishContext() + + public EntityFrameworkRepositoryTest() { var services = new ServiceCollection(); var databaseName = $"TEMP_{Guid.NewGuid()}"; @@ -28,7 +27,7 @@ public async Task EstablishContext() _serviceProvider = services.BuildServiceProvider(); - await SeedDataAsync(_serviceProvider); + SeedDataAsync(_serviceProvider); } private EntityFrameworkRepository GenerateMockRepository() @@ -47,9 +46,8 @@ private EntityFrameworkRepository GenerateMockRe return new EntityFrameworkRepository(dataContext); } - - [Category("AddAsync")] - [Test] + + [Fact] public async Task Verify_insert() { //Arrange @@ -61,12 +59,11 @@ public async Task Verify_insert() //Assert Assert.NotNull(entity); - Assert.AreNotEqual(default, entity.MockId); + Assert.NotEqual(default, entity.MockId); } - [Category("AddAsync")] - [Test] - public void Verify_insert_Except() + [Fact] + public async Task Verify_insert_Except() { //Arrange var dbSet = Substitute.For, IQueryable>(); @@ -79,11 +76,10 @@ public void Verify_insert_Except() var task = mockRepository.AddAsync(entity); //Assert - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Category("FindByIdAsync")] - [Test] + [Fact] public async Task Verify_find_by_id() { //Arrange @@ -95,11 +91,11 @@ public async Task Verify_find_by_id() //Assert Assert.NotNull(entity); - Assert.AreEqual(mockId, entity.MockId); + Assert.Equal(mockId, entity.MockId); } - [Category("FindByIdAsync")] - [Test] - public void Verify_find_by_id_Except() + + [Fact] + public async Task Verify_find_by_id_Except() { //Arrange var dbSet = Substitute.For, IQueryable>(); @@ -110,11 +106,10 @@ public void Verify_find_by_id_Except() var task = mockRepository.FindByIdAsync(mockId); //Assert - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Category("WhereAsync")] - [Test] + [Fact] public async Task Verify_where() { //Arrange @@ -126,13 +121,12 @@ public async Task Verify_where() //Assert Assert.NotNull(result); - Assert.IsNotEmpty(result); - Assert.IsTrue(result.All(o => o.MockTitle.Equals(mockTitle))); + Assert.NotEmpty(result); + Assert.True(result.All(o => o.MockTitle.Equals(mockTitle))); } - [Category("WhereAsync")] - [Test] - public void Verify_where_Except() + [Fact] + public async Task Verify_where_Except() { //Arrange //Arrange @@ -144,11 +138,10 @@ public void Verify_where_Except() var task = mockRepository.WhereAsync(o => o.MockTitle.Equals(mockTitle)); //Assert - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Category("GetAllAsync")] - [Test] + [Fact] public async Task Verify_find_all() { //Arrange @@ -159,12 +152,11 @@ public async Task Verify_find_all() //Assert Assert.NotNull(result); - Assert.IsNotEmpty(result); - Assert.AreEqual(100, result.Count()); + Assert.NotEmpty(result); + Assert.Equal(100, result.Count()); } - [Category("FindAllAsync")] - [Test] + [Fact] public async Task Verify_find_all_Except() { //Arrange @@ -176,11 +168,10 @@ public async Task Verify_find_all_Except() var result = await mockRepository.FindAllAsync(); //Assert - Assert.IsEmpty(result); + Assert.Empty(result); } - [Category("RemoveByIdAsync")] - [Test] + [Fact] public async Task Verify_delete() { //Arrange @@ -192,11 +183,10 @@ public async Task Verify_delete() var anotherEntity = await mockRepository.FindByIdAsync(mockId); //Assert - Assert.IsNull(anotherEntity); + Assert.Null(anotherEntity); } - [Category("RemoveByIdAsync")] - [Test] + [Fact] public async Task Verify_delete_invalid() { //Arrange @@ -208,12 +198,11 @@ public async Task Verify_delete_invalid() var anotherEntity = await mockRepository.FindByIdAsync(mockId); //Assert - Assert.IsNull(anotherEntity); + Assert.Null(anotherEntity); } - [Category("RemoveByIdAsync")] - [Test] - public void Verify_delete_Except() + [Fact] + public async Task Verify_delete_Except() { //Arrange var dbSet = Substitute.For, IQueryable>(); @@ -224,11 +213,10 @@ public void Verify_delete_Except() var task = mockRepository.RemoveByIdAsync(mockId); //Assert - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - [Category("UpdateAsync")] - [Test] + [Fact] public async Task Verify_updates() { //Arrange @@ -243,12 +231,11 @@ public async Task Verify_updates() //Assert Assert.NotNull(anotherEntity); - Assert.AreEqual("TITLE_001_UPDATED", anotherEntity.MockTitle); + Assert.Equal("TITLE_001_UPDATED", anotherEntity.MockTitle); } - [Category("UpdateAsync")] - [Test] - public void Verify_updates_Except() + [Fact] + public async Task Verify_updates_Except() { //Arrange var dbSet = Substitute.For, IQueryable>(); @@ -259,29 +246,28 @@ public void Verify_updates_Except() var task = mockRepository.UpdateAsync(new MockEntity() { MockId = mockId }); //Assert - Assert.ThrowsAsync(() => task); + await Assert.ThrowsAsync(() => task); } - private async Task SeedDataAsync(IServiceProvider serviceProvider) + private void SeedDataAsync(IServiceProvider serviceProvider) { MockDbContext dbContext = serviceProvider.GetService(); for (int i = 1; i <= 100; i++) { - await dbContext.AddAsync(new MockEntity() { MockId = i, MockTitle = $"TITLE_{i:000}", Active = true, CreatedDate = DateTime.Now }); + dbContext.AddAsync(new MockEntity() { MockId = i, MockTitle = $"TITLE_{i:000}", Active = true, CreatedDate = DateTime.Now }) + .GetAwaiter().GetResult(); } - await dbContext.SaveChangesAsync(); + dbContext.SaveChangesAsync().GetAwaiter().GetResult(); } - [Category("ctor")] - [Test] + [Fact] public void EntityFrameworkRepository_WhenCreatedWithoutDataContext_ThrowException() { Assert.Throws(() => new EntityFrameworkRepository(null)); } - [Category("Members")] - [Test] + [Fact] public void EntityFrameworkRepository_WhenCreated_DataContextIsValid() { //Arrange @@ -293,11 +279,11 @@ public void EntityFrameworkRepository_WhenCreated_DataContextIsValid() var entityFrameworkDataContext = mockRepository.EntityDataContext; //Assert - Assert.IsNotNull(dataContext); - Assert.IsInstanceOf(dataContext); + Assert.NotNull(dataContext); + Assert.IsAssignableFrom(dataContext); - Assert.IsNotNull(entityFrameworkDataContext); - Assert.IsInstanceOf>(entityFrameworkDataContext); + Assert.NotNull(entityFrameworkDataContext); + Assert.IsAssignableFrom>(entityFrameworkDataContext); } } } \ No newline at end of file diff --git a/test/Liquid.Repository.EntityFramework.Tests/Liquid.Repository.EntityFramework.Tests.csproj b/test/Liquid.Repository.EntityFramework.Tests/Liquid.Repository.EntityFramework.Tests.csproj index a0759f65..fb8c6315 100644 --- a/test/Liquid.Repository.EntityFramework.Tests/Liquid.Repository.EntityFramework.Tests.csproj +++ b/test/Liquid.Repository.EntityFramework.Tests/Liquid.Repository.EntityFramework.Tests.csproj @@ -1,25 +1,29 @@  - net6.0 + net8.0 false - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - -