Property names now match the C# models (BankTransactionText instead of bankTransactionText). Reading stays case-insensitive. The migrator test now checks casing case-sensitively; Shouldly's ShouldContain ignores case by default, so it never actually checked it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
8.3 KiB
C#
209 lines
8.3 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Repositories;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class XmlToJsonMigratorTestShould : IDisposable
|
|
{
|
|
// Shaped like a file written by the old XmlSerializer-based repository, including the
|
|
// BOM-less declaration, namespaces and an xsi:nil FxRate.
|
|
private const string TransactionsXml = """
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<ArrayOfTransaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
|
<Transaction>
|
|
<Id>t1</Id>
|
|
<ValueDate>2026-01-30T00:00:00</ValueDate>
|
|
<BookingDate>2026-01-31T00:00:00</BookingDate>
|
|
<Text>Gebäudeversicherung</Text>
|
|
<Value>114.20</Value>
|
|
<OriginAccountId>a1</OriginAccountId>
|
|
<TargetAccountId>a2</TargetAccountId>
|
|
<FxRate xsi:nil="true" />
|
|
</Transaction>
|
|
<Transaction>
|
|
<Id>t2</Id>
|
|
<ValueDate>2026-02-01T00:00:00</ValueDate>
|
|
<BookingDate>2026-02-01T00:00:00</BookingDate>
|
|
<Text>EUR Zahlung</Text>
|
|
<Value>-50.5</Value>
|
|
<OriginAccountId>a2</OriginAccountId>
|
|
<TargetAccountId>a1</TargetAccountId>
|
|
<FxRate>0.9312</FxRate>
|
|
</Transaction>
|
|
</ArrayOfTransaction>
|
|
""";
|
|
|
|
private const string AccountsXml = """
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<ArrayOfAccount xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
|
<Account>
|
|
<Id>a1</Id>
|
|
<Number>1020</Number>
|
|
<Name>Bank</Name>
|
|
<Currency>CHF</Currency>
|
|
<StartBalance>1000.50</StartBalance>
|
|
<LastBankBalance>0</LastBankBalance>
|
|
</Account>
|
|
</ArrayOfAccount>
|
|
""";
|
|
|
|
private readonly string dataPath;
|
|
private readonly string dataDir;
|
|
private readonly XmlToJsonMigrator sut = new(NullLogger<XmlToJsonMigrator>.Instance);
|
|
|
|
public XmlToJsonMigratorTestShould()
|
|
{
|
|
dataPath = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N"));
|
|
dataDir = Path.Combine(dataPath, "2026", "Mandator", "Data");
|
|
Directory.CreateDirectory(dataDir);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(dataPath))
|
|
{
|
|
Directory.Delete(dataPath, recursive: true);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WriteJsonAndMoveXmlToBackupWhenDataFolderHasXml()
|
|
{
|
|
File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), TransactionsXml);
|
|
|
|
var converted = await sut.MigrateAsync(dataPath);
|
|
|
|
converted.ShouldBe(1);
|
|
File.Exists(Path.Combine(dataDir, "Transactions.json")).ShouldBeTrue();
|
|
File.Exists(Path.Combine(dataDir, "Transactions.xml")).ShouldBeFalse();
|
|
var backup = Directory.GetFiles(Path.Combine(dataPath, "2026", "backup", "Mandator"), "Transactions.xml", SearchOption.AllDirectories);
|
|
backup.Length.ShouldBe(1);
|
|
File.ReadAllText(backup[0]).ShouldBe(TransactionsXml);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WriteHumanReadableJsonWhenConvertingXml()
|
|
{
|
|
File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), TransactionsXml);
|
|
|
|
await sut.MigrateAsync(dataPath);
|
|
|
|
var json = File.ReadAllText(Path.Combine(dataDir, "Transactions.json"));
|
|
json.ShouldContain("\"Text\": \"Gebäudeversicherung\"", Case.Sensitive);
|
|
json.ShouldContain("\n \"Id\": \"t1\"", Case.Sensitive);
|
|
json.ShouldNotContain("FxRate\": null", Case.Insensitive);
|
|
json.ShouldNotContain("ReadOnly", Case.Insensitive);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PreserveAllValuesWhenReadingMigratedDataThroughRepository()
|
|
{
|
|
File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), TransactionsXml);
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml);
|
|
|
|
await sut.MigrateAsync(dataPath);
|
|
|
|
var settingsService = Substitute.For<ISettingsService>();
|
|
settingsService.GetDbPath().Returns(dataDir);
|
|
settingsService.GetLastYearDbPath().Returns(Path.Combine(dataPath, "no-such-dir"));
|
|
var accountRepository = new AccountRepository(settingsService, new RepositoryCache(), NullLogger<AccountRepository>.Instance);
|
|
var transactionRepository = new TransactionRepository(settingsService, new RepositoryCache(), accountRepository, NullLogger<TransactionRepository>.Instance);
|
|
var transactions = await transactionRepository.GetTransactionListAsync();
|
|
var accounts = await accountRepository.GetAccountListAsync();
|
|
|
|
transactions.Count.ShouldBe(2);
|
|
transactions[0].Text.ShouldBe("Gebäudeversicherung");
|
|
transactions[0].Value.ShouldBe(114.20m);
|
|
transactions[0].ValueDate.ShouldBe(new DateTime(2026, 1, 30));
|
|
transactions[0].BookingDate.ShouldBe(new DateTime(2026, 1, 31));
|
|
transactions[0].OriginAccountId.ShouldBe("a1");
|
|
transactions[0].TargetAccountId.ShouldBe("a2");
|
|
transactions[0].FxRate.ShouldBeNull();
|
|
transactions[1].Value.ShouldBe(-50.5m);
|
|
transactions[1].FxRate.ShouldBe(0.9312m);
|
|
accounts.Single().Number.ShouldBe(1020);
|
|
accounts.Single().StartBalance.ShouldBe(1000.50m);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LeaveUnknownXmlFilesUntouchedWhenMigrating()
|
|
{
|
|
var statement = Path.Combine(dataDir, "camt053.xml");
|
|
var upload = Path.Combine(dataPath, "2026", "Mandator", "uploads", "Accounts.xml");
|
|
Directory.CreateDirectory(Path.GetDirectoryName(upload)!);
|
|
File.WriteAllText(statement, "<Document />");
|
|
File.WriteAllText(upload, "<Document />");
|
|
|
|
var converted = await sut.MigrateAsync(dataPath);
|
|
|
|
converted.ShouldBe(0);
|
|
File.Exists(statement).ShouldBeTrue();
|
|
File.Exists(upload).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SkipBackupFolderWhenScanningMandators()
|
|
{
|
|
var backupData = Path.Combine(dataPath, "2026", "backup", "Data");
|
|
Directory.CreateDirectory(backupData);
|
|
File.WriteAllText(Path.Combine(backupData, "Accounts.xml"), AccountsXml);
|
|
|
|
var converted = await sut.MigrateAsync(dataPath);
|
|
|
|
converted.ShouldBe(0);
|
|
File.Exists(Path.Combine(backupData, "Accounts.xml")).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DoNothingWhenAllDataIsAlreadyJson()
|
|
{
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml);
|
|
await sut.MigrateAsync(dataPath);
|
|
|
|
var converted = await sut.MigrateAsync(dataPath);
|
|
|
|
converted.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FinishMigrationWhenJsonAlreadyMatchesXml()
|
|
{
|
|
// Simulates a crash after the JSON was written but before the XML was moved.
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml);
|
|
await sut.MigrateAsync(dataPath);
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml);
|
|
|
|
var converted = await sut.MigrateAsync(dataPath);
|
|
|
|
converted.ShouldBe(1);
|
|
File.Exists(Path.Combine(dataDir, "Accounts.xml")).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ThrowAndKeepBothFilesWhenJsonExistsWithDifferentContent()
|
|
{
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml);
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.json"), "[]");
|
|
|
|
await Should.ThrowAsync<AggregateException>(() => sut.MigrateAsync(dataPath));
|
|
|
|
File.Exists(Path.Combine(dataDir, "Accounts.xml")).ShouldBeTrue();
|
|
File.ReadAllText(Path.Combine(dataDir, "Accounts.json")).ShouldBe("[]");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConvertRemainingFilesAndThrowWhenOneXmlIsCorrupt()
|
|
{
|
|
File.WriteAllText(Path.Combine(dataDir, "Accounts.xml"), AccountsXml);
|
|
File.WriteAllText(Path.Combine(dataDir, "Transactions.xml"), "<ArrayOfTransaction><Transaction>");
|
|
|
|
await Should.ThrowAsync<AggregateException>(() => sut.MigrateAsync(dataPath));
|
|
|
|
File.Exists(Path.Combine(dataDir, "Accounts.json")).ShouldBeTrue();
|
|
File.Exists(Path.Combine(dataDir, "Transactions.xml")).ShouldBeTrue();
|
|
File.Exists(Path.Combine(dataDir, "Transactions.json")).ShouldBeFalse();
|
|
}
|
|
}
|