OpenDataVisualizer/Schaad.OpenData.Tests/StationGeometryServiceTestShould.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

96 lines
4.1 KiB
C#

using NSubstitute;
using Schaad.OpenData.Traffic.Geometry;
using Schaad.OpenData.Traffic.Models;
namespace Schaad.OpenData.Tests;
public class StationGeometryServiceTestShould
{
private readonly SwisstopoClient swisstopoClient = Substitute.For<SwisstopoClient>(new HttpClient());
private readonly StationGeometryService service;
public StationGeometryServiceTestShould()
{
service = new StationGeometryService(swisstopoClient);
swisstopoClient.GetRoadSegmentsAsync(default, default, default).ReturnsForAnyArgs([]);
}
[Fact]
public async Task LookUpEachPartWhenPlaceNameContainsSlash()
{
swisstopoClient.FindPlaceAsync("Wilen", Arg.Any<GeoPoint>(), Arg.Any<CancellationToken>()).Returns((GeoPoint?)null);
swisstopoClient.FindPlaceAsync("Busswil", Arg.Any<GeoPoint>(), Arg.Any<CancellationToken>()).Returns(TestData.Meters(0, 2000));
var geometry = await service.BuildAsync(TestData.Station(413, "Wilen/Busswil", "Gloten"), [], CancellationToken.None);
await swisstopoClient.DidNotReceive().FindPlaceAsync("Wilen/Busswil", Arg.Any<GeoPoint>(), Arg.Any<CancellationToken>());
geometry.Directions[0].Path[^1].Lat.ShouldBeGreaterThan(TestData.Origin.Lat);
}
private static Station Arm(int code, string name, (double East, double North) offset, string address, params string[] directions) =>
TestData.Station(code, directions) with
{
Name = name,
Address = address,
Location = TestData.Meters(offset.East, offset.North),
};
private static Station[] KreiselWest() =>
[
Arm(1, "WEINFELDEN Kreisel West - Weinfelden", (30, 0), "Frauenfelderstrasse", "nach Weinfelden", "von Weinfelden"),
Arm(2, "WEINFELDEN Kreisel West - Umfahrung", (0, -30), "Dunantstrasse", "von Umfahrung", "zur Umfahrung"),
Arm(3, "WEINFELDEN Kreisel West - Märstetten", (-30, 0), "Frauenfelderstrasse", "nach Maerstetten", "von Maerstetten"),
Arm(4, "WEINFELDEN Kreisel West - Egelseestrasse", (0, 30), "Egelseestrasse", "von Egelseestrasse", "zur Egelseestrasse"),
];
[Fact]
public void PointAwayFromRoundaboutCenterWhenComputingOutwardTarget()
{
var stations = KreiselWest();
var target = StationGeometryService.OutwardTarget(stations[1], stations)!.Value;
target.Lat.ShouldBeLessThan(TestData.Meters(0, -2900).Lat);
target.Lon.ShouldBe(TestData.Origin.Lon, 1e-6);
}
[Fact]
public void ReturnNoOutwardTargetWhenRoundaboutHasSingleStation()
{
var station = KreiselWest()[0];
StationGeometryService.OutwardTarget(station, [station]).ShouldBeNull();
}
[Fact]
public async Task FollowArmNamedLikeStationAddressWhenTargetIsNotAPlace()
{
var stations = KreiselWest();
swisstopoClient.GetRoadSegmentsAsync(default, default, default).ReturnsForAnyArgs(
[
new RoadSegment([TestData.Meters(0, -28), TestData.Meters(-80, 60)], true, true, null),
new RoadSegment([TestData.Meters(0, -10), TestData.Meters(0, -200)], true, true, "Dunantstrasse"),
new RoadSegment([TestData.Meters(0, -200), TestData.Meters(0, -700)], true, true, "Dunantstrasse"),
]);
var geometry = await service.BuildAsync(stations[1], stations, CancellationToken.None);
await swisstopoClient.DidNotReceiveWithAnyArgs().FindPlaceAsync(default!, default, default);
var outward = geometry.Directions.Single(d => d.Direction == "zur Umfahrung").Path;
var inward = geometry.Directions.Single(d => d.Direction == "von Umfahrung").Path;
outward[^1].Lat.ShouldBeLessThan(outward[0].Lat);
outward[^1].Lon.ShouldBe(TestData.Origin.Lon, 1e-6);
inward[^1].ShouldBe(outward[0]);
}
[Fact]
public async Task UseStraightPathWhenNoRoadIsFound()
{
swisstopoClient.FindPlaceAsync(default!, default, default).ReturnsForAnyArgs(TestData.Meters(1000, 0));
var geometry = await service.BuildAsync(TestData.Station(1, "nach A", "von A"), [], CancellationToken.None);
geometry.FollowsRoad.ShouldBeFalse();
geometry.Directions.Count.ShouldBe(2);
}
}