OpenDataVisualizer/Schaad.OpenData.Tests/WeatherRefresherTestShould.cs
Claudio Schaad 05f4fa78f6 Add live weather layer from MeteoSwiss stations
- 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>
2026-11-19 06:04:15 +01:00

105 lines
4.6 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Schaad.OpenData.Weather;
using Schaad.OpenData.Weather.Models;
using Schaad.OpenData.Weather.Source;
using Schaad.OpenData.Weather.Storage;
using static Schaad.OpenData.Tests.WeatherTestData;
namespace Schaad.OpenData.Tests;
public class WeatherRefresherTestShould
{
private readonly WeatherOptions settings = new() { Stations = ["GUT", "STK"] };
private readonly MeteoSwissClient client;
private readonly WeatherCacheStorage storage;
private readonly WeatherDataStore store = new();
private readonly TimeProvider timeProvider = Substitute.For<TimeProvider>();
private readonly WeatherRefresher refresher;
public WeatherRefresherTestShould()
{
var options = Options.Create(settings);
client = Substitute.For<MeteoSwissClient>(new HttpClient(), options);
client.FetchStationsAsync(Arg.Any<IReadOnlyCollection<string>>(), Arg.Any<CancellationToken>())
.Returns([Station(), Station("STK", "Steckborn")]);
client.FetchRecentAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(call => [Reading(call.Arg<string>(), Now)]);
client.FetchHistoryAsync(Arg.Any<string>(), Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
.Returns(call => [Reading(call.Arg<string>(), Now.AddDays(-7))]);
storage = Substitute.For<WeatherCacheStorage>(options);
timeProvider.GetUtcNow().Returns(Now);
refresher = new WeatherRefresher(client, storage, store, options, timeProvider, NullLogger<WeatherRefresher>.Instance);
}
private static WeatherSnapshot Covered(DateTimeOffset? stationsFetched) =>
new(
[Station(), Station("STK", "Steckborn")],
[Reading("GUT", Now.AddDays(-7)), Reading("GUT", Now.AddMinutes(-10)), Reading("STK", Now.AddDays(-7)), Reading("STK", Now.AddMinutes(-10))],
Now.AddMinutes(-10),
stationsFetched);
[Fact]
public async Task FetchStationsAndBackfillSevenDaysWhenCacheIsEmpty()
{
await refresher.RefreshAsync(CancellationToken.None);
await client.Received(1).FetchStationsAsync(
Arg.Is<IReadOnlyCollection<string>>(a => a.SequenceEqual(new[] { "GUT", "STK" })),
Arg.Any<CancellationToken>());
await client.Received(1).FetchHistoryAsync("GUT", Now.AddDays(-7), Arg.Any<CancellationToken>());
await client.Received(1).FetchHistoryAsync("STK", Now.AddDays(-7), Arg.Any<CancellationToken>());
store.Current.Stations.Count.ShouldBe(2);
store.Current.Readings.Count.ShouldBe(4);
store.Current.StationsFetched.ShouldBe(Now);
await storage.Received(1).SaveAsync(
store.Current,
Arg.Is<IReadOnlyCollection<DateOnly>>(days => days.Order().SequenceEqual(new[] { new DateOnly(2026, 11, 11), new DateOnly(2026, 11, 18) })),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task FetchOnlyRecentValuesWhenCacheIsCovered()
{
store.Replace(Covered(Now.AddHours(-1)));
await refresher.RefreshAsync(CancellationToken.None);
await client.DidNotReceive().FetchStationsAsync(Arg.Any<IReadOnlyCollection<string>>(), Arg.Any<CancellationToken>());
await client.DidNotReceive().FetchHistoryAsync(Arg.Any<string>(), Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>());
await client.Received(2).FetchRecentAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
store.Current.Readings.Count.ShouldBe(6);
}
[Fact]
public async Task RefreshStationMetadataOnceADayWhenRefreshing()
{
store.Replace(Covered(Now.AddDays(-1)));
await refresher.RefreshAsync(CancellationToken.None);
await client.Received(1).FetchStationsAsync(Arg.Any<IReadOnlyCollection<string>>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task KeepOtherStationsWhenOneStationFails()
{
store.Replace(Covered(Now.AddHours(-1)));
client.FetchRecentAsync("GUT", Arg.Any<CancellationToken>()).ThrowsAsync(new HttpRequestException("404"));
await refresher.RefreshAsync(CancellationToken.None);
store.Current.Readings.Where(r => r.Time == Now).Select(r => r.StationAbbr).ShouldBe(["STK"]);
}
[Fact]
public void RequireRefreshWhenLastRefreshIsOlderThanInterval()
{
store.Replace(new WeatherSnapshot([], [], Now.AddMinutes(-11), null));
refresher.IsRefreshDue(TimeSpan.FromMinutes(10)).ShouldBeTrue();
refresher.IsRefreshDue(TimeSpan.FromMinutes(15)).ShouldBeFalse();
}
}