Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ RUN /bin/bash -c 'ls -la /wait; chmod +x /wait; ls -la /wait'
RUN dotnet tool install dotnet-reportgenerator-globaltool --version 5.1.23 --tool-path /tools

CMD /wait && \
dotnet build ci.slnf --configuration release && \
dotnet test -f net7.0 ci.slnf --configuration release --no-build --no-restore --collect:"xplat code coverage" && \
dotnet build ci.slnf --configuration Release && \
dotnet test -f net7.0 ci.slnf --configuration Release --no-build --no-restore --collect:"xplat code coverage" && \
tools/reportgenerator -reports:Specification*/**/coverage.cobertura.xml -targetdir:/var/temp/TestResults -assemblyfilters:"-*Tests*;"
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="MartinCostello.SqlLocalDb" Version="3.2.0" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

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

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

}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
using MartinCostello.SqlLocalDb;
using System;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;

public class DatabaseFixture : IDisposable
{
public string ConnectionString { get; }

public DatabaseFixture()
{
var databaseName = $"SpecificationTestsDB_{Guid.NewGuid().ToString().Replace('-', '_')}";

using (var localDB = new SqlLocalDbApi())
{
ConnectionString = localDB.IsLocalDBInstalled()
? $"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog={databaseName};Integrated Security=SSPI;MultipleActiveResultSets=True;Connection Timeout=300"
: $"Data Source=databaseEF;Initial Catalog={databaseName};PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!;";
}

Console.WriteLine($"Connection string: {ConnectionString}");

using var dbContext = new TestDbContext(ConnectionString);
dbContext.Database.Delete();
dbContext.Database.Create();

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 void Dispose()
{
using var dbContext = new TestDbContext(ConnectionString);
dbContext.Database.Delete();
GC.SuppressFinalize(this);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using Ardalis.Specification.UnitTests.Fixture.Entities;
using System;
using System.Data.Common;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;

public class TestDbContext : DbContext
{
public TestDbContext(DbConnection connection) : base(connection, false)
public TestDbContext(string connectionString) : base(connectionString)
{
Database.SetInitializer(new DbInitializer());
}

public virtual DbSet<Country> Countries { get; set; }
Expand All @@ -23,16 +18,12 @@ public TestDbContext(DbConnection connection) : base(connection, false)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !string.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType
&& type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
modelBuilder.Entity<Country>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Company>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Address>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Product>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
modelBuilder.Entity<Store>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Store>().HasOptional(x => x.Address).WithRequired(x => x!.Store!);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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;
Expand All @@ -7,30 +8,45 @@

namespace Ardalis.Specification.EntityFramework6.IntegrationTests;

public class RepositoryOfT_AnyAsync : IntegrationTestBase
[Collection("ReadCollection")]
public class RepositoryOfT_AnyAsync
{
public RepositoryOfT_AnyAsync(SharedDatabaseFixture fixture) : base(fixture) { }
private readonly string _connectionString;

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

[Fact]
public async Task ReturnsTrueOnStoresRecords_WithoutSpec()
{
var result = await storeRepository.AnyAsync();
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);

var result = await repo.AnyAsync();

result.Should().BeTrue();
}

[Fact]
public async Task ReturnsTrue_GivenStoreByIdSpecWithValidStore()
{
var result = await storeRepository.AnyAsync(new StoreByIdSpec(StoreSeed.VALID_STORE_ID));
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);

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

result.Should().BeTrue();
}

[Fact]
public async Task ReturnsFalse_GivenStoreByIdSpecWithInvalidStore()
{
var result = await storeRepository.AnyAsync(new StoreByIdSpec(0));
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);

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

result.Should().BeFalse();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture;
using Ardalis.Specification.UnitTests.Fixture.Entities;
using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds;
using FluentAssertions;
using System.Threading.Tasks;
using Xunit;

namespace Ardalis.Specification.EntityFramework6.IntegrationTests;

public class RepositoryOfT_GetById : IntegrationTestBase
[Collection("ReadCollection")]
public class RepositoryOfT_GetById
{
public RepositoryOfT_GetById(SharedDatabaseFixture fixture) : base(fixture) { }
private readonly string _connectionString;

public RepositoryOfT_GetById(DatabaseFixture fixture)
{
_connectionString = fixture.ConnectionString;
}

[Fact]
public async Task ReturnsStore_GivenId()
{
var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID);
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);

var result = await repo.GetByIdAsync(StoreSeed.VALID_STORE_ID);

result.Should().NotBeNull();
result.Name.Should().Be(StoreSeed.VALID_STORE_NAME);
Expand All @@ -22,7 +32,10 @@ public async Task ReturnsStore_GivenId()
[Fact]
public async Task ReturnsStore_GivenGenericId()
{
var result = await storeRepository.GetByIdAsync<int>(StoreSeed.VALID_STORE_ID);
using var dbContext = new TestDbContext(_connectionString);
var repo = new Repository<Store>(dbContext);

var result = await repo.GetByIdAsync<int>(StoreSeed.VALID_STORE_ID);

result.Should().NotBeNull();
result.Name.Should().Be(StoreSeed.VALID_STORE_NAME);
Expand Down
Loading