Implement backup and restore functionality; add IBackupService and BackupService; refactor services to use DbContextFactory
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,15 +17,17 @@ public class BalanceCalculationTests : IDisposable
|
||||
public BalanceCalculationTests()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<FinanceDbContext>()
|
||||
.UseSqlite("Data Source=:memory:")
|
||||
.UseSqlite("Data Source=duempelkas-balance-tests;Mode=Memory;Cache=Shared")
|
||||
.Options;
|
||||
|
||||
_db = new FinanceDbContext(options);
|
||||
_db.Database.OpenConnection();
|
||||
_db.Database.EnsureCreated();
|
||||
|
||||
_balanceQueryService = new BalanceQueryService(_db);
|
||||
_entryService = new EntryService(_db);
|
||||
var dbFactory = new TestDbContextFactory(options);
|
||||
|
||||
_balanceQueryService = new BalanceQueryService(dbFactory);
|
||||
_entryService = new EntryService(dbFactory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -106,4 +108,19 @@ public class BalanceCalculationTests : 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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user