using Shouldly; using Xunit; namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.Tests; public sealed class QuestDbTablesTests { [Fact] public void Sample_ddl_without_ttl_is_pinned() => QuestDbTables.CreateSample("hist_", null).ShouldBe( "CREATE TABLE IF NOT EXISTS hist_sample (ts TIMESTAMP, tag SYMBOL, kind BYTE, num_value DOUBLE, " + "int_value LONG, str_value VARCHAR, status LONG, deleted BOOLEAN, server_ts TIMESTAMP) " + "TIMESTAMP(ts) PARTITION BY DAY WAL DEDUP UPSERT KEYS(ts, tag)"); [Fact] public void Sample_ddl_with_ttl_puts_ttl_between_partition_and_wal() => QuestDbTables.CreateSample("hist_", 7).ShouldBe( "CREATE TABLE IF NOT EXISTS hist_sample (ts TIMESTAMP, tag SYMBOL, kind BYTE, num_value DOUBLE, " + "int_value LONG, str_value VARCHAR, status LONG, deleted BOOLEAN, server_ts TIMESTAMP) " + "TIMESTAMP(ts) PARTITION BY DAY TTL 7 DAYS WAL DEDUP UPSERT KEYS(ts, tag)"); [Fact] public void Event_ddl_is_pinned() { QuestDbTables.CreateEvent("hist_", null).ShouldBe( "CREATE TABLE IF NOT EXISTS hist_event (ts TIMESTAMP, event_id VARCHAR, source SYMBOL, alarm_id VARCHAR, " + "alarm_name VARCHAR, alarm_type_name VARCHAR, severity INT, event_kind VARCHAR, message VARCHAR, " + "user_name VARCHAR, comment VARCHAR, received_utc TIMESTAMP) " + "TIMESTAMP(ts) PARTITION BY DAY WAL DEDUP UPSERT KEYS(ts, event_id)"); QuestDbTables.CreateEvent("hist_", 30).ShouldContain("PARTITION BY DAY TTL 30 DAYS WAL DEDUP"); } [Fact] public void Tag_ddl_is_pinned_and_never_carries_a_ttl() => QuestDbTables.CreateTag("hist_").ShouldBe( "CREATE TABLE IF NOT EXISTS hist_tag (ts TIMESTAMP, name SYMBOL, data_type BYTE, eng_unit VARCHAR, " + "description VARCHAR, updated_utc TIMESTAMP) TIMESTAMP(ts) PARTITION BY YEAR WAL DEDUP UPSERT KEYS(ts, name)"); [Fact] public void The_prefix_is_applied_to_every_table_name() { QuestDbTables.All("t1_").ShouldBe(["t1_sample", "t1_event", "t1_tag", "t1_sample_modified"]); QuestDbTables.Expiring("t1_").ShouldBe(["t1_sample", "t1_event", "t1_sample_modified"]); QuestDbTables.CreateAll("t1_", 3).ShouldAllBe(s => s.Contains("t1_")); } [Fact] public void Sample_modified_ddl_is_pinned_append_only_and_carries_the_ttl() { QuestDbTables.CreateSampleModified("hist_", null).ShouldBe( "CREATE TABLE IF NOT EXISTS hist_sample_modified (ts TIMESTAMP, tag SYMBOL, modified_id LONG, " + "modification_ts TIMESTAMP, update_type BYTE, principal VARCHAR, api_key_id VARCHAR, kind BYTE, num_value DOUBLE, " + "int_value LONG, str_value VARCHAR, status LONG, server_ts TIMESTAMP) TIMESTAMP(ts) PARTITION BY DAY WAL"); QuestDbTables.CreateSampleModified("hist_", 30).ShouldEndWith("PARTITION BY DAY TTL 30 DAYS WAL"); QuestDbTables.CreateSampleModified("hist_", null).ShouldNotContain("DEDUP"); } [Fact] public void An_invalid_prefix_is_refused() => Should.Throw(() => QuestDbTables.CreateSample("a b", null)); [Fact] public void Alter_ttl_is_pinned_and_null_removes_it() { QuestDbTables.AlterTtl("hist_sample", 30).ShouldBe("ALTER TABLE hist_sample SET TTL 30 DAYS"); QuestDbTables.AlterTtl("hist_sample", null).ShouldBe("ALTER TABLE hist_sample SET TTL 0 DAYS"); } [Theory] [InlineData(1, "WEEK", 168)] // 7 days reads back as 1 WEEK [InlineData(2, "WEEK", 336)] [InlineData(30, "DAY", 720)] [InlineData(0, "HOUR", 0)] // no TTL [InlineData(5, "hour", 5)] [InlineData(1, "MONTH", -1)] // unknown unit never equals a wanted value public void Ttl_is_compared_in_hours(long value, string unit, long hours) => QuestDbTables.TtlHours(value, unit).ShouldBe(hours); }