feat: add CompactNordPoolDayPrice with array-format JSON serialization
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using NordPool.Models;
|
||||
|
||||
namespace NordPool.Manager.Tests;
|
||||
|
||||
public class CompactNordPoolDayPriceTests
|
||||
{
|
||||
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();
|
||||
private static readonly long TestEndEpoch = new DateTimeOffset(TestEnd).ToUnixTimeSeconds();
|
||||
|
||||
[Fact]
|
||||
public void FromDayPrice_ShouldConvertDateTimesToUnixEpochSeconds()
|
||||
{
|
||||
var dayPrice = new NordPoolDayPrice { Start = TestStart, End = TestEnd, Price = 12.35 };
|
||||
|
||||
var result = CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
||||
|
||||
result.Start.Should().Be(TestStartEpoch);
|
||||
result.End.Should().Be(TestEndEpoch);
|
||||
result.Price.Should().Be(12.35);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromDayPrice_WhenStartIsNull_ShouldUseZeroEpoch()
|
||||
{
|
||||
var dayPrice = new NordPoolDayPrice { Start = null, End = TestEnd, Price = 5.0 };
|
||||
|
||||
var result = CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
||||
|
||||
result.Start.Should().Be(0L);
|
||||
result.End.Should().Be(TestEndEpoch);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromDayPrice_WhenEndIsNull_ShouldUseZeroEpoch()
|
||||
{
|
||||
var dayPrice = new NordPoolDayPrice { Start = TestStart, End = null, Price = 5.0 };
|
||||
|
||||
var result = CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
||||
|
||||
result.Start.Should().Be(TestStartEpoch);
|
||||
result.End.Should().Be(0L);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FromDayPrice_WhenPriceIsNull_ShouldPreserveNull()
|
||||
{
|
||||
var dayPrice = new NordPoolDayPrice { Start = TestStart, End = TestEnd, Price = null };
|
||||
|
||||
var result = CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
||||
|
||||
result.Price.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_ShouldProduceArrayFormat()
|
||||
{
|
||||
var compact = new CompactNordPoolDayPrice
|
||||
{
|
||||
Start = TestStartEpoch,
|
||||
End = TestEndEpoch,
|
||||
Price = 12.35
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(compact);
|
||||
|
||||
json.Should().Be($"[{TestStartEpoch},{TestEndEpoch},12.35]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_WhenPriceIsNull_ShouldSerializeNullElement()
|
||||
{
|
||||
var compact = new CompactNordPoolDayPrice
|
||||
{
|
||||
Start = TestStartEpoch,
|
||||
End = TestEndEpoch,
|
||||
Price = null
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(compact);
|
||||
|
||||
json.Should().Be($"[{TestStartEpoch},{TestEndEpoch},null]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonDeserialization_ShouldRoundTrip()
|
||||
{
|
||||
var original = new CompactNordPoolDayPrice
|
||||
{
|
||||
Start = TestStartEpoch,
|
||||
End = TestEndEpoch,
|
||||
Price = 12.35
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(original);
|
||||
var deserialized = JsonSerializer.Deserialize<CompactNordPoolDayPrice>(json);
|
||||
|
||||
deserialized.Start.Should().Be(original.Start);
|
||||
deserialized.End.Should().Be(original.End);
|
||||
deserialized.Price.Should().Be(original.Price);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_ListOfCompactPrices_ShouldProduceArrayOfArrays()
|
||||
{
|
||||
var prices = new List<CompactNordPoolDayPrice>
|
||||
{
|
||||
new() { Start = TestStartEpoch, End = TestEndEpoch, Price = 10.0 },
|
||||
new() { Start = TestEndEpoch, End = TestEndEpoch + 900, Price = 11.0 }
|
||||
};
|
||||
|
||||
var json = JsonSerializer.Serialize(prices);
|
||||
|
||||
json.Should().Be($"[[{TestStartEpoch},{TestEndEpoch},10],[{TestEndEpoch},{TestEndEpoch + 900},11]]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NordPool.Models;
|
||||
|
||||
[JsonConverter(typeof(CompactNordPoolDayPriceConverter))]
|
||||
public readonly struct CompactNordPoolDayPrice
|
||||
{
|
||||
public long Start { get; init; }
|
||||
public long End { get; init; }
|
||||
public double? Price { get; init; }
|
||||
|
||||
public static CompactNordPoolDayPrice FromDayPrice(NordPoolDayPrice dayPrice)
|
||||
{
|
||||
return new CompactNordPoolDayPrice
|
||||
{
|
||||
Start = dayPrice.Start.HasValue
|
||||
? new DateTimeOffset(dayPrice.Start.Value, TimeSpan.Zero).ToUnixTimeSeconds()
|
||||
: 0L,
|
||||
End = dayPrice.End.HasValue
|
||||
? new DateTimeOffset(dayPrice.End.Value, TimeSpan.Zero).ToUnixTimeSeconds()
|
||||
: 0L,
|
||||
Price = dayPrice.Price
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class CompactNordPoolDayPriceConverter : JsonConverter<CompactNordPoolDayPrice>
|
||||
{
|
||||
public override CompactNordPoolDayPrice Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType != JsonTokenType.StartArray)
|
||||
throw new JsonException("Expected start of array.");
|
||||
|
||||
reader.Read();
|
||||
var start = reader.GetInt64();
|
||||
|
||||
reader.Read();
|
||||
var end = reader.GetInt64();
|
||||
|
||||
reader.Read();
|
||||
double? price = reader.TokenType == JsonTokenType.Null ? null : reader.GetDouble();
|
||||
|
||||
reader.Read();
|
||||
if (reader.TokenType != JsonTokenType.EndArray)
|
||||
throw new JsonException("Expected end of array.");
|
||||
|
||||
return new CompactNordPoolDayPrice { Start = start, End = end, Price = price };
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, CompactNordPoolDayPrice value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
writer.WriteNumberValue(value.Start);
|
||||
writer.WriteNumberValue(value.End);
|
||||
|
||||
if (value.Price.HasValue)
|
||||
writer.WriteNumberValue(value.Price.Value);
|
||||
else
|
||||
writer.WriteNullValue();
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user