Refactor EntryService to allow shared DisplayId for transfers; update related tests and migration files

This commit is contained in:
2026-04-03 12:00:53 +02:00
parent 387c18e834
commit 69181e66b0
8 changed files with 365 additions and 14 deletions

View File

@@ -0,0 +1,123 @@
using Duempelkas.Domain.Entities;
using Duempelkas.Domain.Enums;
using Duempelkas.Infrastructure.Persistence;
using Duempelkas.Infrastructure.Services;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Duempelkas.Tests;
public class EntryServiceBookingTests : IDisposable
{
private readonly FinanceDbContext _db;
private readonly EntryService _entryService;
private readonly string _connectionString = $"Data Source=duempelkas-entry-tests-{Guid.NewGuid():N};Mode=Memory;Cache=Shared";
public EntryServiceBookingTests()
{
var options = new DbContextOptionsBuilder<FinanceDbContext>()
.UseSqlite(_connectionString)
.Options;
_db = new FinanceDbContext(options);
_db.Database.OpenConnection();
_db.Database.EnsureCreated();
var dbFactory = new TestDbContextFactory(options);
_entryService = new EntryService(dbFactory);
}
[Fact]
public async Task CreateEntry_AssignsSequentialDisplayIds_PerYear()
{
var accountA = new Account { Name = "Konto A" };
var accountB = new Account { Name = "Konto B" };
_db.Accounts.AddRange(accountA, accountB);
await _db.SaveChangesAsync();
var first = await _entryService.CreateEntryAsync(accountA.Id, EntryType.Income, new DateTime(2026, 1, 10), "Einnahme A", 100m);
var second = await _entryService.CreateEntryAsync(accountB.Id, EntryType.Expense, new DateTime(2026, 1, 11), "Ausgabe B", 20m);
var third = await _entryService.CreateEntryAsync(accountA.Id, EntryType.Income, new DateTime(2027, 1, 1), "Neues Jahr", 5m);
first.DisplayId.Should().Be("2026-001");
second.DisplayId.Should().Be("2026-002");
third.DisplayId.Should().Be("2027-001");
}
[Fact]
public async Task CreateEntry_AfterTransfer_UsesNextDisplayId()
{
var source = new Account { Name = "Barkasse" };
var target = new Account { Name = "Girokonto" };
_db.Accounts.AddRange(source, target);
await _db.SaveChangesAsync();
await _entryService.CreateTransferAsync(source.Id, target.Id, new DateTime(2026, 3, 15), "Umbuchung", 500m);
var booking = await _entryService.CreateEntryAsync(source.Id, EntryType.Income, new DateTime(2026, 3, 16), "Einzahlung", 50m);
booking.DisplayId.Should().Be("2026-002");
}
[Fact]
public async Task UpdateEntry_UpdatesBookingFields()
{
var account = new Account { Name = "Konto" };
_db.Accounts.Add(account);
await _db.SaveChangesAsync();
var entry = await _entryService.CreateEntryAsync(account.Id, EntryType.Expense, new DateTime(2026, 2, 2), "Alt", 10m);
await _entryService.UpdateEntryAsync(entry.Id, new DateTime(2026, 2, 3), "Neu", 25m);
_db.ChangeTracker.Clear();
var updated = await _db.Entries.SingleAsync(e => e.Id == entry.Id);
updated.Date.Should().Be(new DateTime(2026, 2, 3));
updated.Title.Should().Be("Neu");
updated.Amount.Should().Be(25m);
}
[Fact]
public async Task DeleteAndRestoreEntry_TogglesSoftDelete_ForBookingOnly()
{
var account = new Account { Name = "Konto" };
_db.Accounts.Add(account);
await _db.SaveChangesAsync();
var entry = await _entryService.CreateEntryAsync(account.Id, EntryType.Expense, new DateTime(2026, 2, 2), "Buchung", 10m);
await _entryService.DeleteEntryAsync(entry.Id);
_db.ChangeTracker.Clear();
var deleted = await _db.Entries.SingleAsync(e => e.Id == entry.Id);
deleted.IsDeleted.Should().BeTrue();
await _entryService.RestoreEntryAsync(entry.Id);
_db.ChangeTracker.Clear();
var restored = await _db.Entries.SingleAsync(e => e.Id == entry.Id);
restored.IsDeleted.Should().BeFalse();
}
public void Dispose()
{
_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));
}
}

