Skip to content

Commit 7c3cdb2

Browse files
authored
Add default testcontainer support in the tests (#3753)
When the environment variable isn't defined
1 parent d038259 commit 7c3cdb2

File tree

9 files changed

+43
-17
lines changed

9 files changed

+43
-17
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ jobs:
113113
- name: Test
114114
run: dotnet test -c ${{ matrix.config }} --logger "GitHubActions;report-warnings=false"
115115
shell: bash
116+
env:
117+
Test__Npgsql__DefaultConnection: Server=localhost;Username=npgsql_tests;Password=npgsql_tests
116118

117119
- id: analyze_tag
118120
name: Analyze tag

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
<PackageVersion Include="xunit.core" Version="2.9.3" />
3434
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
3535
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
36+
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.10.0" />
3637
</ItemGroup>
3738
</Project>

test/EFCore.PG.FunctionalTests/EFCore.PG.FunctionalTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Specification.Tests" />
1919
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
2020
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
21+
<PackageReference Include="Testcontainers.PostgreSql" />
2122
</ItemGroup>
2223

2324
<ItemGroup>
2425
<None Update="Northwind.sql" CopyToOutputDirectory="PreserveNewest" />
25-
<None Update="config.json" CopyToOutputDirectory="PreserveNewest" />
2626
</ItemGroup>
2727

2828
</Project>

test/EFCore.PG.FunctionalTests/Migrations/MigrationsInfrastructureNpgsqlTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public class Post
217217
public class BloggingContext : DbContext
218218
{
219219
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
220-
=> optionsBuilder.UseNpgsql(TestEnvironment.DefaultConnection);
220+
=> optionsBuilder.UseNpgsql(TestEnvironment.ConnectionString);
221221

222222
public DbSet<Blog> Blogs { get; set; }
223223
}

test/EFCore.PG.FunctionalTests/Query/NavigationTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public NavigationTestFixture()
8383
.AddEntityFrameworkNpgsql()
8484
.BuildServiceProvider();
8585

86-
var connStrBuilder = new NpgsqlConnectionStringBuilder(TestEnvironment.DefaultConnection) { Database = "StateManagerBug" };
86+
var connStrBuilder = new NpgsqlConnectionStringBuilder(TestEnvironment.ConnectionString) { Database = "StateManagerBug" };
8787

8888
_options = new DbContextOptionsBuilder()
8989
.UseNpgsql(connStrBuilder.ConnectionString)

test/EFCore.PG.FunctionalTests/TestUtilities/NpgsqlTestStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ private static DbCommand CreateCommand(
420420

421421
public static string CreateConnectionString(string name, string? options = null)
422422
{
423-
var builder = new NpgsqlConnectionStringBuilder(TestEnvironment.DefaultConnection) { Database = name };
423+
var builder = new NpgsqlConnectionStringBuilder(TestEnvironment.ConnectionString) { Database = name };
424424

425425
if (options is not null)
426426
{

test/EFCore.PG.FunctionalTests/TestUtilities/TestEnvironment.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
using System.Globalization;
22
using Microsoft.Extensions.Configuration;
3+
using Testcontainers.PostgreSql;
34

45
namespace Microsoft.EntityFrameworkCore.TestUtilities;
56

67
public static class TestEnvironment
78
{
89
public static IConfiguration Config { get; }
910

11+
public static string ConnectionString { get; }
12+
13+
// Keep a reference to prevent GC from collecting (and finalizing/stopping) the container while tests are running.
14+
private static readonly PostgreSqlContainer? _postgreSqlContainer;
15+
1016
static TestEnvironment()
1117
{
1218
var configBuilder = new ConfigurationBuilder()
@@ -18,13 +24,37 @@ static TestEnvironment()
1824
Config = configBuilder.Build()
1925
.GetSection("Test:Npgsql");
2026

21-
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
22-
}
27+
if (Config["DefaultConnection"] is { } connectionString)
28+
{
29+
Console.WriteLine("Using connection string configured via Test:Npgsql:DefaultConnection: " + connectionString);
2330

24-
private const string DefaultConnectionString = "Server=localhost;Username=npgsql_tests;Password=npgsql_tests;Port=5432";
31+
ConnectionString = connectionString;
32+
}
33+
else
34+
{
35+
Console.WriteLine("No connection string configured via Test:Npgsql:DefaultConnection, starting up testcontainer...");
2536

26-
public static string DefaultConnection
27-
=> Config["DefaultConnection"] ?? DefaultConnectionString;
37+
_postgreSqlContainer = new PostgreSqlBuilder("postgres:latest")
38+
.WithCommand("-c", "max_connections=200")
39+
.Build();
40+
_postgreSqlContainer.StartAsync().GetAwaiter().GetResult();
41+
ConnectionString = _postgreSqlContainer.GetConnectionString();
42+
43+
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
44+
{
45+
try
46+
{
47+
_postgreSqlContainer?.DisposeAsync().AsTask().GetAwaiter().GetResult();
48+
}
49+
catch
50+
{
51+
// Ignore exceptions during process-exit cleanup.
52+
}
53+
};
54+
}
55+
56+
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
57+
}
2858

2959
private static Version? _postgresVersion;
3060

test/EFCore.PG.FunctionalTests/TestUtilities/TestNpgsqlRetryingExecutionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public TestNpgsqlRetryingExecutionStrategy()
1313
new DbContext(
1414
new DbContextOptionsBuilder()
1515
.EnableServiceProviderCaching(false)
16-
.UseNpgsql(TestEnvironment.DefaultConnection).Options),
16+
.UseNpgsql(TestEnvironment.ConnectionString).Options),
1717
DefaultMaxRetryCount, DefaultMaxDelay, AdditionalSqlStates)
1818
{
1919
}

test/EFCore.PG.FunctionalTests/config.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)