24729ee267
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
296 lines
9.6 KiB
C#
296 lines
9.6 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using FluentAssertions;
|
|
using NordPool.Models;
|
|
using static NordPool.Models.NordPoolConsts;
|
|
|
|
namespace NordPool.Manager.Tests;
|
|
|
|
public class CompactNordPoolDayPriceTests
|
|
{
|
|
private const int IntervalDurationSeconds = MinutesPerInterval * 60;
|
|
|
|
private static readonly DateTime TestStart = new(2026, 3, 31, 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();
|
|
|
|
public static TheoryData<string, string> InvalidJsonPayloads => new()
|
|
{
|
|
{ "empty array", "[]" },
|
|
{ "too few elements", "[1,2]" },
|
|
{ "extra elements", "[1,2,3,4]" },
|
|
{ "missing end of array", "[1,2,3" },
|
|
{ "non-numeric start", "[\"abc\",2,3]" },
|
|
{ "non-numeric end", "[1,\"abc\",3]" },
|
|
{ "non-numeric price", "[1,2,\"abc\"]" },
|
|
{ "object instead of array", "{\"start\":1}" }
|
|
};
|
|
|
|
[Fact]
|
|
public void FromDayPrice_ShouldConvertDateTimesToUnixEpochSeconds()
|
|
{
|
|
var dayPrice = new NordPoolDayPrice { Start = TestStart, End = TestEnd, Price = 17.91 };
|
|
|
|
var result = CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
|
|
|
result.Start.Should().Be(TestStartEpoch);
|
|
result.End.Should().Be(TestEndEpoch);
|
|
result.Price.Should().Be(17.91);
|
|
}
|
|
|
|
[Fact]
|
|
public void FromDayPrice_WhenStartIsNull_ShouldUseZeroEpoch()
|
|
{
|
|
var dayPrice = new NordPoolDayPrice { Start = null, End = TestEnd, Price = 8.96 };
|
|
|
|
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 = 8.96 };
|
|
|
|
var result = CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
|
|
|
result.Start.Should().Be(TestStartEpoch);
|
|
result.End.Should().Be(0L);
|
|
}
|
|
|
|
[Fact]
|
|
public void FromDayPrice_WhenDateTimeKindIsLocal_ShouldConvertToUtcWithoutThrowing()
|
|
{
|
|
var localStart = new DateTime(2026, 3, 31, 12, 0, 0, DateTimeKind.Local);
|
|
var localEnd = new DateTime(2026, 3, 31, 12, 15, 0, DateTimeKind.Local);
|
|
var dayPrice = new NordPoolDayPrice { Start = localStart, End = localEnd, Price = 12.50 };
|
|
|
|
var act = () => CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
|
|
|
act.Should().NotThrow();
|
|
var result = act();
|
|
var expectedStart = new DateTimeOffset(localStart.ToUniversalTime(), TimeSpan.Zero).ToUnixTimeSeconds();
|
|
result.Start.Should().Be(expectedStart);
|
|
}
|
|
|
|
[Fact]
|
|
public void FromDayPrice_WhenDateTimeKindIsUnspecified_ShouldConvertWithoutThrowing()
|
|
{
|
|
var unspecifiedStart = new DateTime(2026, 3, 31, 12, 0, 0, DateTimeKind.Unspecified);
|
|
var unspecifiedEnd = new DateTime(2026, 3, 31, 12, 15, 0, DateTimeKind.Unspecified);
|
|
var dayPrice = new NordPoolDayPrice { Start = unspecifiedStart, End = unspecifiedEnd, Price = 12.50 };
|
|
|
|
var act = () => CompactNordPoolDayPrice.FromDayPrice(dayPrice);
|
|
|
|
act.Should().NotThrow();
|
|
}
|
|
|
|
[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 = 17.91
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(compact);
|
|
|
|
json.Should().Be($"[{TestStartEpoch},{TestEndEpoch},17.91]");
|
|
}
|
|
|
|
[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]");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(17.91)]
|
|
[InlineData(-12.34)]
|
|
[InlineData(0.0)]
|
|
[InlineData(0.001)]
|
|
[InlineData(17.9159)]
|
|
[InlineData(null)]
|
|
public void JsonDeserialization_ShouldRoundTripPrice(double? price)
|
|
{
|
|
var original = new CompactNordPoolDayPrice
|
|
{
|
|
Start = TestStartEpoch,
|
|
End = TestEndEpoch,
|
|
Price = price
|
|
};
|
|
|
|
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 = 12.50 },
|
|
new() { Start = TestEndEpoch, End = TestEndEpoch + IntervalDurationSeconds, Price = 13.26 }
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(prices);
|
|
|
|
json.Should()
|
|
.Be(
|
|
$"[[{TestStartEpoch},{TestEndEpoch},12.5],[{TestEndEpoch},{TestEndEpoch + IntervalDurationSeconds},13.26]]");
|
|
}
|
|
|
|
[Fact]
|
|
public void FromDayPrice_WhenNull_ShouldThrowArgumentNullException()
|
|
{
|
|
var act = () => CompactNordPoolDayPrice.FromDayPrice(null!);
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(InvalidJsonPayloads))]
|
|
public void JsonDeserialization_WithInvalidPayload_ShouldThrowJsonException(string scenario, string json)
|
|
{
|
|
var act = () => JsonSerializer.Deserialize<CompactNordPoolDayPrice>(json);
|
|
|
|
act.Should().Throw<JsonException>($"scenario: {scenario}");
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonSerialization_EmptyList_ShouldProduceEmptyArray()
|
|
{
|
|
var json = JsonSerializer.Serialize(new List<CompactNordPoolDayPrice>());
|
|
|
|
json.Should().Be("[]");
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCompact_ShouldConvertAllPrices()
|
|
{
|
|
var prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = TestStart, End = TestEnd, Price = 12.50 },
|
|
new() { Start = TestEnd, End = TestEnd.AddMinutes(MinutesPerInterval), Price = 13.26 }
|
|
};
|
|
|
|
var result = prices.ToCompact();
|
|
|
|
result.Should().HaveCount(2);
|
|
result[0].Start.Should().Be(TestStartEpoch);
|
|
result[0].Price.Should().Be(12.50);
|
|
result[1].Price.Should().Be(13.26);
|
|
}
|
|
|
|
[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 ToCompact_WithGeneratedDay_ShouldPreserveOrderAndCount()
|
|
{
|
|
var prices = NordPoolTestData.CreateIntervals(96, i => 4.93 + i * 0.22);
|
|
|
|
var result = prices.ToCompact();
|
|
|
|
result.Should().HaveCount(96);
|
|
result.Should().BeInAscendingOrder(p => p.Start);
|
|
result[0].Start.Should().Be(new DateTimeOffset(NordPoolTestData.DayStart).ToUnixTimeSeconds());
|
|
result[95].Start.Should().Be(new DateTimeOffset(NordPoolTestData.DayStart.AddMinutes(95 * MinutesPerInterval))
|
|
.ToUnixTimeSeconds());
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonSerialization_WithGeneratedDay_ShouldRoundTripAllEntries()
|
|
{
|
|
var compact = NordPoolTestData.CreateGeneratedDay().ToCompact();
|
|
|
|
var json = JsonSerializer.Serialize(compact);
|
|
var roundTripped = JsonSerializer.Deserialize<List<CompactNordPoolDayPrice>>(json);
|
|
|
|
roundTripped.Should().NotBeNull();
|
|
roundTripped.Should().HaveCount(compact.Count);
|
|
roundTripped.Should().BeEquivalentTo(compact);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCompact_ShouldPreserveNullPriceAndNullTimestamps()
|
|
{
|
|
var prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = null, End = null, Price = null }
|
|
};
|
|
|
|
var result = prices.ToCompact();
|
|
|
|
result.Should().ContainSingle();
|
|
result[0].Start.Should().Be(0L);
|
|
result[0].End.Should().Be(0L);
|
|
result[0].Price.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void SerializedSize_WithMaxEntries_ShouldBeUnderHaAttributeLimit()
|
|
{
|
|
const int haAttributeByteLimitBytes = 16_384;
|
|
var prices = NordPoolTestData.CreateIntervals(MaxExpectedPriceEntries, i => 4.93 + i * 0.22);
|
|
|
|
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 = Encoding.UTF8.GetByteCount(json);
|
|
|
|
byteCount.Should().BeLessThan(haAttributeByteLimitBytes,
|
|
$"serialized attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit");
|
|
}
|
|
} |