OpenDataVisualizer/Schaad.OpenData.Tests/VehicleCacheStorageTestShould.cs
Claudio Schaad 81c3661687 Add Thurgau vehicle stock map
Choropleth of the 80 Thurgau municipalities from the road vehicle stock
dataset (djs-stv-17): vehicles per 1000 inhabitants by vehicle group or
share of a car brand among passenger cars, with year controls and a
municipality panel. Population comes from sk-stat-56, outlines from
swisstopo swissBOUNDARIES3D.

Also fixes the broken ENV line in the Dockerfile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 22:41:47 +01:00

62 lines
2.1 KiB
C#

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]);
}
}