Path: redb/redb.Tests.Integration/Tests/Base/QueryRowsAsJsonTestsBase.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/QueryRowsAsJsonTestsBase.cs
----
using System.Text.Json;
using redb.Core;
using redb.Tests.Integration.Helpers;
namespace redb.Tests.Integration.Tests.Base;
///
/// IRedbContext.QueryRowsAsJsonAsync on every provider: each row an object keyed by column name, numbers as numbers,
/// text as text, NULL kept as null, a parameter bound, "[]" for no rows, and one command (one telemetry span) per call.
///
public abstract class QueryRowsAsJsonTestsBase
{
protected readonly IRedbService Redb;
protected QueryRowsAsJsonTestsBase(IRedbService redb) => Redb = redb;
private const string TwoRows =
"SELECT $1 AS label, 7 AS n, 2.5 AS d, CAST(NULL AS INT) AS empty " +
"UNION ALL SELECT 'b', 8, 3.5, CAST(NULL AS INT)";
[Fact]
public async Task Rows_AreObjectsKeyedByColumn_WithNullsKept()
{
var json = await Redb.Context.QueryRowsAsJsonAsync(TwoRows, new object[] { "a" });
using var doc = JsonDocument.Parse(json);
var rows = doc.RootElement.EnumerateArray().OrderBy(r => r.GetProperty("label").GetString()).ToList();
rows.Should().HaveCount(2);
rows[0].GetProperty("label").GetString().Should().Be("a", "the parameter is bound");
rows[0].GetProperty("n").GetInt32().Should().Be(7);
rows[0].GetProperty("d").GetDecimal().Should().Be(2.5m);
rows[0].GetProperty("empty").ValueKind.Should().Be(JsonValueKind.Null, "a NULL column is kept, as null");
rows[1].GetProperty("label").GetString().Should().Be("b");
rows[1].GetProperty("n").GetInt32().Should().Be(8);
rows[1].GetProperty("d").GetDecimal().Should().Be(3.5m);
}
[Fact]
public async Task NoRows_IsAnEmptyArray()
=> (await Redb.Context.QueryRowsAsJsonAsync("SELECT 1 AS n WHERE 1 = 0", Array.Empty