|
| 1 | +// <copyright file="FormatCompatPolicyTests.cs" company="MPCoreDeveloper"> |
| 2 | +// Copyright (c) 2026 MPCoreDeveloper. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// </copyright> |
| 5 | +namespace SharpCoreDB.Tests; |
| 6 | + |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using SharpCoreDB.Interfaces; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.IO; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Encoding of the compatibility policy in <c>docs/manual/upgrade-and-downgrade.md</c>: |
| 16 | +/// (1) files written in the legacy variable-length format (the pre-marker / pre-fixed-width |
| 17 | +/// representation) must read back and keep working across reopen cycles, and (2) commit-time |
| 18 | +/// tombstone markers written by the current version must stay stable across repeated reopen cycles |
| 19 | +/// and coexist with rows appended afterwards. These are the forward-compatibility guarantees that |
| 20 | +/// make downgrade the only unsupported direction. |
| 21 | +/// </summary> |
| 22 | +public sealed class FormatCompatPolicyTests : IDisposable |
| 23 | +{ |
| 24 | + private readonly DatabaseFactory _factory; |
| 25 | + private readonly string _dirPath; |
| 26 | + |
| 27 | + public FormatCompatPolicyTests() |
| 28 | + { |
| 29 | + var services = new ServiceCollection(); |
| 30 | + services.AddSharpCoreDB(); |
| 31 | + _factory = services.BuildServiceProvider().GetRequiredService<DatabaseFactory>(); |
| 32 | + _dirPath = Path.Combine(Path.GetTempPath(), $"SCDB_FormatCompat_{Guid.NewGuid():N}"); |
| 33 | + } |
| 34 | + |
| 35 | + public void Dispose() |
| 36 | + { |
| 37 | + try { if (Directory.Exists(_dirPath)) Directory.Delete(_dirPath, true); } catch { } |
| 38 | + } |
| 39 | + |
| 40 | + private static void InsertRange(IDatabase db, int from, int to) |
| 41 | + { |
| 42 | + var stmts = new List<string>(to - from + 1); |
| 43 | + for (int i = from; i <= to; i++) |
| 44 | + { |
| 45 | + stmts.Add($"INSERT INTO docs VALUES ({i}, 'user{i}', {i * 0.5})"); |
| 46 | + } |
| 47 | + |
| 48 | + db.ExecuteBatchSQL(stmts); |
| 49 | + db.Flush(); |
| 50 | + } |
| 51 | + |
| 52 | + private static void DeleteRange(IDatabase db, int from, int to) |
| 53 | + { |
| 54 | + var stmts = new List<string>(to - from + 1); |
| 55 | + for (int i = from; i <= to; i++) |
| 56 | + { |
| 57 | + stmts.Add($"DELETE FROM docs WHERE id = {i}"); |
| 58 | + } |
| 59 | + |
| 60 | + db.ExecuteBatchSQL(stmts); |
| 61 | + db.Flush(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void LegacyVariableLengthFile_ReadsBackAndKeepsWorking_AcrossReopens() |
| 66 | + { |
| 67 | + // A legacy variable-length database (no fixed-width layout) is byte-compatible with the |
| 68 | + // pre-fixed-width / pre-marker format. It must open, round-trip and then accept marker |
| 69 | + // writes from the current version. |
| 70 | + IDatabase? db = _factory.Create(_dirPath, "pw", isReadOnly: false, |
| 71 | + config: new DatabaseConfig { NoEncryptMode = true, AutoFixedWidthRecords = false, FixedWidthRecordLayout = false }); |
| 72 | + try |
| 73 | + { |
| 74 | + db.ExecuteSQL("CREATE TABLE docs (id INTEGER PRIMARY KEY, name TEXT, score REAL)"); |
| 75 | + InsertRange(db, 1, 300); |
| 76 | + Assert.Equal(300, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 77 | + } |
| 78 | + finally { (db as IDisposable)?.Dispose(); } |
| 79 | + |
| 80 | + // First reopen = "old file read by new version" (no markers written yet). |
| 81 | + db = _factory.Create(_dirPath, "pw", isReadOnly: false, |
| 82 | + config: new DatabaseConfig { NoEncryptMode = true, AutoFixedWidthRecords = false, FixedWidthRecordLayout = false }); |
| 83 | + try |
| 84 | + { |
| 85 | + Assert.Equal(300, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 86 | + DeleteRange(db, 1, 40); |
| 87 | + Assert.Equal(260, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 88 | + } |
| 89 | + finally { (db as IDisposable)?.Dispose(); } |
| 90 | + |
| 91 | + // Final reopen: the legacy rows plus current-version tombstone markers coexist. |
| 92 | + db = _factory.Create(_dirPath, "pw", isReadOnly: false, |
| 93 | + config: new DatabaseConfig { NoEncryptMode = true, AutoFixedWidthRecords = false, FixedWidthRecordLayout = false }); |
| 94 | + try |
| 95 | + { |
| 96 | + Assert.Equal(260, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 97 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE id = 10")); |
| 98 | + Assert.Single(db.ExecuteQuery("SELECT id FROM docs WHERE id = 41")); |
| 99 | + } |
| 100 | + finally { (db as IDisposable)?.Dispose(); } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void CommitTimeTombstoneMarkers_StableAcrossReopenCycles_AndCoexistWithAppends() |
| 105 | + { |
| 106 | + IDatabase? db = _factory.Create(_dirPath, "pw", isReadOnly: false, config: new DatabaseConfig()); |
| 107 | + try |
| 108 | + { |
| 109 | + db.ExecuteSQL("CREATE TABLE docs (id INTEGER PRIMARY KEY, name TEXT, score REAL)"); |
| 110 | + InsertRange(db, 1, 1000); |
| 111 | + DeleteRange(db, 1, 500); |
| 112 | + Assert.Equal(500, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 113 | + } |
| 114 | + finally { (db as IDisposable)?.Dispose(); } |
| 115 | + |
| 116 | + db = _factory.Create(_dirPath, "pw", isReadOnly: false, config: new DatabaseConfig()); |
| 117 | + try |
| 118 | + { |
| 119 | + Assert.Equal(500, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 120 | + InsertRange(db, 1001, 1200); // appends after the marker region |
| 121 | + Assert.Equal(700, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 122 | + DeleteRange(db, 1001, 1100); |
| 123 | + } |
| 124 | + finally { (db as IDisposable)?.Dispose(); } |
| 125 | + |
| 126 | + db = _factory.Create(_dirPath, "pw", isReadOnly: false, config: new DatabaseConfig()); |
| 127 | + try |
| 128 | + { |
| 129 | + Assert.Equal(600, db.ExecuteQuery("SELECT id FROM docs").Count); |
| 130 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE id = 1")); |
| 131 | + Assert.Empty(db.ExecuteQuery("SELECT id FROM docs WHERE id = 1050")); |
| 132 | + Assert.Single(db.ExecuteQuery("SELECT id FROM docs WHERE id = 1101")); |
| 133 | + Assert.Single(db.ExecuteQuery("SELECT id FROM docs WHERE id = 1200")); |
| 134 | + } |
| 135 | + finally { (db as IDisposable)?.Dispose(); } |
| 136 | + } |
| 137 | +} |
0 commit comments