Skip to content

Commit

Permalink
Refactor the test suite (#437)
Browse files Browse the repository at this point in the history
* Refactored the test infrastructure and all tests.

* Remove DatabaseFixture.
  • Loading branch information
fiseni authored Feb 13, 2025
1 parent 0600c1d commit 237f2e2
Show file tree
Hide file tree
Showing 198 changed files with 4,997 additions and 3,039 deletions.
6 changes: 3 additions & 3 deletions Ardalis.Specification.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ VisualStudioVersion = 17.0.31612.314
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification", "Specification\src\Ardalis.Specification\Ardalis.Specification.csproj", "{5FF21FE4-19B9-4BD2-958D-EAFEDE72D7F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.UnitTests", "Specification\tests\Ardalis.Specification.UnitTests\Ardalis.Specification.UnitTests.csproj", "{85D42214-D223-4F13-9A19-9CFA0D3C1981}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.Tests", "Specification\tests\Ardalis.Specification.Tests\Ardalis.Specification.Tests.csproj", "{85D42214-D223-4F13-9A19-9CFA0D3C1981}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.EntityFrameworkCore", "Specification.EntityFrameworkCore\src\Ardalis.Specification.EntityFrameworkCore\Ardalis.Specification.EntityFrameworkCore.csproj", "{5EB4ADA4-5258-4964-BD7D-11D4F926E344}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.EntityFrameworkCore.IntegrationTests", "Specification.EntityFrameworkCore\tests\Ardalis.Specification.EntityFrameworkCore.IntegrationTests\Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj", "{5AFD1454-E625-451D-A615-CEB7BB09AA65}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.EntityFrameworkCore.Tests", "Specification.EntityFrameworkCore\tests\Ardalis.Specification.EntityFrameworkCore.Tests\Ardalis.Specification.EntityFrameworkCore.Tests.csproj", "{5AFD1454-E625-451D-A615-CEB7BB09AA65}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docker", "Docker", "{A74A4C02-5F33-4667-A627-78A5B295A61F}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -33,7 +33,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample", "Sample", "{1FCFDF
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.EntityFramework6", "Specification.EntityFramework6\src\Ardalis.Specification.EntityFramework6\Ardalis.Specification.EntityFramework6.csproj", "{37EC09C7-702D-4539-B98D-F67B15E1E6CE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.EntityFramework6.IntegrationTests", "Specification.EntityFramework6\tests\Ardalis.Specification.EntityFramework6.IntegrationTests\Ardalis.Specification.EntityFramework6.IntegrationTests.csproj", "{4BEB4DC4-DE33-4DF1-8A2F-CE76C1D72A4A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Specification.EntityFramework6.Tests", "Specification.EntityFramework6\tests\Ardalis.Specification.EntityFramework6.Tests\Ardalis.Specification.EntityFramework6.Tests.csproj", "{4BEB4DC4-DE33-4DF1-8A2F-CE76C1D72A4A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Specification", "Specification", "{9CA909ED-E3A6-4D76-A4F3-250E93942813}"
EndProject
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<LangVersion>13.0</LangVersion>
<Nullable>enable</Nullable>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
Expand All @@ -14,6 +15,7 @@
<PackageReference Include="MartinCostello.SqlLocalDb" Version="3.3.0" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="Testcontainers.MsSql" Version="4.2.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.analyzers" Version="1.6.0" />
Expand All @@ -25,11 +27,15 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Polyfill" Version="7.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Specification\src\Ardalis.Specification\Ardalis.Specification.csproj" />
<ProjectReference Include="..\..\..\Specification\tests\Ardalis.Specification.UnitTests\Ardalis.Specification.UnitTests.csproj" />
<ProjectReference Include="..\..\..\Specification\tests\Ardalis.Specification.Tests\Ardalis.Specification.Tests.csproj" />
<ProjectReference Include="..\..\src\Ardalis.Specification.EntityFramework6\Ardalis.Specification.EntityFramework6.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Tests.Fixture;

[CollectionDefinition("ReadCollection")]
public class ReadCollection : ICollectionFixture<TestFactory>
{
public ReadCollection()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Tests.Fixture;

[CollectionDefinition("WriteCollection")]
public class WriteCollection : ICollectionFixture<TestFactory>
{
public WriteCollection()
{

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Ardalis.Specification.UnitTests.Fixture.Entities;
namespace Tests.Fixture;

public class Address
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities;
namespace Tests.Fixture;

public class Company
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities;
namespace Tests.Fixture;

public class Country
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Ardalis.Specification.UnitTests.Fixture.Entities;
namespace Tests.Fixture;

public class Product
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
namespace Tests.Fixture;

public class AddressSeed
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
namespace Tests.Fixture;

public class CompanySeed
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
namespace Tests.Fixture;

public class CountrySeed
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
namespace Tests.Fixture;

public class ProductSeed
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
namespace Tests.Fixture;

public class StoreSeed
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace Ardalis.Specification.UnitTests.Fixture.Entities;
namespace Tests.Fixture;

public class Store
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;
namespace Tests.Fixture;

/// <inheritdoc/>
public class Repository<T> : RepositoryBase<T> where T : class
{
protected readonly TestDbContext dbContext;

public Repository(TestDbContext dbContext) : this(dbContext, EntityFramework6.SpecificationEvaluator.Default)
public Repository(TestDbContext dbContext) : this(dbContext, Ardalis.Specification.EntityFramework6.SpecificationEvaluator.Default)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using Ardalis.Specification.UnitTests.Fixture.Entities;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;
namespace Tests.Fixture;

public class TestDbContext : DbContext
{
public TestDbContext(string connectionString) : base(connectionString)
{
}

public virtual DbSet<Country> Countries { get; set; }
public virtual DbSet<Company> Companies { get; set; }
public virtual DbSet<Store> Stores { get; set; }
public virtual DbSet<Address> Addresses { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Country> Countries { get; set; } = null!;
public virtual DbSet<Company> Companies { get; set; } = null!;
public virtual DbSet<Store> Stores { get; set; } = null!;
public virtual DbSet<Address> Addresses { get; set; } = null!;
public virtual DbSet<Product> Products { get; set; } = null!;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using MartinCostello.SqlLocalDb;
using System;
using System.Threading.Tasks;
using Testcontainers.MsSql;

namespace Tests.Fixture;

public class TestFactory : IAsyncLifetime
{
// Flag to force using Docker SQL Server. Update it manually if you want to avoid localDb locally.
private const bool _forceDocker = false;
private MsSqlContainer? _dbContainer = null;

public string ConnectionString { get; private set; } = null!;

public async Task InitializeAsync()
{
using (var localDB = new SqlLocalDbApi())
{
if (_forceDocker || !localDB.IsLocalDBInstalled())
{
_dbContainer = CreateContainer();
await _dbContainer.StartAsync();
ConnectionString = _dbContainer.GetConnectionString();
}
else
{
var databaseName = $"SpecificationEF6TestsDB_{Guid.NewGuid().ToString().Replace('-', '_')}";
ConnectionString = $"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog={databaseName};Integrated Security=SSPI;TrustServerCertificate=True;";
}
}

using var dbContext = new TestDbContext(ConnectionString);

dbContext.Database.CreateIfNotExists();

dbContext.Countries.AddRange(CountrySeed.Get());
dbContext.Companies.AddRange(CompanySeed.Get());
dbContext.Stores.AddRange(StoreSeed.Get());
dbContext.Addresses.AddRange(AddressSeed.Get());
dbContext.Products.AddRange(ProductSeed.Get());

dbContext.SaveChanges();
}

public async Task DisposeAsync()
{
if (_dbContainer is not null)
{
await _dbContainer.StopAsync();
}
else
{
using var dbContext = new TestDbContext(ConnectionString);
dbContext.Database.Delete();
}
}

private static MsSqlContainer CreateContainer() => new MsSqlBuilder()
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
//.WithName("SpecificationEFCoreTestsDB")
.WithPassword("P@ssW0rd!")
.Build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using Ardalis.Specification;
global using Ardalis.Specification.EntityFramework6;
global using FluentAssertions;
global using System.Collections.Generic;
global using System.Linq;
global using Tests.Fixture;
global using Xunit;
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;
using Ardalis.Specification.UnitTests.Fixture.Entities;
using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
using Ardalis.Specification.UnitTests.Fixture.Specs;
using FluentAssertions;
using System.Threading.Tasks;
using Xunit;
using System.Threading.Tasks;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests;
namespace Tests;

[Collection("ReadCollection")]
public class RepositoryOfT_AnyAsync
{
private readonly string _connectionString;

public RepositoryOfT_AnyAsync(DatabaseFixture fixture)
public RepositoryOfT_AnyAsync(TestFactory factory)
{
_connectionString = fixture.ConnectionString;
_connectionString = factory.ConnectionString;
}

[Fact]
Expand All @@ -34,8 +28,10 @@ public async Task ReturnsTrue_GivenStoreByIdSpecWithValidStore()
{
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);
var spec = new Specification<Store>();
spec.Query.Where(x => x.Id == StoreSeed.VALID_STORE_ID);

var result = await repo.AnyAsync(new StoreByIdSpec(StoreSeed.VALID_STORE_ID));
var result = await repo.AnyAsync(spec);

result.Should().BeTrue();
}
Expand All @@ -45,8 +41,10 @@ public async Task ReturnsFalse_GivenStoreByIdSpecWithInvalidStore()
{
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);
var spec = new Specification<Store>();
spec.Query.Where(x => x.Id == 0);

var result = await repo.AnyAsync(new StoreByIdSpec(0));
var result = await repo.AnyAsync(spec);

result.Should().BeFalse();
}
Expand Down
Loading

0 comments on commit 237f2e2

Please sign in to comment.