Clear the residual Qodana findings
- Drop the leftover unused using, parameter and date-time attribute reader - Merge the remaining enum guard into its pattern - Make the NordPool price DTO init-only - Replace the four non-serializable theory matrices with single tests that iterate the same rows, so xUnit can still report them - Replace Array.Empty with collection expressions and drop unused locals, redundant default arguments and duplicated tuple names - Throw from the unreachable shower default instead of an empty section - Cover the configurable PowerMonitor option overrides in a test so the init setters have a caller
This commit is contained in:
@@ -432,84 +432,79 @@ public class NordPoolEntityServiceTests
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
public static TheoryData<string, NordPoolPrice?, NordPoolPrice?, int> MergedPriceScenarios()
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_WithMergedPriceScenarios_ShouldPublishSortedFilteredPrices()
|
||||
{
|
||||
var data = new TheoryData<string, NordPoolPrice?, NordPoolPrice?, int>();
|
||||
// Arrange - xUnit cannot serialize NordPoolPrice rows, so the matrix is exercised in one test
|
||||
var todayStart = new DateTime(2026, 3, 31, 6, 0, 0, DateTimeKind.Utc);
|
||||
var tomorrowStart = new DateTime(2026, 4, 1, 6, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
data.Add(
|
||||
"today and tomorrow with null-start entry filtered",
|
||||
new NordPoolPrice
|
||||
{
|
||||
Prices =
|
||||
[
|
||||
new NordPoolDayPrice { Start = todayStart.AddHours(2), End = todayStart.AddHours(2).AddMinutes(15), Price = 17.91 },
|
||||
new NordPoolDayPrice { Start = null, End = todayStart.AddHours(3).AddMinutes(15), Price = 1.0 }
|
||||
]
|
||||
},
|
||||
new NordPoolPrice
|
||||
{
|
||||
Prices = [new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 18.71 }]
|
||||
},
|
||||
2
|
||||
);
|
||||
(string Scenario, NordPoolPrice? TodayPrice, NordPoolPrice? TomorrowPrice, int ExpectedMergedCount)[] cases =
|
||||
[
|
||||
(
|
||||
"today and tomorrow with null-start entry filtered",
|
||||
new NordPoolPrice
|
||||
{
|
||||
Prices =
|
||||
[
|
||||
new NordPoolDayPrice { Start = todayStart.AddHours(2), End = todayStart.AddHours(2).AddMinutes(15), Price = 17.91 },
|
||||
new NordPoolDayPrice { Start = null, End = todayStart.AddHours(3).AddMinutes(15), Price = 1.0 }
|
||||
]
|
||||
},
|
||||
new NordPoolPrice
|
||||
{
|
||||
Prices = [new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 18.71 }]
|
||||
},
|
||||
2
|
||||
),
|
||||
(
|
||||
"only today",
|
||||
new NordPoolPrice { Prices = [new NordPoolDayPrice { Start = todayStart, End = todayStart.AddMinutes(15), Price = 17.91 }] },
|
||||
new NordPoolPrice { Prices = [] },
|
||||
1
|
||||
),
|
||||
(
|
||||
"only tomorrow",
|
||||
new NordPoolPrice { Prices = [] },
|
||||
new NordPoolPrice { Prices = [new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 18.71 }] },
|
||||
1
|
||||
),
|
||||
("empty lists", new NordPoolPrice { Prices = [] }, new NordPoolPrice { Prices = [] }, 0),
|
||||
("null lists", new NordPoolPrice { Prices = null }, new NordPoolPrice { Prices = null }, 0),
|
||||
("null price objects", null, null, 0)
|
||||
];
|
||||
|
||||
data.Add(
|
||||
"only today",
|
||||
new NordPoolPrice { Prices = [new NordPoolDayPrice { Start = todayStart, End = todayStart.AddMinutes(15), Price = 17.91 }] },
|
||||
new NordPoolPrice { Prices = [] },
|
||||
1
|
||||
);
|
||||
|
||||
data.Add(
|
||||
"only tomorrow",
|
||||
new NordPoolPrice { Prices = [] },
|
||||
new NordPoolPrice { Prices = [new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 18.71 }] },
|
||||
1
|
||||
);
|
||||
|
||||
data.Add("empty lists", new NordPoolPrice { Prices = [] }, new NordPoolPrice { Prices = [] }, 0);
|
||||
data.Add("null lists", new NordPoolPrice { Prices = null }, new NordPoolPrice { Prices = null }, 0);
|
||||
data.Add("null price objects", null, null, 0);
|
||||
return data;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(MergedPriceScenarios))]
|
||||
public async Task UpdateEntitiesAsync_WithMergedPriceScenarios_ShouldPublishSortedFilteredPrices(
|
||||
string scenario,
|
||||
NordPoolPrice? todayPrice,
|
||||
NordPoolPrice? tomorrowPrice,
|
||||
int expectedMergedCount)
|
||||
{
|
||||
// Arrange
|
||||
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() with
|
||||
foreach (var (scenario, todayPrice, tomorrowPrice, expectedMergedCount) in cases)
|
||||
{
|
||||
TodayNordPoolPrice = todayPrice,
|
||||
TomorrowNordPoolPrice = tomorrowPrice
|
||||
};
|
||||
// Arrange
|
||||
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);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
var context = CreateDefaultContext() with
|
||||
{
|
||||
TodayNordPoolPrice = todayPrice,
|
||||
TomorrowNordPoolPrice = tomorrowPrice
|
||||
};
|
||||
|
||||
// Assert
|
||||
capturedAttributes.Should().NotBeNull();
|
||||
var attributes = capturedAttributes!;
|
||||
var type = attributes.GetType();
|
||||
var prices = (List<CompactNordPoolDayPrice>)type.GetProperty("prices")!.GetValue(attributes)!;
|
||||
prices.Should().HaveCount(expectedMergedCount, $"scenario: {scenario}");
|
||||
prices.Select(p => p.Start).Should().BeInAscendingOrder();
|
||||
type.GetProperty("prices_count")!.GetValue(attributes).Should().Be(expectedMergedCount);
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
capturedAttributes.Should().NotBeNull();
|
||||
var attributes = capturedAttributes!;
|
||||
var type = attributes.GetType();
|
||||
var prices = (List<CompactNordPoolDayPrice>)type.GetProperty("prices")!.GetValue(attributes)!;
|
||||
prices.Should().HaveCount(expectedMergedCount, $"scenario: {scenario}");
|
||||
prices.Select(p => p.Start).Should().BeInAscendingOrder();
|
||||
type.GetProperty("prices_count")!.GetValue(attributes).Should().Be(expectedMergedCount);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user