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]]");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user