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:
@@ -14,6 +14,70 @@ public class PowerCostCalculationServiceTests
|
||||
|
||||
private readonly PowerCostCalculationService _sut = new();
|
||||
|
||||
#region Snapshot defaults
|
||||
|
||||
[Theory]
|
||||
[InlineData(CostBucket.Total)]
|
||||
[InlineData(CostBucket.Other)]
|
||||
public void GetSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot(CostBucket bucket)
|
||||
{
|
||||
// Act
|
||||
var snapshot = bucket == CostBucket.Total
|
||||
? _sut.GetTotalCostSnapshot()
|
||||
: _sut.GetOtherCostSnapshot();
|
||||
|
||||
// Assert
|
||||
snapshot.Cost15Min.Should().Be(0);
|
||||
snapshot.Cost1Hour.Should().Be(0);
|
||||
snapshot.CostDaily.Should().Be(0);
|
||||
snapshot.CostMonthly.Should().Be(0);
|
||||
snapshot.PartialWindow.Should().BeTrue();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Thread safety
|
||||
|
||||
[Fact]
|
||||
public async Task RecordCosts_ConcurrentAccess_ShouldNotLoseData()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
const int callsPerThread = 100;
|
||||
const int threadCount = 10;
|
||||
|
||||
// Act
|
||||
var tasks = Enumerable.Range(0, threadCount)
|
||||
.Select(_ => Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < callsPerThread; i++)
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(1));
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// Assert
|
||||
var total = _sut.GetTotalCostSnapshot();
|
||||
total.CostDaily.Should().BeGreaterThan(0);
|
||||
|
||||
var devices = _sut.GetAllDeviceCostSnapshots();
|
||||
devices.Should().HaveCount(2);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static double ExpectedCost(
|
||||
double powerWatts,
|
||||
double pricePerKwh,
|
||||
int pollIntervalSeconds = PowerMonitorConsts.DefaultPollIntervalSeconds)
|
||||
{
|
||||
return powerWatts / PowerMonitorConsts.WattsPerKilowatt
|
||||
* pricePerKwh
|
||||
* ((double)pollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
|
||||
}
|
||||
|
||||
#region CalculateCostIncrement
|
||||
|
||||
public static TheoryData<string, double, double, int, double> CostIncrementCases => new()
|
||||
@@ -57,15 +121,14 @@ public class PowerCostCalculationServiceTests
|
||||
|
||||
// Act
|
||||
var costs = samples
|
||||
.Select(watts => PowerCostCalculationService.CalculateCostIncrement(watts, pricePerKwh, pollIntervalSeconds))
|
||||
.Select(watts =>
|
||||
PowerCostCalculationService.CalculateCostIncrement(watts, pricePerKwh, pollIntervalSeconds))
|
||||
.ToArray();
|
||||
|
||||
// Assert
|
||||
costs.Should().BeInAscendingOrder("every generated sample is larger than the previous one");
|
||||
for (var i = 0; i < samples.Count; i++)
|
||||
{
|
||||
costs[i].Should().BeApproximately(samples[i] / 1_000.0 * unitCost, 1e-15);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -80,11 +143,14 @@ public class PowerCostCalculationServiceTests
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, now);
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds,
|
||||
now);
|
||||
|
||||
// Assert
|
||||
var expectedTotal = ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
var expectedOther = ExpectedCost(PowerMonitorTestData.DefaultSmoothedOtherWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
var expectedTotal =
|
||||
ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
var expectedOther = ExpectedCost(PowerMonitorTestData.DefaultSmoothedOtherWatts,
|
||||
PowerMonitorTestData.DefaultPricePerKwh);
|
||||
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().BeApproximately(expectedTotal, 1e-12);
|
||||
_sut.GetOtherCostSnapshot().CostDaily.Should().BeApproximately(expectedOther, 1e-12);
|
||||
@@ -101,7 +167,8 @@ public class PowerCostCalculationServiceTests
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, now);
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds,
|
||||
now);
|
||||
|
||||
// Assert
|
||||
var snapshot = _sut.GetDeviceCostSnapshot(entityId);
|
||||
@@ -119,7 +186,8 @@ public class PowerCostCalculationServiceTests
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, now);
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds,
|
||||
now);
|
||||
|
||||
// Assert
|
||||
var total = _sut.GetTotalCostSnapshot();
|
||||
@@ -157,7 +225,8 @@ public class PowerCostCalculationServiceTests
|
||||
_sut.RecordCosts(firstResult, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(1);
|
||||
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(15));
|
||||
_sut.RecordCosts(PowerMonitorTestData.CreateCalculation(), PowerMonitorTestData.DefaultPricePerKwh, 15,
|
||||
now.AddSeconds(15));
|
||||
|
||||
// Assert
|
||||
_sut.GetAllDeviceCostSnapshots().Should().HaveCount(2);
|
||||
@@ -202,9 +271,9 @@ public class PowerCostCalculationServiceTests
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: 0,
|
||||
smoothedOtherWatts: 0,
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 0)));
|
||||
0,
|
||||
0,
|
||||
PowerMonitorTestData.CreateReadings(("sensor.sonoff_100215c534_power", 0)));
|
||||
|
||||
// Act
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now);
|
||||
@@ -321,9 +390,9 @@ public class PowerCostCalculationServiceTests
|
||||
for (var slot = 0; slot < timestamps.Count; slot++)
|
||||
{
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: wattSamples[slot],
|
||||
smoothedOtherWatts: wattSamples[slot] / 2,
|
||||
deviceReadings: new Dictionary<string, double>());
|
||||
wattSamples[slot],
|
||||
wattSamples[slot] / 2,
|
||||
new Dictionary<string, double>());
|
||||
_sut.RecordCosts(result, pricePerKwh, PowerMonitorConsts.DefaultPollIntervalSeconds, timestamps[slot]);
|
||||
}
|
||||
|
||||
@@ -348,9 +417,9 @@ public class PowerCostCalculationServiceTests
|
||||
{
|
||||
var watts = 500.0 + start.Hour * 125.0;
|
||||
var result = PowerMonitorTestData.CreateCalculation(
|
||||
totalWatts: watts,
|
||||
smoothedOtherWatts: watts / 4,
|
||||
deviceReadings: PowerMonitorTestData.CreateReadings(("sensor.tv_power", watts / 2)));
|
||||
watts,
|
||||
watts / 4,
|
||||
PowerMonitorTestData.CreateReadings(("sensor.tv_power", watts / 2)));
|
||||
_sut.RecordCosts(result, pricePerKwh, pollIntervalSeconds, start);
|
||||
expectedPerHour.Add(ExpectedCost(watts, pricePerKwh, pollIntervalSeconds));
|
||||
}
|
||||
@@ -407,7 +476,8 @@ public class PowerCostCalculationServiceTests
|
||||
// Assert
|
||||
_sut.LastKnownPrice.Should().Be(PowerMonitorTestData.DefaultPricePerKwh);
|
||||
_sut.PriceSource.Should().Be("live");
|
||||
var expected = 2 * ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
var expected =
|
||||
2 * ExpectedCost(PowerMonitorTestData.DefaultTotalWatts, PowerMonitorTestData.DefaultPricePerKwh);
|
||||
_sut.GetTotalCostSnapshot().CostDaily.Should().BeApproximately(expected, 1e-12);
|
||||
}
|
||||
|
||||
@@ -486,68 +556,4 @@ public class PowerCostCalculationServiceTests
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Snapshot defaults
|
||||
|
||||
[Theory]
|
||||
[InlineData(CostBucket.Total)]
|
||||
[InlineData(CostBucket.Other)]
|
||||
public void GetSnapshot_BeforeAnyRecording_ShouldReturnEmptySnapshot(CostBucket bucket)
|
||||
{
|
||||
// Act
|
||||
var snapshot = bucket == CostBucket.Total
|
||||
? _sut.GetTotalCostSnapshot()
|
||||
: _sut.GetOtherCostSnapshot();
|
||||
|
||||
// Assert
|
||||
snapshot.Cost15Min.Should().Be(0);
|
||||
snapshot.Cost1Hour.Should().Be(0);
|
||||
snapshot.CostDaily.Should().Be(0);
|
||||
snapshot.CostMonthly.Should().Be(0);
|
||||
snapshot.PartialWindow.Should().BeTrue();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Thread safety
|
||||
|
||||
[Fact]
|
||||
public async Task RecordCosts_ConcurrentAccess_ShouldNotLoseData()
|
||||
{
|
||||
// Arrange
|
||||
var now = PowerMonitorTestData.FixedNow;
|
||||
var result = PowerMonitorTestData.CreateCalculation();
|
||||
const int callsPerThread = 100;
|
||||
const int threadCount = 10;
|
||||
|
||||
// Act
|
||||
var tasks = Enumerable.Range(0, threadCount)
|
||||
.Select(_ => Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < callsPerThread; i++)
|
||||
{
|
||||
_sut.RecordCosts(result, PowerMonitorTestData.DefaultPricePerKwh, 15, now.AddSeconds(1));
|
||||
}
|
||||
}))
|
||||
.ToArray();
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
|
||||
// Assert
|
||||
var total = _sut.GetTotalCostSnapshot();
|
||||
total.CostDaily.Should().BeGreaterThan(0);
|
||||
|
||||
var devices = _sut.GetAllDeviceCostSnapshots();
|
||||
devices.Should().HaveCount(2);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static double ExpectedCost(
|
||||
double powerWatts,
|
||||
double pricePerKwh,
|
||||
int pollIntervalSeconds = PowerMonitorConsts.DefaultPollIntervalSeconds) =>
|
||||
powerWatts / PowerMonitorConsts.WattsPerKilowatt
|
||||
* pricePerKwh
|
||||
* ((double)pollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user