Path: redb/redb.Tests.Integration/Tests/Base/CaseFoldingTestsBase.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/CaseFoldingTestsBase.cs ---- using redb.Core; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// Case-insensitive text search across scripts, with the feature ON. /// /// /// Case folding is driven by the database's own rules and those cover only ASCII in two of the three /// providers. The point of this suite is not Cyrillic: it is that a single setting fixes every script /// whose case mapping is one character to one character, and that three specific classes are NOT /// fixed by it and never will be. Both halves are asserted, because a suite that only proves the /// happy path lets an over-promise ship in the documentation. /// /// /// /// On PostgreSQL these tests are meaningful only against a database created with /// LC_CTYPE=C (see PostgresCollationFixture). On the ordinary test database every one /// of them passes with the feature removed, which is exactly how the defect went unnoticed. /// /// public abstract class CaseFoldingTestsBase { protected readonly IRedbService Redb; private bool _seeded; protected CaseFoldingTestsBase(IRedbService redb) => Redb = redb; /// /// One row per script, upper-cased at the source so a lower-case needle has to fold to find it. /// private static readonly (string Label, string Text)[] Rows = { ("cyrillic", "ПРИВЕТ МИР"), ("greek", "ΑΘΗΝΑ ΠΟΛΗ"), ("hungarian", "ŐSZ ÚTON JÁRTAM"), ("polish", "ŻÓŁW ŁÓDŹ"), ("czech", "ŘEKA ČESKÁ"), ("french", "ÉTÉ À PARIS"), ("german", "STRAßE GROß"), ("turkish", "İSTANBUL"), ("umlaut", "MÜLLER"), ("ascii", "HELLO WORLD"), }; protected async Task EnsureSeededAsync() { if (_seeded) return; // Scheme name is the CLR FullName: the positional [RedbScheme("...")] argument is the alias. await Redb.Context.ExecuteAsync( "DELETE FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = " + $"'{typeof(CollationProps).FullName}')"); var objects = Rows.Select(r => new RedbObject { name = r.Label, Props = new CollationProps { Text = r.Text, Label = r.Label } }).ToList(); await Redb.SaveAsync(objects); _seeded = true; } private async Task> FindAsync(string needle) { var results = await Redb.Query() .Where(c => c.Text.Contains(needle, StringComparison.OrdinalIgnoreCase)) .Take(50).ToListAsync(); return results.Select(r => r.Props.Label).OrderBy(x => x).ToList(); } // ===================================================================== // Fixed by the setting: every one-to-one case mapping // ===================================================================== /// /// The whole point in one test: this is not about Cyrillic. Each of these scripts maps case one /// character to one character, and one setting covers all of them with no per-language work. /// [Theory] [InlineData("привет", "cyrillic")] [InlineData("αθηνα", "greek")] [InlineData("ősz úton", "hungarian")] [InlineData("żółw", "polish")] [InlineData("řeka česká", "czech")] [InlineData("été à paris", "french")] [InlineData("hello", "ascii")] public async Task ContainsIgnoreCase_FoldsEveryOneToOneScript(string needle, string expectedLabel) { await EnsureSeededAsync(); (await FindAsync(needle)).Should().Contain(expectedLabel); } /// The reverse direction: an upper-case needle against the same rows. [Theory] [InlineData("ПРИВЕТ", "cyrillic")] [InlineData("ŻÓŁW", "polish")] [InlineData("ÉTÉ", "french")] public async Task ContainsIgnoreCase_FoldsInBothDirections(string needle, string expectedLabel) { await EnsureSeededAsync(); (await FindAsync(needle)).Should().Contain(expectedLabel); } [Fact] public async Task StartsWithIgnoreCase_Folds() { await EnsureSeededAsync(); var results = await Redb.Query() .Where(c => c.Text.StartsWith("привет", StringComparison.OrdinalIgnoreCase)) .Take(50).ToListAsync(); results.Select(r => r.Props.Label).Should().Contain("cyrillic"); } [Fact] public async Task EndsWithIgnoreCase_Folds() { await EnsureSeededAsync(); var results = await Redb.Query() .Where(c => c.Text.EndsWith("мир", StringComparison.OrdinalIgnoreCase)) .Take(50).ToListAsync(); results.Select(r => r.Props.Label).Should().Contain("cyrillic"); } /// /// ToLower is the other half of "one solution covers everything". Fixing the search while /// leaving ToLower ASCII-only would let a query find a row that a later comparison rejects. /// [Fact] public async Task ToLower_Folds() { await EnsureSeededAsync(); var results = await Redb.Query() .Where(c => c.Text.ToLower().Contains("привет")) .Take(50).ToListAsync(); results.Select(r => r.Props.Label).Should().Contain("cyrillic"); } [Fact] public async Task ToUpper_Folds() { await EnsureSeededAsync(); var results = await Redb.Query() .Where(c => c.Text.ToUpper().Contains("ПРИВЕТ")) .Take(50).ToListAsync(); results.Select(r => r.Props.Label).Should().Contain("cyrillic"); } /// A needle matching nothing must still match nothing. Folding is not widening. [Fact] public async Task ContainsIgnoreCase_DoesNotMatchUnrelatedRows() { await EnsureSeededAsync(); (await FindAsync("привет")).Should().NotContain("greek").And.NotContain("ascii"); } /// The same word with ß on both sides matches everywhere: that mapping is one to one. [Fact] public async Task GermanSharpS_MatchesItself() { await EnsureSeededAsync(); (await FindAsync("straße")).Should().Contain("german"); } /// With the diacritic present it matches, which is the case-folding half working. [Fact] public async Task Diacritics_MatchWhenPresent() { await EnsureSeededAsync(); (await FindAsync("müller")).Should().Contain("umlaut"); } // ===================================================================== // Boundaries. Provider-specific on purpose — see the remark below. // ===================================================================== /// /// German ß against SS, Turkish İ against i, and diacritics are the three things a collation /// cannot fold uniformly. What each provider actually does with them differs, and that surprised /// this suite: SQL Server's CI_AS folds ß to SS and İ to i, while the ICU root collation used on /// PostgreSQL folds neither. Asserting one answer for all three providers would have been wrong, /// so each states its own and the differences are the documentation. /// /// /// Diacritics are the one boundary that holds everywhere: "muller" never finds "Müller". That is /// not case folding at all, and on PostgreSQL it is refused outright — a nondeterministic /// collation, which is what accent-insensitivity needs, cannot be used with ILIKE. /// /// [Fact] public async Task KnownBoundary_Diacritics_AreNotFolded() { await EnsureSeededAsync(); (await FindAsync("muller")).Should().NotContain("umlaut"); } /// /// Does this provider fold ß to SS? Overridden per provider rather than assumed; see /// for why the boundaries are not shared. /// protected abstract bool FoldsSharpSToDoubleS { get; } /// Does this provider fold the Turkish dotted capital İ to a plain i? protected abstract bool FoldsTurkishDottedI { get; } /// /// BR-6 (owner decision 2026-09-02): the PLAIN string predicates (StartsWith/Contains/EndsWith /// without a comparison) translate to LIKE and keep each database's own case rules - redb does /// not decide for the programmer. PostgreSQL: case-sensitive; SQLite: ASCII case-insensitive; /// MSSQL: whatever the collation says (CI on the default). Explicit control is the *IgnoreCase /// forms plus StringCollation. This flag states the provider's documented default. /// protected abstract bool PlainStartsWithIsCaseSensitive { get; } [Fact] public async Task PlainStartsWith_IsTheDatabasesOwnSemantics_ByContract() { await EnsureSeededAsync(); // Exact case matches everywhere - the sanity half of the contract. var exact = await Redb.Query() .Where(c => c.Text.StartsWith("HELLO")).Take(50).ToListAsync(); exact.Select(r => r.Props.Label).Should().Contain("ascii"); // A lower-case needle against the upper-cased row: the database's own rule decides. var folded = await Redb.Query() .Where(c => c.Text.StartsWith("hello")).Take(50).ToListAsync(); if (PlainStartsWithIsCaseSensitive) folded.Select(r => r.Props.Label).Should().NotContain("ascii", "this database's LIKE is case-sensitive and redb passes its rule through"); else folded.Select(r => r.Props.Label).Should().Contain("ascii", "this database's LIKE folds ASCII case and redb passes its rule through"); } [Fact] public async Task Boundary_GermanSharpS_AgainstDoubleS() { await EnsureSeededAsync(); var found = await FindAsync("strasse"); if (FoldsSharpSToDoubleS) found.Should().Contain("german", "this provider's collation expands ß to SS"); else found.Should().NotContain("german", "pattern matching folds character by character and cannot change a string's length, " + "even though UPPER on the same database does expand ß to SS"); } [Fact] public async Task Boundary_TurkishDottedI() { await EnsureSeededAsync(); var found = await FindAsync("istanbul"); if (FoldsTurkishDottedI) found.Should().Contain("turkish", "this provider's collation folds İ to i"); else found.Should().NotContain("turkish", "İ (U+0130) folds to i with a combining dot, not to plain i. Unfixable by one " + "setting anyway: Turkish wants I→ı where every other locale wants I→i"); } }