View File

@@ -13,11 +13,12 @@ public class TransferServiceTests : IDisposable
private readonly FinanceDbContext _db;
private readonly EntryService _entryService;
private readonly BalanceQueryService _balanceQueryService;
private readonly string _connectionString = $"Data Source=duempelkas-transfer-tests-{Guid.NewGuid():N};Mode=Memory;Cache=Shared";
public TransferServiceTests()
{
var options = new DbContextOptionsBuilder<FinanceDbContext>()
.UseSqlite("Data Source=duempelkas-transfer-tests;Mode=Memory;Cache=Shared")
.UseSqlite(_connectionString)
.Options;
_db = new FinanceDbContext(options);
@@ -52,7 +53,8 @@ public class TransferServiceTests : IDisposable
var targetEntry = entries.Single(e => e.AccountId == accountB.Id);
targetEntry.Type.Should().Be(EntryType.Income);
targetEntry.Amount.Should().Be(100.00m);
targetEntry.DisplayId.Should().Be("2026-002");
targetEntry.DisplayId.Should().Be("2026-001");
targetEntry.DisplayId.Should().Be(sourceEntry.DisplayId);
var links = await _db.TransferLinks.ToListAsync();
links.Should().HaveCount(1);
@@ -105,6 +107,29 @@ public class TransferServiceTests : IDisposable
await act.Should().ThrowAsync<InvalidOperationException>();
}
[Fact]
public async Task UpdateTransfer_ChangeLinkedAccount_KeepsSharedDisplayId()
{
var source = new Account { Name = "Barkasse" };
var initialTarget = new Account { Name = "Girokonto" };
var newTarget = new Account { Name = "Sparkonto" };
_db.Accounts.AddRange(source, initialTarget, newTarget);
await _db.SaveChangesAsync();
await _entryService.CreateTransferAsync(source.Id, initialTarget.Id, new DateTime(2026, 3, 15), "Umbuchung", 500.00m);
var sourceEntry = await _db.Entries.SingleAsync(e => e.AccountId == source.Id);
await _entryService.UpdateTransferAsync(sourceEntry.Id, newTarget.Id, new DateTime(2026, 3, 16), "Umbuchung angepasst", 550.00m);
_db.ChangeTracker.Clear();
var updatedSourceEntry = await _db.Entries.SingleAsync(e => e.AccountId == source.Id);
var updatedTargetEntry = await _db.Entries.SingleAsync(e => e.AccountId == newTarget.Id);
updatedSourceEntry.DisplayId.Should().Be("2026-001");
updatedTargetEntry.DisplayId.Should().Be(updatedSourceEntry.DisplayId);
}
public void Dispose()
{
_db.Database.CloseConnection();

View File

@@ -13,11 +13,12 @@ public class BalanceCalculationTests : IDisposable
private readonly FinanceDbContext _db;
private readonly BalanceQueryService _balanceQueryService;
private readonly EntryService _entryService;
private readonly string _connectionString = $"Data Source=duempelkas-balance-tests-{Guid.NewGuid():N};Mode=Memory;Cache=Shared";
public BalanceCalculationTests()
{
var options = new DbContextOptionsBuilder<FinanceDbContext>()
.UseSqlite("Data Source=duempelkas-balance-tests;Mode=Memory;Cache=Shared")
.UseSqlite(_connectionString)
.Options;
_db = new FinanceDbContext(options);