69 lines
2.9 KiB
C#
69 lines
2.9 KiB
C#
using System.Data.Common;
|
|
using Microsoft.Data.Sqlite;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types;
|
|
using ZB.MOM.WW.ScadaBridge.InboundAPI;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.InboundAPI.Tests;
|
|
|
|
public class InboundDatabaseHelperTests
|
|
{
|
|
private sealed class SqliteGateway : IDatabaseGateway
|
|
{
|
|
private readonly string _cs;
|
|
public SqliteGateway(string cs) => _cs = cs;
|
|
public async Task<DbConnection> GetConnectionAsync(string name, CancellationToken ct = default)
|
|
{ var c = new SqliteConnection(_cs); await c.OpenAsync(ct); return c; }
|
|
public Task<ExternalCallResult> CachedWriteAsync(string c, string s,
|
|
IReadOnlyDictionary<string, object?>? p = null, string? o = null, CancellationToken ct = default,
|
|
TrackedOperationId? t = null, Guid? e = null, string? src = null, Guid? pe = null)
|
|
=> throw new NotImplementedException();
|
|
}
|
|
|
|
private static SqliteGateway SeededGateway()
|
|
{
|
|
var keep = new SqliteConnection("DataSource=file:movein?mode=memory&cache=shared");
|
|
keep.Open(); // keep-alive: shared in-memory db lives until process exit
|
|
using var cmd = keep.CreateCommand();
|
|
cmd.CommandText = "CREATE TABLE IF NOT EXISTS Machine(Code TEXT, SAPID TEXT); DELETE FROM Machine; INSERT INTO Machine VALUES('Z28061A','131453');";
|
|
cmd.ExecuteNonQuery();
|
|
return new SqliteGateway("DataSource=file:movein?mode=memory&cache=shared");
|
|
}
|
|
|
|
[Fact]
|
|
public void QuerySingle_returns_first_column_with_bound_parameter()
|
|
{
|
|
var helper = new InboundDatabaseHelper(SeededGateway(), CancellationToken.None);
|
|
var code = helper.QuerySingle<string>("BTDB", "SELECT Code FROM Machine WHERE SAPID=@s", new { s = "131453" });
|
|
Assert.Equal("Z28061A", code);
|
|
}
|
|
|
|
[Fact]
|
|
public void QuerySingle_returns_default_when_no_rows()
|
|
{
|
|
var helper = new InboundDatabaseHelper(SeededGateway(), CancellationToken.None);
|
|
var code = helper.QuerySingle<string>("BTDB", "SELECT Code FROM Machine WHERE SAPID=@s", new { s = "999999" });
|
|
Assert.Null(code);
|
|
}
|
|
|
|
[Fact]
|
|
public void Query_returns_row_with_case_insensitive_keys()
|
|
{
|
|
var helper = new InboundDatabaseHelper(SeededGateway(), CancellationToken.None);
|
|
var rows = helper.Query("BTDB", "SELECT Code, SAPID FROM Machine WHERE SAPID=@s", new { s = "131453" });
|
|
Assert.Single(rows);
|
|
var row = rows[0];
|
|
Assert.Equal("Z28061A", row["code"]);
|
|
Assert.Equal("Z28061A", row["CODE"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void Query_returns_empty_list_when_no_rows_match()
|
|
{
|
|
var helper = new InboundDatabaseHelper(SeededGateway(), CancellationToken.None);
|
|
var rows = helper.Query("BTDB", "SELECT Code, SAPID FROM Machine WHERE SAPID=@s", new { s = "999999" });
|
|
Assert.Empty(rows);
|
|
}
|
|
}
|