24729ee267
- 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
379 lines
14 KiB
C#
379 lines
14 KiB
C#
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NordPool.Models;
|
|
using NordPoolManager;
|
|
|
|
namespace NordPool.Manager.Tests;
|
|
|
|
public class CheapestIntervalsFinderTests
|
|
{
|
|
private const int Hour = 3_600;
|
|
private const int Quarter = 15 * 60;
|
|
private static readonly DateTime DayStart = NordPoolTestData.DayStart;
|
|
private static readonly DateTime DeadlineEndOfDay = DayStart.AddDays(1);
|
|
|
|
private readonly Mock<ILogger<CheapestIntervalsFinder>> _loggerMock = new();
|
|
|
|
private CheapestIntervalsFinder CreateSut()
|
|
{
|
|
return new CheapestIntervalsFinder(_loggerMock.Object);
|
|
}
|
|
|
|
private static bool IsPrice(double actual, double expected)
|
|
{
|
|
return Math.Abs(actual - expected) < 0.000_001;
|
|
}
|
|
|
|
private static NordPoolPrice CreatePricesForDay(DateTime dayStart,
|
|
int intervalCount = NordPoolConsts.MaxDailyIntervals)
|
|
{
|
|
return new NordPoolPrice
|
|
{
|
|
Prices = NordPoolTestData.CreateIntervals(intervalCount, i => 4.93 + i * 0.22, dayStart)
|
|
};
|
|
}
|
|
|
|
private static NordPoolPrice CreatePricesWithValues(DateTime dayStart, params double[] priceValues)
|
|
{
|
|
return new NordPoolPrice
|
|
{
|
|
Prices = NordPoolTestData.CreateIntervals(priceValues.Length, i => priceValues[i], dayStart)
|
|
};
|
|
}
|
|
|
|
private static CheapestIntervalResult Interval(int startSeconds, int endSeconds, double price = 5.0, int rank = 1)
|
|
{
|
|
return new CheapestIntervalResult
|
|
{
|
|
Start = DayStart.AddSeconds(startSeconds),
|
|
End = DayStart.AddSeconds(endSeconds),
|
|
Price = price,
|
|
Rank = rank
|
|
};
|
|
}
|
|
|
|
#region FindCheapestIntervals
|
|
|
|
public static TheoryData<string, double[], int, double, double, int> FilteringCases => new()
|
|
{
|
|
{ "deadline includes interval ending exactly at it", [4.93, 5.63, 6.20], 10, double.MaxValue, 1_800.0, 2 },
|
|
{ "deadline excludes interval ending one second late", [4.93, 5.63, 6.20], 10, double.MaxValue, 1_799.0, 1 },
|
|
{ "deadline before first interval", [4.93, 5.63, 6.20], 10, double.MaxValue, -1.0, 0 },
|
|
{ "price cap is inclusive", [12.50, 12.51], 10, 12.50, 86_400.0, 1 },
|
|
{ "zero price cap keeps non-positive prices", [-1.50, 0.0, 0.01, 2.0], 10, 0.0, 86_400.0, 2 },
|
|
{ "all prices above cap", [17.91, 24.92, 22.44], 4, 10.0, 86_400.0, 0 },
|
|
{ "fewer available than requested", [6.20, 8.96], 10, double.MaxValue, 86_400.0, 2 },
|
|
{ "zero intervals requested", [17.91, 6.20], 0, double.MaxValue, 86_400.0, 0 },
|
|
{ "negative intervals requested", [17.91, 6.20], -1, double.MaxValue, 86_400.0, 0 },
|
|
{ "deadline and price cap combined", [17.91, 6.20, 12.50, 4.93, 8.96, 2.50], 2, 10.0, 4_500.0, 2 },
|
|
{ "all prices equal", [12.50, 12.50, 12.50, 12.50, 12.50], 3, double.MaxValue, 86_400.0, 3 }
|
|
};
|
|
|
|
[Theory]
|
|
[MemberData(nameof(FilteringCases))]
|
|
public void FindCheapestIntervals_ShouldApplyFiltersAndReturnExpectedCount(
|
|
string scenario,
|
|
double[] priceValues,
|
|
int requestedIntervals,
|
|
double priceCap,
|
|
double deadlineSeconds,
|
|
int expectedCount)
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, priceValues);
|
|
var deadline = DayStart.AddSeconds(deadlineSeconds);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, requestedIntervals, priceCap, deadline);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(expectedCount, $"scenario: {scenario}");
|
|
result.Should().OnlyContain(r => r.End <= deadline && r.Price <= priceCap);
|
|
result.Select(r => r.Rank).Should().OnlyHaveUniqueItems();
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithValidPrices_ShouldReturnRequestedCount()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesForDay(DayStart);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(4);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_ShouldReturnCheapestByPrice()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, 17.91, 6.20, 12.50, 4.93, 8.96, 24.92, 5.63, 13.26);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 3, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(3);
|
|
var sortedByRank = result.OrderBy(r => r.Rank).ToList();
|
|
sortedByRank[0].Price.Should().Be(4.93);
|
|
sortedByRank[1].Price.Should().Be(5.63);
|
|
sortedByRank[2].Price.Should().Be(6.20);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_ShouldAssignCorrectRanks()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, 17.91, 8.96, 4.93, 13.26);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Single(r => IsPrice(r.Price, 4.93)).Rank.Should().Be(1);
|
|
result.Single(r => IsPrice(r.Price, 8.96)).Rank.Should().Be(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_ResultShouldBeSortedChronologically()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, 17.91, 6.20, 12.50, 4.93, 8.96);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 3, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().BeInAscendingOrder(r => r.Start);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithMaxPriceCap_ShouldIncludeAllIntervalsBeforeDeadline()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, 17.91, 6.20, 12.50, 4.93, 8.96, 24.92);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert — all six intervals are eligible, the four cheapest are taken
|
|
result.Should().HaveCount(4);
|
|
result.Select(r => r.Price).OrderBy(p => p).Should().ContainInOrder(4.93, 6.20, 8.96, 12.50);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithNullPrices_ShouldReturnEmpty()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = new NordPoolPrice { Prices = null };
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithEmptyPrices_ShouldReturnEmpty()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithEqualPrices_ShouldPreferEarliestStart()
|
|
{
|
|
// Arrange — four equally priced intervals, only two are requested
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, 5.0, 5.0, 5.0, 5.0);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 2, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert — tie-break is by Start ascending
|
|
result.Should().HaveCount(2);
|
|
result.Select(r => r.Start).Should().ContainInOrder(DayStart, DayStart.AddMinutes(15));
|
|
result.Select(r => r.Rank).Should().ContainInOrder(1, 2);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithNegativePrices_ShouldRankCheapestFirst()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = CreatePricesWithValues(DayStart, -2.10, 0.50, -7.40, 3.30);
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 2, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert — results are chronological, ranks still follow price order
|
|
result.Should().HaveCount(2);
|
|
result.Select(r => r.Price).Should().ContainInOrder(-2.10, -7.40);
|
|
result.Single(r => IsPrice(r.Price, -7.40)).Rank.Should().Be(1);
|
|
result.Single(r => IsPrice(r.Price, -2.10)).Rank.Should().Be(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithDuplicateIntervals_ShouldKeepBoth()
|
|
{
|
|
// Arrange — duplicate payload entries are not deduplicated
|
|
var sut = CreateSut();
|
|
var prices = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = DayStart, End = DayStart.AddMinutes(15), Price = 6.20 },
|
|
new() { Start = DayStart, End = DayStart.AddMinutes(15), Price = 6.20 }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 2, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(2);
|
|
result.Select(r => r.Rank).Should().BeEquivalentTo([1, 2]);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithNullPriceEntries_ShouldSkipNulls()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
var prices = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = DayStart, End = DayStart.AddMinutes(15), Price = 8.96 },
|
|
new() { Start = DayStart.AddMinutes(15), End = DayStart.AddMinutes(30), Price = null },
|
|
new() { Start = DayStart.AddMinutes(30), End = DayStart.AddMinutes(45), Price = 6.20 },
|
|
new() { Start = null, End = DayStart.AddMinutes(60), Price = 4.93 },
|
|
new() { Start = DayStart.AddMinutes(60), End = null, Price = 5.63 }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, 10, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
result.Should().HaveCount(2);
|
|
}
|
|
|
|
[Fact]
|
|
public void FindCheapestIntervals_WithGeneratedFullDay_ShouldSelectExactlyTheCheapestIntervals()
|
|
{
|
|
// Arrange — deterministic 96-interval dataset with negative, cheap and expensive prices
|
|
var sut = CreateSut();
|
|
var prices = new NordPoolPrice { Prices = NordPoolTestData.CreateGeneratedDay() };
|
|
const int requestedIntervals = 8;
|
|
const double priceCap = 12.0;
|
|
|
|
var expected = prices.Prices!
|
|
.Where(p => p.Price <= priceCap)
|
|
.OrderBy(p => p.Price)
|
|
.ThenBy(p => p.Start)
|
|
.Take(requestedIntervals)
|
|
.OrderBy(p => p.Start)
|
|
.ToList();
|
|
|
|
// Act
|
|
var result = sut.FindCheapestIntervals(prices, requestedIntervals, priceCap, DeadlineEndOfDay);
|
|
|
|
// Assert
|
|
expected.Should().HaveCount(requestedIntervals);
|
|
result.Should().HaveCount(requestedIntervals);
|
|
result.Select(r => r.Start).Should().ContainInOrder(expected.Select(p => p.Start!.Value));
|
|
result.Should().BeInAscendingOrder(r => r.Start);
|
|
result.Select(r => r.Rank).Should().BeEquivalentTo(Enumerable.Range(1, requestedIntervals));
|
|
result.Should().OnlyContain(r => r.Price <= priceCap);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsCurrentIntervalCheap
|
|
|
|
[Fact]
|
|
public void IsCurrentIntervalCheap_ShouldMatchIntervalBounds()
|
|
{
|
|
// Arrange - xUnit cannot serialize CheapestIntervalResult rows, so the matrix is exercised in one test
|
|
var sut = CreateSut();
|
|
(string Scenario, int NowSeconds, CheapestIntervalResult[] Intervals, bool Expected)[] cases =
|
|
[
|
|
("inside interval", 10 * Hour + 7 * 60, [Interval(10 * Hour, 10 * Hour + Quarter)], true),
|
|
("exactly at start", 10 * Hour, [Interval(10 * Hour, 10 * Hour + Quarter)], true),
|
|
("exactly at end", 10 * Hour + Quarter, [Interval(10 * Hour, 10 * Hour + Quarter)], false),
|
|
("outside all intervals", 12 * Hour,
|
|
[Interval(10 * Hour, 10 * Hour + Quarter), Interval(14 * Hour, 14 * Hour + Quarter)], false),
|
|
("boundary between adjacent intervals", 10 * Hour + Quarter,
|
|
[
|
|
Interval(10 * Hour, 10 * Hour + Quarter, 8.96),
|
|
Interval(10 * Hour + Quarter, 10 * Hour + 2 * Quarter, 6.20, 2)
|
|
], true),
|
|
("unsorted intervals", 14 * Hour + 5 * 60,
|
|
[Interval(14 * Hour, 14 * Hour + Quarter, 4.93, 2), Interval(10 * Hour, 10 * Hour + Quarter, 6.20)],
|
|
true),
|
|
("before first interval", 10 * Hour - 1, [Interval(10 * Hour, 10 * Hour + Quarter)], false),
|
|
("in gap between intervals", 10 * Hour + 2 * Quarter,
|
|
[Interval(10 * Hour, 10 * Hour + Quarter), Interval(11 * Hour, 11 * Hour + Quarter)], false),
|
|
("middle of three consecutive intervals", 10 * Hour + Quarter + 5 * 60,
|
|
[
|
|
Interval(10 * Hour, 10 * Hour + Quarter, rank: 1),
|
|
Interval(10 * Hour + Quarter, 10 * Hour + 2 * Quarter, rank: 2),
|
|
Interval(10 * Hour + 2 * Quarter, 10 * Hour + 3 * Quarter, rank: 3)
|
|
], true),
|
|
("empty interval list", 12 * Hour, [], false)
|
|
];
|
|
|
|
foreach (var (scenario, nowSeconds, intervals, expected) in cases)
|
|
{
|
|
var now = DayStart.AddSeconds(nowSeconds);
|
|
|
|
// Act
|
|
var result = sut.IsCurrentIntervalCheap(intervals, now);
|
|
|
|
// Assert
|
|
result.Should().Be(expected, $"scenario: {scenario}");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void IsCurrentIntervalCheap_WithGeneratedDay_ShouldBeTrueOnlyForSelectedQuarters()
|
|
{
|
|
// Arrange — pick the four cheapest intervals of a generated day and probe every quarter
|
|
var sut = CreateSut();
|
|
var prices = new NordPoolPrice { Prices = NordPoolTestData.CreateGeneratedDay() };
|
|
var cheapest = sut.FindCheapestIntervals(prices, 4, double.MaxValue, DeadlineEndOfDay);
|
|
|
|
for (var quarter = 0; quarter < NordPoolConsts.MaxDailyIntervals; quarter++)
|
|
{
|
|
var now = DayStart.AddMinutes(quarter * NordPoolConsts.MinutesPerInterval);
|
|
var expected = cheapest.Any(i => now >= i.Start && now < i.End);
|
|
|
|
// Act
|
|
var result = sut.IsCurrentIntervalCheap(cheapest, now);
|
|
|
|
// Assert
|
|
result.Should().Be(expected, $"quarter {quarter} at {now:O}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |