Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -33,13 +33,65 @@ public class PowerMonitorEntityServiceTests
|
||||
public PowerMonitorEntityServiceTests()
|
||||
{
|
||||
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.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);
|
||||
}
|
||||
|
||||
#region GetDeviceCostEntityIds
|
||||
|
||||
[Theory]
|
||||
[InlineData("sonoff_abc")]
|
||||
[InlineData("tv")]
|
||||
[InlineData("akvariumas")]
|
||||
public void GetDeviceCostEntityIds_ShouldReturnFourEntityIds(string deviceName)
|
||||
{
|
||||
// Act
|
||||
var result = PowerMonitorEntityService.GetDeviceCostEntityIds(deviceName).ToList();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(4);
|
||||
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
|
||||
|
||||
#region CreateDeviceEntitiesAsync
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(1, 4)]
|
||||
[InlineData(2, 8)]
|
||||
[InlineData(5, 20)]
|
||||
public async Task CreateDeviceEntitiesAsync_ShouldCreateFourEntitiesPerSensor(int sensorCount, int expectedCreates)
|
||||
{
|
||||
// Arrange
|
||||
var sensors = Enumerable.Range(0, sensorCount)
|
||||
.Select(index => PowerMonitorTestData.CreateSensor($"sensor.device{index}_power", $"Device {index}"))
|
||||
.ToList();
|
||||
|
||||
// Act
|
||||
await _sut.CreateDeviceEntitiesAsync(sensors);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()),
|
||||
Times.Exactly(expectedCreates));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetSystemEntitiesList
|
||||
|
||||
[Fact]
|
||||
@@ -159,31 +211,6 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetDeviceCostEntityIds
|
||||
|
||||
[Theory]
|
||||
[InlineData("sonoff_abc")]
|
||||
[InlineData("tv")]
|
||||
[InlineData("akvariumas")]
|
||||
public void GetDeviceCostEntityIds_ShouldReturnFourEntityIds(string deviceName)
|
||||
{
|
||||
// Act
|
||||
var result = PowerMonitorEntityService.GetDeviceCostEntityIds(deviceName).ToList();
|
||||
|
||||
// Assert
|
||||
result.Should().HaveCount(4);
|
||||
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
|
||||
|
||||
#region CreateSystemEntitiesAsync
|
||||
|
||||
[Fact]
|
||||
@@ -201,7 +228,8 @@ public class PowerMonitorEntityServiceTests
|
||||
{
|
||||
// Arrange - both 1h cost entities fail, the remaining twelve must still be created.
|
||||
_entityManagerMock
|
||||
.Setup(em => em.CreateEntityAsync(It.Is<PowerMonitorEntity>(entity => entity.Identifier.EndsWith("_cost_1h"))))
|
||||
.Setup(em =>
|
||||
em.CreateEntityAsync(It.Is<PowerMonitorEntity>(entity => entity.Identifier.EndsWith("_cost_1h"))))
|
||||
.ThrowsAsync(new InvalidOperationException("Entity creation failed"));
|
||||
|
||||
// Act
|
||||
@@ -213,29 +241,6 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreateDeviceEntitiesAsync
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(1, 4)]
|
||||
[InlineData(2, 8)]
|
||||
[InlineData(5, 20)]
|
||||
public async Task CreateDeviceEntitiesAsync_ShouldCreateFourEntitiesPerSensor(int sensorCount, int expectedCreates)
|
||||
{
|
||||
// Arrange
|
||||
var sensors = Enumerable.Range(0, sensorCount)
|
||||
.Select(index => PowerMonitorTestData.CreateSensor($"sensor.device{index}_power", $"Device {index}"))
|
||||
.ToList();
|
||||
|
||||
// Act
|
||||
await _sut.CreateDeviceEntitiesAsync(sensors);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()), Times.Exactly(expectedCreates));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResetEntitiesAsync
|
||||
|
||||
[Theory]
|
||||
@@ -299,7 +304,8 @@ public class PowerMonitorEntityServiceTests
|
||||
await _sut.RemoveSystemEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity(It.Is<string>(id => SystemEntityIds.Contains(id))), Times.Exactly(14));
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity(It.Is<string>(id => SystemEntityIds.Contains(id))),
|
||||
Times.Exactly(14));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -399,11 +405,9 @@ public class PowerMonitorEntityServiceTests
|
||||
|
||||
// Assert
|
||||
foreach (var window in new[] { "15min", "1h", "daily", "monthly" })
|
||||
{
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState($"sensor.powermonitor_{deviceName}_cost_{window}", It.IsAny<string>()),
|
||||
Times.Once);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -412,13 +416,14 @@ public class PowerMonitorEntityServiceTests
|
||||
public async Task UpdateEntitiesAsync_ShouldProjectAnomalyState(bool isAnomaly, string expectedState)
|
||||
{
|
||||
// Arrange
|
||||
var context = PowerMonitorTestData.CreateUpdateContext(isAnomaly: isAnomaly);
|
||||
var context = PowerMonitorTestData.CreateUpdateContext(isAnomaly);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("binary_sensor.powermonitor_anomaly", expectedState), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("binary_sensor.powermonitor_anomaly", expectedState),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -450,7 +455,8 @@ public class PowerMonitorEntityServiceTests
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_total_power", "342.0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_other_power", "128.5"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_total_cost_daily", "1.0000"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_total_cost_daily", "1.0000"),
|
||||
Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_tv_cost_15min", "0.0005"), Times.Once);
|
||||
}
|
||||
|
||||
@@ -469,7 +475,8 @@ public class PowerMonitorEntityServiceTests
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.powermonitor_status", "ok"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.powermonitor_total_power", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.powermonitor_total_power", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -513,7 +520,7 @@ public class PowerMonitorEntityServiceTests
|
||||
public static TheoryData<string, Func<PowerMonitorEntityService, Task>> CancelledOperations()
|
||||
{
|
||||
var data = new TheoryData<string, Func<PowerMonitorEntityService, Task>>();
|
||||
var cancelled = new CancellationToken(canceled: true);
|
||||
var cancelled = new CancellationToken(true);
|
||||
List<PowerSensorInfo> sensors = [PowerMonitorTestData.CreateSensor("sensor.tv_power")];
|
||||
var context = PowerMonitorTestData.CreateUpdateContext();
|
||||
|
||||
@@ -545,4 +552,4 @@ public class PowerMonitorEntityServiceTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user