Skip to content

Commit

Permalink
upgrade Testcontainers to v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dalet committed Mar 8, 2023
1 parent f0164cd commit 45b1b3e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
39 changes: 20 additions & 19 deletions LeaderboardBackend.Test/Fixtures/PostgresDatabaseFixture.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using LeaderboardBackend.Test.Fixtures;
using Npgsql;
using NUnit.Framework;
using Testcontainers.PostgreSql;

// Fixtures apply to all tests in its namespace
// It has no namespace on purpose, so that the fixture applies to all tests in this assembly

[SetUpFixture] // https://docs.nunit.org/articles/nunit/writing-tests/attributes/setupfixture.html
internal class PostgresDatabaseFixture
{
public static TestcontainerDatabase? PostgresContainer { get; private set; }
public static PostgreSqlContainer? PostgresContainer { get; private set; }
public static string? Database { get; private set; }
public static string? Username { get; private set; }
public static string? Password { get; private set; }
public static int Port { get; private set; }
public static bool HasCreatedTemplate { get; private set; } = false;

private static string TemplateDatabase => PostgresContainer?.Database + "_template";
private static string TemplateDatabase => Database! + "_template";

[OneTimeSetUp]
public static async Task OneTimeSetup()
Expand All @@ -26,16 +27,16 @@ public static async Task OneTimeSetup()
return;
}

PostgresContainer = new ContainerBuilder<PostgreSqlTestcontainer>()
.WithDatabase(new PostgreSqlTestcontainerConfiguration
{
Database = "db",
Username = "dbuser",
Password = "weft5gst768sr",
})
PostgresContainer = new PostgreSqlBuilder()
.WithTmpfsMount("/var/lib/postgresql/data") // db files in-memory
.Build();
await PostgresContainer.StartAsync();

NpgsqlConnectionStringBuilder connStrBuilder = new(PostgresContainer.GetConnectionString());
Username = connStrBuilder.Username!;
Password = connStrBuilder.Password!;
Database = connStrBuilder.Database!;
Port = connStrBuilder.Port;
}

[OneTimeTearDown]
Expand All @@ -58,8 +59,8 @@ public static void CreateTemplateFromCurrentDb()
conn.CreateCommand(@$"
DROP DATABASE IF EXISTS {TemplateDatabase};
CREATE DATABASE {TemplateDatabase}
WITH TEMPLATE {PostgresContainer!.Database}
OWNER '{PostgresContainer!.Username}';
WITH TEMPLATE {Database}
OWNER '{Username}';
")
.ExecuteNonQuery();
HasCreatedTemplate = true;
Expand All @@ -78,18 +79,18 @@ public static void ResetDatabaseToTemplate()
NpgsqlConnection.ClearAllPools(); // can't drop a DB if connections remain open
using NpgsqlDataSource conn = CreateConnectionToTemplate();
conn.CreateCommand(@$"
DROP DATABASE IF EXISTS {PostgresContainer!.Database};
CREATE DATABASE {PostgresContainer!.Database}
DROP DATABASE IF EXISTS {Database};
CREATE DATABASE {Database}
WITH TEMPLATE {TemplateDatabase}
OWNER '{PostgresContainer!.Username}';
OWNER '{Username}';
")
.ExecuteNonQuery();
}

private static NpgsqlDataSource CreateConnectionToTemplate()
{
ThrowIfNotInitialized();
NpgsqlConnectionStringBuilder connStrBuilder = new(PostgresContainer!.ConnectionString)
NpgsqlConnectionStringBuilder connStrBuilder = new(PostgresContainer!.GetConnectionString())
{
Database = "template1"
};
Expand Down
3 changes: 2 additions & 1 deletion LeaderboardBackend.Test/LeaderboardBackend.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Testcontainers" Version="2.4.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions LeaderboardBackend.Test/TestApi/TestApiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
conf.UseInMemoryDb = false;
conf.Pg = new PostgresConfig
{
Db = PostgresDatabaseFixture.PostgresContainer.Database,
Port = (ushort)PostgresDatabaseFixture.PostgresContainer.Port,
Db = PostgresDatabaseFixture.Database!,
Port = (ushort)PostgresDatabaseFixture.Port,
Host = PostgresDatabaseFixture.PostgresContainer.Hostname,
User = PostgresDatabaseFixture.PostgresContainer.Username,
Password = PostgresDatabaseFixture.PostgresContainer.Password
User = PostgresDatabaseFixture.Username!,
Password = PostgresDatabaseFixture.Password!
};
});
}
Expand Down

0 comments on commit 45b1b3e

Please sign in to comment.