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:
@@ -1,105 +1,128 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using ShowerMonitor.Models;
|
||||
using ShowerMonitorManager;
|
||||
|
||||
namespace ShowerMonitor.Manager.Tests;
|
||||
|
||||
public class HumidityRateCalculatorTests
|
||||
{
|
||||
private const int DefaultWindowSize = 5;
|
||||
private const int DefaultWindowSize = ShowerMonitorConsts.DefaultRateWindowSize;
|
||||
|
||||
private readonly FakeTimeProvider _timeProvider = ShowerMonitorTestData.CreateTimeProvider();
|
||||
|
||||
private DateTimeOffset Now => _timeProvider.GetUtcNow();
|
||||
|
||||
public static IEnumerable<object[]> RateCases =>
|
||||
[
|
||||
["no readings", 5, Array.Empty<double>(), Array.Empty<int>(), 0.0],
|
||||
["single reading", 5, new[] { 52.9 }, new[] { 0 }, 0.0],
|
||||
["rising humidity", 5, new[] { 52.9, 62.9 }, new[] { 0, 60 }, 10.0],
|
||||
["falling humidity", 5, new[] { 62.9, 52.9 }, new[] { 0, 120 }, -5.0],
|
||||
["same timestamp", 5, new[] { 52.9, 62.9 }, new[] { 0, 0 }, 0.0],
|
||||
["flat humidity", 5, new[] { 55.0, 55.0, 55.0 }, new[] { 0, 60, 120 }, 0.0],
|
||||
["linear three readings", 5, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 5.0],
|
||||
["window drops stalest reading", 3, new[] { 52.9, 57.9, 62.9, 64.9 }, new[] { 0, 60, 120, 180 }, 3.5],
|
||||
["window keeps only last two readings", 2, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 5.0],
|
||||
["single-sample window always returns zero", 1, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 0.0],
|
||||
["zero-sized window always returns zero", 0, new[] { 50.0, 55.0, 60.0 }, new[] { 0, 60, 120 }, 0.0],
|
||||
["gap larger than window", 5, new[] { 20.0, 26.0 }, new[] { 0, 3_600 }, 0.1],
|
||||
["out of order timestamps", 5, new[] { 60.0, 50.0 }, new[] { 120, 0 }, 0.0],
|
||||
["very large rise", 5, new[] { 0.0, 10_000.0 }, new[] { 0, 60 }, 10_000.0],
|
||||
["negative humidity values", 5, new[] { -20.0, 0.0 }, new[] { 0, 60 }, 20.0],
|
||||
["tiny rise", 5, new[] { 50.0, 50.001 }, new[] { 0, 60 }, 0.001],
|
||||
["shower spike over sixty seconds", 5, new[] { 52.9, 59.9, 64.9 }, new[] { 0, 30, 60 }, 12.0],
|
||||
["middle readings do not affect the window endpoints", 3, new[] { 40.0, 80.0, 60.0, 45.0 }, new[] { 0, 30, 60, 90 }, -35.0],
|
||||
["small change over fifteen minutes", 5, new[] { 50.0, 50.6 }, new[] { 0, 900 }, 0.04]
|
||||
];
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RateCases))]
|
||||
public void GetRatePerMinute_ShouldReturnExpectedRate(
|
||||
string scenario,
|
||||
int windowSize,
|
||||
double[] humidities,
|
||||
int[] offsetSeconds,
|
||||
double expectedRate)
|
||||
{
|
||||
humidities.Should().HaveSameCount(offsetSeconds);
|
||||
var calculator = new HumidityRateCalculator(windowSize);
|
||||
|
||||
for (var i = 0; i < humidities.Length; i++)
|
||||
{
|
||||
calculator.RecordReading(humidities[i], Now.AddSeconds(offsetSeconds[i]));
|
||||
}
|
||||
|
||||
calculator.GetRatePerMinute().Should().BeApproximately(expectedRate, 1e-6, $"scenario: {scenario}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithNoReadings_ReturnsZero()
|
||||
public void GetRatePerMinute_WithGeneratedLinearSeries_ReturnsSlopeForEveryWindowSize()
|
||||
{
|
||||
const double ratePerMinute = 2.5;
|
||||
var series = ShowerMonitorTestData.CreateLinearHumiditySeries(
|
||||
startHumidity: 45.0,
|
||||
ratePerMinute: ratePerMinute,
|
||||
sampleCount: 12);
|
||||
|
||||
for (var windowSize = 2; windowSize <= series.Count; windowSize++)
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(windowSize);
|
||||
|
||||
foreach (var sample in series)
|
||||
{
|
||||
calculator.RecordReading(sample.Humidity, Now + sample.Offset);
|
||||
}
|
||||
|
||||
calculator.GetRatePerMinute()
|
||||
.Should()
|
||||
.BeApproximately(ratePerMinute, 1e-9, $"window size {windowSize}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_AfterReset_AndWithASingleNewReading_ReturnsZero()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
calculator.RecordReading(52.9, Now);
|
||||
calculator.RecordReading(62.9, Now.AddMinutes(1));
|
||||
|
||||
calculator.Reset();
|
||||
calculator.RecordReading(70.0, Now.AddMinutes(2));
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithSingleReading_ReturnsZero()
|
||||
public void PreviousReading_IsNullBeforeAnyReading()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
calculator.RecordReading(52.9, DateTimeOffset.UtcNow);
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
calculator.PreviousReading.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithRisingHumidity_ReturnsPositiveRate()
|
||||
public void PreviousReading_TracksMostRecentReadingEvenAfterWindowEviction()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
var calculator = new HumidityRateCalculator(2);
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(62.9, baseTime.AddMinutes(1));
|
||||
calculator.RecordReading(52.9, Now);
|
||||
calculator.RecordReading(57.9, Now.AddMinutes(1));
|
||||
calculator.RecordReading(64.9, Now.AddMinutes(2));
|
||||
|
||||
calculator.GetRatePerMinute().Should().BeApproximately(10.0, 0.01);
|
||||
calculator.PreviousReading.Should().Be(64.9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithFallingHumidity_ReturnsNegativeRate()
|
||||
public void Reset_ClearsReadingsAndPreviousReading()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(62.9, baseTime);
|
||||
calculator.RecordReading(52.9, baseTime.AddMinutes(2));
|
||||
|
||||
calculator.GetRatePerMinute().Should().BeApproximately(-5.0, 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_ExceedingWindowSize_DropsStalestReading()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(3);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(57.9, baseTime.AddMinutes(1));
|
||||
calculator.RecordReading(62.9, baseTime.AddMinutes(2));
|
||||
calculator.RecordReading(64.9, baseTime.AddMinutes(3));
|
||||
|
||||
var rate = calculator.GetRatePerMinute();
|
||||
rate.Should().BeApproximately(3.5, 0.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_WithSameTimestamp_ReturnsZero()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var sameTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, sameTime);
|
||||
calculator.RecordReading(62.9, sameTime);
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsAllReadings()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(62.9, baseTime.AddMinutes(1));
|
||||
calculator.RecordReading(52.9, Now);
|
||||
calculator.RecordReading(62.9, Now.AddMinutes(1));
|
||||
|
||||
calculator.Reset();
|
||||
|
||||
calculator.GetRatePerMinute().Should().Be(0.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRatePerMinute_SimulatesShowerSpike_DetectsHighRate()
|
||||
{
|
||||
var calculator = new HumidityRateCalculator(DefaultWindowSize);
|
||||
var baseTime = DateTimeOffset.UtcNow;
|
||||
|
||||
calculator.RecordReading(52.9, baseTime);
|
||||
calculator.RecordReading(59.9, baseTime.AddSeconds(30));
|
||||
calculator.RecordReading(64.9, baseTime.AddSeconds(60));
|
||||
|
||||
var rate = calculator.GetRatePerMinute();
|
||||
rate.Should().BeGreaterThan(4.0);
|
||||
calculator.PreviousReading.Should().BeNull();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user