Skip to content

Commit

Permalink
Add more integration tests for ContextFactoryRepositoryBaseOfT and te…
Browse files Browse the repository at this point in the history
…sts for EFRepositoryFactory
  • Loading branch information
jasonsummers committed Oct 16, 2022
1 parent 78a2361 commit 79617dd
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDbContextFactory<TestDbContext>>();
var companyRetrievalRepository = new ContextFactoryRepository<Company, TestDbContext>(contextFactory);
Expand All @@ -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<IDbContextFactory<TestDbContext>>();
var companyRetrievalRepository = new ContextFactoryRepository<Company, TestDbContext>(contextFactory);
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<SharedDatabaseFixture>
{
protected TestDbContext dbContext;
protected IServiceProvider serviceProvider;
protected IRepositoryFactory<IRepositoryBase<Company>> repositoryFactory;
protected IDbContextFactory<TestDbContext> contextFactory;

public EFRepositoryFactoryTests(SharedDatabaseFixture fixture)
{
dbContext = fixture.CreateContext();

serviceProvider = new ServiceCollection()
.AddDbContextFactory<TestDbContext>((builder => builder.UseSqlServer(fixture.Connection)),
ServiceLifetime.Transient).BuildServiceProvider();

contextFactory = serviceProvider.GetService<IDbContextFactory<TestDbContext>>();
repositoryFactory =
new EFRepositoryFactory<IRepositoryBase<Company>, Repository<Company>, 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public class Repository<T> : RepositoryBase<T> 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;
Expand Down

0 comments on commit 79617dd

Please sign in to comment.