Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -18,103 +18,181 @@ public class OtherPowerCalculationServiceTests
|
||||
|
||||
#region Basic calculation
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ShouldReturnOtherAsTotalMinusDevices()
|
||||
public static IEnumerable<object[]> CalculationCases =>
|
||||
[
|
||||
["two devices", 341.97, new[] { 8.55, 36.93 }, 296.49, "ok"],
|
||||
["no devices", 341.97, System.Array.Empty<double>(), 341.97, "ok"],
|
||||
["devices exceed total", 341.97, new[] { 200.0, 180.0 }, 0.0, "device_sum_exceeds_total"],
|
||||
["devices equal total", 100.0, new[] { 60.0, 40.0 }, 0.0, "ok"],
|
||||
["zero power, no devices", 0.0, System.Array.Empty<double>(), 0.0, "ok"],
|
||||
["negative total, no devices", -120.0, System.Array.Empty<double>(), 0.0, "device_sum_exceeds_total"],
|
||||
["negative device reading", 100.0, new[] { 150.0, -40.0 }, 0.0, "device_sum_exceeds_total"],
|
||||
["very large values", 1_000_000.0, new[] { 250_000.0, 250_000.0 }, 500_000.0, "ok"],
|
||||
["very small values", 0.5, new[] { 0.25 }, 0.25, "ok"]
|
||||
];
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CalculationCases))]
|
||||
public void Calculate_ShouldDeriveOtherPowerFromTotalAndDevices(
|
||||
string scenario,
|
||||
double totalPowerWatts,
|
||||
double[] deviceWatts,
|
||||
double expectedOtherWatts,
|
||||
string expectedDataQuality)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 8.55,
|
||||
["sensor.sonoff_10018909cb_power"] = 36.93
|
||||
};
|
||||
var devices = deviceWatts
|
||||
.Select((watts, index) => ($"sensor.device{index}_power", watts))
|
||||
.ToDictionary(entry => entry.Item1, entry => entry.Item2);
|
||||
|
||||
var result = sut.Calculate(341.97, devices);
|
||||
// Act
|
||||
var result = sut.Calculate(totalPowerWatts, devices);
|
||||
|
||||
result.TotalPowerWatts.Should().Be(341.97);
|
||||
result.KnownDevicesSumWatts.Should().BeApproximately(45.48, 0.01);
|
||||
result.RawOtherPowerWatts.Should().BeApproximately(296.49, 0.01);
|
||||
result.DataQuality.Should().Be("ok");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WhenDevicesSumExceedsTotal_ShouldClampToZero()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 200.0,
|
||||
["sensor.sonoff_10018909cb_power"] = 180.0
|
||||
};
|
||||
|
||||
var result = sut.Calculate(341.97, devices);
|
||||
|
||||
result.RawOtherPowerWatts.Should().Be(0);
|
||||
result.SmoothedOtherPowerWatts.Should().Be(0);
|
||||
result.DataQuality.Should().Be("device_sum_exceeds_total");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WithNoDevices_ShouldReturnTotalAsOther()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
var result = sut.Calculate(341.97, devices);
|
||||
|
||||
result.RawOtherPowerWatts.Should().Be(341.97);
|
||||
result.KnownDevicesSumWatts.Should().Be(0);
|
||||
// Assert
|
||||
result.TotalPowerWatts.Should().Be(totalPowerWatts, $"scenario: {scenario}");
|
||||
result.KnownDevicesSumWatts.Should().BeApproximately(deviceWatts.Sum(), 1e-6, $"scenario: {scenario}");
|
||||
result.RawOtherPowerWatts.Should().BeApproximately(expectedOtherWatts, 1e-6, $"scenario: {scenario}");
|
||||
result.SmoothedOtherPowerWatts.Should().BeApproximately(expectedOtherWatts, 1e-6, $"scenario: {scenario}");
|
||||
result.DataQuality.Should().Be(expectedDataQuality, $"scenario: {scenario}");
|
||||
result.DevicePowerReadings.Should().HaveCount(deviceWatts.Length, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ShouldIncludeAllDeviceReadingsInResult()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>
|
||||
{
|
||||
["sensor.sonoff_100215c534_power"] = 8.55,
|
||||
["sensor.sonoff_10018909cb_power"] = 36.93
|
||||
};
|
||||
var devices = PowerMonitorTestData.CreateCostDeviceReadings();
|
||||
|
||||
var result = sut.Calculate(341.97, devices);
|
||||
// Act
|
||||
var result = sut.Calculate(PowerMonitorTestData.DefaultTotalWatts, devices);
|
||||
|
||||
// Assert
|
||||
result.DevicePowerReadings.Should().HaveCount(2);
|
||||
result.DevicePowerReadings["sensor.sonoff_100215c534_power"].Should().Be(8.55);
|
||||
result.DevicePowerReadings["sensor.sonoff_10018909cb_power"].Should().Be(36.93);
|
||||
result.DevicePowerReadings["sensor.sonoff_100215c534_power"].Should().Be(101.0);
|
||||
result.DevicePowerReadings["sensor.sonoff_10018909cb_power"].Should().Be(109.93);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ShouldCopyDeviceReadingsInsteadOfAliasingThem()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double> { ["sensor.tv_power"] = 100.0 };
|
||||
|
||||
// Act
|
||||
var result = sut.Calculate(150.0, devices);
|
||||
devices["sensor.tv_power"] = 900.0;
|
||||
|
||||
// Assert
|
||||
result.DevicePowerReadings["sensor.tv_power"].Should().Be(100.0);
|
||||
result.KnownDevicesSumWatts.Should().Be(100.0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region EMA smoothing
|
||||
|
||||
[Fact]
|
||||
public void Calculate_FirstSample_ShouldReturnRawValueWithoutSmoothing()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
|
||||
// Act
|
||||
var result = sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result.SmoothedOtherPowerWatts.Should().Be(341.97);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WithSmoothing_ShouldApplyExponentialMovingAverage()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 4);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
var result1 = sut.Calculate(341.97, devices);
|
||||
result1.SmoothedOtherPowerWatts.Should().BeApproximately(341.97, 0.1);
|
||||
|
||||
var result2 = sut.Calculate(115.0, devices);
|
||||
|
||||
// Assert
|
||||
result1.SmoothedOtherPowerWatts.Should().BeApproximately(341.97, 0.1);
|
||||
result2.SmoothedOtherPowerWatts.Should().BeLessThan(341.97);
|
||||
result2.SmoothedOtherPowerWatts.Should().BeGreaterThan(115.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_MultipleCallsWithSameValue_ShouldConverge()
|
||||
public void Calculate_WithSmoothingWindowOfOne_ShouldFollowRawValueExactly()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 5);
|
||||
// Arrange - alpha becomes 1, so the previous value has no influence.
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
|
||||
// Act
|
||||
sut.Calculate(500.0, new Dictionary<string, double>());
|
||||
var result = sut.Calculate(25.0, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result.SmoothedOtherPowerWatts.Should().Be(25.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ShouldDampenSingleSpike()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
sut.Calculate(100.0, new Dictionary<string, double>());
|
||||
|
||||
// Act
|
||||
var result = sut.Calculate(10_000.0, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result.RawOtherPowerWatts.Should().Be(10_000.0);
|
||||
result.SmoothedOtherPowerWatts.Should().BeLessThan(result.RawOtherPowerWatts * 0.25);
|
||||
result.SmoothedOtherPowerWatts.Should().BeGreaterThan(100.0);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(2)]
|
||||
[InlineData(4)]
|
||||
[InlineData(5)]
|
||||
[InlineData(10)]
|
||||
[InlineData(20)]
|
||||
public void Calculate_MultipleCallsWithSameValue_ShouldConverge(int smoothingWindowSize)
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
PowerCalculationResult? result = null;
|
||||
for (var i = 0; i < 50; i++)
|
||||
for (var i = 0; i < 200; i++)
|
||||
{
|
||||
result = sut.Calculate(200.0, devices);
|
||||
}
|
||||
|
||||
// Assert
|
||||
result!.SmoothedOtherPowerWatts.Should().BeApproximately(200.0, 0.1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WithGeneratedSeries_ShouldNeverOvershootTheInputRange()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 5);
|
||||
var samples = PowerMonitorTestData.CreatePowerSampleSeries(50);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
var results = samples.Select(sample => sut.Calculate(sample, devices)).ToList();
|
||||
|
||||
// Assert
|
||||
results.Should().OnlyContain(r => r.SmoothedOtherPowerWatts >= samples.Min());
|
||||
results.Should().OnlyContain(r => r.SmoothedOtherPowerWatts <= samples.Max());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reset
|
||||
@@ -122,16 +200,33 @@ public class OtherPowerCalculationServiceTests
|
||||
[Fact]
|
||||
public void Reset_ShouldClearSmoothedState()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
var devices = new Dictionary<string, double>();
|
||||
sut.Calculate(341.97, devices);
|
||||
sut.Calculate(341.97, devices);
|
||||
|
||||
// Act
|
||||
sut.Reset();
|
||||
var result = sut.Calculate(115.0, devices);
|
||||
|
||||
// Assert
|
||||
result.SmoothedOtherPowerWatts.Should().BeApproximately(115.0, 0.1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_WhenCalledBeforeAnyCalculation_ShouldKeepFirstSampleRaw()
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
|
||||
// Act
|
||||
sut.Reset();
|
||||
var result = sut.Calculate(42.5, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result.SmoothedOtherPowerWatts.Should().Be(42.5);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user