234 lines
6.3 KiB
C#
234 lines
6.3 KiB
C#
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
|
|
}
|