Files
Khaemhat/tests/Khaemhat.Fiscal.Italy.Tests/ItalianIdentifiersTests.cs
T
Alby96andClaude Opus 5.5 25f8a7ad30 feat: identifier validators and the VIES client (#40)
Parties need their identifiers checked before they reach documents. IBAN, EAN
and e-mail checks are international and live in Core. The Italian VAT number,
fiscal code (with calculator, decoder and omocodia) and SDI recipient code are
Italian, so they sit in the Italian pack behind ITaxIdentifierRules, as the
non-negotiable rule of SPEC 1.10 wins over the class list of SPEC 21 (ADR 0025).
IBankDirectory reads ABI and CAB from an Italian IBAN; its bank list arrives
with the base data.

ViesClient calls the VIES REST service and never reports a number as invalid
when the service could not answer.

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

141 lines
5.7 KiB
C#

using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Fiscal.Italy.Tests;
[TestClass]
public sealed class ItalianVatNumberValidatorTests
{
[TestMethod]
[DataRow("01234567897")]
[DataRow("IT 01234567897")]
[DataRow("it01234567897")]
public void ValidNumbers_PassWithOrWithoutPrefix(string number) => Assert.IsTrue(ItalianVatNumberValidator.IsValid(number));
[TestMethod]
[DataRow("01234567890")] // wrong check digit
[DataRow("1234567897")] // ten digits
[DataRow("00000000000")] // no taxpayer number
[DataRow("0123456789A")]
public void InvalidNumbers_AreRefused(string number) => Assert.IsFalse(ItalianVatNumberValidator.IsValid(number));
}
[TestClass]
public sealed class FiscalCodeTests
{
private static readonly DateOnly Today = new(2026, 11, 11);
[TestMethod]
public void Compute_GivesTheKnownCode()
{
var code = FiscalCodeCalculator.Compute(new PersonalTaxCodeData("Rossi", "Mario", new DateOnly(1980, 1, 1), PersonSex.Male, "H501"));
Assert.AreEqual("RSSMRA80A01H501U", code);
Assert.IsTrue(FiscalCodeValidator.IsValid(code));
}
[TestMethod]
public void Compute_FollowsTheRulesForNamesAndWomen()
{
var code = FiscalCodeCalculator.Compute(new PersonalTaxCodeData("De Luca", "Giovanna Maria", new DateOnly(1975, 12, 31), PersonSex.Female, "f205"));
// Surname: consonants D, L, C. Name with four consonants or more: 1st, 3rd and 4th (G, N, N).
StringAssert.StartsWith(code, "DLCGNN75T71F205");
Assert.IsTrue(FiscalCodeValidator.IsValidPersonal(code));
}
[TestMethod]
public void Compute_PadsShortNamesWithVowelsAndX()
{
var code = FiscalCodeCalculator.Compute(new PersonalTaxCodeData("Fo", "Ugo", new DateOnly(2001, 7, 9), PersonSex.Male, "L219"));
StringAssert.StartsWith(code, "FOXGUO01L09L219");
}
[TestMethod]
public void Compute_IgnoresAccentsAndApostrophes()
{
var plain = FiscalCodeCalculator.Compute(new PersonalTaxCodeData("DAMICO", "NICOLO", new DateOnly(1990, 5, 20), PersonSex.Male, "A944"));
var typed = FiscalCodeCalculator.Compute(new PersonalTaxCodeData("D'Amico", "Nicolò", new DateOnly(1990, 5, 20), PersonSex.Male, "A944"));
Assert.AreEqual(plain, typed);
}
[TestMethod]
public void Validator_AcceptsOmocodiaAndRefusesAWrongCheckCharacter()
{
// H501 becomes H50M (1 -> M) and the check character is computed again.
var first15 = "RSSMRA80A01H50M";
var omocodic = first15 + FiscalCodeValidator.CheckCharacter(first15);
Assert.IsTrue(FiscalCodeValidator.IsValidPersonal(omocodic));
Assert.IsFalse(FiscalCodeValidator.IsValidPersonal("RSSMRA80A01H501A"));
Assert.IsFalse(FiscalCodeValidator.IsValidPersonal("RSSMRA80Z01H501U"));
}
[TestMethod]
public void Validator_AcceptsCompanyCodes() => Assert.IsTrue(FiscalCodeValidator.IsValid("01234567897"));
[TestMethod]
public void Decode_ReadsBirthDateSexAndPlace()
{
var man = FiscalCodeCalculator.Decode("RSSMRA80A01H501U", Today);
var woman = FiscalCodeCalculator.Decode(FiscalCodeCalculator.Compute(new("Bianchi", "Anna", new DateOnly(2010, 3, 15), PersonSex.Female, "F205")), Today);
Assert.AreEqual(new PersonalTaxCodeInfo(new DateOnly(1980, 1, 1), PersonSex.Male, "H501"), man);
Assert.AreEqual(new DateOnly(2010, 3, 15), woman!.BirthDate);
Assert.AreEqual(PersonSex.Female, woman.Sex);
Assert.IsNull(FiscalCodeCalculator.Decode("RSSMRA80A01H501A", Today));
}
}
[TestClass]
public sealed class SdiRecipientCodeTests
{
[TestMethod]
[DataRow("ABC1234", RecipientKind.Business)]
[DataRow("0000000", RecipientKind.Business)]
[DataRow("UFE3RT", RecipientKind.PublicAdministration)]
[DataRow("0000000", RecipientKind.Consumer)]
[DataRow("", RecipientKind.Consumer)]
[DataRow("XXXXXXX", RecipientKind.Foreign)]
public void ValidCodes_HaveNoProblems(string code, RecipientKind kind) =>
Assert.IsEmpty(SdiRecipientCodeValidator.Check(code, kind));
[TestMethod]
[DataRow("ABC123", RecipientKind.Business, "fiscal.recipient.business-length")]
[DataRow("XXXXXXX", RecipientKind.Business, "fiscal.recipient.foreign-code-domestic")]
[DataRow("ABC1234", RecipientKind.PublicAdministration, "fiscal.recipient.pa-length")]
[DataRow("", RecipientKind.PublicAdministration, "fiscal.recipient.required")]
[DataRow("0000000", RecipientKind.Foreign, "fiscal.recipient.foreign")]
[DataRow("AB-1234", RecipientKind.Business, "fiscal.recipient.characters")]
public void WrongCodes_SayWhy(string code, RecipientKind kind, string problem) =>
Assert.AreEqual(problem, SdiRecipientCodeValidator.Check(code, kind).Single().Code);
[TestMethod]
public void Defaults_DependOnTheCustomer()
{
var rules = new ItalianTaxIdentifierRules();
Assert.AreEqual("XXXXXXX", rules.DefaultRecipientCode(RecipientKind.Foreign));
Assert.AreEqual("0000000", rules.DefaultRecipientCode(RecipientKind.Consumer));
Assert.IsNull(rules.DefaultRecipientCode(RecipientKind.PublicAdministration));
}
}
[TestClass]
public sealed class ItalianBankDirectoryTests
{
[TestMethod]
public void FindByIban_ReadsAbiAndCab()
{
var directory = new ItalianBankDirectory([new BankInfo("05428", "Banca di prova", "BPROITMMXXX", null)]);
var bank = directory.FindByIban("IT60 X054 2811 1010 0000 0123 456");
Assert.AreEqual("Banca di prova", bank!.Name);
Assert.AreEqual("11101", bank.BranchCode);
Assert.IsNull(directory.FindByIban("DE89 3704 0044 0532 0130 00"));
Assert.AreEqual(("05428", "11101"), ItalianBankDirectory.CodesOf("IT60X0542811101000000123456"));
}
}