72 lines
2.9 KiB
C#
72 lines
2.9 KiB
C#
using Xunit;
|
|
|
|
namespace ScadaLink.ConfigurationDatabase.Tests.Migrations;
|
|
|
|
/// <summary>
|
|
/// Audit Log ParentExecutionId integration test for the
|
|
/// <c>AddNotificationOriginParentExecutionId</c> migration: applies the EF
|
|
/// migrations to a freshly-created MSSQL test database on the running
|
|
/// infra/mssql container and asserts that the <c>Notifications</c> table carries
|
|
/// the new <c>OriginParentExecutionId</c> column as a nullable
|
|
/// <c>uniqueidentifier</c>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Unlike <c>AuditLog</c>, the <c>Notifications</c> table is not partitioned, so
|
|
/// the column is a plain metadata-only <c>ALTER TABLE … ADD</c> with no index.
|
|
/// Tests pair <see cref="SkippableFactAttribute"/> with <c>Skip.IfNot(...)</c> so
|
|
/// the runner reports them as Skipped (not Passed) when MSSQL is unreachable. The
|
|
/// fixture applies the migrations once at construction time.
|
|
/// </remarks>
|
|
public class AddNotificationOriginParentExecutionIdMigrationTests : IClassFixture<MsSqlMigrationFixture>
|
|
{
|
|
private readonly MsSqlMigrationFixture _fixture;
|
|
|
|
public AddNotificationOriginParentExecutionIdMigrationTests(MsSqlMigrationFixture fixture)
|
|
{
|
|
_fixture = fixture;
|
|
}
|
|
|
|
[SkippableFact]
|
|
public async Task AppliesMigration_AddsOriginParentExecutionIdColumn_ToNotifications()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
|
|
var present = await ScalarAsync<int>(
|
|
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
"WHERE TABLE_NAME = 'Notifications' AND COLUMN_NAME = 'OriginParentExecutionId' " +
|
|
"AND TABLE_SCHEMA = 'dbo';");
|
|
Assert.Equal(1, present);
|
|
}
|
|
|
|
[SkippableFact]
|
|
public async Task OriginParentExecutionIdColumn_IsNullableUniqueIdentifier()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
|
|
var dataType = await ScalarAsync<string?>(
|
|
"SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
"WHERE TABLE_NAME = 'Notifications' AND COLUMN_NAME = 'OriginParentExecutionId';");
|
|
Assert.Equal("uniqueidentifier", dataType);
|
|
|
|
var isNullable = await ScalarAsync<string?>(
|
|
"SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
"WHERE TABLE_NAME = 'Notifications' AND COLUMN_NAME = 'OriginParentExecutionId';");
|
|
Assert.Equal("YES", isNullable);
|
|
}
|
|
|
|
// --- helpers ------------------------------------------------------------
|
|
|
|
private async Task<T> ScalarAsync<T>(string sql)
|
|
{
|
|
await using var conn = _fixture.OpenConnection();
|
|
await using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = sql;
|
|
var result = await cmd.ExecuteScalarAsync();
|
|
if (result is null || result is DBNull)
|
|
{
|
|
return default!;
|
|
}
|
|
return (T)Convert.ChangeType(result, typeof(T) == typeof(string) ? typeof(string) : Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T))!;
|
|
}
|
|
}
|