using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit; using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase; using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Services; namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests; /// /// A real over an in-memory SQLite configuration database, for the /// code review 2026-12-02 DEP-1 / MGT-1 / TPL-3 regression tests. The pre-fix unit tests /// mocked IAuditService and only asserted the call, which passed while the staged row /// was never saved; these tests read the row back through a SECOND context on the same /// connection, so only a row that was actually committed counts. /// internal sealed class RealAuditDatabase : IDisposable { private readonly SqliteConnection _connection; private readonly DbContextOptions _options; /// Opens the database and builds the audit service over a fresh context. public RealAuditDatabase() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); _options = new DbContextOptionsBuilder().UseSqlite(_connection).Options; Context = new ScadaBridgeDbContext(_options); Context.Database.EnsureCreated(); AuditService = new AuditService(Context, new AuditCorrelationContext()); } /// The unit-of-work context the audit service writes through. public ScadaBridgeDbContext Context { get; } /// The real audit service under test. public AuditService AuditService { get; } /// Reads the committed audit rows through an independent context. /// Every persisted audit row. public async Task> PersistedRowsAsync() { await using var reader = new ScadaBridgeDbContext(_options); return await reader.AuditLogEntries.AsNoTracking().ToListAsync(); } /// public void Dispose() { Context.Dispose(); _connection.Dispose(); } }