OpenDataVisualizer/Schaad.OpenData.Tests/TrafficCacheStorageTestShould.cs
Claudio Schaad 7b65926aa2 Add Thurgau traffic visualizer and start page (#1)
- Thurgau hourly traffic counts (data.tg.ch) refreshed every 6 h into a per-day JSON cache
- Road geometry per station direction from swisstopo swissTLM3D; roundabout arms run outward from the roundabout centre
- Leaflet map with animated vehicles by class, time controls and base map switcher (grey, colour, aerial)
- Station panel with per-class counts and Chart.js charts
- Start page with map tiles; traffic map at /verkehr/thurgau
- Dockerfile and docker-compose, xUnit tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-11-06 20:17:26 +00: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();
}
}