Skip to content

Commit 12e9e05

Browse files
committed
Add default testcontainer support in the tests
When the environment variable isn't defined
1 parent d038259 commit 12e9e05

File tree

8 files changed

+27
-17
lines changed

8 files changed

+27
-17
lines changed

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: 21 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,23 @@ 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+
ConnectionString = connectionString;
30+
}
31+
else
32+
{
33+
_postgreSqlContainer = new PostgreSqlBuilder("postgres:latest")
34+
.Build();
35+
_postgreSqlContainer.StartAsync().GetAwaiter().GetResult();
36+
ConnectionString = _postgreSqlContainer.GetConnectionString();
2337

24-
private const string DefaultConnectionString = "Server=localhost;Username=npgsql_tests;Password=npgsql_tests;Port=5432";
38+
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
39+
_postgreSqlContainer.DisposeAsync().AsTask().GetAwaiter().GetResult();
40+
}
2541

26-
public static string DefaultConnection
27-
=> Config["DefaultConnection"] ?? DefaultConnectionString;
42+
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
43+
}
2844

2945
private static Version? _postgresVersion;
3046

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)