Files
jdescopingtool/NEW/tests/JdeScoping.DataSync.IntegrationTests/Infrastructure/SqlServerFixture.cs
T
Joseph Doherty 26ff8d9b4f Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/),
new .NET 10 Blazor solution (NEW/), OpenSpec specifications,
documentation, and project configuration.
2026-01-02 07:43:29 -05:00

89 lines
2.8 KiB
C#

using Testcontainers.MsSql;
namespace JdeScoping.DataSync.IntegrationTests.Infrastructure;
/// <summary>
/// Shared fixture that manages the SQL Server Testcontainer lifecycle.
/// Container is started once per test collection and shared across all tests.
/// </summary>
public class SqlServerFixture : IAsyncLifetime
{
private readonly MsSqlContainer _container;
/// <summary>
/// Gets the connection string to the test SQL Server instance.
/// </summary>
public string ConnectionString => _container.GetConnectionString();
public SqlServerFixture()
{
_container = new MsSqlBuilder()
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
.WithPassword("Test@Password123!")
.Build();
}
/// <summary>
/// Starts the container and initializes the test database schema.
/// </summary>
public async Task InitializeAsync()
{
await _container.StartAsync();
await TestDatabaseInitializer.InitializeAsync(ConnectionString);
}
/// <summary>
/// Stops and disposes the container.
/// </summary>
public async Task DisposeAsync()
{
await _container.DisposeAsync();
}
/// <summary>
/// Creates a new open connection to the test database.
/// Caller is responsible for disposing the connection.
/// </summary>
public async Task<SqlConnection> CreateConnectionAsync()
{
var connection = new SqlConnection(ConnectionString);
await connection.OpenAsync();
return connection;
}
/// <summary>
/// Truncates all test tables to ensure clean state between tests.
/// </summary>
public async Task CleanupTablesAsync()
{
await using var connection = await CreateConnectionAsync();
await connection.ExecuteAsync(@"
TRUNCATE TABLE WorkOrder_Test;
TRUNCATE TABLE Item_Test;
TRUNCATE TABLE LotUsage_Test;
TRUNCATE TABLE DataUpdate_Test;
TRUNCATE TABLE BulkMergeTest;
");
}
/// <summary>
/// Cleans up just the BulkMergeTest table.
/// </summary>
public async Task CleanupBulkMergeTestTableAsync()
{
await using var connection = await CreateConnectionAsync();
await connection.ExecuteAsync("TRUNCATE TABLE BulkMergeTest;");
}
}
/// <summary>
/// Collection definition for sharing the SQL Server fixture across test classes.
/// </summary>
[CollectionDefinition("Database")]
public class DatabaseCollection : ICollectionFixture<SqlServerFixture>
{
// This class has no code, and is never created.
// Its purpose is to be the place to apply [CollectionDefinition]
// and all the ICollectionFixture<> interfaces.
}