Path: redb/redb.Tests.Integration/Tests/Base/TemporalRoundTripTestsBase.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/TemporalRoundTripTestsBase.cs ---- using System.Globalization; using redb.Core; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// The temporal contract, end to end against a real database. /// /// /// DateTime carries no time zone. The clock reading is the datum: 14:00 written is 14:00 /// read, on any host, in any zone, through any read path — materialization, ordering, aggregate. /// DateTimeOffset is the opposite and deliberately untouched: it is the native .NET type and /// carries a real instant. /// /// /// /// No form follows the culture. and are stored as their Ticks in /// _values._Long, independent of the ambient ; a row written on one host loads on /// another. /// /// /// /// Why this suite exists: every other test in the repository uses DateTime.UtcNow or a /// DateTimeKind.Utc literal, where the zone-less and the instant reading coincide exactly, /// and none of them touches DateTimeOffset, DateOnly, TimeOnly or TimeSpan at all. The conversion /// paths below were unreachable from a test. /// /// public abstract class TemporalRoundTripTestsBase { protected readonly IRedbService Redb; private bool _seeded; protected TemporalRoundTripTestsBase(IRedbService redb) => Redb = redb; // A fixed non-UTC offset, so "instant" and "reading" are never accidentally equal. private static readonly TimeSpan Offset = TimeSpan.FromHours(3); private static DateTime Reading(int day, int hour) => new(2026, 3, day, hour, 30, 0, DateTimeKind.Unspecified); /// Runs under a specific culture, restoring the previous one. private static async Task UnderCultureAsync(string name, Func body) { var previous = CultureInfo.CurrentCulture; try { CultureInfo.CurrentCulture = new CultureInfo(name); await body(); } finally { CultureInfo.CurrentCulture = previous; } } /// /// Ten rows, one per day 1..10 of 2026-03, hour = 10 + (i % 3). Every temporal field moves /// together so a filter on one can be cross-checked against another. /// protected async Task EnsureSeededAsync() { if (_seeded) return; // Scheme name is the CLR FullName: the positional [RedbScheme("...")] argument is the // ALIAS, never the name. Deleting by the alias silently matched nothing and rows piled up // across tests, which is exactly how this line was wrong the first time. await Redb.Context.ExecuteAsync( "DELETE FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = " + $"'{typeof(TemporalProps).FullName}')"); var rows = Enumerable.Range(1, 10).Select(i => new RedbObject { name = $"temporal-{i:00}", Props = new TemporalProps { When = Reading(i, 10 + (i % 3)), Moment = new DateTimeOffset(2026, 3, i, 10 + (i % 3), 30, 0, Offset), Day = new DateOnly(2026, 3, i), Clock = new TimeOnly(10 + (i % 3), 30, 0), Span = TimeSpan.FromDays(i).Add(TimeSpan.FromHours(2)), Label = $"row-{i:00}" } }).ToList(); await Redb.SaveAsync(rows); _seeded = true; } // ===================================================================== // Round trip // ===================================================================== /// /// Every temporal type survives a save/load unchanged, whatever culture the host runs under. /// The DateOnly/TimeOnly/TimeSpan write path used to go through the current culture's short /// pattern ("23.08.2026", "2:30 PM") and the read path parsed it back the same /// way, so the pair only agreed while the culture stayed put. /// [Theory] [InlineData("ru-RU")] [InlineData("en-US")] [InlineData("de-DE")] public async Task RoundTrip_AllTemporalTypes_SurviveAnyCulture(string culture) { await UnderCultureAsync(culture, async () => { var source = new TemporalRoundTripProps { When = new DateTime(2026, 8, 23, 14, 0, 0, DateTimeKind.Unspecified), Moment = new DateTimeOffset(2026, 8, 23, 14, 0, 0, Offset), Day = new DateOnly(2026, 8, 23), Clock = new TimeOnly(14, 30, 15), Span = TimeSpan.FromDays(3).Add(TimeSpan.FromHours(2)), Label = $"roundtrip-{culture}" }; var obj = new RedbObject { name = source.Label, Props = source }; var id = await Redb.SaveAsync(obj); var loaded = await Redb.LoadAsync(id); // Zone-less: the reading comes back as written, labelled UTC. loaded.Props.When.Should().Be(new DateTime(2026, 8, 23, 14, 0, 0, DateTimeKind.Utc)); loaded.Props.When.Kind.Should().Be(DateTimeKind.Utc); // Zone-aware: the instant comes back, the offset is normalised away. loaded.Props.Moment.UtcDateTime.Should().Be(source.Moment.UtcDateTime); loaded.Props.Day.Should().Be(source.Day); loaded.Props.Clock.Should().Be(source.Clock); loaded.Props.Span.Should().Be(source.Span, "the day component must survive"); }); } /// /// A value keeps its reading rather than being shifted to the /// host's UTC offset. This is the whole point of the contract, and it is invisible on a host /// running in UTC — hence the explicit non-UTC assertion below. /// [Fact] public async Task RoundTrip_DateTime_KeepsReadingNotInstant() { var local = new DateTime(2026, 8, 23, 14, 0, 0, DateTimeKind.Local); var id = await Redb.SaveAsync(new RedbObject { name = "kind-local", Props = new TemporalRoundTripProps { When = local, Label = "kind-local" } }); var loaded = await Redb.LoadAsync(id); loaded.Props.When.Hour.Should().Be(14); loaded.Props.When.Should().Be(new DateTime(2026, 8, 23, 14, 0, 0, DateTimeKind.Utc)); } /// /// The edges of the CLR types come back as themselves: , /// and are what a model that never /// set them carries. SQLite encoded a datetime through the OLE automation date, whose zero stands for /// MinValue: a default date came back as 1899-12-30. /// [Fact] public async Task RoundTrip_TemporalMinValues_ComeBackAsThemselves() { var id = await Redb.SaveAsync(new RedbObject { name = $"temporal-min-{Guid.NewGuid():N}", Props = new TemporalRoundTripProps { When = DateTime.MinValue, Moment = DateTimeOffset.MinValue, Day = DateOnly.MinValue, Clock = TimeOnly.MinValue, Span = TimeSpan.Zero, Label = "min" } }); var loaded = (await Redb.LoadAsync(id))!; loaded.Props.When.Should().Be(DateTime.MinValue); loaded.Props.Moment.Should().Be(DateTimeOffset.MinValue); loaded.Props.Day.Should().Be(DateOnly.MinValue); loaded.Props.Clock.Should().Be(TimeOnly.MinValue); loaded.Props.Span.Should().Be(TimeSpan.Zero); } /// /// A time of day before 1899-12-30 is kept, and the row is found by the value it was written from. The OLE /// automation date encodes a day before its epoch as sign and magnitude, not as a linear count: through it /// 1850-05-03 12:34 moved a day on the way in and again on the way out, and a filter built from the same value /// compared against a day that was never stored. /// [Fact] public async Task RoundTrip_TimeOfDayBefore1900_IsKept_AndFoundByItsValue() { var label = $"pre1900-{Guid.NewGuid():N}"; var reading = new DateTime(1850, 5, 3, 12, 34, 56, DateTimeKind.Unspecified); var id = await Redb.SaveAsync(new RedbObject { name = label, Props = new TemporalRoundTripProps { When = reading, Moment = new DateTimeOffset(reading, Offset), Day = new DateOnly(1850, 5, 3), Clock = new TimeOnly(12, 34, 56), Span = TimeSpan.Zero, Label = label } }); var loaded = (await Redb.LoadAsync(id))!; loaded.Props.When.Should().Be(reading); loaded.Props.Moment.UtcDateTime.Should().Be(new DateTimeOffset(reading, Offset).UtcDateTime); loaded.Props.Day.Should().Be(new DateOnly(1850, 5, 3)); var found = await Redb.Query() .Where(t => t.Label == label && t.When == reading) .Take(5).ToListAsync(); found.Should().ContainSingle().Which.id.Should().Be(id); } // ===================================================================== // Comparison: DateTime // ===================================================================== [Fact] public async Task Where_DateTime_GreaterThan() { await EnsureSeededAsync(); var cutoff = Reading(5, 0); var results = await Redb.Query() .Where(t => t.When > cutoff) .Take(50).ToListAsync(); results.Should().NotBeEmpty(); results.Should().AllSatisfy(r => r.Props.When.Should().BeAfter(cutoff)); results.Should().HaveCount(6, "days 5..10 all start after 2026-03-05T00:30 except day 5 itself is 10:30+"); } [Fact] public async Task Where_DateTime_LessThan() { await EnsureSeededAsync(); var cutoff = Reading(5, 0); var results = await Redb.Query() .Where(t => t.When < cutoff) .Take(50).ToListAsync(); results.Should().NotBeEmpty(); results.Should().AllSatisfy(r => r.Props.When.Should().BeBefore(cutoff)); } [Fact] public async Task Where_DateTime_Between() { await EnsureSeededAsync(); var from = Reading(3, 0); var to = Reading(7, 0); var results = await Redb.Query() .Where(t => t.When >= from && t.When < to) .Take(50).ToListAsync(); results.Should().HaveCount(4); results.Should().AllSatisfy(r => { r.Props.When.Should().BeOnOrAfter(from); r.Props.When.Should().BeBefore(to); }); } /// /// Equality closes the loop: the value the filter is built from and the value the writer stored /// must be the same, or a saved row becomes unfindable by its own value. Write and search go /// through the same normalisation, which is what makes this hold. /// [Fact] public async Task Where_DateTime_Equal_FindsTheRowItWasWrittenFrom() { await EnsureSeededAsync(); var exact = Reading(4, 11); var results = await Redb.Query() .Where(t => t.When == exact) .Take(50).ToListAsync(); results.Should().ContainSingle(); results[0].Props.Label.Should().Be("row-04"); } // ===================================================================== // Comparison: DateTimeOffset // ===================================================================== [Fact] public async Task Where_DateTimeOffset_GreaterThan() { await EnsureSeededAsync(); var cutoff = new DateTimeOffset(2026, 3, 5, 0, 0, 0, Offset); var results = await Redb.Query() .Where(t => t.Moment > cutoff) .Take(50).ToListAsync(); results.Should().NotBeEmpty(); results.Should().AllSatisfy(r => r.Props.Moment.Should().BeAfter(cutoff)); } /// /// Two ways of writing the same instant must select the same rows. A DateTimeOffset carries a /// real moment, so 10:30+03:00 and 07:30Z are one and the same cutoff. /// [Fact] public async Task Where_DateTimeOffset_SameInstantDifferentOffset_SelectsSameRows() { await EnsureSeededAsync(); var withOffset = new DateTimeOffset(2026, 3, 5, 0, 0, 0, Offset); var asUtc = withOffset.ToUniversalTime(); var a = await Redb.Query().Where(t => t.Moment > withOffset).Take(50).ToListAsync(); var b = await Redb.Query().Where(t => t.Moment > asUtc).Take(50).ToListAsync(); a.Select(x => x.Id).OrderBy(x => x).Should().Equal(b.Select(x => x.Id).OrderBy(x => x)); } // ===================================================================== // Comparison: DateOnly // ===================================================================== [Fact] public async Task Where_DateOnly_GreaterThan() { await EnsureSeededAsync(); var cutoff = new DateOnly(2026, 3, 5); var results = await Redb.Query() .Where(t => t.Day > cutoff) .Take(50).ToListAsync(); results.Should().HaveCount(5); results.Should().AllSatisfy(r => r.Props.Day.Should().BeAfter(cutoff)); } [Fact] public async Task Where_DateOnly_Equal() { await EnsureSeededAsync(); var exact = new DateOnly(2026, 3, 7); var results = await Redb.Query() .Where(t => t.Day == exact) .Take(50).ToListAsync(); results.Should().ContainSingle(); results[0].Props.Label.Should().Be("row-07"); } // ===================================================================== // Ordering and the analytics path // ===================================================================== [Fact] public async Task OrderBy_DateTime_IsChronological() { await EnsureSeededAsync(); var results = await Redb.Query() .OrderBy(t => t.When) .Take(50).ToListAsync(); results.Select(r => r.Props.When).Should().BeInAscendingOrder(); } /// /// Base-field aggregates go through JsonValueConverter, a different converter from the /// one object materialization uses. It used to parse a zoned ISO string into the caller's LOCAL /// zone, so the same stored field answered differently depending on how it was read. The two /// paths must agree exactly, and must not follow the ambient culture. /// [Fact] public async Task Aggregate_BaseDateField_AgreesWithMaterialization() { await EnsureSeededAsync(); var min = await Redb.Query().MinRedbAsync(o => o.DateCreate); var max = await Redb.Query().MaxRedbAsync(o => o.DateCreate); var all = await Redb.Query().Take(50).ToListAsync(); min.Should().NotBeNull(); max.Should().NotBeNull(); min!.Value.UtcDateTime.Should().Be(all.Min(r => r.DateCreate).UtcDateTime); max!.Value.UtcDateTime.Should().Be(all.Max(r => r.DateCreate).UtcDateTime); } [Fact] public async Task Aggregate_BaseDateField_IsCultureIndependent() { await EnsureSeededAsync(); DateTimeOffset? underRu = default, underUs = default; await UnderCultureAsync("ru-RU", async () => underRu = await Redb.Query().MinRedbAsync(o => o.DateCreate)); await UnderCultureAsync("en-US", async () => underUs = await Redb.Query().MinRedbAsync(o => o.DateCreate)); underRu.Should().Be(underUs); } // KNOWN BOUNDARY, not covered by a test on purpose. // // MinAsync/MaxAsync over a *Props* field return through ExecuteAggregateAsync, whose contract // is Task. That works for a backend which stores datetimes as a number (SQLite, whose // REAL Julian day is decoded by TemporalDecoder) and cannot work on PostgreSQL or SQL Server, // where the aggregate comes back as an ISO string and the decimal conversion throws // FormatException. The failure is provider-shaped, so a shared assertion here would be either // wrong on one backend or vacuous on all. Widening the contract to a type-preserving one // touches every provider and both tiers — deferred, see DATETIME.md. // // Base-field aggregates (the two tests above) are unaffected: they return JSON and go through // JsonValueConverter. // ===================================================================== // TimeOnly / TimeSpan: equality works, ordering is a documented boundary // ===================================================================== [Fact] public async Task Where_TimeOnly_Equal() { await EnsureSeededAsync(); var exact = new TimeOnly(12, 30, 0); // i % 3 == 2 → days 2, 5, 8 var results = await Redb.Query() .Where(t => t.Clock == exact) .Take(50).ToListAsync(); results.Should().NotBeEmpty(); results.Should().AllSatisfy(r => r.Props.Clock.Should().Be(exact)); } [Fact] public async Task Where_TimeSpan_Equal() { await EnsureSeededAsync(); var exact = TimeSpan.FromDays(4).Add(TimeSpan.FromHours(2)); var results = await Redb.Query() .Where(t => t.Span == exact) .Take(50).ToListAsync(); results.Should().ContainSingle(); results[0].Props.Label.Should().Be("row-04"); } /// /// and are stored as their Ticks in _values._Long, so ordered /// comparison and sorting are chronological. As text they were lexicographic: "10.02:00:00" sorted before /// "2.02:00:00", and a filter Span > 9 days 2 hours left the row of 10 days out. /// [Fact] public async Task Where_TimeSpan_OrderedComparison_IsChronological() { await EnsureSeededAsync(); var results = await Redb.Query() .Where(t => t.Span > TimeSpan.FromDays(8).Add(TimeSpan.FromHours(2))) .Take(50).ToListAsync(); results.Select(r => r.Props.Label).Should().BeEquivalentTo(["row-09", "row-10"], "9 and 10 days are longer than 8 days"); } [Fact] public async Task OrderBy_TimeSpan_And_TimeOnly_AreChronological() { await EnsureSeededAsync(); var bySpan = await Redb.Query().OrderBy(t => t.Span).Take(50).ToListAsync(); bySpan.Select(r => r.Props.Span).Should().BeInAscendingOrder(); bySpan.Select(r => r.Props.Label).Should().Equal(Enumerable.Range(1, 10).Select(i => $"row-{i:00}")); var byClock = await Redb.Query().OrderByDescending(t => t.Clock).Take(50).ToListAsync(); byClock.Select(r => r.Props.Clock).Should().BeInDescendingOrder(); } }