using System.Text.Json; using Schaad.OpenData.Vehicles.Analysis; using Schaad.OpenData.Vehicles.Models; using Schaad.OpenData.Vehicles.Storage; using static Schaad.OpenData.Tests.VehicleTestData; namespace Schaad.OpenData.Tests; public class VehicleAnalysisTestShould { private static VehicleSnapshot Snapshot(IReadOnlyList stock, IReadOnlyList? population = null) => new(stock, population ?? [], [Outline("4566"), Outline("4671"), Outline("9999")], null, null, null); [Fact] public void OrderGroupsTypesAndBrandsByFrequencyWhenBuildingOverview() { var snapshot = Snapshot( [ Entry(brand: "Andere", count: 50), Entry(brand: "VW", count: 20), Entry(brand: "TESLA", count: 30), Entry(group: "Motorräder", type: "Motorrad", brand: "HONDA", count: 25), Entry(group: "Motorräder", type: "Kleinmotorrad", brand: "Andere", count: 5), ]); var overview = VehicleAnalysis.BuildOverview(snapshot); overview.Groups.ShouldBe(["Personenwagen", "Motorräder"]); overview.Types.ShouldBe([new("Personenwagen", "Personenwagen"), new("Motorrad", "Motorräder"), new("Kleinmotorrad", "Motorräder")]); overview.Brands.ShouldBe(["TESLA", "VW", "Andere"]); } [Fact] public void SumCountsAndPopulationPerYearWhenBuildingOverview() { var snapshot = Snapshot( [Entry(2023, count: 10), Entry(2024, count: 12), Entry(2024, "4671", "Kreuzlingen", count: 8)], [new(2023, "4566", 26000), new(2023, "4671", 22000), new(2024, "4566", 26500)]); var overview = VehicleAnalysis.BuildOverview(snapshot); overview.Years.ShouldBe( [ new(2023, new DateOnly(2023, 12, 31), 10, 48000), new(2024, new DateOnly(2024, 12, 31), 20, 48500), ]); } [Fact] public void CountGroupsTypesAndPassengerCarBrandsPerMunicipalityWhenBuildingYears() { var snapshot = Snapshot( [ Entry(brand: "VW", count: 30), Entry(brand: "TESLA", count: 5), Entry(group: "Motorräder", type: "Motorrad", brand: "VW", count: 4), Entry(id: "4671", municipality: "Kreuzlingen", brand: "VW", count: 7), ], [new(2024, "4566", 1000), new(2024, "4671", 700)]); var year = VehicleAnalysis.BuildYears(snapshot).ShouldHaveSingleItem(); year.Year.ShouldBe(2024); year.Municipalities.Select(m => m.Name).ShouldBe(["Frauenfeld", "Kreuzlingen"]); var frauenfeld = year.Municipalities[0]; frauenfeld.Population.ShouldBe(1000); frauenfeld.Count.ShouldBe(39); frauenfeld.Groups.ShouldBe([35, 4]); frauenfeld.Types.ShouldBe([35, 4]); frauenfeld.Brands.ShouldBe([30, 5]); year.Canton.Name.ShouldBe(VehicleAnalysis.CantonName); year.Canton.Population.ShouldBe(1700); year.Canton.Count.ShouldBe(46); year.Canton.Brands.ShouldBe([37, 5]); } [Fact] public void UseLatestEarlierPopulationWhenYearIsMissing() { var snapshot = Snapshot([Entry(2025)], [new(2023, "4566", 900), new(2024, "4566", 1000)]); VehicleAnalysis.BuildYears(snapshot).ShouldHaveSingleItem().Municipalities[0].Population.ShouldBe(1000); } [Fact] public void ReturnShapesOnlyForMunicipalitiesInStockWhenBuildingShapes() { var snapshot = Snapshot([Entry(2023, municipality: "Frauenfeld alt"), Entry(2024), Entry(id: "4671", municipality: "Kreuzlingen")]); var shapes = VehicleAnalysis.BuildShapes(snapshot); shapes.Select(s => (s.Id, s.Name)).ShouldBe([("4566", "Frauenfeld"), ("4671", "Kreuzlingen")]); JsonSerializer.Serialize(shapes[0].Polygons).ShouldBe(JsonSerializer.Serialize(Outline("4566").Polygons)); } }