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:
2026-11-29 21:42:41 +02:00
parent ada3c77778
commit 17774b40b8
31 changed files with 649 additions and 693 deletions
@@ -121,25 +121,25 @@ public class BoilerControlServiceTests
/// Complete truth table for a boiler that is currently ON: it may only stay on when
/// it is cheap, water is not ready and the budget is available (or overridden).
/// </summary>
public static IEnumerable<object[]> SwitchOnActionMatrix =>
[
[false, false, false, false, BoilerAction.TurnOff],
[false, false, false, true, BoilerAction.TurnOff],
[false, false, true, false, BoilerAction.TurnOff],
[false, false, true, true, BoilerAction.TurnOff],
[false, true, false, false, BoilerAction.TurnOff],
[false, true, false, true, BoilerAction.TurnOff],
[false, true, true, false, BoilerAction.TurnOff],
[false, true, true, true, BoilerAction.TurnOff],
[true, false, false, false, BoilerAction.NoChange],
[true, false, false, true, BoilerAction.NoChange],
[true, false, true, false, BoilerAction.TurnOff],
[true, false, true, true, BoilerAction.NoChange],
[true, true, false, false, BoilerAction.TurnOff],
[true, true, false, true, BoilerAction.TurnOff],
[true, true, true, false, BoilerAction.TurnOff],
[true, true, true, true, BoilerAction.TurnOff]
];
public static TheoryData<bool, bool, bool, bool, BoilerAction> SwitchOnActionMatrix => new()
{
{ false, false, false, false, BoilerAction.TurnOff },
{ false, false, false, true, BoilerAction.TurnOff },
{ false, false, true, false, BoilerAction.TurnOff },
{ false, false, true, true, BoilerAction.TurnOff },
{ false, true, false, false, BoilerAction.TurnOff },
{ false, true, false, true, BoilerAction.TurnOff },
{ false, true, true, false, BoilerAction.TurnOff },
{ false, true, true, true, BoilerAction.TurnOff },
{ true, false, false, false, BoilerAction.NoChange },
{ true, false, false, true, BoilerAction.NoChange },
{ true, false, true, false, BoilerAction.TurnOff },
{ true, false, true, true, BoilerAction.NoChange },
{ true, true, false, false, BoilerAction.TurnOff },
{ true, true, false, true, BoilerAction.TurnOff },
{ true, true, true, false, BoilerAction.TurnOff },
{ true, true, true, true, BoilerAction.TurnOff }
};
[Theory]
[MemberData(nameof(SwitchOnActionMatrix))]
@@ -160,25 +160,25 @@ public class BoilerControlServiceTests
/// Complete truth table for a boiler that is currently OFF: it may only turn on when
/// it is cheap, water is not ready and the budget is available (or overridden).
/// </summary>
public static IEnumerable<object[]> SwitchOffActionMatrix =>
[
[false, false, false, false, BoilerAction.NoChange],
[false, false, false, true, BoilerAction.NoChange],
[false, false, true, false, BoilerAction.NoChange],
[false, false, true, true, BoilerAction.NoChange],
[false, true, false, false, BoilerAction.NoChange],
[false, true, false, true, BoilerAction.NoChange],
[false, true, true, false, BoilerAction.NoChange],
[false, true, true, true, BoilerAction.NoChange],
[true, false, false, false, BoilerAction.TurnOn],
[true, false, false, true, BoilerAction.TurnOn],
[true, false, true, false, BoilerAction.NoChange],
[true, false, true, true, BoilerAction.TurnOn],
[true, true, false, false, BoilerAction.NoChange],
[true, true, false, true, BoilerAction.NoChange],
[true, true, true, false, BoilerAction.NoChange],
[true, true, true, true, BoilerAction.NoChange]
];
public static TheoryData<bool, bool, bool, bool, BoilerAction> SwitchOffActionMatrix => new()
{
{ false, false, false, false, BoilerAction.NoChange },
{ false, false, false, true, BoilerAction.NoChange },
{ false, false, true, false, BoilerAction.NoChange },
{ false, false, true, true, BoilerAction.NoChange },
{ false, true, false, false, BoilerAction.NoChange },
{ false, true, false, true, BoilerAction.NoChange },
{ false, true, true, false, BoilerAction.NoChange },
{ false, true, true, true, BoilerAction.NoChange },
{ true, false, false, false, BoilerAction.TurnOn },
{ true, false, false, true, BoilerAction.TurnOn },
{ true, false, true, false, BoilerAction.NoChange },
{ true, false, true, true, BoilerAction.TurnOn },
{ true, true, false, false, BoilerAction.NoChange },
{ true, true, false, true, BoilerAction.NoChange },
{ true, true, true, false, BoilerAction.NoChange },
{ true, true, true, true, BoilerAction.NoChange }
};
[Theory]
[MemberData(nameof(SwitchOffActionMatrix))]
@@ -8,37 +8,37 @@ public class BoilerMonitorConfigTests
private const string PowerEntityId = BoilerMonitorTestData.PowerEntityId;
private const string SwitchEntityId = BoilerMonitorTestData.SwitchEntityId;
public static IEnumerable<object?[]> ConfigurationCases =>
[
["fully configured", PowerEntityId, SwitchEntityId, 30, 5.0, true],
["minimum valid poll interval and threshold", PowerEntityId, SwitchEntityId, 5, 1.0, true],
["poll interval of one second", PowerEntityId, SwitchEntityId, 1, 5.0, true],
["maximum poll interval", PowerEntityId, SwitchEntityId, int.MaxValue, 5.0, true],
["smallest positive threshold", PowerEntityId, SwitchEntityId, 30, double.Epsilon, true],
["maximum threshold", PowerEntityId, SwitchEntityId, 30, double.MaxValue, true],
["infinite threshold", PowerEntityId, SwitchEntityId, 30, double.PositiveInfinity, true],
public static TheoryData<string, string?, string?, int?, double?, bool> ConfigurationCases => new()
{
{ "fully configured", PowerEntityId, SwitchEntityId, 30, 5.0, true },
{ "minimum valid poll interval and threshold", PowerEntityId, SwitchEntityId, 5, 1.0, true },
{ "poll interval of one second", PowerEntityId, SwitchEntityId, 1, 5.0, true },
{ "maximum poll interval", PowerEntityId, SwitchEntityId, int.MaxValue, 5.0, true },
{ "smallest positive threshold", PowerEntityId, SwitchEntityId, 30, double.Epsilon, true },
{ "maximum threshold", PowerEntityId, SwitchEntityId, 30, double.MaxValue, true },
{ "infinite threshold", PowerEntityId, SwitchEntityId, 30, double.PositiveInfinity, true },
["power entity id null", (string?)null, SwitchEntityId, 30, 5.0, false],
["power entity id empty", "", SwitchEntityId, 30, 5.0, false],
["power entity id whitespace", " ", SwitchEntityId, 30, 5.0, false],
["power entity id tabs and newlines", "\t\n", SwitchEntityId, 30, 5.0, false],
["switch entity id null", PowerEntityId, (string?)null, 30, 5.0, false],
["switch entity id empty", PowerEntityId, "", 30, 5.0, false],
["switch entity id whitespace", PowerEntityId, " ", 30, 5.0, false],
["both entity ids null", (string?)null, (string?)null, 30, 5.0, false],
{ "power entity id null", null, SwitchEntityId, 30, 5.0, false },
{ "power entity id empty", "", SwitchEntityId, 30, 5.0, false },
{ "power entity id whitespace", " ", SwitchEntityId, 30, 5.0, false },
{ "power entity id tabs and newlines", "\t\n", SwitchEntityId, 30, 5.0, false },
{ "switch entity id null", PowerEntityId, null, 30, 5.0, false },
{ "switch entity id empty", PowerEntityId, "", 30, 5.0, false },
{ "switch entity id whitespace", PowerEntityId, " ", 30, 5.0, false },
{ "both entity ids null", null, null, 30, 5.0, false },
["poll interval null", PowerEntityId, SwitchEntityId, (int?)null, 5.0, false],
["poll interval zero", PowerEntityId, SwitchEntityId, 0, 5.0, false],
["poll interval negative", PowerEntityId, SwitchEntityId, -1, 5.0, false],
["poll interval minimum value", PowerEntityId, SwitchEntityId, int.MinValue, 5.0, false],
{ "poll interval null", PowerEntityId, SwitchEntityId, null, 5.0, false },
{ "poll interval zero", PowerEntityId, SwitchEntityId, 0, 5.0, false },
{ "poll interval negative", PowerEntityId, SwitchEntityId, -1, 5.0, false },
{ "poll interval minimum value", PowerEntityId, SwitchEntityId, int.MinValue, 5.0, false },
["threshold null", PowerEntityId, SwitchEntityId, 30, (double?)null, false],
["threshold zero", PowerEntityId, SwitchEntityId, 30, 0.0, false],
["threshold negative", PowerEntityId, SwitchEntityId, 30, -1.0, false],
["threshold minimum value", PowerEntityId, SwitchEntityId, 30, double.MinValue, false],
["threshold NaN", PowerEntityId, SwitchEntityId, 30, double.NaN, false],
["threshold negative infinity", PowerEntityId, SwitchEntityId, 30, double.NegativeInfinity, false]
];
{ "threshold null", PowerEntityId, SwitchEntityId, 30, null, false },
{ "threshold zero", PowerEntityId, SwitchEntityId, 30, 0.0, false },
{ "threshold negative", PowerEntityId, SwitchEntityId, 30, -1.0, false },
{ "threshold minimum value", PowerEntityId, SwitchEntityId, 30, double.MinValue, false },
{ "threshold NaN", PowerEntityId, SwitchEntityId, 30, double.NaN, false },
{ "threshold negative infinity", PowerEntityId, SwitchEntityId, 30, double.NegativeInfinity, false }
};
[Theory]
[MemberData(nameof(ConfigurationCases))]
@@ -72,7 +72,7 @@ public class BoilerMonitorEntityServiceTests
}
result.Should().NotBeNull();
result!.AccumulatedRunTime.Should().Be(expectedRunTime.Value);
result.AccumulatedRunTime.Should().Be(expectedRunTime.Value);
result.SessionCount.Should().Be(2);
}
@@ -94,7 +94,7 @@ public class BoilerMonitorEntityServiceTests
}
result.Should().NotBeNull();
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromHours(1));
result.AccumulatedRunTime.Should().Be(TimeSpan.FromHours(1));
result.SessionCount.Should().Be(expectedSessionCount.Value);
}
@@ -107,7 +107,7 @@ public class BoilerMonitorEntityServiceTests
var result = CreateSut().ReadPersistedState();
result.Should().NotBeNull();
result!.AccumulatedRunTime.Should().Be(TimeSpan.FromHours(2));
result.AccumulatedRunTime.Should().Be(TimeSpan.FromHours(2));
result.SessionCount.Should().Be(3);
result.SkipPriceCapState.Should().Be("ON");
result.BudgetOverrideState.Should().Be("OFF");
@@ -127,7 +127,7 @@ public class BoilerMonitorEntityServiceTests
var result = CreateSut().ReadPersistedState();
result.Should().NotBeNull();
result!.SkipPriceCapState.Should().Be(switchState);
result.SkipPriceCapState.Should().Be(switchState);
result.BudgetOverrideState.Should().Be(switchState);
}
@@ -362,8 +362,8 @@ public class BoilerMonitorEntityServiceTests
await CreateSut().CreateEntitiesAsync();
createdEntityIds.Should().HaveCount(15).And.OnlyHaveUniqueItems();
createdEntityIds.Should().Contain(new[]
{
createdEntityIds.Should().Contain(
[
ConfigPowerEntityId,
ConfigSwitchEntityId,
ConfigPollIntervalEntityId,
@@ -373,7 +373,7 @@ public class BoilerMonitorEntityServiceTests
TodayRuntimeEntityId,
BudgetExhaustedEntityId,
AutoManagementEntityId
});
]);
}
[Fact]
@@ -1,5 +1,4 @@
using System.Text.Json;
using BoilerMonitor.Models;
using BoilerMonitorManager.Models;
using Microsoft.Extensions.Time.Testing;
using Moq;
@@ -47,19 +46,6 @@ internal static class BoilerMonitorTestData
CleanupEntitiesOnStartup = cleanupEntitiesOnStartup
};
public static PersistedBoilerState CreatePersistedState(
TimeSpan? accumulatedRunTime = null,
int sessionCount = 3,
string? skipPriceCapState = "ON",
string? budgetOverrideState = "OFF") =>
new()
{
AccumulatedRunTime = accumulatedRunTime ?? TimeSpan.FromHours(2),
SessionCount = sessionCount,
SkipPriceCapState = skipPriceCapState,
BudgetOverrideState = budgetOverrideState
};
public static BoilerMonitorUpdateContext CreateUpdateContext(
TimeSpan? todayRunTime = null,
TimeSpan? remainingTime = null,
@@ -80,13 +66,10 @@ internal static class BoilerMonitorTestData
BudgetExhausted = budgetExhausted,
SessionCount = sessionCount,
BudgetHours = budgetHours,
IsCurrentlyHeating = false,
WaterReady = waterReady,
BoilerSwitchState = boilerSwitchState,
BoilerPowerWatts = boilerPowerWatts,
CooldownSince = cooldownSince,
IsNowCheap = isNowCheap,
NextCheapIntervalStart = null
};
/// <summary>
@@ -114,156 +97,170 @@ internal static class BoilerMonitorTestData
public static EntityState CreateWaterReadyEntityStateWithCooldown(object? cooldownSince) =>
CreateEntityStateWithAttributes(new Dictionary<string, object?> { ["cooldown_since"] = cooldownSince });
public static void SetupEntityStates(
this Mock<IHaContext> haContextMock,
IReadOnlyDictionary<string, string?> states)
extension(Mock<IHaContext> haContextMock)
{
foreach (var (entityId, state) in states)
public void SetupEntityStates(IReadOnlyDictionary<string, string?> states)
{
var entityState = state is null ? null : CreateEntityState(state);
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
foreach (var (entityId, state) in states)
{
var entityState = state is null ? null : CreateEntityState(state);
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
}
}
}
public static void SetupEntityState(
this Mock<IHaContext> haContextMock,
string entityId,
EntityState? entityState) =>
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
public void SetupEntityState(string entityId, EntityState? entityState) =>
haContextMock.Setup(h => h.GetState(entityId)).Returns(entityState);
}
/// <summary>
/// Entity-state samples for the today-runtime sensor: valid values with the expected
/// <see cref="TimeSpan"/>, plus unavailable/negative/overflow values that must be rejected.
/// </summary>
public static IEnumerable<object?[]> RuntimeStateSamples()
public static TheoryData<string?, TimeSpan?> RuntimeStateSamples()
{
yield return ["7200", TimeSpan.FromHours(2)];
yield return ["0", TimeSpan.Zero];
yield return ["134.66", TimeSpan.FromSeconds(134.66)];
yield return ["1E3", TimeSpan.FromSeconds(1000)];
yield return ["0.001", TimeSpan.FromSeconds(0.001)];
yield return [" 60 ", TimeSpan.FromSeconds(60)];
yield return ["86400", TimeSpan.FromDays(1)];
yield return ["1e9", TimeSpan.FromSeconds(1e9)];
var data = new TheoryData<string?, TimeSpan?>();
data.Add("7200", TimeSpan.FromHours(2));
data.Add("0", TimeSpan.Zero);
data.Add("134.66", TimeSpan.FromSeconds(134.66));
data.Add("1E3", TimeSpan.FromSeconds(1000));
data.Add("0.001", TimeSpan.FromSeconds(0.001));
data.Add(" 60 ", TimeSpan.FromSeconds(60));
data.Add("86400", TimeSpan.FromDays(1));
data.Add("1e9", TimeSpan.FromSeconds(1e9));
data.Add(null, null);
data.Add("", null);
data.Add(" ", null);
data.Add("\t", null);
data.Add("unavailable", null);
data.Add("unknown", null);
data.Add("Unavailable", null);
data.Add("not_a_number", null);
data.Add("NaN", null);
data.Add("Infinity", null);
data.Add("1e308", null);
data.Add("-1", null);
data.Add("-0.001", null);
return data;
yield return [(string?)null, (TimeSpan?)null];
yield return ["", (TimeSpan?)null];
yield return [" ", (TimeSpan?)null];
yield return ["\t", (TimeSpan?)null];
yield return ["unavailable", (TimeSpan?)null];
yield return ["unknown", (TimeSpan?)null];
yield return ["Unavailable", (TimeSpan?)null];
yield return ["not_a_number", (TimeSpan?)null];
yield return ["NaN", (TimeSpan?)null];
yield return ["Infinity", (TimeSpan?)null];
yield return ["1e308", (TimeSpan?)null];
yield return ["-1", (TimeSpan?)null];
yield return ["-0.001", (TimeSpan?)null];
}
/// <summary>
/// Entity-state samples for the session-count sensor, including the int boundaries.
/// </summary>
public static IEnumerable<object?[]> SessionCountStateSamples()
public static TheoryData<string?, int?> SessionCountStateSamples()
{
yield return ["0", 0];
yield return ["3", 3];
yield return [" 2 ", 2];
yield return ["+4", 4];
yield return ["2147483647", int.MaxValue];
var data = new TheoryData<string?, int?>();
data.Add("0", 0);
data.Add("3", 3);
data.Add(" 2 ", 2);
data.Add("+4", 4);
data.Add("2147483647", int.MaxValue);
data.Add(null, null);
data.Add("", null);
data.Add(" ", null);
data.Add("\t", null);
data.Add("unavailable", null);
data.Add("unknown", null);
data.Add("abc", null);
data.Add("1.5", null);
data.Add("-1", null);
data.Add("2147483648", null);
data.Add("1e3", null);
return data;
yield return [(string?)null, (int?)null];
yield return ["", (int?)null];
yield return [" ", (int?)null];
yield return ["\t", (int?)null];
yield return ["unavailable", (int?)null];
yield return ["unknown", (int?)null];
yield return ["abc", (int?)null];
yield return ["1.5", (int?)null];
yield return ["-1", (int?)null];
yield return ["2147483648", (int?)null];
yield return ["1e3", (int?)null];
}
/// <summary>
/// Water-ready detection samples around the heating threshold, including negative,
/// zero, exact-boundary, huge and NaN readings.
/// </summary>
public static IEnumerable<object?[]> PowerReadingSamples()
public static TheoryData<string, double, bool> PowerReadingSamples()
{
yield return ["negative sensor glitch", -1_000.0, true];
yield return ["just below zero", -0.001, true];
yield return ["zero", 0.0, true];
yield return ["just below threshold", 4.999, true];
yield return ["exactly at threshold", 5.0, false];
yield return ["just above threshold", 5.001, false];
yield return ["actively heating", 2_150.0, false];
yield return ["maximum", double.MaxValue, false];
yield return ["not-a-number", double.NaN, true];
var data = new TheoryData<string, double, bool>();
data.Add("negative sensor glitch", -1_000.0, true);
data.Add("just below zero", -0.001, true);
data.Add("zero", 0.0, true);
data.Add("just below threshold", 4.999, true);
data.Add("exactly at threshold", 5.0, false);
data.Add("just above threshold", 5.001, false);
data.Add("actively heating", 2_150.0, false);
data.Add("maximum", double.MaxValue, false);
data.Add("not-a-number", double.NaN, true);
return data;
}
/// <summary>
/// Elapsed-time sweep showing that the water-ready cooldown never expires on its own.
/// </summary>
public static IEnumerable<object?[]> GeneratedElapsedTimes()
public static TheoryData<TimeSpan> GeneratedElapsedTimes()
{
foreach (var hours in new[] { 1, 2, 6, 12, 23, 24, 25, 48, 168 })
{
yield return [TimeSpan.FromHours(hours)];
}
var data = new TheoryData<TimeSpan>();
data.Add(TimeSpan.FromHours(1));
data.Add(TimeSpan.FromHours(2));
data.Add(TimeSpan.FromHours(6));
data.Add(TimeSpan.FromHours(12));
data.Add(TimeSpan.FromHours(23));
data.Add(TimeSpan.FromHours(24));
data.Add(TimeSpan.FromHours(25));
data.Add(TimeSpan.FromHours(48));
data.Add(TimeSpan.FromHours(168));
return data;
}
/// <summary>
/// Cooldown restore samples around the UTC-midnight cut-off, including DST start/end
/// timestamps carrying non-zero offsets.
/// </summary>
public static IEnumerable<object?[]> CooldownRestoreSamples()
public static TheoryData<string, DateTimeOffset, DateTimeOffset, bool> CooldownRestoreSamples()
{
yield return ["30 minutes ago", BaseTime, BaseTime - TimeSpan.FromMinutes(30), true];
yield return ["5 hours ago", BaseTime, BaseTime - TimeSpan.FromHours(5), true];
yield return ["exactly at today's UTC midnight", BaseTime, TodayMidnight(), true];
yield return ["one tick after today's UTC midnight", BaseTime, TodayMidnight().AddTicks(1), true];
yield return ["one tick before today's UTC midnight", BaseTime, TodayMidnight().AddTicks(-1), false];
yield return ["one second before today's UTC midnight", BaseTime, TodayMidnight().AddSeconds(-1), false];
yield return ["yesterday same time", BaseTime, BaseTime.AddDays(-1), false];
yield return ["two days ago", BaseTime, BaseTime.AddDays(-2), false];
yield return ["future timestamp", BaseTime, BaseTime.AddHours(1), true];
yield return ["maximum timestamp", BaseTime, DateTimeOffset.MaxValue, true];
var data = new TheoryData<string, DateTimeOffset, DateTimeOffset, bool>();
data.Add("30 minutes ago", BaseTime, BaseTime - TimeSpan.FromMinutes(30), true);
data.Add("5 hours ago", BaseTime, BaseTime - TimeSpan.FromHours(5), true);
data.Add("exactly at today's UTC midnight", BaseTime, TodayMidnight(), true);
data.Add("one tick after today's UTC midnight", BaseTime, TodayMidnight().AddTicks(1), true);
data.Add("one tick before today's UTC midnight", BaseTime, TodayMidnight().AddTicks(-1), false);
data.Add("one second before today's UTC midnight", BaseTime, TodayMidnight().AddSeconds(-1), false);
data.Add("yesterday same time", BaseTime, BaseTime.AddDays(-1), false);
data.Add("two days ago", BaseTime, BaseTime.AddDays(-2), false);
data.Add("future timestamp", BaseTime, BaseTime.AddHours(1), true);
data.Add("maximum timestamp", BaseTime, DateTimeOffset.MaxValue, true);
// Europe DST starts 2026-03-29 02:00 CET → 03:00 CEST.
// now = 03:30+02:00 = 01:30Z, so UTC midnight is 2026-03-29T00:00Z.
yield return
[
data.Add(
"DST spring-forward, same UTC day",
new DateTimeOffset(2026, 3, 29, 3, 30, 0, TimeSpan.FromHours(2)),
new DateTimeOffset(2026, 3, 29, 1, 30, 0, TimeSpan.FromHours(1)),
true
];
yield return
[
);
data.Add(
"DST spring-forward, previous UTC day",
new DateTimeOffset(2026, 3, 29, 3, 30, 0, TimeSpan.FromHours(2)),
new DateTimeOffset(2026, 3, 28, 23, 30, 0, TimeSpan.FromHours(1)),
false
];
);
// Europe DST ends 2026-10-25 03:00 CEST → 02:00 CET. The 02:30 wall-clock hour occurs twice.
// now = 03:30+01:00 = 02:30Z, timestamp 02:30+02:00 = 00:30Z is still "today" in UTC.
yield return
[
data.Add(
"DST fall-back, ambiguous local hour",
new DateTimeOffset(2026, 10, 25, 3, 30, 0, TimeSpan.FromHours(1)),
new DateTimeOffset(2026, 10, 25, 2, 30, 0, TimeSpan.FromHours(2)),
true
];
);
yield return
[
data.Add(
"crossing midnight between restore and now",
new DateTimeOffset(2026, 4, 1, 0, 0, 1, TimeSpan.Zero),
new DateTimeOffset(2026, 3, 31, 23, 59, 59, TimeSpan.Zero),
false
];
);
return data;
}
private static DateTimeOffset TodayMidnight() =>