Skip to content

Commit dec082c

Browse files
committed
Added TestContainers.
1 parent bfbc523 commit dec082c

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

.github/workflows/ci2.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Build and Test
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches:
7+
- main
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
- name: Setup dotnet
17+
uses: actions/setup-dotnet@v4
18+
with:
19+
dotnet-version: 9.x
20+
dotnet-quality: 'preview'
21+
- name: Build
22+
run: dotnet build --configuration Release
23+
- name: Test
24+
run: dotnet test --configuration Release --no-build --no-restore

Directory.Packages.props

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
55
</PropertyGroup>
6-
6+
77
<ItemGroup Label="Package dependencies">
88
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
99
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" />
1010
</ItemGroup>
11-
11+
1212
<ItemGroup Label="Test projects dependencies">
1313
<PackageVersion Include="ManagedObjectSize.ObjectPool" Version="0.0.7-gd53ba9da59" />
1414
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
1515
<PackageVersion Include="AutoMapper" Version="13.0.1" />
1616
<PackageVersion Include="MartinCostello.SqlLocalDb" Version="3.4.0" />
1717
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
1818
<PackageVersion Include="Respawn" Version="6.2.1" />
19+
<PackageVersion Include="Testcontainers.MsSql" Version="3.10.0" />
1920
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
2021
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
2122
<PackageVersion Include="xunit" Version="2.9.0" />
@@ -27,5 +28,5 @@
2728
<PackageVersion Include="System.Formats.Asn1" Version="8.0.1" />
2829
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
2930
</ItemGroup>
30-
31-
</Project>
31+
32+
</Project>

tests/QuerySpecification.EntityFrameworkCore.Tests/Fixture/TestFactory.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using MartinCostello.SqlLocalDb;
22
using Respawn;
3+
using Testcontainers.MsSql;
34

45
namespace Tests.Fixture;
56

67
public class TestFactory : IAsyncLifetime
78
{
9+
// Flag to force using Docker SQL Server. Update it manually if you want to avoid localDb locally.
10+
private const bool _forceDocker = false;
11+
812
private string _connectionString = default!;
13+
private MsSqlContainer? _dbContainer = null;
914
private Respawner _respawner = default!;
1015

1116
public DbContextOptions<TestDbContext> DbContextOptions { get; private set; } = default!;
@@ -16,9 +21,16 @@ public async Task InitializeAsync()
1621
{
1722
using (var localDB = new SqlLocalDbApi())
1823
{
19-
_connectionString = localDB.IsLocalDBInstalled()
20-
? $"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=QuerySpecificationTestsDB;Integrated Security=SSPI;TrustServerCertificate=True;"
21-
: $"Data Source=databaseEF;Initial Catalog=QuerySpecificationTestsDB;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!;Encrypt=False;TrustServerCertificate=True;";
24+
if (_forceDocker || !localDB.IsLocalDBInstalled())
25+
{
26+
_dbContainer = CreateContainer();
27+
await _dbContainer.StartAsync();
28+
_connectionString = _dbContainer.GetConnectionString();
29+
}
30+
else
31+
{
32+
_connectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=QuerySpecificationTestsDB;Integrated Security=SSPI;TrustServerCertificate=True;";
33+
}
2234
}
2335

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

46-
public Task DisposeAsync() => Task.CompletedTask;
58+
public async Task DisposeAsync()
59+
{
60+
if (_dbContainer is not null)
61+
{
62+
await _dbContainer.StopAsync();
63+
}
64+
else
65+
{
66+
//using var dbContext = new TestDbContext(DbContextOptions);
67+
//await dbContext.Database.EnsureDeletedAsync();
68+
}
69+
}
4770

48-
//public async Task DisposeAsync()
49-
//{
50-
// using var dbContext = new TestDbContext(DbContextOptions);
51-
// await dbContext.Database.EnsureDeletedAsync();
52-
//}
71+
private static MsSqlContainer CreateContainer() => new MsSqlBuilder()
72+
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
73+
.WithName("QuerySpecificationTestsDB")
74+
.WithPassword("P@ssW0rd!")
75+
.Build();
5376
}

tests/QuerySpecification.EntityFrameworkCore.Tests/QuerySpecification.EntityFrameworkCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<PackageReference Include="MartinCostello.SqlLocalDb" />
66
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
77
<PackageReference Include="Respawn" />
8+
<PackageReference Include="Testcontainers.MsSql" />
89
</ItemGroup>
910

1011
<ItemGroup>

0 commit comments

Comments
 (0)