NeoVac only reports per-mille shares of the property; the own costs in CHF come from the property manager's statement. The new page stores both in water-heating.json (per mandator) and calculates the CHF parts: water base and consumption costs by their per-mille share, consumption split by m³ into warm and cold water, and the warm water part of the heating costs (the rest is room heating). Charts for water (m³), heat (MWh) and the stacked costs; the dialog has a guide image showing where each value is on the bills. The Wasser page stays for bills with the CHF values entered directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
293 lines
11 KiB
C#
293 lines
11 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using Schaad.Accounting.Datasets;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Repositories;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class RawDataRepositoryTestShould : IDisposable
|
|
{
|
|
private const string Accounts = """
|
|
[
|
|
{ "Id": "a1", "Number": 1020, "Name": "Bank", "Currency": "CHF" },
|
|
{ "Id": "a2", "Number": 4000, "Name": "Lebensmittel", "Currency": "CHF" }
|
|
]
|
|
""";
|
|
|
|
private const string Transactions = """
|
|
[
|
|
{ "Id": "t1", "ValueDate": "2026-01-10T00:00:00", "Text": "Migros", "Value": 12.5, "OriginAccountId": "a1", "TargetAccountId": "a2" }
|
|
]
|
|
""";
|
|
|
|
private readonly string dataPath;
|
|
private readonly string dbDir;
|
|
private readonly string mandatorDir;
|
|
private readonly RepositoryCache cache = new();
|
|
private readonly ISettingsService settingsService;
|
|
private readonly RawDataRepository sut;
|
|
|
|
public RawDataRepositoryTestShould()
|
|
{
|
|
dataPath = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N"));
|
|
mandatorDir = Path.Combine(dataPath, "Mandator");
|
|
dbDir = Path.Combine(mandatorDir, "2026");
|
|
Directory.CreateDirectory(dbDir);
|
|
File.WriteAllText(Path.Combine(dbDir, "Accounts.json"), Accounts);
|
|
File.WriteAllText(Path.Combine(dbDir, "Transactions.json"), Transactions);
|
|
|
|
settingsService = Substitute.For<ISettingsService>();
|
|
settingsService.GetDbPath().Returns(dbDir);
|
|
settingsService.GetLastYearDbPath().Returns(Path.Combine(dataPath, "no-such-dir"));
|
|
settingsService.GetMandatorPath().Returns(mandatorDir);
|
|
settingsService.GetSettings().Returns(new SettingsDataset { DataPath = dataPath });
|
|
settingsService.GetMandator().Returns("Mandator");
|
|
settingsService.GetYear().Returns(2026);
|
|
|
|
sut = new RawDataRepository(settingsService, cache, NullLogger<RawDataRepository>.Instance);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(dataPath))
|
|
{
|
|
Directory.Delete(dataPath, recursive: true);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReturnFileTextWhenLoading()
|
|
{
|
|
var file = await sut.LoadAsync("Transactions.json");
|
|
|
|
file.Text.ShouldBe(Transactions);
|
|
file.Hash.ShouldNotBeNullOrEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAndBackUpPreviousVersionWhenTextIsValid()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
var edited = loaded.Text.Replace("\"Migros\"", "\"Migros Bern\"");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", edited, loaded.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
result.Saved.ShouldBeTrue();
|
|
result.File!.Text.ShouldContain("\"Text\": \"Migros Bern\"");
|
|
result.File.Hash.ShouldNotBe(loaded.Hash);
|
|
File.ReadAllText(result.BackupPath!).ShouldBe(Transactions);
|
|
result.BackupPath!.ShouldStartWith(Path.Combine(dataPath, "backup", "Mandator", "2026", "json-"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefreshCachedDataWhenSaving()
|
|
{
|
|
var transactionRepository = new TransactionRepository(settingsService, cache, Substitute.For<IAccountRepository>(), NullLogger<TransactionRepository>.Instance);
|
|
(await transactionRepository.GetTransactionListAsync()).Single().Text.ShouldBe("Migros");
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
await sut.SaveAsync("Transactions.json", loaded.Text.Replace("\"Migros\"", "\"Coop\""), loaded.Hash);
|
|
|
|
(await transactionRepository.GetTransactionListAsync()).Single().Text.ShouldBe("Coop");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectInvalidJsonWithLineAndColumnWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", "[\n { \"Id\": \"t1\",, }\n]", loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldStartWith("Zeile 2, Spalte");
|
|
File.ReadAllText(Path.Combine(dbDir, "Transactions.json")).ShouldBe(Transactions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectUnknownPropertyWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", loaded.Text.Replace("\"Value\"", "\"Vlaue\""), loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("Vlaue");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectUnknownAccountWhenSavingTransactions()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", loaded.Text.Replace("\"a2\"", "\"a9\""), loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("TargetAccountId \"a9\"");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectMissingAndDuplicateIdsWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Accounts.json");
|
|
var text = """
|
|
[
|
|
{ "Id": "a1", "Number": 1020, "Name": "Bank" },
|
|
{ "Id": "a1", "Number": 4000, "Name": "Lebensmittel" },
|
|
{ "Number": 4100, "Name": "Miete" }
|
|
]
|
|
""";
|
|
|
|
var result = await sut.SaveAsync("Accounts.json", text, loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.ShouldContain(e => e.Contains("Eintrag 3"));
|
|
result.Errors.ShouldContain(e => e.Contains("\"a1\" kommt 2-mal vor"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectRemovingAccountStillUsedByTransactionsWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Accounts.json");
|
|
var text = """[ { "Id": "a1", "Number": 1020, "Name": "Bank" } ]""";
|
|
|
|
var result = await sut.SaveAsync("Accounts.json", text, loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("\"a2\" fehlt, wird aber von 1 Buchungen");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseToOverwriteWhenFileChangedSinceLoading()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
File.WriteAllText(Path.Combine(dbDir, "Transactions.json"), Transactions.Replace("12.5", "13"));
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", loaded.Text, loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("seit dem Laden geändert");
|
|
File.ReadAllText(Path.Combine(dbDir, "Transactions.json")).ShouldContain("13");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReadAndWriteElectricityFileInMandatorFolderNotYearFolderWhenEditing()
|
|
{
|
|
File.WriteAllText(Path.Combine(mandatorDir, "electricity.json"), """
|
|
[ { "Id": "e1", "PeriodFrom": "2023-07-01", "PeriodTo": "2024-06-30", "GridKwh": 100 } ]
|
|
""");
|
|
var loaded = await sut.LoadAsync("electricity.json");
|
|
loaded.Text.ShouldContain("\"GridKwh\": 100");
|
|
|
|
var result = await sut.SaveAsync("electricity.json", loaded.Text.Replace("100", "150"), loaded.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
result.Saved.ShouldBeTrue();
|
|
File.Exists(Path.Combine(mandatorDir, "electricity.json")).ShouldBeTrue();
|
|
File.ReadAllText(Path.Combine(mandatorDir, "electricity.json")).ShouldContain("150");
|
|
File.Exists(Path.Combine(dbDir, "electricity.json")).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReadAndWriteWaterFileInMandatorFolderNotYearFolderWhenEditing()
|
|
{
|
|
File.WriteAllText(Path.Combine(mandatorDir, "water.json"), """
|
|
[ { "Id": "w1", "PeriodFrom": "2023-07-01", "PeriodTo": "2024-06-30", "WarmWaterM3": 100 } ]
|
|
""");
|
|
var loaded = await sut.LoadAsync("water.json");
|
|
loaded.Text.ShouldContain("\"WarmWaterM3\": 100");
|
|
|
|
var result = await sut.SaveAsync("water.json", loaded.Text.Replace("100", "150"), loaded.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
result.Saved.ShouldBeTrue();
|
|
File.Exists(Path.Combine(mandatorDir, "water.json")).ShouldBeTrue();
|
|
File.ReadAllText(Path.Combine(mandatorDir, "water.json")).ShouldContain("150");
|
|
File.Exists(Path.Combine(dbDir, "water.json")).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReadAndWriteWaterHeatingFileInMandatorFolderNotYearFolderWhenEditing()
|
|
{
|
|
File.WriteAllText(Path.Combine(mandatorDir, "water-heating.json"), """
|
|
[ { "Id": "h1", "PeriodFrom": "2024-07-01", "PeriodTo": "2025-06-30", "HeatingCostsChf": 100 } ]
|
|
""");
|
|
var loaded = await sut.LoadAsync("water-heating.json");
|
|
|
|
var result = await sut.SaveAsync("water-heating.json", loaded.Text.Replace("100", "150"), loaded.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
result.Saved.ShouldBeTrue();
|
|
File.ReadAllText(Path.Combine(mandatorDir, "water-heating.json")).ShouldContain("150");
|
|
File.Exists(Path.Combine(dbDir, "water-heating.json")).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectUnknownFileNameWhenLoading()
|
|
{
|
|
await Should.ThrowAsync<ArgumentException>(() => sut.LoadAsync("../appsettings.json"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReturnParsedTransactionsWithHashWhenLoadingTransactions()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
snapshot.Transactions.Single().Text.ShouldBe("Migros");
|
|
snapshot.Hash.ShouldBe((await sut.LoadAsync("Transactions.json")).Hash);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChangeOnlyTheTextWhenSavingTransactionTexts()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["t1"] = "Luzern Migros" }, snapshot.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
var saved = (await sut.LoadTransactionsAsync()).Transactions.Single();
|
|
saved.Text.ShouldBe("Luzern Migros");
|
|
saved.Value.ShouldBe(12.5m);
|
|
saved.OriginAccountId.ShouldBe("a1");
|
|
saved.TargetAccountId.ShouldBe("a2");
|
|
saved.ValueDate.ShouldBe(new DateTime(2026, 1, 10));
|
|
File.ReadAllText(result.BackupPath!).ShouldBe(Transactions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseEmptyTextWhenSavingTransactionTexts()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["t1"] = " " }, snapshot.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("darf nicht leer sein");
|
|
File.ReadAllText(Path.Combine(dbDir, "Transactions.json")).ShouldBe(Transactions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseUnknownTransactionWhenSavingTransactionTexts()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["gone"] = "Text" }, snapshot.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("\"gone\" gibt es nicht mehr");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseWhenFileChangedSinceLoadingTransactions()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
File.WriteAllText(Path.Combine(dbDir, "Transactions.json"), Transactions.Replace("12.5", "13"));
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["t1"] = "Coop" }, snapshot.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("seit dem Laden geändert");
|
|
}
|
|
}
|