Skip to content

Commit be564b5

Browse files
committed
Enable use of Testcontainers
1 parent 2b0e1f1 commit be564b5

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
<PackageReference Include="FirebirdSql.Data.FirebirdClient" Version="9.1.1" />
7070
<PackageReference Include="Npgsql" Version="8.0.3" />
7171
<PackageReference Include="MySql.Data" Version="8.0.30" />
72+
<PackageReference Include="Testcontainers.Db2" Version="4.8.1" />
73+
<PackageReference Include="Testcontainers.FirebirdSql" Version="4.8.1" />
74+
<PackageReference Include="Testcontainers.MariaDb" Version="4.8.1" />
75+
<PackageReference Include="Testcontainers.MsSql" Version="4.8.1" />
76+
<PackageReference Include="Testcontainers.MySql" Version="4.8.1" />
77+
<PackageReference Include="Testcontainers.Oracle" Version="4.8.1" />
78+
<PackageReference Include="Testcontainers.PostgreSql" Version="4.8.1" />
7279
</ItemGroup>
7380
<ItemGroup Condition="$(NhNetFx)">
7481
<Reference Include="System.Configuration" />

src/NHibernate.Test/TestConfigurationHelper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public static Configuration GetDefaultConfiguration()
4747
Configuration result = new Configuration();
4848
if (hibernateConfigFile != null)
4949
result.Configure(hibernateConfigFile);
50+
51+
var connectionString = result.GetProperty(Cfg.Environment.ConnectionString);
52+
if (connectionString?.StartsWith("testcontainers=") == true)
53+
{
54+
result.SetProperty(Cfg.Environment.ConnectionString, TestContainerSetup.GetConnectionString(connectionString));
55+
}
5056
return result;
5157
}
5258

@@ -71,4 +77,4 @@ private static string FindCurrentTestConfigurationFile(string filename)
7177
return null;
7278
}
7379
}
74-
}
80+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
namespace NHibernate.Test
2+
{
3+
using System;
4+
using System.Threading.Tasks;
5+
using DotNet.Testcontainers.Containers;
6+
using NUnit.Framework;
7+
using Testcontainers.Db2;
8+
using Testcontainers.FirebirdSql;
9+
using Testcontainers.MariaDb;
10+
using Testcontainers.MsSql;
11+
using Testcontainers.MySql;
12+
using Testcontainers.Oracle;
13+
using Testcontainers.PostgreSql;
14+
15+
[SetUpFixture]
16+
public class TestContainerSetup
17+
{
18+
private static IDatabaseContainer _container;
19+
private static readonly object _lock = new object();
20+
21+
internal static string GetConnectionString(string connectionString)
22+
{
23+
var parts = connectionString.Split('=');
24+
if (parts.Length != 2 || parts[0] != "testcontainers")
25+
{
26+
throw new System.ArgumentException("Invalid testcontainers connection string format. Expected format: testcontainers=DbType");
27+
}
28+
// For now, only one container is supported. In the future, we can extend this to support multiple containers.
29+
if (_container == null)
30+
{
31+
lock (_lock)
32+
{
33+
if (_container == null)
34+
{
35+
var container = GetContainer(parts[1]);
36+
Task.Run(() => container.StartAsync()).GetAwaiter().GetResult();
37+
_container = container;
38+
}
39+
}
40+
}
41+
return _container.GetConnectionString();
42+
}
43+
44+
private static IDatabaseContainer GetContainer(string dbType)
45+
{
46+
switch (dbType.ToLower())
47+
{
48+
case "db2":
49+
return new Db2Builder().Build();
50+
case "firebirdsql":
51+
return new FirebirdSqlBuilder().Build();
52+
case "mariadb":
53+
return new MariaDbBuilder().Build();
54+
case "mssql":
55+
return new MsSqlBuilder().Build();
56+
case "mysql":
57+
return new MySqlBuilder().Build();
58+
case "oracle":
59+
return new OracleBuilder().Build();
60+
case "postgresql":
61+
return new PostgreSqlBuilder().Build();
62+
default:
63+
throw new NotSupportedException("Database type not supported: " + dbType);
64+
}
65+
}
66+
67+
[OneTimeTearDown]
68+
public async Task TearDown()
69+
{
70+
if (_container != null)
71+
{
72+
await _container.DisposeAsync();
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)