test: add compact format verification and size limit tests for NordPool entity attributes
Made-with: Cursor
This commit is contained in:
@@ -532,5 +532,82 @@ public class NordPoolEntityServiceTests
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_TodayTomorrowPrices_ShouldUseCompactFormat()
|
||||
{
|
||||
// Arrange
|
||||
var todayPrice = new NordPoolPrice
|
||||
{
|
||||
Prices = [new NordPoolDayPrice
|
||||
{
|
||||
Start = new DateTime(2026, 3, 19, 10, 0, 0, DateTimeKind.Utc),
|
||||
End = new DateTime(2026, 3, 19, 10, 15, 0, DateTimeKind.Utc),
|
||||
Price = 12.0
|
||||
}]
|
||||
};
|
||||
var context = CreateDefaultContext(todayPrice: todayPrice, tomorrowPrice: new NordPoolPrice { Prices = [] });
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.Is<object>(o =>
|
||||
o.GetType().GetProperty("prices")!.GetValue(o) is List<CompactNordPoolDayPrice>)),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_WithFullDayPrices_AttributesShouldBeUnderSizeLimit()
|
||||
{
|
||||
// Arrange
|
||||
const int intervalsPerDay = 96;
|
||||
const int haAttributeByteLimitBytes = 16_384;
|
||||
var todayStart = new DateTime(2026, 3, 19, 0, 0, 0, DateTimeKind.Utc);
|
||||
var tomorrowStart = new DateTime(2026, 3, 20, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
var todayPrices = Enumerable.Range(0, intervalsPerDay).Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = todayStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = todayStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 5.0 + i * 0.1
|
||||
}).ToList();
|
||||
|
||||
var tomorrowPrices = Enumerable.Range(0, intervalsPerDay).Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = tomorrowStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = tomorrowStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 8.0 + i * 0.1
|
||||
}).ToList();
|
||||
|
||||
object? capturedAttributes = null;
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_today_tomorrow_prices"), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var context = CreateDefaultContext(
|
||||
todayPrice: new NordPoolPrice { Prices = todayPrices },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = tomorrowPrices });
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
capturedAttributes.Should().NotBeNull();
|
||||
var json = System.Text.Json.JsonSerializer.Serialize(capturedAttributes);
|
||||
var byteCount = System.Text.Encoding.UTF8.GetByteCount(json);
|
||||
byteCount.Should().BeLessThan(haAttributeByteLimitBytes,
|
||||
$"today+tomorrow attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user