Init
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
using FluentAssertions;
|
||||
using PowerMonitorManager;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
private readonly OtherPowerCalculationService _sut = new();
|
||||
|
||||
#region Normal Calculation
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WithValidValues_ShouldReturnDifference()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 3000.0;
|
||||
IReadOnlyList<double?> monitoredValues = [500.0, 300.0, 200.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(2000.0);
|
||||
result.SumOfMonitored.Should().Be(1000.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenMonitoredEqualsTotal_ShouldReturnZero()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 1000.0;
|
||||
IReadOnlyList<double?> monitoredValues = [400.0, 350.0, 250.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(0.0);
|
||||
result.SumOfMonitored.Should().Be(1000.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WithSingleMonitoredSensor_ShouldReturnDifference()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 5000.0;
|
||||
IReadOnlyList<double?> monitoredValues = [1500.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(3500.0);
|
||||
result.SumOfMonitored.Should().Be(1500.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WithFractionalValues_ShouldReturnPreciseDifference()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 1234.56;
|
||||
IReadOnlyList<double?> monitoredValues = [100.11, 200.22, 300.33];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().BeApproximately(633.9, 0.01);
|
||||
result.SumOfMonitored.Should().BeApproximately(600.66, 0.01);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Clamping to Zero
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenMonitoredExceedsTotal_ShouldClampToZero()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 500.0;
|
||||
IReadOnlyList<double?> monitoredValues = [300.0, 400.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(0.0);
|
||||
result.SumOfMonitored.Should().Be(700.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenTotalIsZeroWithMonitoredValues_ShouldClampToZero()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 0.0;
|
||||
IReadOnlyList<double?> monitoredValues = [100.0, 200.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(0.0);
|
||||
result.SumOfMonitored.Should().Be(300.0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Null and Unavailable Handling
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenTotalIsNull_ShouldTreatAsZero()
|
||||
{
|
||||
// Arrange
|
||||
IReadOnlyList<double?> monitoredValues = [100.0, 200.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(null, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(0.0);
|
||||
result.SumOfMonitored.Should().Be(300.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenSomeMonitoredAreNull_ShouldTreatNullsAsZero()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 1000.0;
|
||||
IReadOnlyList<double?> monitoredValues = [200.0, null, 300.0, null];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(500.0);
|
||||
result.SumOfMonitored.Should().Be(500.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenAllMonitoredAreNull_ShouldReturnTotal()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 1500.0;
|
||||
IReadOnlyList<double?> monitoredValues = [null, null, null];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(1500.0);
|
||||
result.SumOfMonitored.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WhenBothTotalAndMonitoredAreNull_ShouldReturnZero()
|
||||
{
|
||||
// Arrange
|
||||
IReadOnlyList<double?> monitoredValues = [null, null];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(null, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(0.0);
|
||||
result.SumOfMonitored.Should().Be(0.0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Edge Cases
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WithEmptyMonitoredList_ShouldReturnTotal()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 2500.0;
|
||||
IReadOnlyList<double?> monitoredValues = [];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(2500.0);
|
||||
result.SumOfMonitored.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WithAllZeroValues_ShouldReturnZero()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 0.0;
|
||||
IReadOnlyList<double?> monitoredValues = [0.0, 0.0, 0.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(0.0);
|
||||
result.SumOfMonitored.Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_WithVeryLargeValues_ShouldHandleCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 100_000.0;
|
||||
IReadOnlyList<double?> monitoredValues = [25_000.0, 30_000.0, 15_000.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().Be(30_000.0);
|
||||
result.SumOfMonitored.Should().Be(70_000.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CalculateOtherPower_ResultShouldNeverBeNegative()
|
||||
{
|
||||
// Arrange
|
||||
double? totalPower = 100.0;
|
||||
IReadOnlyList<double?> monitoredValues = [50.0, 60.0, 70.0, 80.0];
|
||||
|
||||
// Act
|
||||
var result = _sut.CalculateOtherPower(totalPower, monitoredValues);
|
||||
|
||||
// Assert
|
||||
result.OtherPower.Should().BeGreaterThanOrEqualTo(0.0);
|
||||
result.SumOfMonitored.Should().Be(260.0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="8.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.72" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PowerMonitor.Manager\PowerMonitor.Manager.csproj" />
|
||||
<ProjectReference Include="..\PowerMonitor.Models\PowerMonitor.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,285 @@
|
||||
using FluentAssertions;
|
||||
using HomeAssistant.Entities.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PowerMonitor.Models;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class PowerMonitorEntityServiceTests
|
||||
{
|
||||
private readonly Mock<IEntityManager> _entityManagerMock;
|
||||
private readonly Mock<ILogger<PowerMonitorEntityService>> _loggerMock;
|
||||
private readonly PowerMonitorEntityService _sut;
|
||||
|
||||
public PowerMonitorEntityServiceTests()
|
||||
{
|
||||
_entityManagerMock = new Mock<IEntityManager>();
|
||||
_loggerMock = new Mock<ILogger<PowerMonitorEntityService>>();
|
||||
_sut = new PowerMonitorEntityService(_entityManagerMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
#region CreateEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntityAsync_ShouldCreateMqttEntity()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.CreateEntityAsync(It.Is<PowerMonitorEntity>(e =>
|
||||
e.EntityId == "sensor.other_power_usage" &&
|
||||
e.Domain == "sensor" &&
|
||||
e.Identifier == "other_power_usage")),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntityAsync_WhenCreateFails_ShouldNotThrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Entity creation failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.CreateEntityAsync();
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResetEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntityAsync_ShouldSetStateToZero()
|
||||
{
|
||||
// Arrange
|
||||
_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);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.other_power_usage", "0"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntityAsync_ShouldSetDefaultAttributes()
|
||||
{
|
||||
// Arrange
|
||||
_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);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.other_power_usage", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UpdateEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntityAsync_ShouldSetStateWithFormattedPower()
|
||||
{
|
||||
// Arrange
|
||||
var context = new PowerUpdateContext
|
||||
{
|
||||
TotalHomePower = 3000.0,
|
||||
TotalMonitoredPower = 1500.0,
|
||||
OtherPower = 1500.0,
|
||||
MonitoredSensorCount = 21
|
||||
};
|
||||
|
||||
_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);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntityAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.other_power_usage", "1500.00"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntityAsync_ShouldSetAttributes()
|
||||
{
|
||||
// Arrange
|
||||
var context = new PowerUpdateContext
|
||||
{
|
||||
TotalHomePower = 5000.0,
|
||||
TotalMonitoredPower = 2000.0,
|
||||
OtherPower = 3000.0,
|
||||
MonitoredSensorCount = 15
|
||||
};
|
||||
|
||||
_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);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntityAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.other_power_usage", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntityAsync_WithZeroPower_ShouldSetZeroState()
|
||||
{
|
||||
// Arrange
|
||||
var context = new PowerUpdateContext
|
||||
{
|
||||
TotalHomePower = 0.0,
|
||||
TotalMonitoredPower = 0.0,
|
||||
OtherPower = 0.0,
|
||||
MonitoredSensorCount = 10
|
||||
};
|
||||
|
||||
_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);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntityAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.other_power_usage", "0.00"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CleanupEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupEntityAsync_ShouldRemoveEntity()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CleanupEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.RemoveEntity("sensor.other_power_usage"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupEntityAsync_WhenRemoveFails_ShouldRethrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Remove failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.CleanupEntityAsync();
|
||||
|
||||
// Assert
|
||||
await act.Should().ThrowAsync<InvalidOperationException>()
|
||||
.WithMessage("Remove failed");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetEntityStateSafe
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityStateSafe_ShouldCallSetEntityState()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.SetEntityStateSafe("sensor.test", "42");
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.test", "42"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityStateSafe_WhenExceptionThrown_ShouldNotRethrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.SetEntityStateSafe("sensor.test", "42");
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetEntityAttributesSafe
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityAttributesSafe_ShouldCallSetEntityAttributes()
|
||||
{
|
||||
// Arrange
|
||||
var attributes = new { unit_of_measurement = "W" };
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.SetEntityAttributesSafe("sensor.test", attributes);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.test", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityAttributesSafe_WhenExceptionThrown_ShouldNotRethrow()
|
||||
{
|
||||
// Arrange
|
||||
var attributes = new { unit_of_measurement = "W" };
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Attributes update failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.SetEntityAttributesSafe("sensor.test", attributes);
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user