using System.Text.Json; using Microsoft.Extensions.Options; using Schaad.OpenData.Vehicles; using Schaad.OpenData.Vehicles.Models; using Schaad.OpenData.Vehicles.Storage; using static Schaad.OpenData.Tests.VehicleTestData; namespace Schaad.OpenData.Tests; public sealed class VehicleCacheStorageTestShould : IDisposable { private readonly string dataPath = Path.Combine(Path.GetTempPath(), "odv-tests-" + Guid.NewGuid().ToString("N")); private readonly VehicleCacheStorage storage; public VehicleCacheStorageTestShould() { storage = new VehicleCacheStorage(Options.Create(new VehicleOptions { DataPath = dataPath })); } public void Dispose() { if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, true); } } [Fact] public async Task ReturnEmptySnapshotWhenCacheDoesNotExist() { var snapshot = await storage.LoadAsync(CancellationToken.None); snapshot.Stock.ShouldBeEmpty(); snapshot.Outlines.ShouldBeEmpty(); snapshot.LastRefresh.ShouldBeNull(); } [Fact] public async Task RestoreSavedSnapshotWhenLoading() { var refreshedAt = new DateTimeOffset(2026, 11, 6, 12, 0, 0, TimeSpan.Zero); var stockVersion = new DatasetVersion("djs-stv-17", refreshedAt.AddDays(-30)); var snapshot = new VehicleSnapshot( [Entry(2023, municipality: "Bürglen"), Entry(2024)], [new PopulationEntry(2024, "4566", 27000)], [Outline("4566")], stockVersion, null, refreshedAt); await storage.SaveAsync(snapshot, CancellationToken.None); var loaded = await storage.LoadAsync(CancellationToken.None); loaded.Stock.ShouldBe(snapshot.Stock); loaded.Population.ShouldBe(snapshot.Population); JsonSerializer.Serialize(loaded.Outlines.ShouldHaveSingleItem().Polygons).ShouldBe(JsonSerializer.Serialize(Outline("4566").Polygons)); loaded.StockVersion.ShouldBe(stockVersion); loaded.PopulationVersion.ShouldBeNull(); loaded.LastRefresh.ShouldBe(refreshedAt); loaded.Years.ShouldBe([2023, 2024]); } }