Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using FluentAssertions;
|
||||
using LeakMonitor.Models;
|
||||
using LeakMonitorManager;
|
||||
@@ -8,59 +10,61 @@ using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace LeakMonitor.Manager.Tests;
|
||||
|
||||
public class LeakNotificationServiceTests
|
||||
{
|
||||
private readonly Mock<IHaContext> _haContextMock = new();
|
||||
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock = new();
|
||||
private readonly LeakMonitorTestData.ManualTimeProvider _timeProvider = new();
|
||||
private readonly Mock<IHaContext> _haContextMock = new Mock<IHaContext>();
|
||||
private readonly Mock<ILogger<LeakNotificationService>> _loggerMock = new Mock<ILogger<LeakNotificationService>>();
|
||||
private readonly LeakMonitorTestData.ManualTimeProvider _timeProvider = new LeakMonitorTestData.ManualTimeProvider();
|
||||
|
||||
private LeakNotificationService CreateSut(LeakMonitorOptions? options = null)
|
||||
{
|
||||
return new LeakNotificationService(
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object,
|
||||
Options.Create(options ?? LeakMonitorTestData.CreateOptions()),
|
||||
_timeProvider);
|
||||
_haContextMock.Object,
|
||||
_loggerMock.Object,
|
||||
Options.Create(options ?? LeakMonitorTestData.CreateOptions()),
|
||||
_timeProvider);
|
||||
}
|
||||
|
||||
private void VerifyMobilePush(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
}
|
||||
|
||||
private void VerifyPersistentCreate(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationCreateService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
}
|
||||
|
||||
private void VerifyPersistentDismiss(Times times)
|
||||
{
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
h => h.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationDismissService,
|
||||
It.IsAny<ServiceTarget?>(),
|
||||
It.IsAny<object?>()),
|
||||
times);
|
||||
}
|
||||
|
||||
#region SendLeakAlert
|
||||
|
||||
public static TheoryData<string, string> SendAlertServiceCalls { get; } = new()
|
||||
public static TheoryData<string, string> SendAlertServiceCalls { get; } = new TheoryData<string, string>
|
||||
{
|
||||
{ LeakMonitorConsts.NotifyDomain, LeakMonitorConsts.MobileAppService },
|
||||
{ LeakMonitorConsts.PersistentNotificationDomain, LeakMonitorConsts.PersistentNotificationCreateService }
|
||||
@@ -75,8 +79,8 @@ public class LeakNotificationServiceTests
|
||||
sut.SendLeakAlert(LeakMonitorTestData.AquariumSensorId);
|
||||
|
||||
_haContextMock.Verify(
|
||||
h => h.CallService(domain, service, It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once());
|
||||
h => h.CallService(domain, service, It.IsAny<ServiceTarget?>(), It.IsAny<object?>()),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -110,8 +114,11 @@ public class LeakNotificationServiceTests
|
||||
var persistentPayload = LeakMonitorTestData.SerializePayload(persistentCall.Data);
|
||||
persistentPayload.GetProperty("title").GetString().Should().Be(LeakMonitorConsts.NotificationTitle);
|
||||
persistentPayload.GetProperty("message").GetString().Should().Be(expectedMessage);
|
||||
persistentPayload.GetProperty("notification_id").GetString().Should()
|
||||
.Be(LeakNotificationService.BuildNotificationId(sensorId));
|
||||
|
||||
persistentPayload.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(LeakNotificationService.BuildNotificationId(sensorId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -136,17 +143,17 @@ public class LeakNotificationServiceTests
|
||||
sut.SendLeakAlert(sensorId);
|
||||
|
||||
var persistentCall = calls.Single(c =>
|
||||
c is
|
||||
{
|
||||
Domain: LeakMonitorConsts.PersistentNotificationDomain,
|
||||
Service: LeakMonitorConsts.PersistentNotificationCreateService
|
||||
});
|
||||
c is
|
||||
{
|
||||
Domain: LeakMonitorConsts.PersistentNotificationDomain,
|
||||
Service: LeakMonitorConsts.PersistentNotificationCreateService
|
||||
});
|
||||
|
||||
LeakMonitorTestData.SerializePayload(persistentCall.Data)
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -156,7 +163,10 @@ public class LeakNotificationServiceTests
|
||||
var sut = CreateSut();
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++) sut.SendLeakAlert(sensorId);
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Once());
|
||||
VerifyPersistentCreate(Times.Once());
|
||||
@@ -169,7 +179,10 @@ public class LeakNotificationServiceTests
|
||||
var sut = CreateSut(LeakMonitorTestData.CreateOptions(notificationCooldownMinutes: 0));
|
||||
const string sensorId = LeakMonitorTestData.AquariumSensorId;
|
||||
|
||||
for (var i = 0; i < repeats; i++) sut.SendLeakAlert(sensorId);
|
||||
for (var i = 0; i < repeats; i++)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Exactly(repeats));
|
||||
VerifyPersistentCreate(Times.Exactly(repeats));
|
||||
@@ -214,7 +227,10 @@ public class LeakNotificationServiceTests
|
||||
{
|
||||
var sut = CreateSut();
|
||||
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds) sut.SendLeakAlert(sensorId);
|
||||
foreach (var sensorId in LeakMonitorTestData.DefaultSensorIds)
|
||||
{
|
||||
sut.SendLeakAlert(sensorId);
|
||||
}
|
||||
|
||||
VerifyMobilePush(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
VerifyPersistentCreate(Times.Exactly(LeakMonitorTestData.DefaultSensorIds.Length));
|
||||
@@ -236,11 +252,11 @@ public class LeakNotificationServiceTests
|
||||
VerifyMobilePush(Times.Exactly(2));
|
||||
|
||||
var boilerNotifications = calls
|
||||
.Where(c => c.Domain == LeakMonitorConsts.NotifyDomain)
|
||||
.Where(c => LeakMonitorTestData.SerializePayload(c.Data)
|
||||
.GetProperty("message")
|
||||
.GetString()!
|
||||
.Contains(LeakMonitorTestData.BoilerSensorId, StringComparison.Ordinal));
|
||||
.Where(c => c.Domain == LeakMonitorConsts.NotifyDomain)
|
||||
.Where(c => LeakMonitorTestData.SerializePayload(c.Data)
|
||||
.GetProperty("message")
|
||||
.GetString()!
|
||||
.Contains(LeakMonitorTestData.BoilerSensorId, StringComparison.Ordinal));
|
||||
|
||||
boilerNotifications.Should().ContainSingle();
|
||||
}
|
||||
@@ -286,11 +302,12 @@ public class LeakNotificationServiceTests
|
||||
dismissCall.Domain.Should().Be(LeakMonitorConsts.PersistentNotificationDomain);
|
||||
dismissCall.Service.Should().Be(LeakMonitorConsts.PersistentNotificationDismissService);
|
||||
dismissCall.Target.Should().BeNull();
|
||||
|
||||
LeakMonitorTestData.SerializePayload(dismissCall.Data)
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
.GetProperty("notification_id")
|
||||
.GetString()
|
||||
.Should()
|
||||
.Be(expectedNotificationId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user