using Microsoft.Extensions.Options; using Schaad.OpenData.Weather; using Schaad.OpenData.Weather.Storage; using static Schaad.OpenData.Tests.WeatherTestData; namespace Schaad.OpenData.Tests; public sealed class WeatherCacheStorageTestShould : IDisposable { private readonly string dataPath = Path.Combine(Path.GetTempPath(), "odv-tests-" + Guid.NewGuid().ToString("N")); private readonly WeatherCacheStorage storage; public WeatherCacheStorageTestShould() { storage = new WeatherCacheStorage(Options.Create(new WeatherOptions { DataPath = dataPath })); } public void Dispose() { if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, true); } } [Fact] public async Task ReturnEmptySnapshotWhenCacheDoesNotExist() { var snapshot = await storage.LoadAsync(CancellationToken.None); snapshot.Stations.ShouldBeEmpty(); snapshot.Readings.ShouldBeEmpty(); snapshot.StationsFetched.ShouldBeNull(); } [Fact] public async Task RestoreStationsReadingsAndTimesWhenLoading() { var readings = new[] { Reading("GUT", Now.AddDays(-1)), Reading("STK", Now, temperature: null) }; var snapshot = new WeatherSnapshot([Station(), Station("STK", "Steckborn")], readings, Now, Now.AddHours(-3)); await storage.SaveAsync(snapshot, [new DateOnly(2026, 11, 17), new DateOnly(2026, 11, 18)], CancellationToken.None); var loaded = await storage.LoadAsync(CancellationToken.None); loaded.Stations.ShouldBe([Station(), Station("STK", "Steckborn")]); loaded.Readings.ShouldBe(readings); loaded.LastRefresh.ShouldBe(Now); loaded.StationsFetched.ShouldBe(Now.AddHours(-3)); Directory.GetFiles(Path.Combine(dataPath, "weather", "thurgau", "days")).Length.ShouldBe(2); } }