-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new IDatabaseContainer interface
This is required in order to implement a generic database fixture that could look like this. ```csharp using System; using System.Data.Common; using System.Threading.Tasks; using DotNet.Testcontainers.Builders; using DotNet.Testcontainers.Containers; using Testcontainers.MsSql; using Testcontainers.MySql; using Testcontainers.PostgreSql; using Xunit; namespace SampleCode; public abstract class DatabaseFixture<TBuilder, TContainer> : IAsyncLifetime where TBuilder : IContainerBuilder<TBuilder, TContainer>, new() where TContainer : IContainer, IDatabaseContainer { private string _connectionString; private TContainer _container; protected abstract DbProviderFactory ProviderFactory { get; } public string ConnectionString => _connectionString ?? throw new InvalidOperationException($"{nameof(IAsyncLifetime.InitializeAsync)} must be called before accessing the connection string"); async Task IAsyncLifetime.InitializeAsync() { _container = new TBuilder().Build(); await _container.StartAsync(); _connectionString = _container.GetConnectionString(); using var connection = ProviderFactory.CreateConnection() ?? throw new InvalidOperationException($"ProviderFactory.CreateConnection() returned null ({ProviderFactory})"); connection.ConnectionString = _connectionString; await connection.OpenAsync(); await connection.CloseAsync(); } async Task IAsyncLifetime.DisposeAsync() { if (_container != null) { await _container.StopAsync(); } } } public class MsSqlDbFixture : DatabaseFixture<MsSqlBuilder, MsSqlContainer> { protected override DbProviderFactory ProviderFactory => Microsoft.Data.SqlClient.SqlClientFactory.Instance; } public class MySqlDbFixture : DatabaseFixture<MySqlBuilder, MySqlContainer> { protected override DbProviderFactory ProviderFactory => MySqlConnector.MySqlConnectorFactory.Instance; } public class PostgreSqlDbFixture : DatabaseFixture<PostgreSqlBuilder, PostgreSqlContainer> { protected override DbProviderFactory ProviderFactory => Npgsql.NpgsqlFactory.Instance; } public class PostgreSqlTest : IClassFixture<PostgreSqlDbFixture> { private readonly PostgreSqlDbFixture _dbFixture; public PostgreSqlTest(PostgreSqlDbFixture dbFixture) => _dbFixture = dbFixture; [Fact] public async Task TestOnPostgreSql() { await using var connection = new Npgsql.NpgsqlConnection(_dbFixture.ConnectionString); await using var command = connection.CreateCommand(); // ... } } ``` This will also be required to implement a new wait strategy for database where an ADO.NET provider is available.
- Loading branch information
Showing
29 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace DotNet.Testcontainers.Containers | ||
{ | ||
using JetBrains.Annotations; | ||
|
||
/// <summary> | ||
/// A database container instance. | ||
/// </summary> | ||
[PublicAPI] | ||
public interface IDatabaseContainer | ||
{ | ||
/// <summary> | ||
/// Gets the connection string for connecting to the database. | ||
/// </summary> | ||
/// <returns>The connection string for connecting to the database.</returns> | ||
string GetConnectionString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
root = true |
25 changes: 25 additions & 0 deletions
25
tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Testcontainers.Databases; | ||
|
||
public sealed class DatabaseContainersTest | ||
{ | ||
[Theory] | ||
[MemberData(nameof(DatabaseContainersTheoryData))] | ||
public void ImplementsIDatabaseContainerInterface(Type type) | ||
{ | ||
Assert.True(type.IsAssignableTo(typeof(IDatabaseContainer))); | ||
} | ||
|
||
public static IEnumerable<object[]> DatabaseContainersTheoryData | ||
{ | ||
get | ||
{ | ||
static bool HasGetConnectionStringMethod(Type type) => type.IsAssignableTo(typeof(IContainer)) && type.GetMethod("GetConnectionString") != null; | ||
var assembly = typeof(DatabaseContainersTest).Assembly; | ||
var dependencyContext = DependencyContext.Load(assembly) ?? throw new InvalidOperationException($"DependencyContext.Load({assembly}) returned null"); | ||
return dependencyContext.RuntimeLibraries | ||
.Where(library => library.Name.StartsWith("Testcontainers.")) | ||
.SelectMany(library => Assembly.Load(library.Name).GetExportedTypes().Where(HasGetConnectionStringMethod)) | ||
.Select(type => new[] { type }); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/Testcontainers.Databases.Tests/Testcontainers.Databases.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net6.0</TargetFrameworks> | ||
<IsPackable>false</IsPackable> | ||
<IsPublishable>false</IsPublishable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" /> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="$(SolutionDir)src/Testcontainers.*/Testcontainers.*.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
global using System; | ||
global using System.Collections.Generic; | ||
global using System.Linq; | ||
global using System.Reflection; | ||
global using DotNet.Testcontainers.Containers; | ||
global using Microsoft.Extensions.DependencyModel; | ||
global using Xunit; |