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 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 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 DateTime TestEnd = new(2026, 3, 19, 10, 15, 0, DateTimeKind.Utc);
private static readonly long TestStartEpoch = new DateTimeOffset(TestStart).ToUnixTimeSeconds(); private static readonly long TestStartEpoch = new DateTimeOffset(TestStart).ToUnixTimeSeconds();
@@ -109,11 +111,43 @@ public class CompactNordPoolDayPriceTests
var prices = new List<CompactNordPoolDayPrice> var prices = new List<CompactNordPoolDayPrice>
{ {
new() { Start = TestStartEpoch, End = TestEndEpoch, Price = 10.0 }, 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); 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>();
} }
} }
+12 -5
View File
@@ -6,20 +6,24 @@ namespace NordPool.Models;
[JsonConverter(typeof(CompactNordPoolDayPriceConverter))] [JsonConverter(typeof(CompactNordPoolDayPriceConverter))]
public readonly struct CompactNordPoolDayPrice public readonly struct CompactNordPoolDayPrice
{ {
private const long NullTimestampSentinel = 0L;
public long Start { get; init; } public long Start { get; init; }
public long End { get; init; } public long End { get; init; }
public double? Price { get; init; } public double? Price { get; init; }
public static CompactNordPoolDayPrice FromDayPrice(NordPoolDayPrice dayPrice) public static CompactNordPoolDayPrice FromDayPrice(NordPoolDayPrice dayPrice)
{ {
ArgumentNullException.ThrowIfNull(dayPrice);
return new CompactNordPoolDayPrice return new CompactNordPoolDayPrice
{ {
Start = dayPrice.Start.HasValue Start = dayPrice.Start.HasValue
? new DateTimeOffset(dayPrice.Start.Value, TimeSpan.Zero).ToUnixTimeSeconds() ? new DateTimeOffset(dayPrice.Start.Value, TimeSpan.Zero).ToUnixTimeSeconds()
: 0L, : NullTimestampSentinel,
End = dayPrice.End.HasValue End = dayPrice.End.HasValue
? new DateTimeOffset(dayPrice.End.Value, TimeSpan.Zero).ToUnixTimeSeconds() ? new DateTimeOffset(dayPrice.End.Value, TimeSpan.Zero).ToUnixTimeSeconds()
: 0L, : NullTimestampSentinel,
Price = dayPrice.Price Price = dayPrice.Price
}; };
} }
@@ -32,13 +36,16 @@ internal sealed class CompactNordPoolDayPriceConverter : JsonConverter<CompactNo
if (reader.TokenType != JsonTokenType.StartArray) if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException("Expected start of array."); throw new JsonException("Expected start of array.");
reader.Read(); if (!reader.Read() || reader.TokenType != JsonTokenType.Number)
throw new JsonException("Expected number for Start at position 0.");
var start = reader.GetInt64(); var start = reader.GetInt64();
reader.Read(); if (!reader.Read() || reader.TokenType != JsonTokenType.Number)
throw new JsonException("Expected number for End at position 1.");
var end = reader.GetInt64(); var end = reader.GetInt64();
reader.Read(); if (!reader.Read() || (reader.TokenType != JsonTokenType.Number && reader.TokenType != JsonTokenType.Null))
throw new JsonException("Expected number or null for Price at position 2.");
double? price = reader.TokenType == JsonTokenType.Null ? null : reader.GetDouble(); double? price = reader.TokenType == JsonTokenType.Null ? null : reader.GetDouble();
reader.Read(); reader.Read();