Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: ensure database initialization before OneTimeSetup of IntegrationTestsBase children runs #175

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion LeaderboardBackend.Test/Fixtures/IntegrationTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ public abstract class IntegrationTestsBase
{
protected HttpClient Client { get; private set; } = null!;

protected readonly static TestApiFactory s_factory = new();
protected static readonly TestApiFactory s_factory = new();

[OneTimeSetUp]
public void OneTimeSetup()
{
s_factory.InitializeDatabase();
}

[SetUp]
public void SetUp()
Expand Down
4 changes: 2 additions & 2 deletions LeaderboardBackend.Test/LeaderboardBackend.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Testcontainers" Version="3.2.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.2.0" />
<PackageReference Include="Testcontainers" Version="3.4.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
50 changes: 33 additions & 17 deletions LeaderboardBackend.Test/TestApi/TestApiFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -60,22 +59,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
using IServiceScope scope = services.BuildServiceProvider().CreateScope();
ApplicationContext dbContext =
scope.ServiceProvider.GetRequiredService<ApplicationContext>();

switch (TestConfig.DatabaseBackend)
{
case DatabaseBackend.TestContainer when !PostgresDatabaseFixture.HasCreatedTemplate:
dbContext.MigrateDatabase();
Seed(dbContext);
PostgresDatabaseFixture.CreateTemplateFromCurrentDb();
break;
case DatabaseBackend.InMemory:
if (dbContext.Database.EnsureCreated())
{
Seed(dbContext);
}

break;
}
InitializeDatabase(dbContext);
});
}

Expand All @@ -85,6 +69,38 @@ public TestApiClient CreateTestApiClient()
return new TestApiClient(client);
}

public void InitializeDatabase()
{
using IServiceScope scope = Services.CreateScope();
ApplicationContext dbContext = scope.ServiceProvider.GetRequiredService<ApplicationContext>();
InitializeDatabase(dbContext);
}

private static void InitializeDatabase(ApplicationContext dbContext)
{
switch (TestConfig.DatabaseBackend)
{
case DatabaseBackend.TestContainer:
if (!PostgresDatabaseFixture.HasCreatedTemplate)
{
dbContext.MigrateDatabase();
Seed(dbContext);
PostgresDatabaseFixture.CreateTemplateFromCurrentDb();
}

break;
case DatabaseBackend.InMemory:
TheTedder marked this conversation as resolved.
Show resolved Hide resolved
if (dbContext.Database.EnsureCreated())
{
Seed(dbContext);
}

break;
default:
throw new NotImplementedException("Database initialization is not implemented");
}
}

private static void Seed(ApplicationContext dbContext)
{
Leaderboard leaderboard =
Expand Down
Loading