Path: redb/redb.Tests.Integration/Tests/MsSql/MsSqlArrayHavingTranslatorTests.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/MsSql/MsSqlArrayHavingTranslatorTests.cs
----
using redb.Core;
using redb.Tests.Integration.Fixtures;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.MsSql;
///
/// The HAVING translator of the SQL Server Free array grouping (dbo.pvt_build_array_having_expr) turned a
/// node it did not know into 1=1: the condition was dropped and every group came back. The core's HAVING
/// parser writes no such node today, so the function is called directly - the SQL is the public surface for
/// callers that submit the JSON themselves.
///
[Collection("MsSql")]
public class MsSqlArrayHavingTranslatorTests
{
private readonly IRedbService _redb;
public MsSqlArrayHavingTranslatorTests(MsSqlFixture fixture) => _redb = fixture.Redb;
private async Task BuildAsync(string havingJson)
{
var scheme = await _redb.SyncSchemeAsync();
return await _redb.Context.ExecuteScalarAsync(
"SELECT dbo.pvt_build_array_groupby_sql(" + scheme.Id + ", N'Contacts', NULL, "
+ "N'[{\"field\":\"Type\",\"alias\":\"Type\"}]', N'[{\"field\":\"*\",\"func\":\"COUNT\",\"alias\":\"Count\"}]', $1, N'flat')",
new object[] { havingJson });
}
[Fact]
public async Task AHavingNodeItDoesNotKnow_RefusesTheQuery()
{
var sql = await BuildAsync("{\"$regex\":[{\"$count\":\"*\"},{\"$const\":\"x\"}]}");
sql.Should().Be("--error: the HAVING does not translate", "a HAVING the translator cannot read must not be replaced by 1=1");
}
[Fact]
public async Task AHavingItKnows_IsEmitted()
{
var sql = await BuildAsync("{\"$gt\":[{\"$count\":\"*\"},{\"$const\":1}]}");
sql.Should().Contain("HAVING (COUNT(*) > 1)");
}
}