using Khaemhat.Application.Parties; using Khaemhat.Application.Querying; using Khaemhat.Application.Security; using Khaemhat.Application.Seeding; using Khaemhat.Core.Parties; using Khaemhat.Core.Security; using Khaemhat.Fiscal.Italy; using Khaemhat.Web.Tests.Hosting; using Microsoft.Extensions.DependencyInjection; namespace Khaemhat.Web.Tests.Parties; /// The demo parties (SPEC ยง14, issue #41): loaded once, every kind of subject, and each one passes the checks of a save. [TestClass] public sealed class DemoPartiesTests { private static readonly Actor Root = new(TestInstance.SuperUser, UserLevel.SuperUser, 1, null, null); [TestMethod] public void FictitiousNumbers_AreValid() { var rules = new ItalianTaxIdentifierRules(); foreach (var vat in DemoPartiesSeeder.VatNumbers) { Assert.IsTrue(rules.IsValidVatNumber(vat), vat); } Assert.IsTrue(rules.IsValidTaxCode(DemoPartiesSeeder.AdministrationTaxCode)); foreach (var iban in DemoPartiesSeeder.Ibans) { Assert.IsTrue(Khaemhat.Core.Validation.IbanValidator.IsValid(iban), iban); } } [TestMethod] public async Task DemoSeed_FillsAnEmptyCompanyOnce_WithPartiesThatPassTheChecks() { var (app, _, company) = await TestInstance.StartAsync(CancellationToken.None); await using (app) { var runner = app.Services.GetRequiredService(); var parties = app.Services.GetRequiredService(); var first = await runner.RunAsync(company, SeedKind.Demo); Assert.AreEqual(21, first.Added["parties"]); Assert.IsEmpty((await runner.RunAsync(company, SeedKind.Demo)).Added, "Only an empty company gets the demo data."); var customers = (await parties.ListAsync(Root, company, PartyArchive.Customers, new ListQuery { Limit = 100 })).Items; var suppliers = (await parties.ListAsync(Root, company, PartyArchive.Suppliers, new ListQuery { Limit = 100 })).Items; var contacts = (await parties.ListAsync(Root, company, PartyArchive.Contacts, new ListQuery { Limit = 100 })).Items; Assert.HasCount(12, customers); Assert.HasCount(6, suppliers, "Five suppliers and a customer that is also a supplier."); Assert.HasCount(4, contacts); CollectionAssert.IsSubsetOf(Enum.GetValues(), customers.Select(c => c.SubjectType).Distinct().ToList()); Assert.AreEqual("C000001", customers.Single(c => c.Name == "Rossi Impianti S.r.l.").Code); Assert.IsTrue(customers.Any(c => c.Terms is { Blocked: true }) && customers.Any(c => c.Terms is { SplitPayment: true }) && customers.Any(c => c.Disabled)); foreach (var row in customers.Concat(suppliers).Concat(contacts).DistinctBy(r => r.Id)) { var record = (await parties.GetAsync(Root, company, row.Id))!; Assert.IsTrue(record.Party.IsDemo); var errors = (await parties.ValidateAsync(Root, company, record)).Where(p => !p.IsWarning).Select(p => p.Field + ":" + p.Code).ToList(); Assert.IsEmpty(errors, row.Name + ": " + string.Join(", ", errors)); } await app.StopAsync(); } } }