Files
HomeAssistant/NordPool.Manager.Tests/CompactNordPoolDayPriceTests.cs
T
2026-03-19 16:21:49 +02:00

226 lines
6.9 KiB
C#

using System.Text.Json;
using FluentAssertions;
using NordPool.Models;
using static NordPool.Models.NordPoolConsts;
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 = TestStart.AddMinutes(MinutesPerInterval);
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 + FifteenMinutesInSeconds, Price = 11.0 }
};
var json = JsonSerializer.Serialize(prices);
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>();
}
[Fact]
public void ToCompact_ShouldConvertAllPrices()
{
var prices = new List<NordPoolDayPrice>
{
new() { Start = TestStart, End = TestEnd, Price = 10.0 },
new() { Start = TestEnd, End = TestEnd.AddMinutes(MinutesPerInterval), Price = 11.0 }
};
var result = prices.ToCompact();
result.Should().HaveCount(2);
result[0].Start.Should().Be(TestStartEpoch);
result[0].Price.Should().Be(10.0);
result[1].Price.Should().Be(11.0);
}
[Fact]
public void ToCompact_WhenEmpty_ShouldReturnEmptyList()
{
var prices = Array.Empty<NordPoolDayPrice>();
var result = prices.ToCompact();
result.Should().BeEmpty();
}
[Fact]
public void ToCompact_WhenNull_ShouldReturnEmptyList()
{
IEnumerable<NordPoolDayPrice>? prices = null;
var result = prices.ToCompact();
result.Should().BeEmpty();
}
[Fact]
public void SerializedSize_WithMaxEntries_ShouldBeUnderHaAttributeLimit()
{
const int maxEntries = 192;
const int haAttributeByteLimitBytes = 16_384;
var dayStart = new DateTime(2026, 3, 19, 0, 0, 0, DateTimeKind.Utc);
var prices = new List<NordPoolDayPrice>();
for (var i = 0; i < maxEntries; i++)
{
var start = dayStart.AddMinutes(i * MinutesPerInterval);
prices.Add(new NordPoolDayPrice
{
Start = start,
End = start.AddMinutes(MinutesPerInterval),
Price = 10.0 + i * 0.01
});
}
var compactPrices = prices.ToCompact();
var attributePayload = new
{
prices = compactPrices,
prices_count = compactPrices.Count,
friendly_name = "NordPool today and tomorrow prices",
last_updated = DateTime.UtcNow.ToString("O")
};
var json = JsonSerializer.Serialize(attributePayload);
var byteCount = System.Text.Encoding.UTF8.GetByteCount(json);
byteCount.Should().BeLessThan(haAttributeByteLimitBytes,
$"serialized attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit");
}
}