From 45b1b3ea858747dfd598435872e870afab3df3e5 Mon Sep 17 00:00:00 2001 From: Dalet Date: Wed, 8 Mar 2023 23:59:07 +0900 Subject: [PATCH] upgrade Testcontainers to v3.0.0 --- .../Fixtures/PostgresDatabaseFixture.cs | 39 ++++++++++--------- .../LeaderboardBackend.Test.csproj | 3 +- .../TestApi/TestApiFactory.cs | 8 ++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/LeaderboardBackend.Test/Fixtures/PostgresDatabaseFixture.cs b/LeaderboardBackend.Test/Fixtures/PostgresDatabaseFixture.cs index 6f64cd29..6fc97180 100644 --- a/LeaderboardBackend.Test/Fixtures/PostgresDatabaseFixture.cs +++ b/LeaderboardBackend.Test/Fixtures/PostgresDatabaseFixture.cs @@ -1,11 +1,9 @@ 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 @@ -13,10 +11,13 @@ [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() @@ -26,16 +27,16 @@ public static async Task OneTimeSetup() return; } - PostgresContainer = new ContainerBuilder() - .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] @@ -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; @@ -78,10 +79,10 @@ 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(); } @@ -89,7 +90,7 @@ WITH TEMPLATE {TemplateDatabase} private static NpgsqlDataSource CreateConnectionToTemplate() { ThrowIfNotInitialized(); - NpgsqlConnectionStringBuilder connStrBuilder = new(PostgresContainer!.ConnectionString) + NpgsqlConnectionStringBuilder connStrBuilder = new(PostgresContainer!.GetConnectionString()) { Database = "template1" }; diff --git a/LeaderboardBackend.Test/LeaderboardBackend.Test.csproj b/LeaderboardBackend.Test/LeaderboardBackend.Test.csproj index 475ea7e0..37350fbd 100644 --- a/LeaderboardBackend.Test/LeaderboardBackend.Test.csproj +++ b/LeaderboardBackend.Test/LeaderboardBackend.Test.csproj @@ -16,7 +16,8 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + + diff --git a/LeaderboardBackend.Test/TestApi/TestApiFactory.cs b/LeaderboardBackend.Test/TestApi/TestApiFactory.cs index 8cdb67a2..ca62ce5c 100644 --- a/LeaderboardBackend.Test/TestApi/TestApiFactory.cs +++ b/LeaderboardBackend.Test/TestApi/TestApiFactory.cs @@ -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! }; }); }