Files
Khaemhat/tests/Khaemhat.Infrastructure.Tests/Files/CsvReaderAndEcbTests.cs
T
Alby96andClaude Opus 5.5 34e196424f feat: company tables, profile, fiscal years and base data (#38, #39, #45)
Adds the company tables of SPEC 11.1 behind one generic service so that every
table gets the same rules, history, cache and CSV import/export, plus the
company profile, fiscal years with monthly lock and the base-data seeder.

- 26 coded tables, exchange rates, company profile, fiscal years and periods:
  entities, maps and migrations 010-014
- TableService + TableCatalog: unique codes, per-table rules (VAT codes are
  checked by the fiscal pack), delete or disable when in use, single default,
  CSV export/import all-or-nothing, cache with cross-instance invalidation
- CompanyProfileService (Italian identifiers through the pack),
  FiscalYearService with the date check used by every dated record
- EcbRatesClient and the ECB rates system task; CsvReader
- ISeeder/SeedRunner with the data version in company settings; base tables
  from the pack (IBaseTablesTemplate) and an embedded ISO country list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 22:33:37 +01:00

88 lines
3.1 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text;
using Khaemhat.Infrastructure.Files;
using Khaemhat.Infrastructure.Http;
namespace Khaemhat.Infrastructure.Tests.Files;
[TestClass]
public sealed class CsvReaderTests
{
[TestMethod]
public void Parse_ReadsQuotesNewLinesAndTheByteOrderMark()
{
var rows = CsvReader.Parse("code;description\r\nA;\"Uno; due\"\r\nB;\"riga\ncon \"\"virgolette\"\"\"\r\n\r\nC;\n");
Assert.HasCount(4, rows);
CollectionAssert.AreEqual(new[] { "code", "description" }, rows[0].ToArray());
CollectionAssert.AreEqual(new[] { "A", "Uno; due" }, rows[1].ToArray());
CollectionAssert.AreEqual(new[] { "B", "riga\ncon \"virgolette\"" }, rows[2].ToArray());
CollectionAssert.AreEqual(new[] { "C", string.Empty }, rows[3].ToArray());
}
[TestMethod]
public void Parse_GuessesTheSeparator()
{
Assert.AreEqual("2", CsvReader.Parse("a,b\n1,2")[1][1]);
Assert.AreEqual("2", CsvReader.Parse("a\tb\n1\t2")[1][1]);
Assert.AreEqual("1,5", CsvReader.Parse("a;b\n1,5;2")[1][0]);
}
[TestMethod]
public async Task WriterOutput_IsReadBack()
{
using var buffer = new MemoryStream();
await using (var writer = new CsvWriter(buffer, CsvOptions.Italian, leaveOpen: true))
{
writer.WriteRow("code", "note");
writer.WriteRow("X1", "a;b \"c\"");
}
buffer.Position = 0;
var rows = await CsvReader.ReadAsync(buffer);
CollectionAssert.AreEqual(new[] { "X1", "a;b \"c\"" }, rows[1].ToArray());
}
}
[TestClass]
public sealed class EcbRatesClientTests
{
private const string Daily = """
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<Cube>
<Cube time="2026-11-10">
<Cube currency="USD" rate="1.1563"/>
<Cube currency="JPY" rate="177.12"/>
</Cube>
<Cube time="2026-11-09">
<Cube currency="USD" rate="1.1501"/>
</Cube>
</Cube>
</gesmes:Envelope>
""";
[TestMethod]
public async Task Parse_ReadsEveryDayAndCurrency()
{
using var xml = new MemoryStream(Encoding.UTF8.GetBytes(Daily));
var rates = await EcbRatesClient.ParseAsync(xml);
Assert.HasCount(3, rates);
Assert.AreEqual(new EcbRate("USD", new DateOnly(2026, 11, 10), 1.1563m), rates[0]);
Assert.AreEqual(177.12m, rates.Single(r => r.Currency == "JPY").Rate);
}
[TestMethod]
public async Task Parse_RefusesAnswersThatAreNotRates()
{
using var html = new MemoryStream(Encoding.UTF8.GetBytes("<html><body>Maintenance</body></html>"));
using var broken = new MemoryStream(Encoding.UTF8.GetBytes("not xml"));
await Assert.ThrowsExactlyAsync<FormatException>(() => EcbRatesClient.ParseAsync(html));
await Assert.ThrowsExactlyAsync<FormatException>(() => EcbRatesClient.ParseAsync(broken));
}
}