Convert test data sources to typed TheoryData
Replace untyped MemberData sources with TheoryData<...>, use collection expressions for array and list literals, drop redundant casts and null suppressions, and give log-assertion helpers an IsEnabled setup so guarded log calls stay verifiable.
This commit is contained in:
@@ -18,18 +18,18 @@ public class OtherPowerCalculationServiceTests
|
||||
|
||||
#region Basic calculation
|
||||
|
||||
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"]
|
||||
];
|
||||
public static TheoryData<string, double, double[], double, string> CalculationCases => new()
|
||||
{
|
||||
{ "two devices", 341.97, [8.55, 36.93], 296.49, "ok" },
|
||||
{ "no devices", 341.97, Array.Empty<double>(), 341.97, "ok" },
|
||||
{ "devices exceed total", 341.97, [200.0, 180.0], 0.0, "device_sum_exceeds_total" },
|
||||
{ "devices equal total", 100.0, [60.0, 40.0], 0.0, "ok" },
|
||||
{ "zero power, no devices", 0.0, Array.Empty<double>(), 0.0, "ok" },
|
||||
{ "negative total, no devices", -120.0, Array.Empty<double>(), 0.0, "device_sum_exceeds_total" },
|
||||
{ "negative device reading", 100.0, [150.0, -40.0], 0.0, "device_sum_exceeds_total" },
|
||||
{ "very large values", 1_000_000.0, [250_000.0, 250_000.0], 500_000.0, "ok" },
|
||||
{ "very small values", 0.5, [0.25], 0.25, "ok" }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CalculationCases))]
|
||||
@@ -43,8 +43,8 @@ public class OtherPowerCalculationServiceTests
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = deviceWatts
|
||||
.Select((watts, index) => ($"sensor.device{index}_power", watts))
|
||||
.ToDictionary(entry => entry.Item1, entry => entry.Item2);
|
||||
.Select((watts, index) => (entityId: $"sensor.device{index}_power", watts: watts))
|
||||
.ToDictionary(entry => entry.entityId, entry => entry.watts);
|
||||
|
||||
// Act
|
||||
var result = sut.Calculate(totalPowerWatts, devices);
|
||||
@@ -112,11 +112,10 @@ public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 4);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
var result1 = sut.Calculate(341.97, devices);
|
||||
var result2 = sut.Calculate(115.0, devices);
|
||||
var result1 = sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
var result2 = sut.Calculate(115.0, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result1.SmoothedOtherPowerWatts.Should().BeApproximately(341.97, 0.1);
|
||||
@@ -164,13 +163,12 @@ public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
// Act
|
||||
PowerCalculationResult? result = null;
|
||||
for (var i = 0; i < 200; i++)
|
||||
{
|
||||
result = sut.Calculate(200.0, devices);
|
||||
result = sut.Calculate(200.0, new Dictionary<string, double>());
|
||||
}
|
||||
|
||||
// Assert
|
||||
@@ -183,10 +181,9 @@ public class OtherPowerCalculationServiceTests
|
||||
// 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();
|
||||
var results = samples.Select(sample => sut.Calculate(sample, new Dictionary<string, double>())).ToList();
|
||||
|
||||
// Assert
|
||||
results.Should().OnlyContain(r => r.SmoothedOtherPowerWatts >= samples.Min());
|
||||
@@ -202,13 +199,12 @@ public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
// Arrange
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
var devices = new Dictionary<string, double>();
|
||||
sut.Calculate(341.97, devices);
|
||||
sut.Calculate(341.97, devices);
|
||||
sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
sut.Calculate(341.97, new Dictionary<string, double>());
|
||||
|
||||
// Act
|
||||
sut.Reset();
|
||||
var result = sut.Calculate(115.0, devices);
|
||||
var result = sut.Calculate(115.0, new Dictionary<string, double>());
|
||||
|
||||
// Assert
|
||||
result.SmoothedOtherPowerWatts.Should().BeApproximately(115.0, 0.1);
|
||||
|
||||
Reference in New Issue
Block a user