OpenDataVisualizer/Schaad.OpenData.Tests/TrafficCacheStorageTestShould.cs
Claudio Schaad e729ea3403 Add Thurgau traffic visualizer with animated map
ASP.NET Core 10 API and React client that visualize the canton of
Thurgau's hourly traffic counts by vehicle class (data.tg.ch, CC0).

- Background refresh from data.tg.ch every 6 h into a per-day JSON
  cache, so history grows beyond the source's two-week window
- Road geometry per station direction from swisstopo swissTLM3D,
  oriented by geocoding the direction's target town; roundabout arms
  follow the road outward only
- Leaflet map with canvas animation: dots drive along the road in
  both directions, density by hourly count, colour by vehicle class,
  right-hand traffic; play/pause, time slider and day selection
- Station panel with per-class counts and Chart.js charts (hourly,
  weekday/weekend profile, daily totals, class mix)
- swisstopo base map switcher (grey, colour, aerial)
- Dockerfile and docker-compose with a data volume
- xUnit tests for mapping, cache, geometry, geocoding and analysis

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 20:33:58 +01:00

68 lines
2.4 KiB
C#

using Microsoft.Extensions.Options;
using Schaad.OpenData.Traffic;
using Schaad.OpenData.Traffic.Models;
using Schaad.OpenData.Traffic.Storage;
namespace Schaad.OpenData.Tests;
public sealed class TrafficCacheStorageTestShould : IDisposable
{
private static readonly DateOnly Day = new(2026, 10, 27);
private readonly string dataPath = Path.Combine(Path.GetTempPath(), "odv-tests-" + Guid.NewGuid().ToString("N"));
private readonly TrafficCacheStorage storage;
public TrafficCacheStorageTestShould()
{
storage = new TrafficCacheStorage(Options.Create(new TrafficOptions { 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.Stations.ShouldBeEmpty();
snapshot.Dates.ShouldBeEmpty();
snapshot.LastRefresh.ShouldBeNull();
}
[Fact]
public async Task RestoreSavedDataWhenLoading()
{
var refreshedAt = new DateTimeOffset(2026, 11, 6, 12, 0, 0, TimeSpan.Zero);
var geometry = new StationGeometry(1, true, [new DirectionGeometry("nach A", [TestData.Origin, TestData.Meters(10, 0)])]);
var snapshot = new TrafficSnapshot(
[TestData.Station(1, "nach A")],
[TestData.Count(1, "nach A", Day, 8, 42, 3)],
[geometry],
refreshedAt);
await storage.SaveAsync(snapshot, [Day], CancellationToken.None);
await storage.SaveGeometryAsync(snapshot, CancellationToken.None);
var loaded = await storage.LoadAsync(CancellationToken.None);
loaded.Stations[1].Directions.ShouldBe(["nach A"]);
loaded.CountsByDate[Day].ShouldHaveSingleItem().Counts.ShouldBe(TestData.Cars(42, 3));
loaded.Geometry[1].Directions.ShouldHaveSingleItem().Path.Count.ShouldBe(2);
loaded.LastRefresh.ShouldBe(refreshedAt);
}
[Fact]
public async Task WriteOneFilePerDayWhenSaving()
{
var snapshot = new TrafficSnapshot([], [TestData.Count(1, "nach A", Day, 8, 1)], [], null);
await storage.SaveAsync(snapshot, [Day], CancellationToken.None);
File.Exists(Path.Combine(dataPath, "traffic", "thurgau", "counts", "2026-10-27.json")).ShouldBeTrue();
}
}