Files
Khaemhat/tests/Khaemhat.Infrastructure.Tests/Files/XlsxReaderTests.cs
T
Alby96andClaude Opus 5.5 caa33a2cee feat: guided import and export for parties, items, prices, opening stock and chart of accounts (#44)
Companies starting on Khaemhat need to bring their master data from another
system or a spreadsheet, and the chart of accounts has no Italian exchange
standard, so SPEC 12.12 asks for a guided wizard and an own layout.

- Administration > Import / export: CSV (separator and encoding guessed),
  XLSX (in-house reader, first sheet) and JSON, automatic column mapping with
  aliases, row-by-row check, error report, import confirmation and log entry.
- Rows go through PartyService, ItemService and PriceListService, so the
  import has exactly the rules, error codes and history of the forms; options
  cover existing and missing codes, VIES and city from postal code.
- Export writes the same columns as the import, plus empty templates.
- Opening stock and the chart of accounts (khaemhat-coa layout, natura, tipo,
  flag) are check-only until M6 and M7 add the ledgers to write into.
- PartyService.SaveAsync takes an explicit VIES switch for the import.
- ADR 0031, wiki Import-Export, CHANGELOG and STATUS updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 11:52:59 +01:00

65 lines
2.7 KiB
C#

using System.IO.Compression;
using System.Text;
using Khaemhat.Infrastructure.Files;
namespace Khaemhat.Infrastructure.Tests.Files;
/// <summary>The XLSX reader of the guided import (issue #44): what the writer produces comes back as text.</summary>
[TestClass]
public sealed class XlsxReaderTests
{
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public async Task ReadsBackWhatTheWriterWrites(bool sharedStrings)
{
using var buffer = new MemoryStream();
await using (var writer = await XlsxWriter.CreateAsync(buffer, new XlsxOptions { UseSharedStrings = sharedStrings }, leaveOpen: true))
{
await writer.StartSheetAsync("clienti", [new("codice"), new("prezzo"), new("dal"), new("attivo"), new("note")]);
await writer.WriteRowAsync(["C001", 1234.5m, new DateOnly(2026, 11, 12), true, null]);
await writer.WriteRowAsync([null, null, null, null, null]);
await writer.WriteRowAsync(["C002", -3m, null, false, "Via \"Roma\" & co. <1>"]);
await writer.CompleteAsync();
}
buffer.Position = 0;
var rows = await XlsxReader.ReadAsync(buffer);
Assert.HasCount(3, rows, "Header and two rows: the empty row is skipped.");
CollectionAssert.AreEqual(new[] { "codice", "prezzo", "dal", "attivo", "note" }, rows[0].ToArray());
Assert.AreEqual("C001", rows[1][0]);
Assert.AreEqual("1234.5", rows[1][1], "Numbers come in invariant notation.");
Assert.AreEqual("2026-11-12", rows[1][2], "Date-styled numbers come back as ISO dates.");
Assert.AreEqual("true", rows[1][3]);
Assert.AreEqual("-3", rows[2][1]);
Assert.AreEqual(string.Empty, rows[2][2], "A gap between cells is an empty cell.");
Assert.AreEqual("false", rows[2][3]);
Assert.AreEqual("Via \"Roma\" & co. <1>", rows[2][4]);
}
[TestMethod]
public async Task NotAWorkbook_IsRefused()
{
using var text = new MemoryStream(Encoding.UTF8.GetBytes("codice;descrizione\nA;B\n"));
await Assert.ThrowsExactlyAsync<XlsxReadException>(() => XlsxReader.ReadAsync(text));
using var zip = new MemoryStream();
using (var archive = new ZipArchive(zip, ZipArchiveMode.Create, leaveOpen: true))
{
archive.CreateEntry("readme.txt");
}
zip.Position = 0;
await Assert.ThrowsExactlyAsync<XlsxReadException>(() => XlsxReader.ReadAsync(zip));
}
[TestMethod]
[DataRow("A1", 0)]
[DataRow("C7", 2)]
[DataRow("Z10", 25)]
[DataRow("AA1", 26)]
[DataRow("AB12", 27)]
public void ColumnOf_ReadsTheLetters(string reference, int column) => Assert.AreEqual(column, XlsxReader.ColumnOf(reference));
}