fix: add input validation and error-path tests for CompactNordPoolDayPrice

Made-with: Cursor
This commit is contained in:
Marius
2026-03-19 14:38:41 +02:00
parent 5a4a4ed001
commit b4e465573f
2 changed files with 48 additions and 7 deletions
@@ -6,6 +6,8 @@ namespace NordPool.Manager.Tests;
public class CompactNordPoolDayPriceTests
{
private const int FifteenMinutesInSeconds = 900;
private static readonly DateTime TestStart = new(2026, 3, 19, 10, 0, 0, DateTimeKind.Utc);
private static readonly DateTime TestEnd = new(2026, 3, 19, 10, 15, 0, DateTimeKind.Utc);
private static readonly long TestStartEpoch = new DateTimeOffset(TestStart).ToUnixTimeSeconds();
@@ -109,11 +111,43 @@ public class CompactNordPoolDayPriceTests
var prices = new List<CompactNordPoolDayPrice>
{
new() { Start = TestStartEpoch, End = TestEndEpoch, Price = 10.0 },
new() { Start = TestEndEpoch, End = TestEndEpoch + 900, Price = 11.0 }
new() { Start = TestEndEpoch, End = TestEndEpoch + FifteenMinutesInSeconds, Price = 11.0 }
};
var json = JsonSerializer.Serialize(prices);
json.Should().Be($"[[{TestStartEpoch},{TestEndEpoch},10],[{TestEndEpoch},{TestEndEpoch + 900},11]]");
json.Should().Be($"[[{TestStartEpoch},{TestEndEpoch},10],[{TestEndEpoch},{TestEndEpoch + FifteenMinutesInSeconds},11]]");
}
[Fact]
public void FromDayPrice_WhenNull_ShouldThrowArgumentNullException()
{
var act = () => CompactNordPoolDayPrice.FromDayPrice(null!);
act.Should().Throw<ArgumentNullException>();
}
[Fact]
public void JsonDeserialization_EmptyArray_ShouldThrowJsonException()
{
var act = () => JsonSerializer.Deserialize<CompactNordPoolDayPrice>("[]");
act.Should().Throw<JsonException>();
}
[Fact]
public void JsonDeserialization_TooFewElements_ShouldThrowJsonException()
{
var act = () => JsonSerializer.Deserialize<CompactNordPoolDayPrice>("[1,2]");
act.Should().Throw<JsonException>();
}
[Fact]
public void JsonDeserialization_InvalidTokenType_ShouldThrowJsonException()
{
var act = () => JsonSerializer.Deserialize<CompactNordPoolDayPrice>("[\"abc\",2,3]");
act.Should().Throw<JsonException>();
}
}