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:
@@ -21,6 +21,7 @@ public class LeakMonitorInitTests
|
||||
public LeakMonitorInitTests()
|
||||
{
|
||||
_haContextMock.Setup(h => h.StateAllChanges()).Returns(_stateChanges);
|
||||
_loggerMock.Setup(l => l.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
|
||||
}
|
||||
|
||||
private LeakMonitorInit CreateSut(LeakMonitorOptions? options = null)
|
||||
@@ -40,14 +41,12 @@ public class LeakMonitorInitTests
|
||||
|
||||
private void VerifyLogged(LogLevel level, Times times)
|
||||
{
|
||||
_loggerMock.Verify(
|
||||
l => l.Log(
|
||||
level,
|
||||
It.IsAny<EventId>(),
|
||||
It.IsAny<It.IsAnyType>(),
|
||||
It.IsAny<Exception?>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
|
||||
times);
|
||||
var logCount = _loggerMock.Invocations.Count(invocation =>
|
||||
invocation.Method.Name == nameof(ILogger.Log) &&
|
||||
invocation.Arguments[0] is LogLevel loggedLevel &&
|
||||
loggedLevel == level);
|
||||
|
||||
times.Validate(logCount);
|
||||
}
|
||||
|
||||
#region HandleStateChange
|
||||
@@ -170,7 +169,7 @@ public class LeakMonitorInitTests
|
||||
[MemberData(nameof(LeakMonitorTestData.BlankSensorIds), MemberType = typeof(LeakMonitorTestData))]
|
||||
public void InitializeAsync_WithBlankSensorId_ShouldStillSubscribeAndForward(string sensorEntityId)
|
||||
{
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(sensorEntityIds: [sensorEntityId]));
|
||||
CreateSut(LeakMonitorTestData.CreateOptions(sensorEntityIds: [sensorEntityId]));
|
||||
|
||||
_stateChanges.SubscriptionCount.Should().Be(1);
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ internal static class LeakMonitorTestData
|
||||
|
||||
public static readonly string[] DefaultSensorIds = [AquariumSensorId, BoilerSensorId];
|
||||
|
||||
public static readonly DateTimeOffset DefaultStart = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
private static readonly DateTimeOffset DefaultStart = new(2026, 1, 15, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
/// <summary>
|
||||
/// Builds leak monitor options with production-compatible defaults.
|
||||
@@ -63,8 +63,8 @@ internal static class LeakMonitorTestData
|
||||
/// States that must never send or dismiss a leak notification. Covers unavailable,
|
||||
/// unknown, blank, null and near-miss strings (the comparison is exact apart from casing).
|
||||
/// </summary>
|
||||
public static TheoryData<string?> NonActionableStateCases { get; } = new()
|
||||
{
|
||||
public static TheoryData<string?> NonActionableStateCases { get; } =
|
||||
[
|
||||
"unavailable",
|
||||
"UNAVAILABLE",
|
||||
"unknown",
|
||||
@@ -80,18 +80,18 @@ internal static class LeakMonitorTestData
|
||||
"1",
|
||||
"true",
|
||||
null
|
||||
};
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Blank but non-null sensor ids. Production does not validate ids, so these are
|
||||
/// subscribed and forwarded verbatim.
|
||||
/// </summary>
|
||||
public static TheoryData<string> BlankSensorIds { get; } = new()
|
||||
{
|
||||
public static TheoryData<string> BlankSensorIds { get; } =
|
||||
[
|
||||
string.Empty,
|
||||
" ",
|
||||
"\t"
|
||||
};
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// (sensor id, expected notification id) rows for the id sanitising rules:
|
||||
@@ -132,13 +132,13 @@ internal static class LeakMonitorTestData
|
||||
/// <summary>
|
||||
/// Repetition counts used to exercise duplicate suppression and zero-cooldown behaviour.
|
||||
/// </summary>
|
||||
public static TheoryData<int> RepeatedNotificationCounts { get; } = new()
|
||||
{
|
||||
public static TheoryData<int> RepeatedNotificationCounts { get; } =
|
||||
[
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
10
|
||||
};
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// A deterministic detection/recovery sequence. The repeated "on" transition is filtered
|
||||
@@ -157,7 +157,7 @@ internal static class LeakMonitorTestData
|
||||
/// <summary>
|
||||
/// Builds an <see cref="EntityState"/>, or <c>null</c> when the entity has no state at all.
|
||||
/// </summary>
|
||||
public static EntityState? CreateOptionalState(string? state) =>
|
||||
private static EntityState? CreateOptionalState(string? state) =>
|
||||
state is null ? null : new EntityState { State = state };
|
||||
|
||||
public static StateChange CreateStateChange(
|
||||
@@ -171,9 +171,7 @@ internal static class LeakMonitorTestData
|
||||
IHaContext haContext,
|
||||
string entityId,
|
||||
params (string? OldState, string? NewState)[] transitions) =>
|
||||
transitions
|
||||
.Select(transition => CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))
|
||||
.ToList();
|
||||
[.. transitions.Select(transition => CreateStateChange(haContext, entityId, transition.OldState, transition.NewState))];
|
||||
|
||||
/// <summary>One captured <c>IHaContext.CallService</c> invocation.</summary>
|
||||
public sealed record ServiceCall(string Domain, string Service, ServiceTarget? Target, object? Data);
|
||||
|
||||
@@ -85,7 +85,7 @@ public class LeakNotificationServiceTests
|
||||
var calls = LeakMonitorTestData.CaptureServiceCalls(_haContextMock);
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
var expectedMessage = $"Water detected by {sensorId}! Check immediately.";
|
||||
const string expectedMessage = $"Water detected by {sensorId}! Check immediately.";
|
||||
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
@@ -136,8 +136,11 @@ public class LeakNotificationServiceTests
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
var persistentCall = calls.Single(c =>
|
||||
c.Domain == LeakMonitorConsts.PersistentNotificationDomain &&
|
||||
c.Service == LeakMonitorConsts.PersistentNotificationCreateService);
|
||||
c is
|
||||
{
|
||||
Domain: LeakMonitorConsts.PersistentNotificationDomain,
|
||||
Service: LeakMonitorConsts.PersistentNotificationCreateService
|
||||
});
|
||||
|
||||
LeakMonitorTestData.SerializePayload(persistentCall.Data)
|
||||
.GetProperty("notification_id")
|
||||
@@ -345,7 +348,7 @@ public class LeakNotificationServiceTests
|
||||
public void BuildNotificationId_WithNullSensorId_ShouldThrow()
|
||||
{
|
||||
// Production has no null guard; the string.Replace call fails before any prefix is built.
|
||||
Action act = () => LeakNotificationService.BuildNotificationId(null!);
|
||||
var act = () => LeakNotificationService.BuildNotificationId(null!);
|
||||
|
||||
act.Should().Throw<NullReferenceException>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user