Choropleth of the 80 Thurgau municipalities from the road vehicle stock dataset (djs-stv-17), with population from sk-stat-56 and outlines from swisstopo. Also fixes the broken ENV line in the Dockerfile. 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>
88 lines
4.4 KiB
C#
88 lines
4.4 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using NSubstitute;
|
|
using Schaad.OpenData.Vehicles;
|
|
using Schaad.OpenData.Vehicles.Models;
|
|
using Schaad.OpenData.Vehicles.Source;
|
|
using Schaad.OpenData.Vehicles.Storage;
|
|
using static Schaad.OpenData.Tests.VehicleTestData;
|
|
|
|
namespace Schaad.OpenData.Tests;
|
|
|
|
public class VehicleRefresherTestShould
|
|
{
|
|
private static readonly DateTimeOffset Now = new(2026, 11, 6, 12, 0, 0, TimeSpan.Zero);
|
|
private static readonly IOptions<VehicleOptions> Options = Microsoft.Extensions.Options.Options.Create(new VehicleOptions());
|
|
private static readonly DatasetVersion StockVersion = new("djs-stv-17", Now.AddDays(-50));
|
|
private static readonly DatasetVersion PopulationVersion = new("sk-stat-56", Now.AddDays(-120));
|
|
|
|
private readonly ThurgauVehicleClient client = Substitute.For<ThurgauVehicleClient>(new HttpClient(), Options);
|
|
private readonly MunicipalityBoundaryClient boundaryClient = Substitute.For<MunicipalityBoundaryClient>(new HttpClient(), Options);
|
|
private readonly VehicleCacheStorage storage = Substitute.For<VehicleCacheStorage>(Options);
|
|
private readonly VehicleDataStore store = new();
|
|
private readonly TimeProvider timeProvider = Substitute.For<TimeProvider>();
|
|
private readonly VehicleRefresher refresher;
|
|
|
|
public VehicleRefresherTestShould()
|
|
{
|
|
timeProvider.GetUtcNow().Returns(Now);
|
|
client.GetVersionAsync("djs-stv-17", Arg.Any<CancellationToken>()).Returns(StockVersion);
|
|
client.GetVersionAsync("sk-stat-56", Arg.Any<CancellationToken>()).Returns(PopulationVersion);
|
|
boundaryClient.FetchAsync(Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(call => Outline(call.Arg<string>()));
|
|
refresher = new VehicleRefresher(client, boundaryClient, storage, store, Options, timeProvider, NullLogger<VehicleRefresher>.Instance);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FetchEverythingWhenCacheIsEmpty()
|
|
{
|
|
client.FetchStockAsync(Arg.Any<CancellationToken>()).Returns([Entry(id: "4566"), Entry(id: "4671", municipality: "Kreuzlingen")]);
|
|
client.FetchPopulationAsync(Arg.Any<CancellationToken>()).Returns([new PopulationEntry(2024, "4566", 27000)]);
|
|
|
|
await refresher.RefreshAsync(CancellationToken.None);
|
|
|
|
var snapshot = store.Current;
|
|
snapshot.Stock.Count.ShouldBe(2);
|
|
snapshot.Population.Count.ShouldBe(1);
|
|
snapshot.Outlines.Select(o => o.Id).ShouldBe(["4566", "4671"]);
|
|
snapshot.StockVersion.ShouldBe(StockVersion);
|
|
snapshot.PopulationVersion.ShouldBe(PopulationVersion);
|
|
snapshot.LastRefresh.ShouldBe(Now);
|
|
await storage.Received(1).SaveAsync(snapshot, Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SkipUnchangedDatasetsAndKnownOutlinesWhenRefreshing()
|
|
{
|
|
store.Replace(new VehicleSnapshot([Entry()], [new PopulationEntry(2024, "4566", 27000)], [Outline("4566")], StockVersion, PopulationVersion, Now.AddDays(-1)));
|
|
|
|
await refresher.RefreshAsync(CancellationToken.None);
|
|
|
|
await client.DidNotReceive().FetchStockAsync(Arg.Any<CancellationToken>());
|
|
await client.DidNotReceive().FetchPopulationAsync(Arg.Any<CancellationToken>());
|
|
await boundaryClient.DidNotReceive().FetchAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
|
store.Current.LastRefresh.ShouldBe(Now);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FetchStockAgainWhenDatasetWasModified()
|
|
{
|
|
store.Replace(new VehicleSnapshot([Entry()], [new PopulationEntry(2024, "4566", 27000)], [Outline("4566")], StockVersion with { Modified = Now.AddDays(-400) }, PopulationVersion, null));
|
|
client.FetchStockAsync(Arg.Any<CancellationToken>()).Returns([Entry(2025), Entry(2025, "4401", "Arbon")]);
|
|
|
|
await refresher.RefreshAsync(CancellationToken.None);
|
|
|
|
store.Current.Years.ShouldBe([2025]);
|
|
store.Current.Outlines.Select(o => o.Id).ShouldBe(["4566", "4401"]);
|
|
await client.DidNotReceive().FetchPopulationAsync(Arg.Any<CancellationToken>());
|
|
await boundaryClient.Received(1).FetchAsync("4401", Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public void RequireRefreshWhenLastRefreshIsOlderThanInterval()
|
|
{
|
|
store.Replace(new VehicleSnapshot([], [], [], null, null, Now.AddHours(-25)));
|
|
|
|
refresher.IsRefreshDue(TimeSpan.FromDays(1)).ShouldBeTrue();
|
|
refresher.IsRefreshDue(TimeSpan.FromDays(2)).ShouldBeFalse();
|
|
}
|
|
}
|