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>
63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Watercraft;
|
|
using Schaad.OpenData.Watercraft.Storage;
|
|
using static Schaad.OpenData.Tests.WatercraftTestData;
|
|
|
|
namespace Schaad.OpenData.Tests;
|
|
|
|
public sealed class WatercraftCacheStorageTestShould : IDisposable
|
|
{
|
|
private readonly string dataPath = Path.Combine(Path.GetTempPath(), "odv-tests-" + Guid.NewGuid().ToString("N"));
|
|
private readonly WatercraftCacheStorage storage;
|
|
|
|
public WatercraftCacheStorageTestShould()
|
|
{
|
|
storage = new WatercraftCacheStorage(Options.Create(new WatercraftOptions { 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.Years.ShouldBeEmpty();
|
|
snapshot.LastRefresh.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RestoreSavedYearsWhenLoading()
|
|
{
|
|
var refreshedAt = new DateTimeOffset(2026, 11, 6, 12, 0, 0, TimeSpan.Zero);
|
|
var snapshot = new WatercraftSnapshot([Year(2023, Vessel("Güttingen", fuel: "Elektrizität"))], refreshedAt);
|
|
|
|
await storage.SaveAsync(snapshot, [2023], CancellationToken.None);
|
|
var loaded = await storage.LoadAsync(CancellationToken.None);
|
|
|
|
var year = loaded.Years.ShouldHaveSingleItem();
|
|
year.Year.ShouldBe(2023);
|
|
year.DatasetId.ShouldBe("djs-kpo-2023");
|
|
year.ReferenceDate.ShouldBe(new DateOnly(2023, 12, 31));
|
|
year.Vessels.ShouldHaveSingleItem().ShouldBe(Vessel("Güttingen", fuel: "Elektrizität"));
|
|
loaded.LastRefresh.ShouldBe(refreshedAt);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WriteOnlyChangedYearsWhenSaving()
|
|
{
|
|
var snapshot = new WatercraftSnapshot([Year(2022, Vessel()), Year(2023, Vessel())], null);
|
|
|
|
await storage.SaveAsync(snapshot, [2023], CancellationToken.None);
|
|
|
|
var yearsPath = Path.Combine(dataPath, "watercraft", "thurgau", "years");
|
|
File.Exists(Path.Combine(yearsPath, "2023.json")).ShouldBeTrue();
|
|
File.Exists(Path.Combine(yearsPath, "2022.json")).ShouldBeFalse();
|
|
}
|
|
}
|