Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
@@ -8,6 +10,8 @@ using NordPool.Models;
|
||||
using NordPoolManager;
|
||||
using NordPoolManager.Models;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace NordPool.Manager.Tests;
|
||||
|
||||
public class NordPoolEntityServiceTests
|
||||
@@ -99,7 +103,7 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntitiesAsync("EUR/kWh");
|
||||
@@ -113,14 +117,16 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var callCount = 0;
|
||||
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()))
|
||||
.Returns(() =>
|
||||
{
|
||||
callCount++;
|
||||
return callCount == 2
|
||||
? throw new InvalidOperationException("Entity creation failed")
|
||||
: Task.CompletedTask;
|
||||
});
|
||||
.Returns(() =>
|
||||
{
|
||||
callCount++;
|
||||
|
||||
return callCount == 2
|
||||
? throw new InvalidOperationException("Entity creation failed")
|
||||
: Task.CompletedTask;
|
||||
});
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntitiesAsync("EUR/kWh");
|
||||
@@ -134,7 +140,7 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntitiesAsync(null);
|
||||
@@ -152,7 +158,7 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CleanupEntitiesAsync();
|
||||
@@ -172,7 +178,7 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Remove failed"));
|
||||
.ThrowsAsync(new InvalidOperationException("Remove failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.CleanupEntitiesAsync();
|
||||
@@ -190,7 +196,7 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.SetEntityStateSafe("sensor.test", "42");
|
||||
@@ -204,7 +210,7 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.SetEntityStateSafe("sensor.test", "42");
|
||||
@@ -222,8 +228,9 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var attributes = new { unit_of_measurement = "EUR/kWh" };
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.SetEntityAttributesSafe("sensor.test", attributes);
|
||||
@@ -237,8 +244,9 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var attributes = new { unit_of_measurement = "EUR/kWh" };
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Attributes update failed"));
|
||||
.ThrowsAsync(new InvalidOperationException("Attributes update failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.SetEntityAttributesSafe("sensor.test", attributes);
|
||||
@@ -256,20 +264,23 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_last_update_time", "unknown"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", ""), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", string.Empty), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "0"), Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", "unknown"),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_cheapest_intervals", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("button.nordpool_fetch_prices", "unknown"), Times.Once);
|
||||
}
|
||||
@@ -279,28 +290,35 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_last_update_time", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_prices", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_tomorrow_prices", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny<object>()), Times.Once);
|
||||
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny<object>()), Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.nordpool_cheapest_intervals", It.IsAny<object>()), Times.Once);
|
||||
em => em.SetEntityAttributes("sensor.nordpool_cheapest_intervals", It.IsAny<object>()), Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("button.nordpool_fetch_prices", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -308,12 +326,14 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState("sensor.nordpool_current_price", It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
|
||||
_entityManagerMock.Setup(em =>
|
||||
em.SetEntityState(It.Is<string>(id => id != "sensor.nordpool_current_price"), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
em.SetEntityState(It.Is<string>(id => id != "sensor.nordpool_current_price"), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
var act = () => _sut.ResetEntitiesAsync();
|
||||
@@ -327,17 +347,19 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange - 7 entities get state and attributes
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(7));
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()),
|
||||
Times.Exactly(7));
|
||||
Times.Exactly(7));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -356,14 +378,16 @@ public class NordPoolEntityServiceTests
|
||||
DayQuartersIndex = 5,
|
||||
LastApiUpdateDate = DateTime.UtcNow,
|
||||
UsingCachedData = false,
|
||||
TodayNordPoolPrice = todayPrice ?? new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
|
||||
},
|
||||
TomorrowNordPoolPrice = tomorrowPrice ?? new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new() { Price = 18.71 } }
|
||||
},
|
||||
TodayNordPoolPrice = todayPrice ??
|
||||
new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new NordPoolDayPrice { Price = 12.50 } }
|
||||
},
|
||||
TomorrowNordPoolPrice = tomorrowPrice ??
|
||||
new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new NordPoolDayPrice { Price = 18.71 } }
|
||||
},
|
||||
CheapestIntervals = [],
|
||||
ConfiguredHours = 2.0,
|
||||
ConfiguredIntervalCount = 4,
|
||||
@@ -382,24 +406,28 @@ public class NordPoolEntityServiceTests
|
||||
var context = CreateDefaultContext();
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_last_update_time", It.IsAny<string>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", "17.91"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "1"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "1"), Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices",
|
||||
It.Is<string>(s => s.Contains('-') && s.Contains(':'))), Times.Once);
|
||||
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices",
|
||||
It.Is<string>(s => s.Contains('-') && s.Contains(':'))), Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_cheapest_intervals", It.IsAny<string>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -409,22 +437,26 @@ public class NordPoolEntityServiceTests
|
||||
var context = CreateDefaultContext();
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_prices", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_tomorrow_prices", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
Times.Once);
|
||||
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny<object>()), Times.Once);
|
||||
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny<object>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -432,13 +464,14 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateDefaultContext(
|
||||
todayPrice: new NordPoolPrice { Prices = null },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = null });
|
||||
todayPrice: new NordPoolPrice { Prices = null },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = null });
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
@@ -446,12 +479,12 @@ public class NordPoolEntityServiceTests
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", It.Is<string>(s =>
|
||||
s.Length > 0 && s != "0" && s.Contains('-'))),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.nordpool_today_tomorrow_prices", It.Is<string>(s =>
|
||||
s.Length > 0 && s != "0" && s.Contains('-'))),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_WithMergedPriceScenarios_ShouldPublishSortedFilteredPrices()
|
||||
@@ -519,16 +552,19 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
object? capturedAttributes = null;
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.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);
|
||||
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);
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_today_tomorrow_prices"),
|
||||
It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var context = CreateDefaultContext() with
|
||||
{
|
||||
@@ -566,21 +602,23 @@ public class NordPoolEntityServiceTests
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
var context = CreateDefaultContext(todayPrice: todayPrice, tomorrowPrice: new NordPoolPrice { Prices = [] });
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.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);
|
||||
em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.Is<object>(o =>
|
||||
o.GetType().GetProperty("prices")!.GetValue(o) is List<CompactNordPoolDayPrice>)),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -592,35 +630,42 @@ public class NordPoolEntityServiceTests
|
||||
var todayStart = new DateTime(2026, 3, 31, 0, 0, 0, DateTimeKind.Utc);
|
||||
var tomorrowStart = new DateTime(2026, 4, 1, 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 = 4.93 + i * 0.22
|
||||
}).ToList();
|
||||
var todayPrices = Enumerable.Range(0, intervalsPerDay)
|
||||
.Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = todayStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = todayStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 4.93 + i * 0.22
|
||||
})
|
||||
.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 = 5.50 + i * 0.22
|
||||
}).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 = 5.50 + i * 0.22
|
||||
})
|
||||
.ToList();
|
||||
|
||||
object? capturedAttributes = null;
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.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);
|
||||
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);
|
||||
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 });
|
||||
todayPrice: new NordPoolPrice { Prices = todayPrices },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = tomorrowPrices });
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
@@ -629,8 +674,10 @@ public class NordPoolEntityServiceTests
|
||||
capturedAttributes.Should().NotBeNull();
|
||||
var json = JsonSerializer.Serialize(capturedAttributes);
|
||||
var byteCount = Encoding.UTF8.GetByteCount(json);
|
||||
byteCount.Should().BeLessThan(haAttributeByteLimitBytes,
|
||||
$"today+tomorrow attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit");
|
||||
|
||||
byteCount.Should()
|
||||
.BeLessThan(haAttributeByteLimitBytes,
|
||||
$"today+tomorrow attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -640,15 +687,16 @@ public class NordPoolEntityServiceTests
|
||||
[InlineData(1234.5, "1234.50")]
|
||||
[InlineData(17.916, "17.92")]
|
||||
public async Task UpdateEntitiesAsync_ShouldFormatCurrentPriceWithTwoDecimalsAndInvariantCulture(double price,
|
||||
string expected)
|
||||
string expected)
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateDefaultContext(price);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
@@ -662,14 +710,17 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
object? capturedAttributes = null;
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em =>
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_current_price"), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_current_price"), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var context = CreateDefaultContext() with
|
||||
{
|
||||
@@ -700,15 +751,18 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
object? capturedAttributes = null;
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes("sensor.nordpool_cheapest_intervals", It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em =>
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_cheapest_intervals"),
|
||||
It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_cheapest_intervals"),
|
||||
It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var first = new CheapestIntervalResult
|
||||
{
|
||||
@@ -717,6 +771,7 @@ public class NordPoolEntityServiceTests
|
||||
Price = 4.93,
|
||||
Rank = 1
|
||||
};
|
||||
|
||||
var second = new CheapestIntervalResult
|
||||
{
|
||||
Start = new DateTime(2026, 3, 31, 23, 0, 0, DateTimeKind.Utc),
|
||||
@@ -746,8 +801,10 @@ public class NordPoolEntityServiceTests
|
||||
capturedAttributes.Should().NotBeNull();
|
||||
var attributes = capturedAttributes!;
|
||||
var type = attributes.GetType();
|
||||
|
||||
((IEnumerable<CheapestIntervalResult>)type.GetProperty("intervals")!.GetValue(attributes)!).Should()
|
||||
.HaveCount(2);
|
||||
.HaveCount(2);
|
||||
|
||||
type.GetProperty("total_heating_minutes")!.GetValue(attributes).Should().Be(30);
|
||||
type.GetProperty("is_now_cheap")!.GetValue(attributes).Should().Be(true);
|
||||
type.GetProperty("next_cheap_interval")!.GetValue(attributes).Should().Be(first.Start.ToString("O"));
|
||||
@@ -763,15 +820,18 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
object? capturedAttributes = null;
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes("sensor.nordpool_cheapest_intervals", It.IsAny<object>()))
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
.Callback<string, object>((_, attrs) => capturedAttributes = attrs)
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em =>
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_cheapest_intervals"),
|
||||
It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_cheapest_intervals"),
|
||||
It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var context = CreateDefaultContext() with { CheapestIntervals = null };
|
||||
|
||||
@@ -794,18 +854,22 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
object? capturedAttributes = null;
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.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);
|
||||
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);
|
||||
em.SetEntityAttributes(It.Is<string>(id => id != "sensor.nordpool_today_tomorrow_prices"),
|
||||
It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var dayStart = new DateTime(2026, 3, 31, 0, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
var todayPrice = new NordPoolPrice
|
||||
{
|
||||
Prices =
|
||||
@@ -815,6 +879,7 @@ public class NordPoolEntityServiceTests
|
||||
new NordPoolDayPrice { Start = null, End = dayStart.AddHours(3).AddMinutes(15), Price = 1.0 }
|
||||
]
|
||||
};
|
||||
|
||||
var tomorrowPrice = new NordPoolPrice
|
||||
{
|
||||
Prices =
|
||||
@@ -823,6 +888,7 @@ public class NordPoolEntityServiceTests
|
||||
{ Start = dayStart.AddHours(0.5), End = dayStart.AddHours(0.5).AddMinutes(15), Price = 18.71 }
|
||||
]
|
||||
};
|
||||
|
||||
var context = CreateDefaultContext(todayPrice: todayPrice, tomorrowPrice: tomorrowPrice);
|
||||
|
||||
// Act
|
||||
@@ -843,42 +909,49 @@ public class NordPoolEntityServiceTests
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
var todayStart = new DateTime(2026, 3, 31, 0, 0, 0, DateTimeKind.Utc);
|
||||
var tomorrowStart = new DateTime(2026, 4, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
var todayPrices = Enumerable.Range(0, 100).Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = todayStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = todayStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 10.0
|
||||
}).ToList();
|
||||
var tomorrowPrices = Enumerable.Range(0, 100).Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = tomorrowStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = tomorrowStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 10.0
|
||||
}).ToList();
|
||||
|
||||
var todayPrices = Enumerable.Range(0, 100)
|
||||
.Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = todayStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = todayStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 10.0
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var tomorrowPrices = Enumerable.Range(0, 100)
|
||||
.Select(i => new NordPoolDayPrice
|
||||
{
|
||||
Start = tomorrowStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval),
|
||||
End = tomorrowStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval),
|
||||
Price = 10.0
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var context = CreateDefaultContext(
|
||||
todayPrice: new NordPoolPrice { Prices = todayPrices },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = tomorrowPrices });
|
||||
todayPrice: new NordPoolPrice { Prices = todayPrices },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = tomorrowPrices });
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_loggerMock.Verify(
|
||||
l => l.Log(
|
||||
LogLevel.Warning,
|
||||
It.IsAny<EventId>(),
|
||||
It.Is<It.IsAnyType>((state, _) =>
|
||||
state != null && state.ToString()!.Contains("exceeds expected maximum")),
|
||||
It.IsAny<Exception?>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
|
||||
Times.Once);
|
||||
l => l.Log(
|
||||
LogLevel.Warning,
|
||||
It.IsAny<EventId>(),
|
||||
It.Is<It.IsAnyType>((state, _) =>
|
||||
state != null && state.ToString()!.Contains("exceeds expected maximum")),
|
||||
It.IsAny<Exception?>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -895,6 +968,7 @@ public class NordPoolEntityServiceTests
|
||||
data.Add("reset entities", sut => sut.ResetEntitiesAsync(cancelled));
|
||||
data.Add("update entities", sut => sut.UpdateEntitiesAsync(context, cancelled));
|
||||
data.Add("cleanup entities", sut => sut.CleanupEntitiesAsync(cancelled));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user