using Khaemhat.Fiscal.Abstractions;
namespace Khaemhat.Fiscal.Italy.Tests;
/// The provinces and municipalities of the ISTAT list shipped with the pack (ADR 0028).
[TestClass]
public sealed class ItalianPlacesTests
{
private static readonly IPlaceDirectory Places = new ItalyFiscalPack().Require();
[TestMethod]
public void List_HasEveryProvinceAndMunicipality_WithUniqueCodes()
{
Assert.HasCount(110, Places.Provinces);
Assert.HasCount(7894, Places.Municipalities);
Assert.HasCount(Places.Provinces.Count, Places.Provinces.Select(p => p.Code).Distinct(StringComparer.Ordinal));
Assert.HasCount(Places.Municipalities.Count, Places.Municipalities.Select(m => m.Code).Distinct(StringComparer.Ordinal));
Assert.Contains("ISTAT", Places.Source);
}
[TestMethod]
public void EveryMunicipality_BelongsToAProvinceOfItsRegion()
{
var provinces = Places.Provinces.ToDictionary(p => p.Code, StringComparer.Ordinal);
foreach (var municipality in Places.Municipalities)
{
Assert.IsTrue(provinces.TryGetValue(municipality.ProvinceCode, out var province), municipality.Code);
Assert.AreEqual(province.Region, municipality.Region, municipality.Code);
Assert.MatchesRegex("^[A-Z][0-9]{3}$", municipality.Code);
Assert.MatchesRegex("^[0-9]{6}$", municipality.StatisticalCode!);
}
}
[TestMethod]
[DataRow("L219", "Torino", "TO", "Piemonte", "001272", "10100,10121-10156")]
[DataRow("A952", "Bolzano", "BZ", "Trentino-Alto Adige/Südtirol", "021008", "39100")]
[DataRow("B354", "Cagliari", "CA", "Sardegna", "118006", "09100,09121-09131,09134")]
[DataRow("I138", "Sanremo", "IM", "Liguria", "008055", null)]
public void Municipality_HasTheCadastralCodeOfTheTaxCode_AndThePostalCodesFound(string code, string name, string province, string region, string istat, string? postalCodes)
{
Assert.AreEqual(new MunicipalityTemplate(code, name, province, region, istat, postalCodes), Places.Municipalities.Single(m => m.Code == code));
}
[TestMethod]
public void PostalCodes_CoverAlmostEveryMunicipality_AndAreFiveDigitsOrRanges()
{
var withCodes = Places.Municipalities.Where(m => m.PostalCodes is not null).ToList();
Assert.IsGreaterThan(7700, withCodes.Count);
Assert.Contains("GeoNames", Places.Source);
foreach (var municipality in withCodes)
{
Assert.MatchesRegex(@"^[0-9]{5}(-[0-9]{5})?(,[0-9]{5}(-[0-9]{5})?)*$", municipality.PostalCodes!, municipality.Code);
}
}
[TestMethod]
public void Provinces_FollowTheSardinianReform()
{
var sardinia = Places.Provinces.Where(p => p.Region == "Sardegna").Select(p => p.Code).Order(StringComparer.Ordinal).ToArray();
CollectionAssert.AreEqual(new[] { "CA", "CI", "NU", "OG", "OR", "OT", "SS", "VS" }, sardinia);
}
}