Skip to content

Commit

Permalink
tests: update testcontainers to 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
followynne committed Mar 11, 2023
1 parent d8a539b commit e2ffc32
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Npgsql" Version="6.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Testcontainers" Version="2.2.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.2" />
</ItemGroup>

Expand Down
28 changes: 8 additions & 20 deletions tests/Serilog.Ui.Common.Tests/SqlUtil/DatabaseInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Threading;
using System.Threading.Tasks;
using Ardalis.GuardClauses;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Microsoft.Data.SqlClient;
using MySql.Data.MySqlClient;
Expand All @@ -15,20 +13,16 @@
namespace Serilog.Ui.Common.Tests.SqlUtil
{
#nullable enable
public abstract class DatabaseInstance<TContainer, TConfiguration> : IIntegrationRunner
where TContainer : TestcontainerDatabase
where TConfiguration : TestcontainerDatabaseConfiguration, new()
public abstract class DatabaseInstance : IIntegrationRunner
{
private bool disposedValue;

protected TConfiguration? Configuration = new() { Password = "#DockerFakePw#" };

protected virtual string Name { get; } = nameof(TContainer);
protected virtual string Name { get; } = nameof(IContainer);

/// <summary>
/// Gets or sets the Testcontainers container.
/// </summary>
protected TContainer? Container { get; set; }
protected virtual IContainer? Container { get; set; }

/// <summary>
/// Gets or sets the IDataProvider.
Expand All @@ -41,16 +35,10 @@ public abstract class DatabaseInstance<TContainer, TConfiguration> : IIntegratio

public LogModelPropsCollector GetPropsCollector() => Guard.Against.Null(Collector)!;

public Task InitializeAsync()
public async Task InitializeAsync()
{
Container = new TestcontainersBuilder<TContainer>()
.WithDatabase(Configuration)
.WithName($"Testing_{Name}_{Guid.NewGuid()}")
.WithWaitStrategy(Wait.ForUnixContainer())
.WithStartupCallback(async (dc, token) => await GetDbContextInstanceAsync(token))
.Build();

return Container.StartAsync();
await Container!.StartAsync();
await GetDbContextInstanceAsync();
}

/// <summary>
Expand All @@ -68,7 +56,7 @@ public Task InitializeAsync()
/// <returns><see cref="Task"/></returns>
protected abstract Task InitializeAdditionalAsync();

private async Task GetDbContextInstanceAsync(CancellationToken token)
private async Task GetDbContextInstanceAsync(CancellationToken token = default)
{
int retry = default;

Expand Down Expand Up @@ -110,7 +98,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Configuration?.Dispose();
// additional dispose items
}

disposedValue = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Testcontainers" Version="2.2.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="Testcontainers.MsSql" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Dapper;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Testcontainers.MsSql;
using Microsoft.Data.SqlClient;
using Serilog.Ui.Common.Tests.DataSamples;
using Serilog.Ui.Common.Tests.SqlUtil;
Expand All @@ -14,19 +13,24 @@ namespace MsSql.Tests.Util
[CollectionDefinition(nameof(SqlServerDataProvider))]
public class SqlServerCollection : ICollectionFixture<MsSqlServerTestProvider> { }

public sealed class MsSqlServerTestProvider : DatabaseInstance<MsSqlTestcontainer, MsSqlTestcontainerConfiguration>
public sealed class MsSqlServerTestProvider : DatabaseInstance
{
public MsSqlServerTestProvider()
{
Container = new MsSqlBuilder().Build();
}

public RelationalDbOptions DbOptions { get; set; } = new()
{
TableName = "Logs",
Schema = "dbo"
};

protected override string Name => nameof(MsSqlTestcontainer);
protected override string Name => nameof(MsSqlContainer);

protected override async Task CheckDbReadinessAsync()
{
DbOptions.ConnectionString = Container?.ConnectionString + "TrustServerCertificate=True;";
DbOptions.ConnectionString = (Container as MsSqlContainer)?.GetConnectionString();

using var dataContext = new SqlConnection(DbOptions.ConnectionString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MySql.Data" Version="8.0.23" />
<PackageReference Include="Testcontainers" Version="2.2.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="Testcontainers.MySql" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
19 changes: 9 additions & 10 deletions tests/Serilog.Ui.MySqlProvider.Tests/Util/MySqlTestProvider.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
using Ardalis.GuardClauses;
using Dapper;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using MySql.Data.MySqlClient;
using Serilog.Ui.Common.Tests.DataSamples;
using Serilog.Ui.Common.Tests.SqlUtil;
using Serilog.Ui.Core;
using Serilog.Ui.MySqlProvider;
using System.Threading.Tasks;
using Testcontainers.MySql;
using Xunit;

namespace MySql.Tests.Util
{
[CollectionDefinition(nameof(MySqlDataProvider))]
public class MySqlCollection : ICollectionFixture<MySqlTestProvider> { }
public sealed class MySqlTestProvider : DatabaseInstance<MySqlTestcontainer, MySqlTestcontainerConfiguration>
{
protected override string Name => nameof(MySqlTestcontainer);

public MySqlTestProvider()
public sealed class MySqlTestProvider : DatabaseInstance
{
protected override string Name => nameof(MySqlContainer);
public MySqlTestProvider() : base()
{
Guard.Against.Null(Configuration);
Configuration.Username = "mysql-tests";
Configuration.Database = "testdatabase";
Container = new MySqlBuilder().Build();
}

public RelationalDbOptions DbOptions { get; set; } = new()
Expand All @@ -33,7 +30,9 @@ public MySqlTestProvider()

protected override async Task CheckDbReadinessAsync()
{
DbOptions.ConnectionString = Container?.ConnectionString;
Guard.Against.Null(Container);

DbOptions.ConnectionString = (Container as MySqlContainer)?.GetConnectionString();

using var dataContext = new MySqlConnection(DbOptions.ConnectionString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Npgsql" Version="6.0.9" />
<PackageReference Include="Testcontainers" Version="2.2.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
using Ardalis.GuardClauses;
using Dapper;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
//using
using Npgsql;
using Serilog.Ui.Common.Tests.DataSamples;
using Serilog.Ui.Common.Tests.SqlUtil;
using Serilog.Ui.Core;
using Serilog.Ui.PostgreSqlProvider;
using System.Linq;
using System.Threading.Tasks;
using Testcontainers.PostgreSql;
using Xunit;

namespace Postgres.Tests.Util
{
[CollectionDefinition(nameof(PostgresDataProvider))]
public class PostgresCollection : ICollectionFixture<PostgresTestProvider> { }
public sealed class PostgresTestProvider : DatabaseInstance<PostgreSqlTestcontainer, PostgreSqlTestcontainerConfiguration>
public sealed class PostgresTestProvider : DatabaseInstance
{
protected override string Name => nameof(PostgreSqlTestcontainer);
protected override string Name => nameof(PostgreSqlContainer);

public PostgresTestProvider()
{
Guard.Against.Null(Configuration);
Configuration.Username = "mysql-tests";
Configuration.Database = "testdatabase";
Container = new PostgreSqlBuilder().Build();
}

public RelationalDbOptions DbOptions { get; set; } = new()
Expand All @@ -34,7 +32,7 @@ public PostgresTestProvider()

protected override async Task CheckDbReadinessAsync()
{
DbOptions.ConnectionString = Container?.ConnectionString ?? string.Empty;
DbOptions.ConnectionString = (Container as PostgreSqlContainer)?.GetConnectionString() ?? string.Empty;

using var dataContext = new NpgsqlConnection(DbOptions.ConnectionString);

Expand Down

0 comments on commit e2ffc32

Please sign in to comment.