Files
scadalink-design/tests/ScadaLink.ConfigurationDatabase.Tests/Migrations/AddNotificationSourceNodeMigrationTests.cs
2026-05-23 16:34:30 -04:00

77 lines
3.0 KiB
C#

using Xunit;
namespace ScadaLink.ConfigurationDatabase.Tests.Migrations;
/// <summary>
/// SourceNode-stamping (#23) integration tests for the
/// <c>AddNotificationSourceNode</c> migration: applies the EF migrations to a
/// freshly-created MSSQL test database on the running infra/mssql container and
/// asserts that the central <c>Notifications</c> table carries the new
/// <c>SourceNode varchar(64) NULL</c> column. No index — Notification Outbox KPIs
/// are per-site, not per-node, so the column is never a query predicate on this
/// table; it's only echoed onto NotifyDeliver audit rows (#23) for cross-row
/// correlation.
/// </summary>
/// <remarks>
/// <c>Notifications</c> is non-partitioned (operational state, not audit), so this
/// is a plain metadata-only <c>ALTER TABLE … ADD</c> with no index.
/// </remarks>
public class AddNotificationSourceNodeMigrationTests : IClassFixture<MsSqlMigrationFixture>
{
private readonly MsSqlMigrationFixture _fixture;
public AddNotificationSourceNodeMigrationTests(MsSqlMigrationFixture fixture)
{
_fixture = fixture;
}
[SkippableFact]
public async Task AppliesMigration_AddsSourceNodeColumn_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 = 'SourceNode' " +
"AND TABLE_SCHEMA = 'dbo';");
Assert.Equal(1, present);
}
[SkippableFact]
public async Task SourceNodeColumn_IsNullableVarchar64()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
// varchar(64), not nvarchar — SourceNode is ASCII (`node-a`, `central-a` etc.).
var dataType = await ScalarAsync<string?>(
"SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = 'Notifications' AND COLUMN_NAME = 'SourceNode';");
Assert.Equal("varchar", dataType);
var maxLength = await ScalarAsync<int>(
"SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = 'Notifications' AND COLUMN_NAME = 'SourceNode';");
Assert.Equal(64, maxLength);
var isNullable = await ScalarAsync<string?>(
"SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE TABLE_NAME = 'Notifications' AND COLUMN_NAME = 'SourceNode';");
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))!;
}
}