Path: redb/redb.Tests.Integration/Tests/Base/TypeChangeTicksAndKeysTestsBase.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/TypeChangeTicksAndKeysTestsBase.cs ---- using AwesomeAssertions.Execution; using Microsoft.Extensions.DependencyInjection; using redb.Core; using redb.Core.Attributes; using redb.Core.Models.Entities; using redb.Core.Query; namespace redb.Tests.Integration.Tests.Base; [RedbScheme("Type change: date to time span", Name = "type_change_date_to_span", Version = 1)] public class TypeChangeDateToSpanProps { /// Staged as DateTime, declared here as TimeSpan. public TimeSpan Span { get; set; } } [RedbScheme("Type change: time span to date", Name = "type_change_span_to_date", Version = 1)] public class TypeChangeSpanToDateProps { /// Staged as TimeSpan, declared here as DateTime. public DateTime When { get; set; } } [RedbScheme("Type change: dictionary key text to int", Name = "type_change_dictionary_key", Version = 1)] public class TypeChangeDictionaryKeyProps { /// Staged with a string key type and a key that is not a number, declared here with int keys. public Dictionary Items { get; set; } = new(); } /// /// Type changes at scheme sync whose stored values the sync does not convert honestly. A TimeSpan lives in the long /// column as ticks, while the long-to-date conversion and back reads and writes Unix seconds: the value changes its /// meaning and the sync reports success. A dictionary key type change only updates the structure: keys that do not /// parse under the new key type pass the sync and fail later, at read. Staged as the type tests do: values written /// through the current class, then the structure and the column rewritten to the shape an earlier class left. /// public abstract class TypeChangeTicksAndKeysTestsBase : IAsyncLifetime { protected readonly IRedbService Redb; private readonly ISqlDialect _dialect; private static readonly string[] Schemes = ["type_change_date_to_span", "type_change_span_to_date", "type_change_dictionary_key"]; protected TypeChangeTicksAndKeysTestsBase(IRedbService redb, IServiceProvider services) { Redb = redb; _dialect = services.GetRequiredService(); } public async Task InitializeAsync() { foreach (var scheme in Schemes) await ResetSchemeAsync(scheme); } public async Task DisposeAsync() { foreach (var scheme in Schemes) await ResetSchemeAsync(scheme); } [Fact] public async Task DateTimeToTimeSpan_WithStoredValues_IsRefused() { await Redb.SyncSchemeAsync(); var id = await Redb.SaveAsync(new RedbObject { name = "date-to-span", Props = new TypeChangeDateToSpanProps { Span = TimeSpan.FromHours(1) } }); var structureId = await StructureIdAsync("type_change_date_to_span", "Span"); var stagedDate = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero); await Redb.Context.ExecuteAsync( $"UPDATE _values SET _DateTimeOffset = {_dialect.FormatDateTimeOffsetLiteral(stagedDate)}, _Long = NULL " + $"WHERE _id_structure = {structureId}"); await SetStructureTypeAsync(structureId, "DateTime"); var syncError = await Record.ExceptionAsync(() => Redb.SyncSchemeAsync()); var storedLong = await Redb.Context.ExecuteScalarAsync( $"SELECT _Long FROM _values WHERE _id_structure = {structureId}"); TimeSpan? read = null; if (syncError == null) read = (await Redb.LoadAsync(id))!.Props.Span; using (new AssertionScope()) { syncError.Should().NotBeNull( $"a date has no time span meaning; the sync stored {storedLong} in _Long and the property reads {read}"); (await StructureTypeNameAsync(structureId)).Should().Be("DateTime", "a refused change keeps the type"); } } [Fact] public async Task TimeSpanToDateTime_WithStoredValues_IsRefused() { await Redb.SyncSchemeAsync(); var id = await Redb.SaveAsync(new RedbObject { name = "span-to-date", Props = new TypeChangeSpanToDateProps { When = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc) } }); var structureId = await StructureIdAsync("type_change_span_to_date", "When"); await Redb.Context.ExecuteAsync( $"UPDATE _values SET _Long = {TimeSpan.FromHours(1).Ticks}, _DateTimeOffset = NULL WHERE _id_structure = {structureId}"); await SetStructureTypeAsync(structureId, "TimeSpan"); var syncError = await Record.ExceptionAsync(() => Redb.SyncSchemeAsync()); var storedDate = await Redb.Context.ExecuteScalarAsync( $"SELECT CAST(_DateTimeOffset AS {TextType}) FROM _values WHERE _id_structure = {structureId}"); object? read = null; if (syncError == null) read = await Record.ExceptionAsync(() => Redb.LoadAsync(id)) is { } loadError ? loadError.GetType().Name : (await Redb.LoadAsync(id))!.Props.When; using (new AssertionScope()) { syncError.Should().NotBeNull( $"a time span has no date meaning; the sync stored {storedDate} in _DateTimeOffset and the property reads {read}"); (await StructureTypeNameAsync(structureId)).Should().Be("TimeSpan", "a refused change keeps the type"); } } [Fact] public async Task DictionaryKeyTextToInt_WithAKeyThatIsNotANumber_IsRefusedAtSync() { await Redb.SyncSchemeAsync(); var id = await Redb.SaveAsync(new RedbObject { name = "dictionary-key", Props = new TypeChangeDictionaryKeyProps { Items = new Dictionary { [1] = "x" } } }); var structureId = await StructureIdAsync("type_change_dictionary_key", "Items"); var stringType = await TypeIdAsync("String"); await Redb.Context.ExecuteAsync( $"UPDATE _values SET _array_index = 'abc' WHERE _id_structure = {structureId} AND _array_index = '1'"); await Redb.Context.ExecuteAsync($"UPDATE _structures SET _key_type = {stringType} WHERE _id = {structureId}"); await Redb.Context.ExecuteAsync( $"UPDATE _schemes SET _version = NULL WHERE _id = (SELECT _id_scheme FROM _structures WHERE _id = {structureId})"); Redb.Cache.Clear(); (await Redb.Context.ExecuteScalarAsync( $"SELECT CAST(COUNT(*) AS BIGINT) FROM _values WHERE _id_structure = {structureId} AND _array_index = 'abc'")) .Should().Be(1, "precondition: the staged key is stored"); var syncError = await Record.ExceptionAsync(() => Redb.SyncSchemeAsync()); var loadError = syncError == null ? await Record.ExceptionAsync(() => Redb.LoadAsync(id)) : null; using (new AssertionScope()) { syncError.Should().NotBeNull( $"a stored key 'abc' does not parse as int; without a refusal the load fails later: {loadError?.GetType().Name}: {loadError?.Message}"); (await Redb.Context.ExecuteScalarAsync($"SELECT _key_type FROM _structures WHERE _id = {structureId}")) .Should().Be(stringType, "a refused change keeps the key type"); } } /// The engine's text type for a cast in a read-back query. protected abstract string TextType { get; } private async Task ResetSchemeAsync(string schemeName) { await Redb.Context.ExecuteAsync( $"DELETE FROM _values WHERE _id_object IN (SELECT _id FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{schemeName}'))"); await Redb.Context.ExecuteAsync( $"DELETE FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{schemeName}')"); await Redb.Context.ExecuteAsync( $"DELETE FROM _structures WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{schemeName}')"); await Redb.Context.ExecuteAsync($"DELETE FROM _schemes WHERE _name = '{schemeName}'"); Redb.Cache.Clear(); } private async Task TypeIdAsync(string typeName) => (await Redb.Context.ExecuteScalarAsync($"SELECT _id FROM _types WHERE _name = '{typeName}'"))!.Value; private async Task StructureIdAsync(string schemeName, string propertyName) => (await Redb.Context.ExecuteScalarAsync( $"SELECT _id FROM _structures WHERE _name = '{propertyName}' AND _id_scheme IN " + $"(SELECT _id FROM _schemes WHERE _name = '{schemeName}')"))!.Value; private Task StructureTypeNameAsync(long structureId) => Redb.Context.ExecuteScalarAsync( $"SELECT t._name FROM _structures s JOIN _types t ON t._id = s._id_type WHERE s._id = {structureId}"); /// Repoints the structure at another type, as an earlier class left it (no scheme version recorded). private async Task SetStructureTypeAsync(long structureId, string typeName) { var typeId = await TypeIdAsync(typeName); await Redb.Context.ExecuteAsync($"UPDATE _structures SET _id_type = {typeId} WHERE _id = {structureId}"); await Redb.Context.ExecuteAsync( $"UPDATE _schemes SET _version = NULL WHERE _id = (SELECT _id_scheme FROM _structures WHERE _id = {structureId})"); Redb.Cache.Clear(); } }