Skip to content

Commit

Permalink
Add IDatabaseContainer only for containers with ADO.NET provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Oct 13, 2023
1 parent 3d49cbe commit f33d7e7
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers.Azurite/AzuriteContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Azurite;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class AzuriteContainer : DockerContainer, IDatabaseContainer
public sealed class AzuriteContainer : DockerContainer
{
private const string AccountName = "devstoreaccount1";

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.CosmosDb/CosmosDbContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.CosmosDb;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class CosmosDbContainer : DockerContainer, IDatabaseContainer
public sealed class CosmosDbContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="CosmosDbContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.CouchDb/CouchDbContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.CouchDb;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class CouchDbContainer : DockerContainer, IDatabaseContainer
public sealed class CouchDbContainer : DockerContainer
{
private readonly CouchDbConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Couchbase/CouchbaseContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Couchbase;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class CouchbaseContainer : DockerContainer, IDatabaseContainer
public sealed class CouchbaseContainer : DockerContainer
{
private readonly CouchbaseConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.DynamoDb/DynamoDbContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.DynamoDb;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class DynamoDbContainer : DockerContainer, IDatabaseContainer
public sealed class DynamoDbContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="DynamoDbContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Elasticsearch/ElasticsearchContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Elasticsearch;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class ElasticsearchContainer : DockerContainer, IDatabaseContainer
public sealed class ElasticsearchContainer : DockerContainer
{
private readonly ElasticsearchConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.EventStoreDb/EventStoreDbContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.EventStoreDb;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class EventStoreDbContainer : DockerContainer, IDatabaseContainer
public sealed class EventStoreDbContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="EventStoreDbContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Kusto/KustoContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Kusto;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class KustoContainer : DockerContainer, IDatabaseContainer
public sealed class KustoContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="KustoContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Minio/MinioContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Minio;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class MinioContainer : DockerContainer, IDatabaseContainer
public sealed class MinioContainer : DockerContainer
{
private readonly MinioConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.MongoDb/MongoDbContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.MongoDb;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class MongoDbContainer : DockerContainer, IDatabaseContainer
public sealed class MongoDbContainer : DockerContainer
{
private readonly MongoDbConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Nats/NatsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Nats;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class NatsContainer : DockerContainer, IDatabaseContainer
public sealed class NatsContainer : DockerContainer
{
private readonly NatsConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Neo4j/Neo4jContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Neo4j;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class Neo4jContainer : DockerContainer, IDatabaseContainer
public sealed class Neo4jContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="Neo4jContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.RabbitMq/RabbitMqContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.RabbitMq;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class RabbitMqContainer : DockerContainer, IDatabaseContainer
public sealed class RabbitMqContainer : DockerContainer
{
private readonly RabbitMqConfiguration _configuration;

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.RavenDb/RavenDbContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.RavenDb;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class RavenDbContainer : DockerContainer, IDatabaseContainer
public sealed class RavenDbContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="RavenDbContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Redis/RedisContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Testcontainers.Redis;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class RedisContainer : DockerContainer, IDatabaseContainer
public sealed class RedisContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="RedisContainer" /> class.
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Containers/IDatabaseContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DotNet.Testcontainers.Containers
{
/// <summary>
/// Represents a database container instance.
/// Represents a database container instance that can be accessed with an ADO.NET provider.
/// </summary>
[PublicAPI]
public interface IDatabaseContainer
Expand Down
51 changes: 33 additions & 18 deletions tests/Testcontainers.Databases.Tests/DatabasesContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,47 @@ namespace Testcontainers.Databases;
public sealed class DatabaseContainersTest
{
[Theory]
[MemberData(nameof(DatabaseContainersTheoryData))]
public void ImplementsIDatabaseContainerInterface(Type type)
[MemberData(nameof(GetContainerTypes), parameters: true)]
public void ImplementsIDatabaseContainer(Type type)
{
Assert.True(type.IsAssignableTo(typeof(IDatabaseContainer)));
}

private static readonly HashSet<Type> NotDatabaseContainerTypes = new()
[Theory]
[MemberData(nameof(GetContainerTypes), parameters: false)]
public void DoesNotImplementIDatabaseContainer(Type type)
{
typeof(LocalStack.LocalStackContainer),
typeof(WebDriver.WebDriverContainer),
};

private static bool IsDatabaseContainerType(Type containerType) => !NotDatabaseContainerTypes.Contains(containerType);
Assert.False(type.IsAssignableTo(typeof(IDatabaseContainer)));
}

public static IEnumerable<object[]> DatabaseContainersTheoryData
public static IEnumerable<object[]> GetContainerTypes(bool adoNetContainers)
{
get
foreach (var dll in Directory.GetFiles(".", "Testcontainers.*.Tests.dll", SearchOption.TopDirectoryOnly))
{
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))
.Where(IsDatabaseContainerType)
.Select(type => new[] { type });
var referencedAssemblies = Assembly.LoadFrom(Path.GetFullPath(dll)).GetReferencedAssemblies().Select(Assembly.Load).ToList();
var hasAdoNetProvider = referencedAssemblies.Any(IsAdoNetProvider);
if (adoNetContainers ? hasAdoNetProvider : !hasAdoNetProvider)
{
var containerAssembly = referencedAssemblies.SingleOrDefault(IsTestcontainer);
if (containerAssembly != null)
{
foreach (var containerType in containerAssembly.GetExportedTypes().Where(type => type.IsAssignableTo(typeof(IContainer))))
{
yield return new object[] { containerType };
}
}
}
}
}

private static bool IsAdoNetProvider(Assembly assembly)
{
return assembly.GetExportedTypes().Any(a => a.IsSubclassOf(typeof(DbProviderFactory)));
}

private static bool IsTestcontainer(Assembly assembly)
{
var name = assembly.GetName().Name ?? "";
return name.StartsWith("Testcontainers.") && name != "Testcontainers.Commons";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2"/>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0"/>
<PackageReference Include="xunit" Version="2.5.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SolutionDir)src/Testcontainers.*/Testcontainers.*.csproj" />
<ProjectReference Include="$(SolutionDir)tests/Testcontainers.*.Tests/Testcontainers.*.Tests.csproj" />
<ProjectReference Remove="$(SolutionDir)tests/Testcontainers.Databases.Tests/Testcontainers.Databases.Tests.csproj" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion tests/Testcontainers.Databases.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
global using System;
global using System.Collections.Generic;
global using System.Data.Common;
global using System.IO;
global using System.Linq;
global using System.Reflection;
global using DotNet.Testcontainers.Containers;
global using Microsoft.Extensions.DependencyModel;
global using Xunit;

0 comments on commit f33d7e7

Please sign in to comment.