OpenDataVisualizer/Schaad.OpenData.Tests/MeteoSwissClientTestShould.cs
Claudio Schaad bd2981ffb8 Add live weather layer from MeteoSwiss stations (#7)
- New Schaad.OpenData.Weather project: MeteoSwissClient reads the
  10-minute CSV of the five SwissMetNet stations in Thurgau every 10
  minutes (station list once a day) and backfills the last 7 days from
  the yearly file when the cache has less than 6 days
- History kept as JSON per day (60 days); a failing station does not
  stop the others
- API: /api/weather/meta, /stations (current values, 1 h/24 h rain,
  24 h min/max, strongest gust), /stations/{abbr}/history
- Combined map: "Wetter (live)" layer with station badges (temperature,
  24 h rain or wind), wind arrows, rain marker, station panel with
  hourly 7-day chart, overview and municipality context entries
- Docs: docs/datasets/wetter.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>
2026-11-19 05:04:46 +00:00

91 lines
3.9 KiB
C#

using System.Net;
using System.Text;
using Microsoft.Extensions.Options;
using Schaad.OpenData.Weather;
using Schaad.OpenData.Weather.Models;
using Schaad.OpenData.Weather.Source;
namespace Schaad.OpenData.Tests;
public class MeteoSwissClientTestShould
{
private sealed class StubHandler(Dictionary<string, byte[]> 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 ByteArrayContent(body) }
: new HttpResponseMessage(HttpStatusCode.NotFound));
}
}
private const string BaseUrl = "https://data.example/ogd-smn";
private const string DataHeader =
"station_abbr;reference_timestamp;tre200s0;ure200s0;rre150z0;sre000z0;fu3010z0;fu3010z1;dkl010z0;pp0qffs0";
private static MeteoSwissClient Client(StubHandler handler) =>
new(new HttpClient(handler), Options.Create(new WeatherOptions { BaseUrl = BaseUrl + "/" }));
[Fact]
public async Task ReadWantedStationsFromLatin1MetadataWhenFetchingStations()
{
var csv =
"station_abbr;station_name;station_canton;station_height_masl;station_coordinates_wgs84_lat;station_coordinates_wgs84_lon;station_exposition_de\n" +
"GUT;Güttingen;TG;440;47.601733;9.279428;\"Ebene; See\"\n" +
"TAE;Aadorf / Tänikon;TG;539;47.479;8.905;Ebene\n" +
"KLO;Zürich / Kloten;ZH;426;47.48;8.536;Ebene\n";
var handler = new StubHandler(new() { [$"{BaseUrl}/ogd-smn_meta_stations.csv"] = Encoding.Latin1.GetBytes(csv) });
var stations = await Client(handler).FetchStationsAsync(["gut", "TAE"], CancellationToken.None);
stations.ShouldBe([
new WeatherStation("GUT", "Güttingen", "TG", 440, 47.601733, 9.279428),
new WeatherStation("TAE", "Aadorf / Tänikon", "TG", 539, 47.479, 8.905),
]);
}
[Fact]
public async Task ParseTenMinuteValuesAsUtcWhenFetchingRecentData()
{
var csv = DataHeader + "\n" +
"GUT;18.11.2026 18:50;8.6;89.2;0.1;0;12.7;50.8;243;1001.4\n" +
"GUT;18.11.2026 19:00;8.4;87.7;0;;11.8;;237;1001.4\n";
var handler = new StubHandler(new() { [$"{BaseUrl}/gut/ogd-smn_gut_t_now.csv"] = Encoding.UTF8.GetBytes(csv) });
var readings = await Client(handler).FetchRecentAsync("GUT", CancellationToken.None);
readings.ShouldBe([
new WeatherReading("GUT", new DateTimeOffset(2026, 11, 18, 18, 50, 0, TimeSpan.Zero), 8.6, 89.2, 0.1, 0, 12.7, 50.8, 243, 1001.4),
new WeatherReading("GUT", new DateTimeOffset(2026, 11, 18, 19, 0, 0, TimeSpan.Zero), 8.4, 87.7, 0, null, 11.8, null, 237, 1001.4),
]);
}
[Fact]
public async Task KeepOnlyValuesSinceStartWhenFetchingHistory()
{
var csv = DataHeader + "\n" +
"GUT;01.01.2026 00:00;-4.9;94.3;0;0;3.8;14;220;1023.9\n" +
"GUT;12.11.2026 00:00;10.2;70;0;0;5;9;200;1010\n";
var handler = new StubHandler(new() { [$"{BaseUrl}/gut/ogd-smn_gut_t_recent.csv"] = Encoding.UTF8.GetBytes(csv) });
var readings = await Client(handler).FetchHistoryAsync("GUT", new DateTimeOffset(2026, 11, 11, 0, 0, 0, TimeSpan.Zero), CancellationToken.None);
readings.ShouldHaveSingleItem().Temperature.ShouldBe(10.2);
}
[Fact]
public async Task SkipRequestWhenAbbreviationIsInvalid()
{
var handler = new StubHandler([]);
var readings = await Client(handler).FetchRecentAsync("../x", CancellationToken.None);
readings.ShouldBeEmpty();
handler.Requests.ShouldBeEmpty();
}
}