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:
@@ -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() =>
|
||||
|
||||
Reference in New Issue
Block a user