Files
Khaemhat/tests/Khaemhat.Core.Tests/Common/CodeMaskTests.cs
T
Alby96andClaude Opus 5.5 07555de700 feat: automatic codes and automatic filling (#43)
Parties, items and the CRM archives need codes given by the system without
duplicates, and the address and bank fields should fill themselves; this adds
both before the party and item screens that use them.

- CodeMask ({seq:0000}, {anno}, {categoria}) and code_sequences (migration
  015): one sequence per rule, year and category, taken under a lock in the
  transaction that saves the record, skipping codes written by hand
- CodeRuleService: rules in company settings with the masks of SPEC 11.12,
  validation, preview of the next code, manual code allowed or not
- AutoFillService: municipality from postal code and back, personal tax code,
  VIES, bank and BIC from the IBAN, delivery date on working days, options
  that can be switched off (the ECB job honours its option)
- IBankDirectory.ReadIban gives ABI and CAB even without the ABI list; the
  Italian pack now registers the directory
- Company preferences page (mockup 074) at /tabelle/preferenze, in the menu
- aria-checked now renders true/false on switch rows and profile radios
- ADR 0027, wiki, CHANGELOG, STATUS, DATA-MODEL

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

70 lines
3.1 KiB
C#

using Khaemhat.Core.Tables;
namespace Khaemhat.Core.Tests.Common;
/// <summary>The masks of the automatic codes (SPEC §11.12, issue #43).</summary>
[TestClass]
public sealed class CodeMaskTests
{
[TestMethod]
[DataRow("C{seq:000000}", 124L, null, "C000124")]
[DataRow("F{seq:000000}", 32L, null, "F000032")]
[DataRow("L{anno}-{seq:0000}", 1L, null, "L2026-0001")]
[DataRow("{categoria}-{seq:0000}", 13L, "valv", "VALV-0013")]
[DataRow("OPP-{year}-{seq:0000}", 7L, null, "OPP-2026-0007")]
[DataRow("cl/{seq}", 1234567L, null, "CL/1234567")]
[DataRow("A{seq:00}", 123L, null, "A123")]
public void Format_WritesTheCode(string text, long number, string? category, string expected)
{
Assert.AreEqual(expected, CodeMask.Parse(text).Format(number, 2026, category));
}
[TestMethod]
public void Parse_NormalizesTheMask_AndReadsWhatTheNumberDependsOn()
{
var mask = CodeMask.Parse(" l{ANNO}-{Seq:0000} ");
Assert.AreEqual("L{anno}-{seq:0000}", mask.Text);
Assert.AreEqual(4, mask.Digits);
Assert.IsTrue(mask.UsesYear);
Assert.IsFalse(mask.UsesCategory);
Assert.AreEqual(10, mask.FixedLength);
Assert.AreEqual("{categoria}-{seq:0000}", CodeMask.Parse("{category}-{seq:0000}").Text);
}
[TestMethod]
[DataRow("C000001", CodeMask.SequenceRequired)]
[DataRow("C{seq:0000}{seq:00}", CodeMask.SequenceRequired)]
[DataRow("C{seq:0a00}", CodeMask.UnknownPlaceholder)]
[DataRow("C{mese}{seq:0000}", CodeMask.UnknownPlaceholder)]
[DataRow("C{seq:0000", CodeMask.UnknownPlaceholder)]
[DataRow("C {seq:0000}", CodeMask.InvalidCharacter)]
[DataRow("È{seq:0000}", CodeMask.InvalidCharacter)]
[DataRow("", CodeMask.SequenceRequired)]
public void TryParse_RefusesWrongMasks(string text, string error)
{
Assert.IsFalse(CodeMask.TryParse(text, out var mask, out var found));
Assert.IsNull(mask);
Assert.AreEqual(error, found);
}
[TestMethod]
public void Check_LooksAtLengthAndCategories()
{
Assert.IsNull(CodeMask.Check("C{seq:000000}", 20, categoriesAllowed: false));
Assert.AreEqual(CodeMask.TooLong, CodeMask.Check("CLIENTE-{anno}-{seq:00000000}", 20, categoriesAllowed: false));
Assert.AreEqual(CodeMask.CategoryNotAllowed, CodeMask.Check("{categoria}{seq:0000}", 20, categoriesAllowed: false));
Assert.IsNull(CodeMask.Check("{categoria}-{seq:0000}", 30, categoriesAllowed: true));
Assert.AreEqual(CodeMask.UnknownPlaceholder, CodeMask.Check("{x}", 20, categoriesAllowed: true));
}
[TestMethod]
public void SequenceKey_SeparatesYearsAndCategories()
{
Assert.AreEqual("customers", CodeMask.Parse("C{seq:000000}").SequenceKey("customers", 2026, "IGNORED"));
Assert.AreEqual("leads:2026", CodeMask.Parse("L{anno}-{seq:0000}").SequenceKey("leads", 2026, null));
Assert.AreEqual("items:VALV", CodeMask.Parse("{categoria}-{seq:0000}").SequenceKey("items", 2026, " valv"));
Assert.AreEqual("items:2027:PUMP", CodeMask.Parse("{categoria}{anno}{seq:000}").SequenceKey("items", 2027, "pump"));
}
}