Skip to content

(#345) Added PostgreSql TestContainer fixture #350

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

Merged
merged 1 commit into from
Apr 26, 2025
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="9.0.4" />
<PackageVersion Include="TestContainers.MySql" Version="4.4.0" />
<PackageVersion Include="TestContainers.PostgreSql" Version="4.4.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using CommunityToolkit.Datasync.TestCommon;
using CommunityToolkit.Datasync.TestCommon.Databases;
using CommunityToolkit.Datasync.TestCommon.Fixtures;
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

Expand All @@ -13,19 +14,16 @@ namespace CommunityToolkit.Datasync.Server.EntityFrameworkCore.Test;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class PgEntityTableRepository_Tests(DatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<PgEntityMovie>, IAsyncLifetime
public class PgEntityTableRepository_Tests(PostgreSqlDatabaseFixture fixture, ITestOutputHelper output) : RepositoryTests<PgEntityMovie>, IClassFixture<PostgreSqlDatabaseFixture>, IAsyncLifetime
{
#region Setup
private readonly Random random = new();
private List<PgEntityMovie> movies = [];

public async Task InitializeAsync()
{
if (!string.IsNullOrEmpty(ConnectionStrings.PgSql))
{
Context = await PgDbContext.CreateContextAsync(ConnectionStrings.PgSql, output);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
}
Context = await PgDbContext.CreateContextAsync(fixture.ConnectionString, output);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
}

public async Task DisposeAsync()
Expand All @@ -38,7 +36,7 @@ public async Task DisposeAsync()

private PgDbContext Context { get; set; }

protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.PgSql);
protected override bool CanRunLiveTests() => true;

protected override async Task<PgEntityMovie> GetEntityAsync(string id)
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,24 @@
using CommunityToolkit.Datasync.Server.EntityFrameworkCore;
using CommunityToolkit.Datasync.Server.Test.Helpers;
using CommunityToolkit.Datasync.TestCommon.Databases;
using CommunityToolkit.Datasync.TestCommon.Fixtures;
using Microsoft.EntityFrameworkCore;
using Xunit.Abstractions;

namespace CommunityToolkit.Datasync.Server.Test.Live;

[ExcludeFromCodeCoverage]
[Collection("LiveTestsCollection")]
public class PgSQL_Controller_Tests(DatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<PgEntityMovie>, IAsyncLifetime
public class PgSQL_Controller_Tests(PostgreSqlDatabaseFixture fixture, ITestOutputHelper output) : LiveControllerTests<PgEntityMovie>, IClassFixture<PostgreSqlDatabaseFixture>, IAsyncLifetime
{
#region Setup
private readonly Random random = new();
private List<PgEntityMovie> movies = [];

public async Task InitializeAsync()
{
if (!string.IsNullOrEmpty(ConnectionStrings.PgSql))
{
// Note: we don't clear entities on every run to speed up the test runs. This can only be done because
// the tests are read-only (associated with the query and get capabilities). If the test being run writes
// to the database then change clearEntities to true.
output.WriteLine($"PgIsInitialized = {fixture.PgIsInitialized}");
Context = await PgDbContext.CreateContextAsync(ConnectionStrings.PgSql, output, clearEntities: !fixture.PgIsInitialized);
this.movies = Context.Movies.AsNoTracking().ToList();
fixture.PgIsInitialized = true;
}
Context = await PgDbContext.CreateContextAsync(fixture.ConnectionString, output);
this.movies = await Context.Movies.AsNoTracking().ToListAsync();
}

public async Task DisposeAsync()
Expand All @@ -44,7 +37,7 @@ public async Task DisposeAsync()

protected override string DriverName { get; } = "PgSQL";

protected override bool CanRunLiveTests() => !string.IsNullOrEmpty(ConnectionStrings.PgSql);
protected override bool CanRunLiveTests() => true;

protected override async Task<PgEntityMovie> GetEntityAsync(string id)
=> await Context.Movies.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" />
<PackageReference Include="TestContainers.MySql" />
<PackageReference Include="TestContainers.PostgreSql" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static class ConnectionStrings
public static readonly string CosmosDb = Environment.GetEnvironmentVariable("COSMOS_CONNECTION_STRING");
public static readonly string MongoCommunity = Environment.GetEnvironmentVariable("MONGOACI_CONNECTION_STRING");
public static readonly string CosmosMongo = Environment.GetEnvironmentVariable("MONGO_CONNECTION_STRING");
public static readonly string PgSql = Environment.GetEnvironmentVariable("PGSQL_CONNECTION_STRING");
public static readonly string Service = Environment.GetEnvironmentVariable("SERVICE_ENDPOINT");
public static readonly bool EnableLogging = (Environment.GetEnvironmentVariable("ENABLE_SQL_LOGGING") ?? "false") == "true";
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using DotNet.Testcontainers.Builders;
using Testcontainers.PostgreSql;
using Xunit;

namespace CommunityToolkit.Datasync.TestCommon.Fixtures;

/// <summary>
/// A test fixture for implementing a PostgreSQL database using Testcontainers.
/// </summary>
[ExcludeFromCodeCoverage]
public class PostgreSqlDatabaseFixture : IAsyncLifetime
{
private readonly PostgreSqlContainer _container;

public PostgreSqlDatabaseFixture()
{
this._container = new PostgreSqlBuilder()
.WithImage("postgres:17-alpine")
.WithCleanUp(true)
.WithUsername("testuser")
.WithPassword("testpassword")
.WithDatabase("testdb")
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(5432))
.Build();
}

/// <inheritdoc />
public async Task DisposeAsync()
{
if (this._container is not null)
{
await this._container.DisposeAsync();
}
}

/// <inheritdoc />
public async Task InitializeAsync()
{
await this._container.StartAsync();
ConnectionString = this._container.GetConnectionString();
}

/// <summary>
/// The connection string for the MySQL database.
/// </summary>
public string ConnectionString { get; private set; } = string.Empty;
}