OpenDataVisualizer/Schaad.OpenData.Tests/HydrodatenPlotClientTestShould.cs
Claudio Schaad a838b17dc1 Add live water layer from BAFU hydrology data
- New Schaad.OpenData.Hydrology project: LindasHydroClient fetches the
  current level, discharge, water temperature and danger level of nine
  BAFU stations around Thurgau from LINDAS every 10 minutes
- LINDAS has no history, so the server keeps readings per day (60 days)
  and fills gaps from the 7-day plot JSON of hydrodaten.admin.ch
- API: /api/hydrology/meta, /stations, /stations/{id}/history
- Combined map: "Gewässer (live)" layer with river circles (size by
  discharge, colour by danger level or water temperature), lake level
  badges, station panel with 7-day chart, overview and municipality
  context entries
- Docs: docs/datasets/gewaesser.md, overview in docs/datasets.md,
  CLAUDE.md; Docker and appsettings wiring

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

82 lines
3.5 KiB
C#

using System.Net;
using System.Text;
using Microsoft.Extensions.Options;
using Schaad.OpenData.Hydrology;
using Schaad.OpenData.Hydrology.Models;
using Schaad.OpenData.Hydrology.Source;
namespace Schaad.OpenData.Tests;
public class HydrodatenPlotClientTestShould
{
private sealed class StubHandler(Dictionary<string, string> bodies) : HttpMessageHandler
{
public List<string> Requests { get; } = [];
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var url = request.RequestUri!.AbsoluteUri;
Requests.Add(url);
return Task.FromResult(bodies.TryGetValue(url, out var body)
? new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body, Encoding.UTF8, "application/json") }
: new HttpResponseMessage(HttpStatusCode.NotFound));
}
}
private const string LevelUrl = "https://hydro.example/plots/p_q_7days/2181_p_q_7days_de.json";
private const string TemperatureUrl = "https://hydro.example/plots/temperature_7days/2181_temperature_7days_de.json";
private const string LevelPlot = """
{"plot":{"layout":{},"data":[
{"name":"Wasserstand","type":"scatter","x":["2026-11-12T00:00:00.000+01:00","2026-11-12T00:05:00.000+01:00","2026-11-12T00:10:00.000+01:00"],"y":[455.835,455.839,455.843]},
{"name":"Abfluss","type":"scatter","x":["2026-11-12T00:00:00.000+01:00","2026-11-12T00:05:00.000+01:00","2026-11-12T00:10:00.000+01:00"],"y":[28.243,28.576,null]}
]},"hoverInfo":{}}
""";
private const string TemperaturePlot = """
{"plot":{"layout":{},"data":[
{"name":"Temperatur","type":"scatter","x":["2026-11-12T00:00:00.000+01:00","2026-11-12T00:20:00.000+01:00"],"y":[10.37,10.36]}
]},"hoverInfo":{}}
""";
private static HydrodatenPlotClient Client(StubHandler handler) =>
new(new HttpClient(handler), Options.Create(new HydrologyOptions { PlotsUrl = "https://hydro.example/plots/" }));
[Fact]
public async Task CombineTracesOnTenMinuteMarksWhenFetchingRecentValues()
{
var handler = new StubHandler(new() { [LevelUrl] = LevelPlot, [TemperatureUrl] = TemperaturePlot });
var readings = await Client(handler).FetchRecentAsync("2181", CancellationToken.None);
var offset = TimeSpan.FromHours(1);
readings.ShouldBe([
new HydroReading("2181", new DateTimeOffset(2026, 11, 12, 0, 0, 0, offset), 455.835, 28.243, 10.37, null),
new HydroReading("2181", new DateTimeOffset(2026, 11, 12, 0, 10, 0, offset), 455.843, null, null, null),
new HydroReading("2181", new DateTimeOffset(2026, 11, 12, 0, 20, 0, offset), null, null, 10.36, null),
]);
}
[Fact]
public async Task ReturnLevelsOnlyWhenTemperaturePlotIsMissing()
{
var handler = new StubHandler(new() { [LevelUrl] = LevelPlot });
var readings = await Client(handler).FetchRecentAsync("2181", CancellationToken.None);
readings.Count.ShouldBe(2);
readings.ShouldAllBe(r => r.WaterTemperature == null);
handler.Requests.ShouldBe([LevelUrl, TemperatureUrl]);
}
[Fact]
public async Task SkipRequestWhenStationIdIsNotNumeric()
{
var handler = new StubHandler([]);
var readings = await Client(handler).FetchRecentAsync("../2181", CancellationToken.None);
readings.ShouldBeEmpty();
handler.Requests.ShouldBeEmpty();
}
}