From 79617dd730548184676c9e824ac12683a42e402b Mon Sep 17 00:00:00 2001 From: Jason Summers <3616919+JasonS-Dev@users.noreply.github.com> Date: Tue, 4 Oct 2022 20:56:53 +0100 Subject: [PATCH] Add more integration tests for ContextFactoryRepositoryBaseOfT and tests for EFRepositoryFactory --- .../ContextFactoryRepositoryBaseOfTTests.cs | 57 ++++++++++++ .../EFRepositoryFactoryTests.cs | 91 +++++++++++++++++++ .../Fixture/RepositoryOfT.cs | 4 + 3 files changed, 152 insertions(+) create mode 100644 Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs index 16da86d7..c98c4c4e 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs @@ -42,6 +42,22 @@ public async Task Saves_new_entity() [Fact] public async Task Updates_existing_entity() + { + var country = await dbContext.Countries.FirstOrDefaultAsync(); + + var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; + await repository.AddAsync(company); + + var existingCompany = await repository.GetByIdAsync(company.Id); + existingCompany.Name = "Updated company name"; + await repository.UpdateAsync(existingCompany); + + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); + } + + [Fact] + public async Task Updates_existing_entity_across_context_instances() { var contextFactory = serviceProvider.GetService>(); var companyRetrievalRepository = new ContextFactoryRepository(contextFactory); @@ -62,6 +78,32 @@ public async Task Updates_existing_entity() [Fact] public async Task Updates_graph() + { + var country = await dbContext.Countries.FirstOrDefaultAsync(); + + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + var store = new Store { Name = "Store Number 1" }; + company.Stores.Add(store); + + await repository.AddAsync(company); + + var spec = new GetCompanyWithStoresSpec(company.Id); + var existingCompany = await repository.FirstOrDefaultAsync(spec); + existingCompany.Name = "Updated company name"; + var existingStore = existingCompany.Stores.FirstOrDefault(); + existingStore.Name = "Updated Store Name"; + + await repository.UpdateAsync(existingCompany); + + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); + + var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); + Assert.Equal(validationStore.Name, existingStore.Name); + } + + [Fact] + public async Task Updates_graph_across_context_instances() { var contextFactory = serviceProvider.GetService>(); var companyRetrievalRepository = new ContextFactoryRepository(contextFactory); @@ -89,5 +131,20 @@ public async Task Updates_graph() var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); Assert.Equal(validationStore.Name, existingStore.Name); } + + [Fact] + public async Task Deletes_entity() + { + var country = await dbContext.Countries.FirstOrDefaultAsync(); + + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + await repository.AddAsync(company); + + var companyId = company.Id; + await repository.DeleteAsync(company); + + var validationCompany = await repository.GetByIdAsync(companyId); + Assert.Null(validationCompany); + } } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs new file mode 100644 index 00000000..c30df303 --- /dev/null +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs @@ -0,0 +1,91 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Internal; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +{ + public class EFRepositoryFactoryTests : IClassFixture + { + protected TestDbContext dbContext; + protected IServiceProvider serviceProvider; + protected IRepositoryFactory> repositoryFactory; + protected IDbContextFactory contextFactory; + + public EFRepositoryFactoryTests(SharedDatabaseFixture fixture) + { + dbContext = fixture.CreateContext(); + + serviceProvider = new ServiceCollection() + .AddDbContextFactory((builder => builder.UseSqlServer(fixture.Connection)), + ServiceLifetime.Transient).BuildServiceProvider(); + + contextFactory = serviceProvider.GetService>(); + repositoryFactory = + new EFRepositoryFactory, Repository, TestDbContext>(contextFactory); + } + + [Fact] + public async Task Saves_new_entity() + { + var repository = repositoryFactory.CreateRepository(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); + + var company = new Company(); + company.Name = "Test save new company name"; + company.CountryId = country.Id; + + await repository.AddAsync(company); + Assert.NotEqual(0, company.Id); + } + + [Fact] + public async Task Updates_existing_entity() + { + var repository = repositoryFactory.CreateRepository(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); + + var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; + await repository.AddAsync(company); + + var existingCompany = await repository.GetByIdAsync(company.Id); + existingCompany.Name = "Updated company name"; + await repository.UpdateAsync(existingCompany); + + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); + } + + [Fact] + public async Task Updates_graph() + { + var repository = repositoryFactory.CreateRepository(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); + + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + var store = new Store { Name = "Store Number 1" }; + company.Stores.Add(store); + + await repository.AddAsync(company); + + var spec = new GetCompanyWithStoresSpec(company.Id); + var existingCompany = await repository.FirstOrDefaultAsync(spec); + existingCompany.Name = "Updated company name"; + var existingStore = existingCompany.Stores.FirstOrDefault(); + existingStore.Name = "Updated Store Name"; + + await repository.UpdateAsync(existingCompany); + + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); + + var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); + Assert.Equal(validationStore.Name, existingStore.Name); + } + } +} diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs index 191ad62c..cc82da8a 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs @@ -5,6 +5,10 @@ public class Repository : RepositoryBase where T : class { protected readonly TestDbContext dbContext; + public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) + { + } + public Repository(TestDbContext dbContext, ISpecificationEvaluator specificationEvaluator) : base(dbContext, specificationEvaluator) { this.dbContext = dbContext;