Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteEventLogging.Tests/SiteEventLogSchemaTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

140 lines
5.0 KiB
C#

using Microsoft.Data.Sqlite;
namespace ZB.MOM.WW.ScadaBridge.SiteEventLogging.Tests;
/// <summary>
/// The <c>site_events</c> DDL is extracted out of
/// <see cref="SiteEventLogger"/> so the Host's <c>AddZbLocalDb</c> onReady callback can
/// create it in the consolidated site database.
/// <para>
/// The extraction also carries the one deliberate schema CHANGE of Phase 1: the primary
/// key moves from <c>INTEGER PRIMARY KEY AUTOINCREMENT</c> to a TEXT GUID. Under the
/// library's last-writer-wins replication, two site nodes each minting autoincrement id 1
/// would be treated as the same row and silently overwrite one another. GUID keys make
/// the event log a pure union across the pair instead.
/// </para>
/// </summary>
public class SiteEventLogSchemaTests
{
private static SqliteConnection OpenConnection()
{
var conn = new SqliteConnection("Data Source=:memory:");
conn.Open();
return conn;
}
private static List<(string Name, string Type, bool NotNull, bool Pk)> TableInfo(
SqliteConnection conn, string table)
{
using var cmd = conn.CreateCommand();
cmd.CommandText = $"PRAGMA table_info('{table}')";
using var reader = cmd.ExecuteReader();
var cols = new List<(string, string, bool, bool)>();
while (reader.Read())
{
cols.Add((reader.GetString(1), reader.GetString(2), reader.GetInt32(3) == 1,
reader.GetInt32(5) > 0));
}
return cols;
}
/// <summary>Verifies that applying the schema creates the table with the expected columns.</summary>
[Fact]
public void Apply_CreatesTableWithExpectedColumns()
{
using var conn = OpenConnection();
SiteEventLogSchema.Apply(conn);
Assert.Equal(
["id", "timestamp", "event_type", "severity", "instance_id", "source", "message", "details"],
TableInfo(conn, "site_events").Select(c => c.Name));
}
/// <summary>Verifies that the id column is a text primary key rather than an autoincrement integer.</summary>
[Fact]
public void Apply_IdIsTextPrimaryKey_NotAutoincrement()
{
using var conn = OpenConnection();
SiteEventLogSchema.Apply(conn);
var pk = Assert.Single(TableInfo(conn, "site_events"), c => c.Pk);
Assert.Equal("id", pk.Name);
Assert.Equal("TEXT", pk.Type);
// AUTOINCREMENT creates the sqlite_sequence bookkeeping table. Its absence
// is the load-bearing assertion: ids must be application-minted GUIDs, not
// server-minted integers that collide across the replicating pair.
using var cmd = conn.CreateCommand();
cmd.CommandText =
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'sqlite_sequence'";
Assert.Equal(0L, Convert.ToInt64(cmd.ExecuteScalar()));
}
/// <summary>Verifies that inserts that omit the text id are rejected.</summary>
[Fact]
public void Apply_RejectsIntegerIdInserts()
{
using var conn = OpenConnection();
SiteEventLogSchema.Apply(conn);
// A TEXT PK with no AUTOINCREMENT means an INSERT omitting id is an error,
// which is what forces the writer to mint one.
using var cmd = conn.CreateCommand();
cmd.CommandText = """
INSERT INTO site_events (timestamp, event_type, severity, source, message)
VALUES ('2026-07-19T00:00:00Z', 'Test', 'Info', 'test', 'no id supplied')
""";
Assert.Throws<SqliteException>(() => cmd.ExecuteNonQuery());
}
/// <summary>Verifies that the schema has no blob columns.</summary>
[Fact]
public void Apply_HasNoBlobColumns()
{
using var conn = OpenConnection();
SiteEventLogSchema.Apply(conn);
Assert.DoesNotContain(
TableInfo(conn, "site_events"),
c => c.Type.Contains("BLOB", StringComparison.OrdinalIgnoreCase));
}
/// <summary>Verifies that applying the schema creates the expected indexes.</summary>
[Fact]
public void Apply_CreatesExpectedIndexes()
{
using var conn = OpenConnection();
SiteEventLogSchema.Apply(conn);
using var cmd = conn.CreateCommand();
cmd.CommandText =
"SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = 'site_events' " +
"AND name NOT LIKE 'sqlite_%' ORDER BY name";
using var reader = cmd.ExecuteReader();
var indexes = new List<string>();
while (reader.Read()) indexes.Add(reader.GetString(0));
Assert.Equal(
["idx_events_instance", "idx_events_severity", "idx_events_timestamp", "idx_events_type"],
indexes);
}
/// <summary>Verifies that applying the schema repeatedly is safe.</summary>
[Fact]
public void Apply_IsIdempotent()
{
using var conn = OpenConnection();
SiteEventLogSchema.Apply(conn);
SiteEventLogSchema.Apply(conn);
Assert.Equal(8, TableInfo(conn, "site_events").Count);
}
}