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:
2026-11-29 21:30:13 +02:00
parent 1152462765
commit 5777e641f6
79 changed files with 7162 additions and 3196 deletions
@@ -154,6 +154,14 @@ public class NordPoolPriceServiceTests
[InlineData(200.0, 24.2)]
[InlineData(0.0, 0.0)]
[InlineData(123.456, 14.94)]
[InlineData(1.0, 0.12)]
[InlineData(10.0, 1.21)]
[InlineData(250.0, 30.25)]
[InlineData(500.0, 60.5)]
[InlineData(1000.0, 121.0)]
[InlineData(-1000.0, -121.0)]
[InlineData(1234.5, 149.37)]
[InlineData(0.01, 0.0)]
public void PriceCorrection_ShouldCalculateCorrectPrice(double mwPrice, double expectedCentsPrice)
{
// Act
@@ -459,7 +467,8 @@ public class NordPoolPriceServiceTests
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
// Act
var result = _sut.UpdateCurrentPrice();
_sut.UpdateCurrentPrice();
var result = _sut.GetCurrentPrice();
// Assert
result.Should().BeGreaterThan(0);
@@ -469,7 +478,8 @@ public class NordPoolPriceServiceTests
public void UpdateCurrentPrice_WhenNordPoolPriceIsNull_ShouldReturnZero()
{
// Act
var result = _sut.UpdateCurrentPrice();
_sut.UpdateCurrentPrice();
var result = _sut.GetCurrentPrice();
// Assert
result.Should().Be(0);
@@ -482,7 +492,8 @@ public class NordPoolPriceServiceTests
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
// Act
var result = _sut.UpdateCurrentPrice();
_sut.UpdateCurrentPrice();
var result = _sut.GetCurrentPrice();
// Assert
result.Should().Be(0);
@@ -495,7 +506,8 @@ public class NordPoolPriceServiceTests
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = null };
// Act
var result = _sut.UpdateCurrentPrice();
_sut.UpdateCurrentPrice();
var result = _sut.GetCurrentPrice();
// Assert
result.Should().Be(0);
@@ -516,7 +528,8 @@ public class NordPoolPriceServiceTests
_sut.CurrentPrice = 17.91;
// Act
var result = _sut.UpdateCurrentPrice();
_sut.UpdateCurrentPrice();
var result = _sut.GetCurrentPrice();
// Assert
result.Should().Be(17.91);
@@ -535,7 +548,8 @@ public class NordPoolPriceServiceTests
_sut.DayQuartersIndex = -1;
// Act
var result = _sut.UpdateCurrentPrice();
_sut.UpdateCurrentPrice();
var result = _sut.GetCurrentPrice();
// Assert
result.Should().Be(17.91);
@@ -559,78 +573,164 @@ public class NordPoolPriceServiceTests
_sut.DayQuartersIndex.Should().BeGreaterThanOrEqualTo(0);
}
[Theory]
[InlineData(-3.25)]
[InlineData(0.0)]
[InlineData(0.01)]
[InlineData(123.45)]
[InlineData(999.99)]
public void UpdateCurrentPrice_WhenCurrentQuarterHasPrice_ShouldStoreThatPrice(double expectedPrice)
{
// Arrange — negative and zero spot prices are valid NordPool scenarios
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
var prices = NordPoolTestData.CreateIntervals(
NordPoolConsts.MaxDailyIntervals,
i => i == index ? expectedPrice : 10.0);
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
_sut.CurrentPrice = 17.91;
_sut.DayQuartersIndex = -1;
// Act
_sut.UpdateCurrentPrice();
// Assert
_sut.CurrentPrice.Should().Be(expectedPrice);
_sut.DayQuartersIndex.Should().Be(index);
}
[Fact]
public void UpdateCurrentPrice_WhenCurrentIndexIsOutsidePricesRange_ShouldKeepPreviousPriceButAdvanceIndex()
{
// Arrange — fewer prices than the current quarter index (e.g. partial day payload)
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
var prices = Enumerable.Range(0, index)
.Select(i => new NordPoolDayPrice { Price = 10.0 + i })
.ToList();
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
_sut.CurrentPrice = 17.91;
_sut.DayQuartersIndex = -1;
// Act
_sut.UpdateCurrentPrice();
// Assert
_sut.CurrentPrice.Should().Be(17.91);
_sut.DayQuartersIndex.Should().Be(index);
}
[Fact]
public void UpdateCurrentPrice_WhenQuarterIndexUnchangedButPricesChanged_ShouldKeepCachedPrice()
{
// Arrange
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
var prices = Enumerable.Range(0, NordPoolConsts.MaxDailyIntervals)
.Select(i => new NordPoolDayPrice { Price = 99.0 })
.ToList();
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
_sut.DayQuartersIndex = index;
_sut.CurrentPrice = 17.91;
// Act
_sut.UpdateCurrentPrice();
// Assert — same quarter means no recompute, cached value survives
_sut.CurrentPrice.Should().Be(17.91);
_sut.DayQuartersIndex.Should().Be(index);
}
[Fact]
public void UpdateCurrentPrice_WhenOnlyCurrentQuarterPriceIsNull_ShouldKeepPreviousPriceAndAdvanceIndex()
{
// Arrange
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
var prices = Enumerable.Range(0, NordPoolConsts.MaxDailyIntervals)
.Select(i => new NordPoolDayPrice { Price = i == index ? null : 10.0 })
.ToList();
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
_sut.CurrentPrice = 17.91;
_sut.DayQuartersIndex = (index + 1) % NordPoolConsts.MaxDailyIntervals;
// Act
_sut.UpdateCurrentPrice();
// Assert
_sut.CurrentPrice.Should().Be(17.91);
_sut.DayQuartersIndex.Should().Be(index);
}
#endregion
#region ShouldFetchNewPrices
[Fact]
public void ShouldFetchNewPrices_WhenNoLastFetchedDate_ShouldReturnTrue()
/// <summary>
/// Scenarios that do not depend on the time of day (tomorrow's prices are published after 13:00,
/// so the "tomorrow missing" cases are covered separately below).
/// </summary>
public static IEnumerable<object?[]> ShouldFetchScenarios()
{
var today = DateOnly.FromDateTime(DateTime.Now);
static NordPoolPrice Prices(params double?[] values) => new()
{
Prices = values.Select(price => new NordPoolDayPrice { Price = price }).ToList()
};
yield return ["no fetch date yet", (DateOnly?)null, Prices(12.50), Prices(18.71), true];
yield return ["last fetch was yesterday", today.AddDays(-1), Prices(12.50), Prices(18.71), true];
yield return ["last fetch date is in the future", today.AddDays(1), Prices(12.50), Prices(18.71), false];
yield return ["today prices are null", today, new NordPoolPrice { Prices = null }, Prices(18.71), true];
yield return ["today prices are empty", today, new NordPoolPrice { Prices = [] }, Prices(18.71), true];
yield return ["today and tomorrow prices present", today, Prices(12.50), Prices(18.71), false];
yield return ["price values are null but entries exist", today, Prices((double?)null), Prices((double?)null), false];
yield return ["all price payloads are null", today, (NordPoolPrice?)null, (NordPoolPrice?)null, true];
}
[Theory]
[MemberData(nameof(ShouldFetchScenarios))]
public void ShouldFetchNewPrices_ShouldMatchScenario(
string scenario,
DateOnly? lastFetchedDate,
NordPoolPrice? todayPrice,
NordPoolPrice? tomorrowPrice,
bool expected)
{
// Arrange
_sut.LastFetchedDate = lastFetchedDate;
_sut.TodayNordPoolPrice = todayPrice;
_sut.TomorrowNordPoolPrice = tomorrowPrice;
// Act
var result = _sut.ShouldFetchNewPrices(DateOnly.FromDateTime(DateTime.Now));
// Assert
result.Should().BeTrue();
result.Should().Be(expected, $"scenario: {scenario}");
}
[Fact]
public void ShouldFetchNewPrices_WhenLastFetchedDateIsYesterday_ShouldReturnTrue()
public void ShouldFetchNewPrices_WhenTomorrowPricesAreNull_ShouldDependOnHourOfDay()
{
// Arrange
// Arrange — tomorrow's prices are not published before 13:00, so cached data is acceptable until then
var today = DateOnly.FromDateTime(DateTime.Now);
_sut.LastFetchedDate = today.AddDays(-1);
_sut.LastFetchedDate = today;
_sut.TodayNordPoolPrice = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
_sut.TomorrowNordPoolPrice = new NordPoolPrice { Prices = null };
// Act
var result = _sut.ShouldFetchNewPrices(today);
// Assert
result.Should().BeTrue();
result.Should().Be(DateTime.Now.Hour >= 13);
}
[Fact]
public void ShouldFetchNewPrices_WhenTodayPricesAreNull_ShouldReturnTrue()
{
// Arrange
var today = DateOnly.FromDateTime(DateTime.Now);
_sut.LastFetchedDate = today;
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = null };
_sut.TomorrowNordPoolPrice = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
// Act
var result = _sut.ShouldFetchNewPrices(today);
// Assert
result.Should().BeTrue();
}
[Fact]
public void ShouldFetchNewPrices_WhenTodayPricesAreEmpty_ShouldReturnTrue()
{
// Arrange
var today = DateOnly.FromDateTime(DateTime.Now);
_sut.LastFetchedDate = today;
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
_sut.TomorrowNordPoolPrice = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
// Act
var result = _sut.ShouldFetchNewPrices(today);
// Assert
result.Should().BeTrue();
}
[Fact]
public void ShouldFetchNewPrices_WhenAllPricesAvailable_ShouldReturnFalse()
public void ShouldFetchNewPrices_WhenTomorrowPricesAreEmpty_ShouldDependOnHourOfDay()
{
// Arrange
var today = DateOnly.FromDateTime(DateTime.Now);
@@ -639,30 +739,117 @@ public class NordPoolPriceServiceTests
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
_sut.TomorrowNordPoolPrice = new NordPoolPrice
_sut.TomorrowNordPoolPrice = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
// Act
var result = _sut.ShouldFetchNewPrices(today);
// Assert
result.Should().Be(DateTime.Now.Hour >= 13);
}
#endregion
#region RollOverPrices
[Fact]
public void RollOverPrices_WithTomorrowPrices_ShouldPromoteTomorrowToToday()
{
// Arrange
var tomorrow = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 18.71 } }
};
_sut.TodayNordPoolPrice = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
_sut.TomorrowNordPoolPrice = tomorrow;
_sut.DayQuartersIndex = 48;
var today = new DateOnly(2026, 4, 1);
// Act
var result = _sut.ShouldFetchNewPrices(today);
_sut.RollOverPrices(today);
// Assert
result.Should().BeFalse();
_sut.TodayNordPoolPrice.Should().BeSameAs(tomorrow);
_sut.TomorrowNordPoolPrice.Should().BeNull();
_sut.DayQuartersIndex.Should().Be(-1);
_sut.LastFetchedDate.Should().Be(today);
}
public static IEnumerable<object?[]> UnusableTomorrowPrices =>
[
[null],
[new NordPoolPrice { Prices = null }],
[new NordPoolPrice { Prices = [] }]
];
[Theory]
[MemberData(nameof(UnusableTomorrowPrices))]
public void RollOverPrices_WithoutUsableTomorrowPrices_ShouldResetTodayToEmptyPrice(NordPoolPrice? tomorrow)
{
// Arrange
_sut.TodayNordPoolPrice = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
_sut.TomorrowNordPoolPrice = tomorrow;
_sut.DayQuartersIndex = 42;
var today = new DateOnly(2026, 4, 1);
// Act
_sut.RollOverPrices(today);
// Assert — nothing usable to promote, so today becomes an empty payload
_sut.TodayNordPoolPrice.Should().NotBeNull();
_sut.TodayNordPoolPrice!.Prices.Should().BeNull();
_sut.TomorrowNordPoolPrice.Should().BeNull();
_sut.DayQuartersIndex.Should().Be(-1);
_sut.LastFetchedDate.Should().Be(today);
}
[Fact]
public void ShouldFetchNewPrices_WhenNordPoolPriceIsNull_ShouldReturnTrue()
public void RollOverPrices_WhenBothPricesAreNull_ShouldNotThrow()
{
// Arrange
var today = DateOnly.FromDateTime(DateTime.Now);
_sut.LastFetchedDate = today;
_sut.TodayNordPoolPrice = null;
_sut.TomorrowNordPoolPrice = null;
var today = new DateOnly(2026, 4, 1);
// Act
var result = _sut.ShouldFetchNewPrices(today);
var act = () => _sut.RollOverPrices(today);
// Assert
result.Should().BeTrue();
act.Should().NotThrow();
_sut.TodayNordPoolPrice.Should().NotBeNull();
_sut.TodayNordPoolPrice!.Prices.Should().BeNull();
}
[Fact]
public void RollOverPrices_ShouldDiscardPreviousTodayPrices()
{
// Arrange
var previousToday = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
};
var tomorrow = new NordPoolPrice
{
Prices = new List<NordPoolDayPrice> { new() { Price = 18.71 } }
};
_sut.TodayNordPoolPrice = previousToday;
_sut.TomorrowNordPoolPrice = tomorrow;
// Act
_sut.RollOverPrices(new DateOnly(2026, 4, 1));
// Assert — the old today payload is replaced, not mutated
_sut.TodayNordPoolPrice.Should().BeSameAs(tomorrow);
previousToday.Prices.Should().HaveCount(1);
previousToday.Prices![0].Price.Should().Be(12.50);
}
#endregion