using Microsoft.Data.SqlClient;
using Testcontainers.MsSql;
namespace SQLInsight.IntegrationTests;
///
/// One SQL Server container for the test run. The image is chosen with SQLINSIGHT_MSSQL_IMAGE so CI can run
/// the same tests against 2017, 2019, 2022 and 2025.
///
public sealed class SqlServerFixture : IAsyncLifetime
{
public const string DefaultImage = "mcr.microsoft.com/mssql/server:2022-latest";
public static string Image => Environment.GetEnvironmentVariable("SQLINSIGHT_MSSQL_IMAGE") ?? DefaultImage;
private readonly MsSqlContainer _container = new MsSqlBuilder(Image).Build();
public string SaConnectionString => _container.GetConnectionString();
public string Server
{
get
{
var builder = new SqlConnectionStringBuilder(SaConnectionString);
return builder.DataSource;
}
}
public Task InitializeAsync() => _container.StartAsync();
public Task DisposeAsync() => _container.DisposeAsync().AsTask();
public async Task ExecuteAsSaAsync(string sql)
{
await using var connection = new SqlConnection(SaConnectionString);
await connection.OpenAsync();
await using var command = connection.CreateCommand();
command.CommandText = sql;
command.CommandTimeout = 120;
await command.ExecuteNonQueryAsync();
}
}
[CollectionDefinition(Name)]
public sealed class SqlServerTestGroup : ICollectionFixture
{
public const string Name = "SQL Server";
}