- 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>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
87 lines
4.3 KiB
C#
87 lines
4.3 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 LindasHydroClientTestShould
|
|
{
|
|
private sealed class StubHandler(string body) : HttpMessageHandler
|
|
{
|
|
public List<(Uri Uri, string Body, string Accept)> Requests { get; } = [];
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
var content = request.Content is null ? string.Empty : await request.Content.ReadAsStringAsync(cancellationToken);
|
|
Requests.Add((request.RequestUri!, content, request.Headers.Accept.ToString()));
|
|
return new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StringContent(body, Encoding.UTF8, "application/sparql-results+json"),
|
|
};
|
|
}
|
|
}
|
|
|
|
private const string Response = """
|
|
{"head":{"vars":["obs","id","name","water","wkt","time","level","discharge","temperature","danger"]},
|
|
"results":{"bindings":[
|
|
{"obs":{"type":"uri","value":"https://environment.ld.admin.ch/foen/hydro/lake/observation/2032"},
|
|
"id":{"type":"literal","value":"2032"},"name":{"type":"literal","value":"Romanshorn"},
|
|
"water":{"type":"literal","value":"Bodensee, Obersee"},
|
|
"wkt":{"type":"literal","value":"POINT(9.380992701081867 47.56479123049356)"},
|
|
"time":{"type":"literal","value":"2026-11-18T20:00:00+01:00"},
|
|
"level":{"type":"literal","value":"394.876"},
|
|
"danger":{"type":"literal","value":"1"}},
|
|
{"obs":{"type":"uri","value":"https://environment.ld.admin.ch/foen/hydro/river/observation/2044"},
|
|
"id":{"type":"literal","value":"2044"},"name":{"type":"literal","value":"Andelfingen "},
|
|
"water":{"type":"literal","value":"Thur"},
|
|
"wkt":{"type":"literal","value":"POINT(8.68 47.6)"},
|
|
"time":{"type":"literal","value":"2026-11-18T20:00:00+01:00"},
|
|
"level":{"type":"literal","value":"355.126"},"discharge":{"type":"literal","value":"36.103"},
|
|
"temperature":{"type":"literal","value":"9.6"},
|
|
"danger":{"type":"uri","value":"https://cube.link/Undefined"}},
|
|
{"obs":{"type":"uri","value":"https://environment.ld.admin.ch/foen/hydro/river/observation/9999"},
|
|
"id":{"type":"literal","value":"9999"},"name":{"type":"literal","value":"Ohne Lage"},
|
|
"time":{"type":"literal","value":"2026-11-18T20:00:00+01:00"}}
|
|
]}}
|
|
""";
|
|
|
|
private static LindasHydroClient Client(StubHandler handler) =>
|
|
new(new HttpClient(handler), Options.Create(new HydrologyOptions { SparqlUrl = "https://lindas.example/query" }));
|
|
|
|
[Fact]
|
|
public async Task MapStationsAndReadingsWhenFetching()
|
|
{
|
|
var handler = new StubHandler(Response);
|
|
|
|
var result = await Client(handler).FetchAsync(["2032", "2044"], CancellationToken.None);
|
|
|
|
result.Stations.ShouldBe([
|
|
new HydroStation("2032", "Romanshorn", "Bodensee, Obersee", HydroStationKind.Lake, 47.56479123049356, 9.380992701081867),
|
|
new HydroStation("2044", "Andelfingen", "Thur", HydroStationKind.River, 47.6, 8.68),
|
|
]);
|
|
var time = new DateTimeOffset(2026, 11, 18, 20, 0, 0, TimeSpan.FromHours(1));
|
|
result.Readings.ShouldBe([
|
|
new HydroReading("2032", time, 394.876, null, null, 1),
|
|
new HydroReading("2044", time, 355.126, 36.103, 9.6, null),
|
|
]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RequestOnlyNumericStationIdsAsJsonWhenFetching()
|
|
{
|
|
var handler = new StubHandler(Response);
|
|
|
|
await Client(handler).FetchAsync(["2032", "20\"44", "abc"], CancellationToken.None);
|
|
|
|
var request = handler.Requests.ShouldHaveSingleItem();
|
|
request.Uri.AbsoluteUri.ShouldBe("https://lindas.example/query");
|
|
request.Accept.ShouldBe("application/sparql-results+json");
|
|
var query = Uri.UnescapeDataString(request.Body.Replace('+', ' '));
|
|
query.ShouldContain("VALUES ?station { <https://environment.ld.admin.ch/foen/hydro/station/2032> }");
|
|
query.ShouldNotContain("abc");
|
|
query.ShouldNotContain("20\"44");
|
|
}
|
|
}
|