Convert test data sources to typed TheoryData
Replace untyped MemberData sources with TheoryData<...>, use collection expressions for array and list literals, drop redundant casts and null suppressions, and give log-assertion helpers an IsEnabled setup so guarded log calls stay verifiable.
This commit is contained in:
@@ -140,24 +140,24 @@ public class CostAccumulatorTests
|
||||
|
||||
#region Window rollovers
|
||||
|
||||
public static IEnumerable<object[]> WindowRolloverCases =>
|
||||
[
|
||||
["15 minute slot ends", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 14, 0), new DateTime(2026, 3, 17, 10, 15, 0), 0.10, 0.02],
|
||||
["15 minute slot ends across the hour", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.10, 0.02],
|
||||
["15 minute slot ends at midnight", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.10, 0.02],
|
||||
["15 minute slot ends on DST spring forward", CostWindow.FifteenMinutes, new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.10, 0.02],
|
||||
["15 minute slot ends on DST fall back", CostWindow.FifteenMinutes, new DateTime(2026, 10, 25, 2, 45, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.10, 0.02],
|
||||
["hour ends", CostWindow.Hour, new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.50, 0.01],
|
||||
["hour ends at midnight", CostWindow.Hour, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.50, 0.01],
|
||||
["hour ends on DST spring forward", CostWindow.Hour, new DateTime(2026, 3, 29, 2, 59, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.50, 0.01],
|
||||
["hour ends on DST fall back", CostWindow.Hour, new DateTime(2026, 10, 25, 2, 59, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.50, 0.01],
|
||||
["day changes", CostWindow.Day, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 5.00, 0.01],
|
||||
["day changes at month end", CostWindow.Day, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 5.00, 0.01],
|
||||
["day changes at year end", CostWindow.Day, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 5.00, 0.01],
|
||||
["month changes", CostWindow.Month, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 100.00, 0.01],
|
||||
["year changes", CostWindow.Month, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 100.00, 0.01],
|
||||
["month changes on leap day", CostWindow.Month, new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), 100.00, 0.01]
|
||||
];
|
||||
public static TheoryData<string, CostWindow, DateTime, DateTime, double, double> WindowRolloverCases => new()
|
||||
{
|
||||
{ "15 minute slot ends", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 14, 0), new DateTime(2026, 3, 17, 10, 15, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends across the hour", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends at midnight", CostWindow.FifteenMinutes, new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends on DST spring forward", CostWindow.FifteenMinutes, new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.10, 0.02 },
|
||||
{ "15 minute slot ends on DST fall back", CostWindow.FifteenMinutes, new DateTime(2026, 10, 25, 2, 45, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.10, 0.02 },
|
||||
{ "hour ends", CostWindow.Hour, new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 0, 0), 0.50, 0.01 },
|
||||
{ "hour ends at midnight", CostWindow.Hour, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 0.50, 0.01 },
|
||||
{ "hour ends on DST spring forward", CostWindow.Hour, new DateTime(2026, 3, 29, 2, 59, 0), new DateTime(2026, 3, 29, 3, 0, 0), 0.50, 0.01 },
|
||||
{ "hour ends on DST fall back", CostWindow.Hour, new DateTime(2026, 10, 25, 2, 59, 0), new DateTime(2026, 10, 25, 3, 0, 0), 0.50, 0.01 },
|
||||
{ "day changes", CostWindow.Day, new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), 5.00, 0.01 },
|
||||
{ "day changes at month end", CostWindow.Day, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 5.00, 0.01 },
|
||||
{ "day changes at year end", CostWindow.Day, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 5.00, 0.01 },
|
||||
{ "month changes", CostWindow.Month, new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), 100.00, 0.01 },
|
||||
{ "year changes", CostWindow.Month, new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), 100.00, 0.01 },
|
||||
{ "month changes on leap day", CostWindow.Month, new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), 100.00, 0.01 }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(WindowRolloverCases))]
|
||||
@@ -227,19 +227,19 @@ public class CostAccumulatorTests
|
||||
|
||||
#region Window boundary detection (static methods)
|
||||
|
||||
public static IEnumerable<object[]> FifteenMinuteBoundaryCases =>
|
||||
[
|
||||
["same slot, first second", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false],
|
||||
["same slot, last second", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 14, 59), false],
|
||||
["slot boundary 14 -> 15", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 15, 0), true],
|
||||
["slot boundary 29 -> 30", new DateTime(2026, 3, 17, 10, 29, 0), new DateTime(2026, 3, 17, 10, 30, 0), true],
|
||||
["slot boundary 44 -> 45", new DateTime(2026, 3, 17, 10, 44, 0), new DateTime(2026, 3, 17, 10, 45, 0), true],
|
||||
["same slot, late", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 10, 59, 59), false],
|
||||
["hour boundary", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), true],
|
||||
["midnight boundary", new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), true],
|
||||
["clock moved backwards", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 10, 44, 0), true],
|
||||
["DST spring forward", new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), true]
|
||||
];
|
||||
public static TheoryData<string, DateTime, DateTime, bool> FifteenMinuteBoundaryCases => new()
|
||||
{
|
||||
{ "same slot, first second", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false },
|
||||
{ "same slot, last second", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 14, 59), false },
|
||||
{ "slot boundary 14 -> 15", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 15, 0), true },
|
||||
{ "slot boundary 29 -> 30", new DateTime(2026, 3, 17, 10, 29, 0), new DateTime(2026, 3, 17, 10, 30, 0), true },
|
||||
{ "slot boundary 44 -> 45", new DateTime(2026, 3, 17, 10, 44, 0), new DateTime(2026, 3, 17, 10, 45, 0), true },
|
||||
{ "same slot, late", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 10, 59, 59), false },
|
||||
{ "hour boundary", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 11, 0, 0), true },
|
||||
{ "midnight boundary", new DateTime(2026, 3, 17, 23, 45, 0), new DateTime(2026, 3, 18, 0, 0, 0), true },
|
||||
{ "clock moved backwards", new DateTime(2026, 3, 17, 10, 45, 0), new DateTime(2026, 3, 17, 10, 44, 0), true },
|
||||
{ "DST spring forward", new DateTime(2026, 3, 29, 2, 45, 0), new DateTime(2026, 3, 29, 3, 0, 0), true }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(FifteenMinuteBoundaryCases))]
|
||||
@@ -248,18 +248,18 @@ public class CostAccumulatorTests
|
||||
CostAccumulator.IsNew15MinWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> HourBoundaryCases =>
|
||||
[
|
||||
["same hour, same minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false],
|
||||
["same hour, later minute", new DateTime(2026, 3, 17, 10, 10, 0), new DateTime(2026, 3, 17, 10, 11, 0), false],
|
||||
["same hour, last minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 59, 59), false],
|
||||
["next hour", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 11, 0, 0), true],
|
||||
["next hour, late minute", new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 59, 0), true],
|
||||
["midnight boundary", new DateTime(2026, 3, 17, 23, 0, 0), new DateTime(2026, 3, 18, 0, 0, 0), true],
|
||||
["clock moved backwards", new DateTime(2026, 3, 17, 11, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), true],
|
||||
["DST spring forward", new DateTime(2026, 3, 29, 2, 0, 0), new DateTime(2026, 3, 29, 3, 0, 0), true],
|
||||
["DST fall back", new DateTime(2026, 10, 25, 2, 0, 0), new DateTime(2026, 10, 25, 3, 0, 0), true]
|
||||
];
|
||||
public static TheoryData<string, DateTime, DateTime, bool> HourBoundaryCases => new()
|
||||
{
|
||||
{ "same hour, same minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), false },
|
||||
{ "same hour, later minute", new DateTime(2026, 3, 17, 10, 10, 0), new DateTime(2026, 3, 17, 10, 11, 0), false },
|
||||
{ "same hour, last minute", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 10, 59, 59), false },
|
||||
{ "next hour", new DateTime(2026, 3, 17, 10, 0, 0), new DateTime(2026, 3, 17, 11, 0, 0), true },
|
||||
{ "next hour, late minute", new DateTime(2026, 3, 17, 10, 59, 0), new DateTime(2026, 3, 17, 11, 59, 0), true },
|
||||
{ "midnight boundary", new DateTime(2026, 3, 17, 23, 0, 0), new DateTime(2026, 3, 18, 0, 0, 0), true },
|
||||
{ "clock moved backwards", new DateTime(2026, 3, 17, 11, 0, 0), new DateTime(2026, 3, 17, 10, 0, 0), true },
|
||||
{ "DST spring forward", new DateTime(2026, 3, 29, 2, 0, 0), new DateTime(2026, 3, 29, 3, 0, 0), true },
|
||||
{ "DST fall back", new DateTime(2026, 10, 25, 2, 0, 0), new DateTime(2026, 10, 25, 3, 0, 0), true }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(HourBoundaryCases))]
|
||||
@@ -268,17 +268,17 @@ public class CostAccumulatorTests
|
||||
CostAccumulator.IsNewHourWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> DayBoundaryCases =>
|
||||
[
|
||||
["same timestamp", new DateTime(2026, 3, 17, 0, 0, 0), new DateTime(2026, 3, 17, 0, 0, 0), false],
|
||||
["same day, last second", new DateTime(2026, 3, 17, 0, 0, 0), new DateTime(2026, 3, 17, 23, 59, 59), false],
|
||||
["next day", new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), true],
|
||||
["month rollover", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true],
|
||||
["year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true],
|
||||
["leap day rollover", new DateTime(2028, 2, 28, 23, 59, 0), new DateTime(2028, 2, 29, 0, 0, 0), true],
|
||||
["DST spring forward day", new DateTime(2026, 3, 28, 23, 59, 0), new DateTime(2026, 3, 29, 0, 0, 0), true],
|
||||
["DST fall back day", new DateTime(2026, 10, 24, 23, 59, 0), new DateTime(2026, 10, 25, 0, 0, 0), true]
|
||||
];
|
||||
public static TheoryData<string, DateTime, DateTime, bool> DayBoundaryCases => new()
|
||||
{
|
||||
{ "same timestamp", new DateTime(2026, 3, 17, 0, 0, 0), new DateTime(2026, 3, 17, 0, 0, 0), false },
|
||||
{ "same day, last second", new DateTime(2026, 3, 17, 0, 0, 0), new DateTime(2026, 3, 17, 23, 59, 59), false },
|
||||
{ "next day", new DateTime(2026, 3, 17, 23, 59, 0), new DateTime(2026, 3, 18, 0, 0, 0), true },
|
||||
{ "month rollover", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true },
|
||||
{ "year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true },
|
||||
{ "leap day rollover", new DateTime(2028, 2, 28, 23, 59, 0), new DateTime(2028, 2, 29, 0, 0, 0), true },
|
||||
{ "DST spring forward day", new DateTime(2026, 3, 28, 23, 59, 0), new DateTime(2026, 3, 29, 0, 0, 0), true },
|
||||
{ "DST fall back day", new DateTime(2026, 10, 24, 23, 59, 0), new DateTime(2026, 10, 25, 0, 0, 0), true }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DayBoundaryCases))]
|
||||
@@ -287,15 +287,15 @@ public class CostAccumulatorTests
|
||||
CostAccumulator.IsNewDayWindow(now, lastReset).Should().Be(expected, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> MonthBoundaryCases =>
|
||||
[
|
||||
["same month, first day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 1, 0, 0, 0), false],
|
||||
["same month, last day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 59), false],
|
||||
["next month", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true],
|
||||
["clock moved into previous month", new DateTime(2026, 4, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 0), true],
|
||||
["year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true],
|
||||
["leap day rollover", new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), true]
|
||||
];
|
||||
public static TheoryData<string, DateTime, DateTime, bool> MonthBoundaryCases => new()
|
||||
{
|
||||
{ "same month, first day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 1, 0, 0, 0), false },
|
||||
{ "same month, last day", new DateTime(2026, 3, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 59), false },
|
||||
{ "next month", new DateTime(2026, 3, 31, 23, 59, 0), new DateTime(2026, 4, 1, 0, 0, 0), true },
|
||||
{ "clock moved into previous month", new DateTime(2026, 4, 1, 0, 0, 0), new DateTime(2026, 3, 31, 23, 59, 0), true },
|
||||
{ "year rollover", new DateTime(2026, 12, 31, 23, 59, 0), new DateTime(2027, 1, 1, 0, 0, 0), true },
|
||||
{ "leap day rollover", new DateTime(2028, 2, 29, 23, 59, 0), new DateTime(2028, 3, 1, 0, 0, 0), true }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(MonthBoundaryCases))]
|
||||
@@ -333,7 +333,7 @@ public class CostAccumulatorTests
|
||||
|
||||
// Assert
|
||||
var snapshot = sut.GetSnapshot();
|
||||
var expected = incrementPerCall * callsPerThread * threadCount;
|
||||
const double expected = incrementPerCall * callsPerThread * threadCount;
|
||||
snapshot.CostMonthly.Should().BeApproximately(expected, 0.0001);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,18 +18,18 @@ public class OtherPowerCalculationServiceTests
|
||||
|
||||
#region Basic calculation
|
||||
|
||||
public static IEnumerable<object[]> CalculationCases =>
|
||||
[
|
||||
["two devices", 341.97, new[] { 8.55, 36.93 }, 296.49, "ok"],
|
||||
["no devices", 341.97, System.Array.Empty<double>(), 341.97, "ok"],
|
||||
["devices exceed total", 341.97, new[] { 200.0, 180.0 }, 0.0, "device_sum_exceeds_total"],
|
||||
["devices equal total", 100.0, new[] { 60.0, 40.0 }, 0.0, "ok"],
|
||||
["zero power, no devices", 0.0, System.Array.Empty<double>(), 0.0, "ok"],
|
||||
["negative total, no devices", -120.0, System.Array.Empty<double>(), 0.0, "device_sum_exceeds_total"],
|
||||
["negative device reading", 100.0, new[] { 150.0, -40.0 }, 0.0, "device_sum_exceeds_total"],
|
||||
["very large values", 1_000_000.0, new[] { 250_000.0, 250_000.0 }, 500_000.0, "ok"],
|
||||
["very small values", 0.5, new[] { 0.25 }, 0.25, "ok"]
|
||||
];
|
||||
public static TheoryData<string, double, double[], double, string> CalculationCases => new()
|
||||
{
|
||||
{ "two devices", 341.97, [8.55, 36.93], 296.49, "ok" },
|
||||
{ "no devices", 341.97, Array.Empty<double>(), 341.97, "ok" },
|
||||
{ "devices exceed total", 341.97, [200.0, 180.0], 0.0, "device_sum_exceeds_total" },
|
||||
{ "devices equal total", 100.0, [60.0, 40.0], 0.0, "ok" },
|
||||
{ "zero power, no devices", 0.0, Array.Empty<double>(), 0.0, "ok" },
|
||||
{ "negative total, no devices", -120.0, Array.Empty<double>(), 0.0, "device_sum_exceeds_total" },
|
||||
{ "negative device reading", 100.0, [150.0, -40.0], 0.0, "device_sum_exceeds_total" },
|
||||
{ "very large values", 1_000_000.0, [250_000.0, 250_000.0], 500_000.0, "ok" },
|
||||
{ "very small values", 0.5, [0.25], 0.25, "ok" }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CalculationCases))]
|
||||
@@ -43,8 +43,8 @@ public class OtherPowerCalculationServiceTests
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = deviceWatts
|
||||
.Select((watts, index) => ($"sensor.device{index}_power", watts))
|
||||
.ToDictionary(entry => entry.Item1, entry => entry.Item2);
|
||||
.Select((watts, index) => (entityId: $"sensor.device{index}_power", watts: watts))
|
||||
.ToDictionary(entry => entry.entityId, entry => entry.watts);
|
||||
|
||||
// Act
|
||||
var result = sut.Calculate(totalPowerWatts, devices);
|
||||
@@ -112,11 +112,10 @@ public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 4);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
var result1 = sut.Calculate(341.97, devices);
|
||||
var result2 = sut.Calculate(115.0, devices);
|
||||
var result1 = sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
var result2 = sut.Calculate(115.0, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result1.SmoothedOtherPowerWatts.Should().BeApproximately(341.97, 0.1);
|
||||
@@ -164,13 +163,12 @@ public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
PowerCalculationResult? result = null;
|
||||
for (var i = 0; i < 200; i++)
|
||||
{
|
||||
result = sut.Calculate(200.0, devices);
|
||||
result = sut.Calculate(200.0, new Dictionary<string, double>());
|
||||
}
|
||||
|
||||
// Assert
|
||||
@@ -183,10 +181,9 @@ public class OtherPowerCalculationServiceTests
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 5);
|
||||
var samples = PowerMonitorTestData.CreatePowerSampleSeries(50);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
var results = samples.Select(sample => sut.Calculate(sample, devices)).ToList();
|
||||
var results = samples.Select(sample => sut.Calculate(sample, new Dictionary<string, double>())).ToList();
|
||||
|
||||
// Assert
|
||||
results.Should().OnlyContain(r => r.SmoothedOtherPowerWatts >= samples.Min());
|
||||
@@ -202,13 +199,12 @@ public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
var devices = new Dictionary<string, double>();
|
||||
sut.Calculate(341.97, devices);
|
||||
sut.Calculate(341.97, devices);
|
||||
sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
|
||||
// Act
|
||||
sut.Reset();
|
||||
var result = sut.Calculate(115.0, devices);
|
||||
var result = sut.Calculate(115.0, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result.SmoothedOtherPowerWatts.Should().BeApproximately(115.0, 0.1);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using FluentAssertions;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
@@ -17,18 +16,18 @@ public class PowerCostCalculationServiceTests
|
||||
|
||||
#region CalculateCostIncrement
|
||||
|
||||
public static IEnumerable<object[]> CostIncrementCases =>
|
||||
[
|
||||
["default polling interval", 341.97, 0.1791, 15, 0.0002551951125],
|
||||
["one kilowatt for one hour", 1000.0, 1.0, PowerMonitorConsts.SecondsPerHour, 1.0],
|
||||
["limit load for one minute", 5500.0, 0.25, 60, 0.0229166666666667],
|
||||
["very large load", 1_000_000.0, 0.5, 900, 125.0],
|
||||
["negative power", -500.0, 0.2, PowerMonitorConsts.SecondsPerHour, -0.1],
|
||||
["zero power", 0.0, 0.1791, 15, 0.0],
|
||||
["zero price", 341.97, 0.0, 15, 0.0],
|
||||
["zero interval", 1000.0, 0.2, 0, 0.0],
|
||||
["tiny values", 0.001, 0.0001, 1, 2.77777777777778e-14]
|
||||
];
|
||||
public static TheoryData<string, double, double, int, double> CostIncrementCases => new()
|
||||
{
|
||||
{ "default polling interval", 341.97, 0.1791, 15, 0.0002551951125 },
|
||||
{ "one kilowatt for one hour", 1000.0, 1.0, PowerMonitorConsts.SecondsPerHour, 1.0 },
|
||||
{ "limit load for one minute", 5500.0, 0.25, 60, 0.0229166666666667 },
|
||||
{ "very large load", 1_000_000.0, 0.5, 900, 125.0 },
|
||||
{ "negative power", -500.0, 0.2, PowerMonitorConsts.SecondsPerHour, -0.1 },
|
||||
{ "zero power", 0.0, 0.1791, 15, 0.0 },
|
||||
{ "zero price", 341.97, 0.0, 15, 0.0 },
|
||||
{ "zero interval", 1000.0, 0.2, 0, 0.0 },
|
||||
{ "tiny values", 0.001, 0.0001, 1, 2.77777777777778e-14 }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CostIncrementCases))]
|
||||
@@ -315,7 +314,7 @@ public class PowerCostCalculationServiceTests
|
||||
var timestamps = PowerMonitorTestData.CreateSampleTimestamps(PowerMonitorTestData.FixedDayStart, 96);
|
||||
var wattSamples = PowerMonitorTestData.CreatePowerSampleSeries(96);
|
||||
var expectedPerSlot = timestamps
|
||||
.Select((_, index) => ExpectedCost(wattSamples[index], pricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds))
|
||||
.Select((_, index) => ExpectedCost(wattSamples[index], pricePerKwh))
|
||||
.ToArray();
|
||||
|
||||
// Act
|
||||
@@ -347,7 +346,7 @@ public class PowerCostCalculationServiceTests
|
||||
// Act
|
||||
foreach (var (start, pricePerKwh) in windows)
|
||||
{
|
||||
var watts = 500.0 + (start.Hour * 125.0);
|
||||
var watts = 500.0 + start.Hour * 125.0;
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: watts,
|
||||
smoothedOtherWatts: watts / 4,
|
||||
|
||||
@@ -16,18 +16,10 @@ public class PowerLimitEnforcementServiceTests
|
||||
private const string BoilerSwitch = PowerMonitorTestData.BoilerSwitchEntityId;
|
||||
private const string AutoManagement = PowerMonitorTestData.AutoManagementEntityId;
|
||||
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<PowerLimitEnforcementService>> _loggerMock;
|
||||
private readonly FakeTimeProvider _timeProvider;
|
||||
private readonly PowerMonitorOptions _options;
|
||||
|
||||
public PowerLimitEnforcementServiceTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<PowerLimitEnforcementService>>();
|
||||
_timeProvider = new FakeTimeProvider(PowerMonitorTestData.FixedUtcNow);
|
||||
_options = PowerMonitorTestData.CreateLimitEnforcementOptions();
|
||||
}
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<PowerLimitEnforcementService>> _loggerMock = new();
|
||||
private readonly FakeTimeProvider _timeProvider = new(PowerMonitorTestData.FixedUtcNow);
|
||||
private readonly PowerMonitorOptions _options = PowerMonitorTestData.CreateLimitEnforcementOptions();
|
||||
|
||||
private PowerLimitEnforcementService CreateSut() =>
|
||||
new(
|
||||
@@ -187,18 +179,18 @@ public class PowerLimitEnforcementServiceTests
|
||||
|
||||
#region Recovery
|
||||
|
||||
public static IEnumerable<object[]> RecoveryCases =>
|
||||
[
|
||||
["not yet due", 300, 3000.0, true, 0],
|
||||
["one second before due", 599, 3000.0, true, 0],
|
||||
["exactly due", 600, 3000.0, false, 1],
|
||||
["just under limit", 601, 5499.99, false, 1],
|
||||
["zero power", 900, 0.0, false, 1],
|
||||
["negative power", 601, -10.0, false, 1],
|
||||
["exactly at limit stays enforced", 601, 5500.0, true, 0],
|
||||
["above limit stays enforced", 601, 5800.0, true, 0],
|
||||
["very high power stays enforced", 601, 25_000.0, true, 0]
|
||||
];
|
||||
public static TheoryData<string, int, double, bool, int> RecoveryCases => new()
|
||||
{
|
||||
{ "not yet due", 300, 3000.0, true, 0 },
|
||||
{ "one second before due", 599, 3000.0, true, 0 },
|
||||
{ "exactly due", 600, 3000.0, false, 1 },
|
||||
{ "just under limit", 601, 5499.99, false, 1 },
|
||||
{ "zero power", 900, 0.0, false, 1 },
|
||||
{ "negative power", 601, -10.0, false, 1 },
|
||||
{ "exactly at limit stays enforced", 601, 5500.0, true, 0 },
|
||||
{ "above limit stays enforced", 601, 5800.0, true, 0 },
|
||||
{ "very high power stays enforced", 601, 25_000.0, true, 0 }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecoveryCases))]
|
||||
@@ -237,8 +229,7 @@ public class PowerLimitEnforcementServiceTests
|
||||
sut.EnforceLimit(5800);
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
|
||||
var immediatelyAfter = sut;
|
||||
immediatelyAfter.EnforceLimit(3000);
|
||||
sut.EnforceLimit(3000);
|
||||
|
||||
// Assert - not enough time has passed for another check
|
||||
sut.IsLimitActive.Should().BeTrue();
|
||||
|
||||
@@ -4,7 +4,6 @@ using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PowerMonitor.Models;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
@@ -28,19 +27,17 @@ public class PowerMonitorEntityServiceTests
|
||||
"button.powermonitor_trigger_limit_breach"
|
||||
];
|
||||
|
||||
private readonly Mock<IEntityManager> _entityManagerMock;
|
||||
private readonly Mock<ILogger<PowerMonitorEntityService>> _loggerMock;
|
||||
private readonly Mock<IEntityManager> _entityManagerMock = new();
|
||||
private readonly PowerMonitorEntityService _sut;
|
||||
|
||||
public PowerMonitorEntityServiceTests()
|
||||
{
|
||||
_entityManagerMock = new Mock<IEntityManager>();
|
||||
_loggerMock = new Mock<ILogger<PowerMonitorEntityService>>();
|
||||
var loggerMock = new Mock<ILogger<PowerMonitorEntityService>>();
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>())).Returns(Task.CompletedTask);
|
||||
_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);
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>())).Returns(Task.CompletedTask);
|
||||
_sut = new PowerMonitorEntityService(_entityManagerMock.Object, _loggerMock.Object);
|
||||
_sut = new PowerMonitorEntityService(_entityManagerMock.Object, loggerMock.Object);
|
||||
}
|
||||
|
||||
#region GetSystemEntitiesList
|
||||
@@ -49,7 +46,7 @@ public class PowerMonitorEntityServiceTests
|
||||
public void GetSystemEntitiesList_ShouldReturnAll14SystemEntities()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetSystemEntitiesList();
|
||||
var result = PowerMonitorEntityService.GetSystemEntitiesList();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(14);
|
||||
@@ -73,7 +70,7 @@ public class PowerMonitorEntityServiceTests
|
||||
public void GetSystemEntitiesList_ShouldContainExpectedEntityId(string entityId)
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetSystemEntitiesList();
|
||||
var result = PowerMonitorEntityService.GetSystemEntitiesList();
|
||||
|
||||
// Assert
|
||||
result.Should().Contain(entity => entity.EntityId == entityId);
|
||||
@@ -83,7 +80,7 @@ public class PowerMonitorEntityServiceTests
|
||||
public void GetSystemEntitiesList_AllEntities_ShouldHaveUniqueEntityIds()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetSystemEntitiesList();
|
||||
var result = PowerMonitorEntityService.GetSystemEntitiesList();
|
||||
|
||||
// Assert
|
||||
result.Select(entity => entity.EntityId).Should().OnlyHaveUniqueItems();
|
||||
@@ -93,7 +90,7 @@ public class PowerMonitorEntityServiceTests
|
||||
public void GetSystemEntitiesList_AllEntities_ShouldExposeCreationOptions()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetSystemEntitiesList();
|
||||
var result = PowerMonitorEntityService.GetSystemEntitiesList();
|
||||
|
||||
// Assert
|
||||
result.Should().OnlyContain(entity => entity.Options != null && entity.Options.UniqueId == entity.EntityId);
|
||||
@@ -103,13 +100,13 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
#region GetPowerDeviceEntitiesNames
|
||||
|
||||
public static IEnumerable<object[]> DeviceEntityCases =>
|
||||
[
|
||||
["sonoff device", "sensor.sonoff_1002230e32_power", "Sonoff Power", "sonoff_1002230e32"],
|
||||
["simple device", "sensor.tv_power", "TV Power", "tv"],
|
||||
["device with suffix in name", "sensor.akvariumas_power", "Akvariumas Power", "akvariumas"],
|
||||
["uppercase entity id", "sensor.TV_POWER", "TV Power", "TV"]
|
||||
];
|
||||
public static TheoryData<string, string, string, string> DeviceEntityCases => new()
|
||||
{
|
||||
{ "sonoff device", "sensor.sonoff_1002230e32_power", "Sonoff Power", "sonoff_1002230e32" },
|
||||
{ "simple device", "sensor.tv_power", "TV Power", "tv" },
|
||||
{ "device with suffix in name", "sensor.akvariumas_power", "Akvariumas Power", "akvariumas" },
|
||||
{ "uppercase entity id", "sensor.TV_POWER", "TV Power", "TV" }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DeviceEntityCases))]
|
||||
@@ -127,21 +124,23 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(4, $"scenario: {scenario}");
|
||||
result.Select(entity => entity.EntityId).Should().BeEquivalentTo(new[]
|
||||
{
|
||||
string[] expectedEntityIds =
|
||||
[
|
||||
$"sensor.powermonitor_{expectedDeviceName}_cost_15min",
|
||||
$"sensor.powermonitor_{expectedDeviceName}_cost_1h",
|
||||
$"sensor.powermonitor_{expectedDeviceName}_cost_daily",
|
||||
$"sensor.powermonitor_{expectedDeviceName}_cost_monthly"
|
||||
}, $"scenario: {scenario}");
|
||||
];
|
||||
result.Select(entity => entity.EntityId).Should().BeEquivalentTo(expectedEntityIds, $"scenario: {scenario}");
|
||||
result.Should().OnlyContain(entity => entity.Domain == "sensor", $"scenario: {scenario}");
|
||||
result.Select(entity => entity.Name).Should().BeEquivalentTo(new[]
|
||||
{
|
||||
string[] expectedNames =
|
||||
[
|
||||
$"{friendlyName} cost (15 min)",
|
||||
$"{friendlyName} cost (1h)",
|
||||
$"{friendlyName} cost (daily)",
|
||||
$"{friendlyName} cost (monthly)"
|
||||
}, $"scenario: {scenario}");
|
||||
];
|
||||
result.Select(entity => entity.Name).Should().BeEquivalentTo(expectedNames, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -173,13 +172,14 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(4);
|
||||
result.Should().BeEquivalentTo(new[]
|
||||
{
|
||||
string[] expectedEntityIds =
|
||||
[
|
||||
$"sensor.powermonitor_{deviceName}_cost_15min",
|
||||
$"sensor.powermonitor_{deviceName}_cost_1h",
|
||||
$"sensor.powermonitor_{deviceName}_cost_daily",
|
||||
$"sensor.powermonitor_{deviceName}_cost_monthly"
|
||||
});
|
||||
];
|
||||
result.Should().BeEquivalentTo(expectedEntityIds);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -510,18 +510,21 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
#region Cancellation
|
||||
|
||||
public static IEnumerable<object[]> CancelledOperations()
|
||||
public static TheoryData<string, Func<PowerMonitorEntityService, Task>> CancelledOperations()
|
||||
{
|
||||
var data = new TheoryData<string, Func<PowerMonitorEntityService, Task>>();
|
||||
var cancelled = new CancellationToken(canceled: true);
|
||||
var sensors = new List<PowerSensorInfo> { PowerMonitorTestData.CreateSensor("sensor.tv_power") };
|
||||
List<PowerSensorInfo> sensors = [PowerMonitorTestData.CreateSensor("sensor.tv_power")];
|
||||
var context = PowerMonitorTestData.CreateUpdateContext();
|
||||
|
||||
yield return ["create system entities", (Func<PowerMonitorEntityService, Task>)(sut => sut.CreateSystemEntitiesAsync(cancelled))];
|
||||
yield return ["create device entities", (Func<PowerMonitorEntityService, Task>)(sut => sut.CreateDeviceEntitiesAsync(sensors, cancelled))];
|
||||
yield return ["update entities", (Func<PowerMonitorEntityService, Task>)(sut => sut.UpdateEntitiesAsync(context, cancelled))];
|
||||
yield return ["reset entities", (Func<PowerMonitorEntityService, Task>)(sut => sut.ResetEntitiesAsync(cancelled))];
|
||||
yield return ["remove system entities", (Func<PowerMonitorEntityService, Task>)(sut => sut.RemoveSystemEntitiesAsync(cancelled))];
|
||||
yield return ["remove device entities", (Func<PowerMonitorEntityService, Task>)(sut => sut.RemoveDeviceEntitiesAsync(["tv"], cancelled))];
|
||||
data.Add("create system entities", sut => sut.CreateSystemEntitiesAsync(cancelled));
|
||||
data.Add("create device entities", sut => sut.CreateDeviceEntitiesAsync(sensors, cancelled));
|
||||
data.Add("update entities", sut => sut.UpdateEntitiesAsync(context, cancelled));
|
||||
data.Add("reset entities", sut => sut.ResetEntitiesAsync(cancelled));
|
||||
data.Add("remove system entities", sut => sut.RemoveSystemEntitiesAsync(cancelled));
|
||||
data.Add("remove device entities", sut => sut.RemoveDeviceEntitiesAsync(["tv"], cancelled));
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
@@ -6,26 +6,17 @@ namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class PowerMonitorOptionsTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Expected defaults for every public, configurable option. Only public properties are
|
||||
/// inspected here - no private state is read through reflection.
|
||||
/// </summary>
|
||||
public static TheoryData<string, object> ExpectedDefaults => new()
|
||||
{
|
||||
{ nameof(PowerMonitorOptions.Enabled), false },
|
||||
{ nameof(PowerMonitorOptions.MainMeterEntityId), string.Empty },
|
||||
{ nameof(PowerMonitorOptions.PollIntervalSeconds), PowerMonitorConsts.DefaultPollIntervalSeconds },
|
||||
{ nameof(PowerMonitorOptions.SmoothingWindowSize), PowerMonitorConsts.DefaultSmoothingWindowSize },
|
||||
{ nameof(PowerMonitorOptions.AnomalyThresholdWatts), PowerMonitorConsts.DefaultAnomalyThresholdWatts },
|
||||
{ nameof(PowerMonitorOptions.HighPowerThresholdWatts), PowerMonitorConsts.DefaultHighPowerThresholdWatts },
|
||||
{ nameof(PowerMonitorOptions.EnforceLimitEnabled), true },
|
||||
{ nameof(PowerMonitorOptions.PowerLimitWatts), PowerMonitorConsts.DefaultPowerLimitWatts },
|
||||
{ nameof(PowerMonitorOptions.RecoverySeconds), PowerMonitorConsts.DefaultRecoverySeconds },
|
||||
{ nameof(PowerMonitorOptions.LimitCooldownSeconds), PowerMonitorConsts.DefaultLimitCooldownSeconds }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ExpectedDefaults))]
|
||||
[InlineData(nameof(PowerMonitorOptions.Enabled), false)]
|
||||
[InlineData(nameof(PowerMonitorOptions.MainMeterEntityId), "")]
|
||||
[InlineData(nameof(PowerMonitorOptions.PollIntervalSeconds), PowerMonitorConsts.DefaultPollIntervalSeconds)]
|
||||
[InlineData(nameof(PowerMonitorOptions.SmoothingWindowSize), PowerMonitorConsts.DefaultSmoothingWindowSize)]
|
||||
[InlineData(nameof(PowerMonitorOptions.AnomalyThresholdWatts), PowerMonitorConsts.DefaultAnomalyThresholdWatts)]
|
||||
[InlineData(nameof(PowerMonitorOptions.HighPowerThresholdWatts), PowerMonitorConsts.DefaultHighPowerThresholdWatts)]
|
||||
[InlineData(nameof(PowerMonitorOptions.EnforceLimitEnabled), true)]
|
||||
[InlineData(nameof(PowerMonitorOptions.PowerLimitWatts), PowerMonitorConsts.DefaultPowerLimitWatts)]
|
||||
[InlineData(nameof(PowerMonitorOptions.RecoverySeconds), PowerMonitorConsts.DefaultRecoverySeconds)]
|
||||
[InlineData(nameof(PowerMonitorOptions.LimitCooldownSeconds), PowerMonitorConsts.DefaultLimitCooldownSeconds)]
|
||||
public void Defaults_ShouldExposeExpectedValue(string propertyName, object expected)
|
||||
{
|
||||
// Arrange
|
||||
@@ -40,12 +31,12 @@ public class PowerMonitorOptionsTests
|
||||
actual.Should().Be(expected);
|
||||
}
|
||||
|
||||
public static TheoryData<string> CollectionProperties => new()
|
||||
{
|
||||
public static TheoryData<string> CollectionProperties =>
|
||||
[
|
||||
nameof(PowerMonitorOptions.ExcludedSensorPatterns),
|
||||
nameof(PowerMonitorOptions.EnforceSwitchOffEntityIds),
|
||||
nameof(PowerMonitorOptions.PauseAndRestoreEntityIds)
|
||||
};
|
||||
];
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CollectionProperties))]
|
||||
@@ -59,7 +50,7 @@ public class PowerMonitorOptionsTests
|
||||
|
||||
// Assert
|
||||
value.Should().BeOfType<string[]>();
|
||||
((string[])value!).Should().BeEmpty();
|
||||
((string[])value).Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -39,7 +39,7 @@ internal static class PowerMonitorTestData
|
||||
/// <summary>
|
||||
/// Realistic seven-device reading set used by the entity projection tests.
|
||||
/// </summary>
|
||||
public static IReadOnlyDictionary<string, double> CreateDeviceReadings() => new Dictionary<string, double>
|
||||
private static Dictionary<string, double> CreateDeviceReadings() => new()
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 8.55,
|
||||
["sensor.sonoff_10018909cb_power"] = 36.93,
|
||||
@@ -72,7 +72,7 @@ internal static class PowerMonitorTestData
|
||||
};
|
||||
}
|
||||
|
||||
public static CostSnapshot CreateCostSnapshot(
|
||||
private static CostSnapshot CreateCostSnapshot(
|
||||
double cost15Min = 0.001,
|
||||
double cost1Hour = 0.004,
|
||||
double costDaily = 0.03,
|
||||
@@ -189,29 +189,34 @@ internal static class PowerMonitorTestData
|
||||
/// used by the looping scenarios.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<double> CreatePowerSampleSeries(int count = 96) =>
|
||||
Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(50.0 + (i * 37.5) + ((i % 7) * 0.5), 2))
|
||||
.ToArray();
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(50.0 + i * 37.5 + i % 7 * 0.5, 2))
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Deterministic, strictly positive hourly tariff series (EUR/kWh).
|
||||
/// </summary>
|
||||
public static IReadOnlyList<double> CreateTariffPriceSeries(int count = 24) =>
|
||||
Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(0.08 + (i * 0.03) + ((i % 5) * 0.0125), 4))
|
||||
.ToArray();
|
||||
private static IReadOnlyList<double> CreateTariffPriceSeries(int count = 24) =>
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => Math.Round(0.08 + i * 0.03 + i % 5 * 0.0125, 4))
|
||||
];
|
||||
|
||||
public static IReadOnlyList<(DateTime Start, double PricePerKwh)> CreateHourlyTariffWindows(DateTime dayStart)
|
||||
{
|
||||
var prices = CreateTariffPriceSeries();
|
||||
|
||||
return Enumerable.Range(0, prices.Count)
|
||||
.Select(hour => (dayStart.AddHours(hour), prices[hour]))
|
||||
.ToArray();
|
||||
return
|
||||
[
|
||||
.. Enumerable.Range(0, prices.Count)
|
||||
.Select(hour => (dayStart.AddHours(hour), prices[hour]))
|
||||
];
|
||||
}
|
||||
|
||||
public static IReadOnlyList<DateTime> CreateSampleTimestamps(DateTime start, int count, int intervalMinutes = 15) =>
|
||||
Enumerable.Range(0, count)
|
||||
[
|
||||
.. Enumerable.Range(0, count)
|
||||
.Select(i => start.AddMinutes(i * intervalMinutes))
|
||||
.ToArray();
|
||||
];
|
||||
}
|
||||
|
||||
@@ -5,20 +5,13 @@ using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class PowerSensorDiscoveryServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock;
|
||||
private readonly Mock<ILogger<PowerSensorDiscoveryService>> _loggerMock;
|
||||
|
||||
public PowerSensorDiscoveryServiceTests()
|
||||
{
|
||||
_haContextMock = new Mock<IHaContext>();
|
||||
_loggerMock = new Mock<ILogger<PowerSensorDiscoveryService>>();
|
||||
}
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<PowerSensorDiscoveryService>> _loggerMock = new();
|
||||
|
||||
private PowerSensorDiscoveryService CreateSut(
|
||||
string mainMeterEntityId = PowerMonitorTestData.MainMeterEntityId,
|
||||
@@ -47,12 +40,12 @@ public class PowerSensorDiscoveryServiceTests
|
||||
params (string EntityId, string? DeviceClass, string? Unit, string? FriendlyName)[] entities) =>
|
||||
_haContextMock
|
||||
.Setup(h => h.GetAllEntities())
|
||||
.Returns(entities.Select(entity => SetupEntity(entity.EntityId, entity.DeviceClass, entity.Unit, entity.FriendlyName)).ToList());
|
||||
.Returns([.. entities.Select(entity => SetupEntity(entity.EntityId, entity.DeviceClass, entity.Unit, entity.FriendlyName))]);
|
||||
|
||||
private void SetupEntitiesWithoutState(params string[] entityIds) =>
|
||||
_haContextMock
|
||||
.Setup(h => h.GetAllEntities())
|
||||
.Returns(entityIds.Select(entityId => new Entity(_haContextMock.Object, entityId)).ToList());
|
||||
.Returns([.. entityIds.Select(entityId => new Entity(_haContextMock.Object, entityId))]);
|
||||
|
||||
#region Discovery
|
||||
|
||||
@@ -346,7 +339,7 @@ public class PowerSensorDiscoveryServiceTests
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsPowerSensor(entity);
|
||||
var result = PowerSensorDiscoveryService.IsPowerSensor(entity);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expected, $"scenario: {scenario}");
|
||||
@@ -361,7 +354,7 @@ public class PowerSensorDiscoveryServiceTests
|
||||
var sut = CreateSut();
|
||||
|
||||
// Act
|
||||
var result = sut.IsPowerSensor(entity);
|
||||
var result = PowerSensorDiscoveryService.IsPowerSensor(entity);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
|
||||
Reference in New Issue
Block a user