Skip to content

Commit

Permalink
Added TestContainers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiseni committed Sep 29, 2024
1 parent bfbc523 commit dec082c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build and Test

on:
workflow_dispatch:
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.x
dotnet-quality: 'preview'
- name: Build
run: dotnet build --configuration Release
- name: Test
run: dotnet test --configuration Release --no-build --no-restore
9 changes: 5 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>

<ItemGroup Label="Package dependencies">
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" />
</ItemGroup>

<ItemGroup Label="Test projects dependencies">
<PackageVersion Include="ManagedObjectSize.ObjectPool" Version="0.0.7-gd53ba9da59" />
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="MartinCostello.SqlLocalDb" Version="3.4.0" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageVersion Include="Respawn" Version="6.2.1" />
<PackageVersion Include="Testcontainers.MsSql" Version="3.10.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="xunit" Version="2.9.0" />
Expand All @@ -27,5 +28,5 @@
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
</ItemGroup>
</Project>

</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using MartinCostello.SqlLocalDb;
using Respawn;
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 string _connectionString = default!;
private MsSqlContainer? _dbContainer = null;
private Respawner _respawner = default!;

public DbContextOptions<TestDbContext> DbContextOptions { get; private set; } = default!;
Expand All @@ -16,9 +21,16 @@ public async Task InitializeAsync()
{
using (var localDB = new SqlLocalDbApi())
{
_connectionString = localDB.IsLocalDBInstalled()
? $"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=QuerySpecificationTestsDB;Integrated Security=SSPI;TrustServerCertificate=True;"
: $"Data Source=databaseEF;Initial Catalog=QuerySpecificationTestsDB;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!;Encrypt=False;TrustServerCertificate=True;";
if (_forceDocker || !localDB.IsLocalDBInstalled())
{
_dbContainer = CreateContainer();
await _dbContainer.StartAsync();
_connectionString = _dbContainer.GetConnectionString();
}
else
{
_connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=QuerySpecificationTestsDB;Integrated Security=SSPI;TrustServerCertificate=True;";
}
}

Console.WriteLine($"Connection string: {_connectionString}");
Expand All @@ -43,11 +55,22 @@ public async Task InitializeAsync()
await ResetDatabase();
}

public Task DisposeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
if (_dbContainer is not null)
{
await _dbContainer.StopAsync();
}
else
{
//using var dbContext = new TestDbContext(DbContextOptions);
//await dbContext.Database.EnsureDeletedAsync();
}
}

//public async Task DisposeAsync()
//{
// using var dbContext = new TestDbContext(DbContextOptions);
// await dbContext.Database.EnsureDeletedAsync();
//}
private static MsSqlContainer CreateContainer() => new MsSqlBuilder()
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
.WithName("QuerySpecificationTestsDB")
.WithPassword("P@ssW0rd!")
.Build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<PackageReference Include="MartinCostello.SqlLocalDb" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Respawn" />
<PackageReference Include="Testcontainers.MsSql" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit dec082c

Please sign in to comment.