Add Schaad.Accounting.Tests (xUnit + NSubstitute + Shouldly, net9.0) wired into Accounting.sln with a direct Reference to Schaad.Finance.Api.dll so it flows into the test binary. Naming convention: file/class is <Subject>TestShould, each test method is DoThisWhenThat. Reads as a sentence: "ViewService test should sum activa and passiva totals separately when getting balance view". All assertions use Shouldly (.ShouldBe, .ShouldBeNull, .ShouldContain, ...) rather than xUnit Assert.*. 38 tests across seven files: - ViewServiceTestShould: balance math (activa/passiva totals, per- account balance from start balance + credits - debits, FX conversion to CHF) and bank-transaction auto-matching (booking-rule text, value-matching preference, same-accounts-last-month fallback, open-transaction filter). - TransactionRepositoryTestShould: FX round-trip against a temp XML directory, unknown-id -> null, and the mutation-on-read regression from PR A (a second Get on the same FX transaction used to divide by FxRate again). - FormattingTestShould: Swiss thousands separator, two-decimal rounding, culture independence. - RepositoryCacheTestShould: loader called once, per-key isolation, invalidation forces reload, case-insensitive keys. - AccountRepositoryTestShould: id assignment, in-place update, currency defaulting, delete, bank-account lookup, bank-balance update, and constructor re-run against an existing file. - ChartServiceTestShould: honours settingsService.GetYear() without mutating it (locks in PR E), account vs sub-class grouping heuristic, skips empty accounts. - FileServiceTestShould: CSV header + running balance for debit and credit lines, ordering by BookingDate/ValueDate/Value, no in-place value mutation (locks in PR A). CLAUDE.md gains a `dotnet test` line. IMPROVEMENT_PLAN.md notes that this landed as three PRs (G, H, I) and was squashed on request. Also add `*.DotSettings.user` to .gitignore so Rider's per-user solution settings don't get accidentally staged. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
138 lines
4.6 KiB
C#
138 lines
4.6 KiB
C#
using NSubstitute;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Models;
|
|
using Schaad.Accounting.Repositories;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class TransactionRepositoryTestShould : IDisposable
|
|
{
|
|
private readonly string dbDir;
|
|
private readonly ISettingsService settingsService;
|
|
private readonly IAccountRepository accountRepo;
|
|
private readonly RepositoryCache cache;
|
|
private readonly TransactionRepository sut;
|
|
|
|
public TransactionRepositoryTestShould()
|
|
{
|
|
dbDir = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N"));
|
|
Directory.CreateDirectory(dbDir);
|
|
|
|
settingsService = Substitute.For<ISettingsService>();
|
|
settingsService.GetDbPath().Returns(dbDir);
|
|
settingsService.GetLastYearDbPath().Returns(Path.Combine(dbDir, "no-such-dir"));
|
|
|
|
accountRepo = Substitute.For<IAccountRepository>();
|
|
cache = new RepositoryCache();
|
|
sut = new TransactionRepository(settingsService, cache, accountRepo);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(dbDir))
|
|
{
|
|
Directory.Delete(dbDir, recursive: true);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void PreserveValueWhenRoundTrippingNonFxTransaction()
|
|
{
|
|
accountRepo.GetAccount("chk").Returns(new Account { Id = "chk", Currency = "CHF" });
|
|
accountRepo.GetAccount("groc").Returns(new Account { Id = "groc", Currency = "CHF" });
|
|
|
|
sut.SaveTransaction(new Transaction
|
|
{
|
|
OriginAccountId = "chk",
|
|
TargetAccountId = "groc",
|
|
Value = 42m,
|
|
Text = "Groceries",
|
|
ValueDate = new DateTime(2026, 5, 10),
|
|
BookingDate = new DateTime(2026, 5, 10)
|
|
});
|
|
|
|
var stored = sut.GetTransactionList().Single();
|
|
var loaded = sut.GetTransaction(stored.Id);
|
|
|
|
loaded.ShouldNotBeNull();
|
|
loaded.Value.ShouldBe(42m);
|
|
loaded.FxRate.ShouldBeNull(); // Non-FX transactions have FxRate cleared
|
|
}
|
|
|
|
[Fact]
|
|
public void StoreInChfAndReturnInAccountCurrencyWhenTransactionIsFx()
|
|
{
|
|
// User enters 100 USD at rate 0.9 CHF/USD -> stored as 90 CHF; read back as 100 USD.
|
|
accountRepo.GetAccount("chk").Returns(new Account { Id = "chk", Currency = "CHF" });
|
|
accountRepo.GetAccount("usd").Returns(new Account { Id = "usd", Currency = "USD" });
|
|
|
|
sut.SaveTransaction(new Transaction
|
|
{
|
|
OriginAccountId = "chk",
|
|
TargetAccountId = "usd",
|
|
Value = 100m,
|
|
FxRate = 0.9m,
|
|
Text = "USD transfer",
|
|
ValueDate = new DateTime(2026, 5, 10),
|
|
BookingDate = new DateTime(2026, 5, 10)
|
|
});
|
|
|
|
var stored = sut.GetTransactionList().Single();
|
|
stored.Value.ShouldBe(90m); // stored in CHF
|
|
|
|
var loaded = sut.GetTransaction(stored.Id);
|
|
loaded.ShouldNotBeNull();
|
|
loaded.Value.ShouldBe(100m); // display currency
|
|
loaded.FxRate.ShouldBe(0.9m);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturnNullWhenGettingUnknownTransactionId()
|
|
{
|
|
sut.GetTransaction("no-such-id").ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void NotMutateStoredValueWhenGettingFxTransactionTwice()
|
|
{
|
|
accountRepo.GetAccount("chk").Returns(new Account { Id = "chk", Currency = "CHF" });
|
|
accountRepo.GetAccount("usd").Returns(new Account { Id = "usd", Currency = "USD" });
|
|
|
|
sut.SaveTransaction(new Transaction
|
|
{
|
|
OriginAccountId = "chk",
|
|
TargetAccountId = "usd",
|
|
Value = 100m,
|
|
FxRate = 0.9m,
|
|
Text = "USD transfer",
|
|
ValueDate = new DateTime(2026, 5, 10),
|
|
BookingDate = new DateTime(2026, 5, 10)
|
|
});
|
|
|
|
var stored = sut.GetTransactionList().Single();
|
|
var id = stored.Id;
|
|
|
|
// Get twice; if Get mutated the stored entity, the second call would divide by FxRate again.
|
|
var first = sut.GetTransaction(id);
|
|
var second = sut.GetTransaction(id);
|
|
|
|
first.ShouldNotBeNull();
|
|
first.Value.ShouldBe(100m);
|
|
second.ShouldNotBeNull();
|
|
second.Value.ShouldBe(100m);
|
|
}
|
|
|
|
[Fact]
|
|
public void RemoveTransactionWhenDeletingById()
|
|
{
|
|
accountRepo.GetAccount(Arg.Any<string>()).Returns(new Account { Currency = "CHF" });
|
|
|
|
sut.SaveTransaction(new Transaction { OriginAccountId = "a", TargetAccountId = "b", Value = 1m, Text = "x", ValueDate = DateTime.Today });
|
|
var id = sut.GetTransactionList().Single().Id;
|
|
|
|
sut.DeleteTransaction(id);
|
|
|
|
sut.GetTransactionList().ShouldBeEmpty();
|
|
}
|
|
}
|