Reformat / cleanup
All checks were successful
NuGet Publish / build-and-pack (push) Successful in 46s
NuGet Publish / publish-to-gitea (push) Successful in 56s

This commit is contained in:
Joseph Doherty
2026-02-21 08:10:36 -05:00
parent 4c6aaa5a3f
commit a70d8befae
176 changed files with 50555 additions and 49587 deletions

View File

@@ -1,10 +1,9 @@
using ZB.MOM.WW.CBDD.Bson;
using System.IO.Compression;
using System.IO.MemoryMappedFiles;
using System.Security.Cryptography;
using ZB.MOM.WW.CBDD.Core.Compression;
using ZB.MOM.WW.CBDD.Core.Storage;
using ZB.MOM.WW.CBDD.Shared;
using System.Security.Cryptography;
using System.IO.Compression;
using System.IO.MemoryMappedFiles;
namespace ZB.MOM.WW.CBDD.Tests;
@@ -13,7 +12,7 @@ public class DbContextTests : IDisposable
private string _dbPath;
/// <summary>
/// Initializes test file paths for database context tests.
/// Initializes test file paths for database context tests.
/// </summary>
public DbContextTests()
{
@@ -21,12 +20,27 @@ public class DbContextTests : IDisposable
}
/// <summary>
/// Verifies the basic database context lifecycle works.
/// Disposes test resources and cleans up generated files.
/// </summary>
public void Dispose()
{
try
{
CleanupDbFiles(_dbPath);
}
catch
{
// Ignore cleanup errors
}
}
/// <summary>
/// Verifies the basic database context lifecycle works.
/// </summary>
[Fact]
public void DbContext_BasicLifecycle_Works()
{
using var db = new Shared.TestDbContext(_dbPath);
using var db = new TestDbContext(_dbPath);
var user = new User { Name = "Alice", Age = 30 };
var id = db.Users.Insert(user);
@@ -38,12 +52,12 @@ public class DbContextTests : IDisposable
}
/// <summary>
/// Verifies multiple CRUD operations execute correctly in one context.
/// Verifies multiple CRUD operations execute correctly in one context.
/// </summary>
[Fact]
public void DbContext_MultipleOperations_Work()
{
using var db = new Shared.TestDbContext(_dbPath);
using var db = new TestDbContext(_dbPath);
// Insert
var alice = new User { Name = "Alice", Age = 30 };
@@ -69,32 +83,33 @@ public class DbContextTests : IDisposable
}
/// <summary>
/// Verifies disposing and reopening context preserves persisted data.
/// Verifies disposing and reopening context preserves persisted data.
/// </summary>
[Fact]
public void DbContext_Dispose_ReleasesResources()
{
_dbPath = Path.Combine(Path.GetTempPath(), $"test_dbcontext_reopen.db");
_dbPath = Path.Combine(Path.GetTempPath(), "test_dbcontext_reopen.db");
var totalUsers = 0;
// First context - insert and dispose (auto-checkpoint)
using (var db = new Shared.TestDbContext(_dbPath))
using (var db = new TestDbContext(_dbPath))
{
db.Users.Insert(new User { Name = "Test", Age = 20 });
db.SaveChanges(); // Explicitly save changes to ensure data is in WAL
var beforeCheckpointTotalUsers = db.Users.FindAll().Count();
int beforeCheckpointTotalUsers = db.Users.FindAll().Count();
db.ForceCheckpoint(); // Force checkpoint to ensure data is persisted to main file
totalUsers = db.Users.FindAll().Count();
var countedUsers = db.Users.Count();
int countedUsers = db.Users.Count();
totalUsers.ShouldBe(beforeCheckpointTotalUsers);
} // Dispose → Commit → ForceCheckpoint → Write to PageFile
// Should be able to open again and see persisted data
using var db2 = new Shared.TestDbContext(_dbPath);
using var db2 = new TestDbContext(_dbPath);
totalUsers.ShouldBe(1);
db2.Users.FindAll().Count().ShouldBe(totalUsers);
db2.Users.Count().ShouldBe(totalUsers);
}
private static string ComputeFileHash(string path)
{
using var stream = File.OpenRead(path);
@@ -103,29 +118,31 @@ public class DbContextTests : IDisposable
}
/// <summary>
/// Verifies database file size and content change after insert and checkpoint.
/// Verifies database file size and content change after insert and checkpoint.
/// </summary>
[Fact]
public void DatabaseFile_SizeAndContent_ChangeAfterInsert()
{
var dbPath = Path.Combine(Path.GetTempPath(), $"test_dbfile_{Guid.NewGuid()}.db");
string dbPath = Path.Combine(Path.GetTempPath(), $"test_dbfile_{Guid.NewGuid()}.db");
// 1. Crea e chiudi database vuoto
using (var db = new Shared.TestDbContext(dbPath))
using (var db = new TestDbContext(dbPath))
{
db.Users.Insert(new User { Name = "Pippo", Age = 42 });
}
var initialSize = new FileInfo(dbPath).Length;
var initialHash = ComputeFileHash(dbPath);
long initialSize = new FileInfo(dbPath).Length;
string initialHash = ComputeFileHash(dbPath);
// 2. Riapri, inserisci, chiudi
using (var db = new Shared.TestDbContext(dbPath))
using (var db = new TestDbContext(dbPath))
{
db.Users.Insert(new User { Name = "Test", Age = 42 });
db.ForceCheckpoint(); // Forza persistenza
}
var afterInsertSize = new FileInfo(dbPath).Length;
var afterInsertHash = ComputeFileHash(dbPath);
long afterInsertSize = new FileInfo(dbPath).Length;
string afterInsertHash = ComputeFileHash(dbPath);
// 3. Verifica che dimensione e hash siano cambiati
afterInsertSize.ShouldNotBe(initialSize);
@@ -133,25 +150,25 @@ public class DbContextTests : IDisposable
}
/// <summary>
/// Verifies the WAL file path is auto-derived from database path.
/// Verifies the WAL file path is auto-derived from database path.
/// </summary>
[Fact]
public void DbContext_AutoDerivesWalPath()
{
using var db = new Shared.TestDbContext(_dbPath);
using var db = new TestDbContext(_dbPath);
db.Users.Insert(new User { Name = "Test", Age = 20 });
var walPath = Path.ChangeExtension(_dbPath, ".wal");
string walPath = Path.ChangeExtension(_dbPath, ".wal");
File.Exists(walPath).ShouldBeTrue();
}
/// <summary>
/// Verifies custom page file and compression options support roundtrip data access.
/// Verifies custom page file and compression options support roundtrip data access.
/// </summary>
[Fact]
public void DbContext_WithCustomPageFileAndCompressionOptions_ShouldSupportRoundTrip()
{
var dbPath = Path.Combine(Path.GetTempPath(), $"test_dbcontext_compression_{Guid.NewGuid():N}.db");
string dbPath = Path.Combine(Path.GetTempPath(), $"test_dbcontext_compression_{Guid.NewGuid():N}.db");
var options = new CompressionOptions
{
EnableCompression = true,
@@ -170,8 +187,8 @@ public class DbContextTests : IDisposable
try
{
using var db = new Shared.TestDbContext(dbPath, config, options);
var payload = string.Concat(Enumerable.Repeat("compressible-", 3000));
using var db = new TestDbContext(dbPath, config, options);
string payload = string.Concat(Enumerable.Repeat("compressible-", 3000));
var id = db.Users.Insert(new User { Name = payload, Age = 77 });
db.SaveChanges();
@@ -187,19 +204,16 @@ public class DbContextTests : IDisposable
}
/// <summary>
/// Verifies compact API returns stats and preserves data consistency.
/// Verifies compact API returns stats and preserves data consistency.
/// </summary>
[Fact]
public void DbContext_CompactApi_ShouldReturnStatsAndPreserveData()
{
var dbPath = Path.Combine(Path.GetTempPath(), $"test_dbcontext_compact_{Guid.NewGuid():N}.db");
string dbPath = Path.Combine(Path.GetTempPath(), $"test_dbcontext_compact_{Guid.NewGuid():N}.db");
try
{
using var db = new Shared.TestDbContext(dbPath);
for (var i = 0; i < 120; i++)
{
db.Users.Insert(new User { Name = $"compact-{i:D3}", Age = i % 20 });
}
using var db = new TestDbContext(dbPath);
for (var i = 0; i < 120; i++) db.Users.Insert(new User { Name = $"compact-{i:D3}", Age = i % 20 });
db.SaveChanges();
db.Users.Count().ShouldBe(120);
@@ -221,29 +235,14 @@ public class DbContextTests : IDisposable
}
}
/// <summary>
/// Disposes test resources and cleans up generated files.
/// </summary>
public void Dispose()
{
try
{
CleanupDbFiles(_dbPath);
}
catch
{
// Ignore cleanup errors
}
}
private static void CleanupDbFiles(string dbPath)
{
if (File.Exists(dbPath)) File.Delete(dbPath);
var walPath = Path.ChangeExtension(dbPath, ".wal");
string walPath = Path.ChangeExtension(dbPath, ".wal");
if (File.Exists(walPath)) File.Delete(walPath);
var markerPath = $"{dbPath}.compact.state";
if (File.Exists(markerPath)) File.Delete(markerPath);
}
}
}