Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Configuration.Tests/DraftValidatorSqlNamedQueryTests.cs
T
Joseph Doherty 288ca70c4a fix(sql): #1058 deploy gate refuses a named query the Sql driver would refuse at start (SqlNamedQueryInvalid)
The driver runs SqlNamedQueryRules.Describe over every queries entry with its provider at
construction, and one refused query fails the whole driver (every tag BadNodeIdUnknown). Since #974
that includes a T-SQL parse for SqlServer/Odbc/no-provider, so an Odbc query written in another
dialect (LIMIT, ILIKE, 'a'::text) that used to pass is refused after the upgrade. The gate now runs
the same rule over the same merged config (factory-equivalent JSON options), naming the instance.
Noted: a bare col::type PARSES as T-SQL static-member syntax and is accepted by both.
2026-12-03 15:41:21 -05:00

69 lines
3.6 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
/// <summary>
/// lmxopcua#1058 (#974 follow-up): a Sql driver's named query is checked by
/// <c>SqlNamedQueryRules.Describe</c> with the instance's provider when the driver is constructed, and ONE refused
/// query fails the whole driver. The deploy gate now runs the same rule over the same merged config, so a config
/// the driver would refuse at start (e.g. an Odbc query written in PostgreSQL that no longer parses as T-SQL) is
/// refused at deploy instead — and the upgrade preflight finds the ones already stored.
/// </summary>
[Trait("Category", "Unit")]
public sealed class DraftValidatorSqlNamedQueryTests
{
private const string Code = SqlTagDeployChecks.NamedQueryInvalidCode;
private static DraftSnapshot Draft(string driverConfig, string deviceConfig = "{}", string driverType = "Sql") => new()
{
GenerationId = 1, ClusterId = "c",
DriverInstances = [new DriverInstance
{ DriverInstanceId = "di", ClusterId = "c", Name = "sql", DriverType = driverType, DriverConfig = driverConfig }],
Devices = [new Device { DeviceId = "dev", DriverInstanceId = "di", Name = "d", DeviceConfig = deviceConfig }],
};
private static string Config(string provider, string sql) =>
"{\"provider\":\"" + provider + "\",\"queries\":{\"line\":{\"sql\":\"" + sql + "\"}}}";
[Theory]
[InlineData("Odbc", "SELECT 'a'::text AS v")] // PostgreSQL cast of a literal: not T-SQL
[InlineData("Odbc", "SELECT (v)::numeric AS v FROM t")] // ... of an expression
[InlineData("Odbc", "SELECT v FROM t WHERE v ILIKE 'a%'")] // ILIKE: not T-SQL
[InlineData("Odbc", "SELECT v FROM t LIMIT 1")] // LIMIT: not T-SQL
[InlineData("SqlServer", "SELECT 1 AS v COMMIT")] // a second statement without ';'
[InlineData("SqlServer", "DELETE FROM t")]
public void A_named_query_the_driver_would_refuse_is_a_deploy_error(string provider, string sql)
{
var errors = DraftValidator.Validate(Draft(Config(provider, sql)));
errors.ShouldContain(e => e.Code == Code && e.Context == "di" && e.Message.Contains("'line'"));
}
[Theory]
[InlineData("Odbc", "SELECT v FROM t WHERE k = ?")]
[InlineData("SqlServer", "SELECT TOP 1 v FROM t")]
[InlineData("Postgres", "SELECT v::int AS v FROM t LIMIT 1")] // native provider: PostgreSQL dialect is fine
// NB: "col::type" (identifier::identifier) PARSES as T-SQL static-member syntax, so the driver accepts it too.
[InlineData("Odbc", "SELECT v::int AS v FROM t")]
public void Positive_control_a_query_the_driver_accepts_is_quiet(string provider, string sql) =>
DraftValidator.Validate(Draft(Config(provider, sql))).ShouldNotContain(e => e.Code == Code);
[Fact]
public void An_absent_provider_reads_as_SqlServer_like_the_driver() =>
DraftValidator.Validate(Draft("""{"queries":{"q":{"sql":"SELECT 1 AS v COMMIT"}}}"""))
.ShouldContain(e => e.Code == Code);
[Fact]
public void Queries_declared_on_the_single_device_config_are_checked() =>
DraftValidator.Validate(Draft("""{"provider":"Odbc"}""", deviceConfig: """{"queries":{"line":{"sql":"SELECT 1 LIMIT 1"}}}"""))
.ShouldContain(e => e.Code == Code);
[Fact]
public void Non_Sql_drivers_are_not_checked() =>
DraftValidator.Validate(Draft(Config("Odbc", "SELECT 1 LIMIT 1"), driverType: "Modbus"))
.ShouldNotContain(e => e.Code == Code);
}