Implement backup and restore functionality; add IBackupService and BackupService; refactor services to use DbContextFactory

This commit is contained in:
2026-04-03 10:53:53 +02:00
parent 0923c037eb
commit 387c18e834
15 changed files with 411 additions and 71 deletions

View File

@@ -17,15 +17,17 @@ public class TransferServiceTests : IDisposable
public TransferServiceTests()
{
var options = new DbContextOptionsBuilder<FinanceDbContext>()
.UseSqlite("Data Source=:memory:")
.UseSqlite("Data Source=duempelkas-transfer-tests;Mode=Memory;Cache=Shared")
.Options;
_db = new FinanceDbContext(options);
_db.Database.OpenConnection();
_db.Database.EnsureCreated();
_entryService = new EntryService(_db);
_balanceQueryService = new BalanceQueryService(_db);
var dbFactory = new TestDbContextFactory(options);
_entryService = new EntryService(dbFactory);
_balanceQueryService = new BalanceQueryService(dbFactory);
}
[Fact]
@@ -77,6 +79,7 @@ public class TransferServiceTests : IDisposable
await _entryService.DeleteEntryAsync(sourceEntry.Id);
_db.ChangeTracker.Clear();
var entries = await _db.Entries.ToListAsync();
entries.Should().HaveCount(2);
entries.Should().AllSatisfy(e => e.IsDeleted.Should().BeTrue());
@@ -107,4 +110,19 @@ public class TransferServiceTests : IDisposable
_db.Database.CloseConnection();
_db.Dispose();
}
private sealed class TestDbContextFactory : IDbContextFactory<FinanceDbContext>
{
private readonly DbContextOptions<FinanceDbContext> _options;
public TestDbContextFactory(DbContextOptions<FinanceDbContext> options)
{
_options = options;
}
public FinanceDbContext CreateDbContext() => new(_options);
public Task<FinanceDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default)
=> Task.FromResult(new FinanceDbContext(_options));
}
}