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
872 lines
27 KiB
C#
872 lines
27 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using AutoMapper;
|
|
using FluentAssertions;
|
|
using HomeAssistantGenerated;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NordPool.Models;
|
|
using NordPool.Models.Dtos;
|
|
using NordPoolManager;
|
|
|
|
namespace NordPool.Manager.Tests;
|
|
|
|
public class NordPoolPriceServiceTests
|
|
{
|
|
private readonly Mock<IMapper> _mapperMock;
|
|
private readonly NordPoolPriceService _sut;
|
|
|
|
public NordPoolPriceServiceTests()
|
|
{
|
|
_mapperMock = new Mock<IMapper>();
|
|
var haContextMock = new Mock<IHaContext>();
|
|
var loggerMock = new Mock<ILogger<NordPoolPriceService>>();
|
|
|
|
var services = new Services(haContextMock.Object);
|
|
_sut = new NordPoolPriceService(services, _mapperMock.Object, loggerMock.Object);
|
|
}
|
|
|
|
#region GetTimeQuarterIndex
|
|
|
|
[Theory]
|
|
[InlineData(0, 0, 8)] // UTC 00:00 = CEST 02:00
|
|
[InlineData(1, 0, 12)] // UTC 01:00 = CEST 03:00
|
|
[InlineData(6, 0, 32)] // UTC 06:00 = CEST 08:00
|
|
[InlineData(12, 0, 56)] // UTC 12:00 = CEST 14:00
|
|
[InlineData(22, 0, 0)] // UTC 22:00 = CEST 00:00 (next day start)
|
|
[InlineData(22, 15, 1)] // UTC 22:15 = CEST 00:15
|
|
[InlineData(22, 30, 2)] // UTC 22:30 = CEST 00:30
|
|
[InlineData(22, 45, 3)] // UTC 22:45 = CEST 00:45
|
|
public void GetTimeQuarterIndex_ShouldReturnCorrectIndex(int hour, int minute, int expectedIndex)
|
|
{
|
|
// Arrange — 2026-03-31 is in CEST (UTC+2)
|
|
var testDate = new DateTime(2026, 3, 31, hour, minute, 0, DateTimeKind.Utc);
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.GetTimeQuarterIndex(testDate);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedIndex);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0, 15, 9)] // UTC 00:15 = CEST 02:15
|
|
[InlineData(1, 30, 14)] // UTC 01:30 = CEST 03:30
|
|
[InlineData(12, 0, 56)] // UTC 12:00 = CEST 14:00
|
|
[InlineData(22, 0, 0)] // UTC 22:00 = CEST 00:00
|
|
[InlineData(22, 59, 3)] // UTC 22:59 = CEST 00:59
|
|
public void GetTimeQuarterIndex_ShouldHandleAllHoursCorrectly(int hour, int minute, int expectedIndex)
|
|
{
|
|
// Arrange — 2026-03-31 is in CEST (UTC+2)
|
|
var testDate = new DateTime(2026, 3, 31, hour, minute, 0, DateTimeKind.Utc);
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.GetTimeQuarterIndex(testDate);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedIndex);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetTimeQuarterIndex_AtUtcMidnight_ShouldReturnCestOffset()
|
|
{
|
|
// Arrange — UTC 00:00 = CEST 02:00 -> index 8
|
|
var testDate = new DateTime(2026, 3, 31, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.GetTimeQuarterIndex(testDate);
|
|
|
|
// Assert
|
|
result.Should().Be(8);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetTimeQuarterIndex_AtEndOfCestDay_ShouldReturnIndex95()
|
|
{
|
|
// Arrange — CEST 23:45 = UTC 21:45 -> index 95
|
|
var testDate = new DateTime(2026, 3, 31, 21, 45, 0, DateTimeKind.Utc);
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.GetTimeQuarterIndex(testDate);
|
|
|
|
// Assert
|
|
result.Should().Be(95);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetTimeQuarterIndex_WithSeconds_ShouldIgnoreSeconds()
|
|
{
|
|
// Arrange
|
|
var withoutSeconds = new DateTime(2026, 3, 31, 12, 0, 0, DateTimeKind.Utc);
|
|
var withSeconds = new DateTime(2026, 3, 31, 12, 0, 59, DateTimeKind.Utc);
|
|
|
|
// Act
|
|
var result1 = NordPoolPriceService.GetTimeQuarterIndex(withoutSeconds);
|
|
var result2 = NordPoolPriceService.GetTimeQuarterIndex(withSeconds);
|
|
|
|
// Assert
|
|
result1.Should().Be(result2);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetTimeQuarterIndex_ShouldReturn96UniqueQuartersPerDay()
|
|
{
|
|
// Arrange & Act — iterate over a full UTC day; CET conversion produces 96 unique quarter indices
|
|
var indices = new HashSet<int>();
|
|
for (var hour = 0; hour < 24; hour++)
|
|
for (var minute = 0; minute < 60; minute += 15)
|
|
{
|
|
var date = new DateTime(2026, 3, 31, hour, minute, 0, DateTimeKind.Utc);
|
|
var index = NordPoolPriceService.GetTimeQuarterIndex(date);
|
|
indices.Add(index);
|
|
}
|
|
|
|
// Assert
|
|
indices.Should().HaveCount(96);
|
|
indices.Min().Should().Be(0);
|
|
indices.Max().Should().Be(95);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(2026, 1, 15, 8, 0, 36)] // CET winter (UTC+1): UTC 08:00 = CET 09:00 -> index 36
|
|
[InlineData(2026, 7, 15, 8, 0, 40)] // CEST summer (UTC+2): UTC 08:00 = CEST 10:00 -> index 40
|
|
public void GetTimeQuarterIndex_ShouldHandleDaylightSavingTimeCorrectly(int year, int month, int day, int hour,
|
|
int minute, int expectedIndex)
|
|
{
|
|
// Arrange
|
|
var testDate = new DateTime(year, month, day, hour, minute, 0, DateTimeKind.Utc);
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.GetTimeQuarterIndex(testDate);
|
|
|
|
// Assert
|
|
result.Should().Be(expectedIndex);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region PriceCorrection
|
|
|
|
[Theory]
|
|
[InlineData(100.0, 12.1)]
|
|
[InlineData(50.0, 6.05)]
|
|
[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
|
|
var result = NordPoolPriceService.PriceCorrection(mwPrice);
|
|
|
|
// Assert
|
|
result.Should().BeApproximately(expectedCentsPrice, 0.01);
|
|
}
|
|
|
|
[Fact]
|
|
public void PriceCorrection_ShouldApplyVatAndConversion()
|
|
{
|
|
// Act
|
|
var result = NordPoolPriceService.PriceCorrection(1000.0);
|
|
|
|
// Assert
|
|
result.Should().Be(121.0);
|
|
}
|
|
|
|
[Fact]
|
|
public void PriceCorrection_WithNegativePrice_ShouldCalculateCorrectly()
|
|
{
|
|
// Act
|
|
var result = NordPoolPriceService.PriceCorrection(-100.0);
|
|
|
|
// Assert
|
|
result.Should().BeApproximately(-12.1, 0.01);
|
|
}
|
|
|
|
[Fact]
|
|
public void PriceCorrection_WithVerySmallPrice_ShouldRoundCorrectly()
|
|
{
|
|
// Act
|
|
var result = NordPoolPriceService.PriceCorrection(0.001);
|
|
|
|
// Assert
|
|
result.Should().Be(0.0);
|
|
}
|
|
|
|
[Fact]
|
|
public void PriceCorrection_WithVeryLargePrice_ShouldCalculateCorrectly()
|
|
{
|
|
// Act
|
|
var result = NordPoolPriceService.PriceCorrection(10000.0);
|
|
|
|
// Assert
|
|
result.Should().BeApproximately(1210.0, 0.01);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ParseNordPoolResult
|
|
|
|
[Fact]
|
|
public void ParseNordPoolResult_WithNullElement_ShouldReturnEmptyNordPoolPrice()
|
|
{
|
|
// Act
|
|
var result = _sut.ParseNordPoolResult(null);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ParseNordPoolResult_WithValidElement_ShouldMapCorrectly()
|
|
{
|
|
// Arrange
|
|
var dto = new NordPoolPriceResultDto
|
|
{
|
|
Prices =
|
|
[
|
|
new NordPoolDayPriceDto
|
|
{
|
|
Start = DateTime.UtcNow.ToString("o"), End = DateTime.UtcNow.AddHours(1).ToString("o"),
|
|
Price = 100.0
|
|
}
|
|
]
|
|
};
|
|
|
|
var jsonString = JsonSerializer.Serialize(dto);
|
|
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonString);
|
|
|
|
var expectedPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new()
|
|
{
|
|
Start = DateTime.Parse(dto.Prices[0].Start!, CultureInfo.InvariantCulture),
|
|
End = DateTime.Parse(dto.Prices[0].End!, CultureInfo.InvariantCulture), Price = 100.0
|
|
}
|
|
}
|
|
};
|
|
|
|
_mapperMock.Setup(m => m.Map<NordPoolPriceResultDto, NordPoolPrice>(It.IsAny<NordPoolPriceResultDto>()))
|
|
.Returns(expectedPrice);
|
|
|
|
// Act
|
|
var result = _sut.ParseNordPoolResult(jsonElement);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().NotBeNull();
|
|
result.Prices.Should().HaveCount(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void ParseNordPoolResult_WhenMapperReturnsNull_ShouldReturnNull()
|
|
{
|
|
// Arrange
|
|
var dto = new NordPoolPriceResultDto
|
|
{
|
|
Prices =
|
|
[
|
|
new NordPoolDayPriceDto
|
|
{
|
|
Start = DateTime.UtcNow.ToString("o"), End = DateTime.UtcNow.AddHours(1).ToString("o"),
|
|
Price = 100.0
|
|
}
|
|
]
|
|
};
|
|
|
|
var jsonString = JsonSerializer.Serialize(dto);
|
|
var jsonElement = JsonSerializer.Deserialize<JsonElement>(jsonString);
|
|
|
|
_mapperMock.Setup(m => m.Map<NordPoolPriceResultDto, NordPoolPrice>(It.IsAny<NordPoolPriceResultDto>()))
|
|
.Returns((NordPoolPrice)null!);
|
|
|
|
// Act
|
|
var result = _sut.ParseNordPoolResult(jsonElement);
|
|
|
|
// Assert
|
|
result.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ParseNordPoolResult_WithEmptyJsonObject_ShouldReturnEmptyNordPoolPrice()
|
|
{
|
|
// Arrange
|
|
var jsonElement = JsonSerializer.Deserialize<JsonElement>("{}");
|
|
|
|
_mapperMock.Setup(m => m.Map<NordPoolPriceResultDto, NordPoolPrice>(It.IsAny<NordPoolPriceResultDto>()))
|
|
.Returns(new NordPoolPrice());
|
|
|
|
// Act
|
|
var result = _sut.ParseNordPoolResult(jsonElement);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region AdjustedNordPoolPrice
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_ShouldApplyPriceCorrectionToAllPrices()
|
|
{
|
|
// Arrange
|
|
var inputPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = DateTime.UtcNow, End = DateTime.UtcNow.AddHours(1), Price = 81.0 },
|
|
new() { Start = DateTime.UtcNow.AddHours(1), End = DateTime.UtcNow.AddHours(2), Price = 148.0 },
|
|
new() { Start = DateTime.UtcNow.AddHours(2), End = DateTime.UtcNow.AddHours(3), Price = 215.0 }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().HaveCount(3);
|
|
result.Prices![0].Price.Should().BeApproximately(9.80, 0.01);
|
|
result.Prices![1].Price.Should().BeApproximately(17.91, 0.01);
|
|
result.Prices![2].Price.Should().BeApproximately(26.02, 0.01);
|
|
}
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_WithNullPriceValue_ShouldSkipThatPrice()
|
|
{
|
|
// Arrange
|
|
var inputPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = DateTime.UtcNow, End = DateTime.UtcNow.AddHours(1), Price = 81.0 },
|
|
new() { Start = DateTime.UtcNow.AddHours(1), End = DateTime.UtcNow.AddHours(2), Price = null },
|
|
new() { Start = DateTime.UtcNow.AddHours(2), End = DateTime.UtcNow.AddHours(3), Price = 215.0 }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().HaveCount(2);
|
|
result.Prices.Should().NotContain(p => p.Price == null);
|
|
}
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_WithNullPricesList_ShouldReturnEmptyPricesList()
|
|
{
|
|
// Arrange
|
|
var inputPrice = new NordPoolPrice { Prices = null };
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().NotBeNull();
|
|
result.Prices.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_ShouldPreserveDateTimes()
|
|
{
|
|
// Arrange
|
|
var startTime = new DateTime(2026, 3, 31, 10, 0, 0, DateTimeKind.Utc);
|
|
var endTime = new DateTime(2026, 3, 31, 11, 0, 0, DateTimeKind.Utc);
|
|
|
|
var inputPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = startTime, End = endTime, Price = 81.0 }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices![0].Start.Should().Be(startTime);
|
|
result.Prices![0].End.Should().Be(endTime);
|
|
}
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_WithEmptyPricesList_ShouldReturnEmptyPricesList()
|
|
{
|
|
// Arrange
|
|
var inputPrice = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().NotBeNull();
|
|
result.Prices.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_WithAllNullPrices_ShouldReturnEmptyPricesList()
|
|
{
|
|
// Arrange
|
|
var inputPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = DateTime.UtcNow, End = DateTime.UtcNow.AddHours(1), Price = null },
|
|
new() { Start = DateTime.UtcNow.AddHours(1), End = DateTime.UtcNow.AddHours(2), Price = null }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Prices.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void AdjustedNordPoolPrice_ShouldNotModifyOriginalInput()
|
|
{
|
|
// Arrange
|
|
var inputPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice>
|
|
{
|
|
new() { Start = DateTime.UtcNow, End = DateTime.UtcNow.AddHours(1), Price = 81.0 }
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var result = NordPoolPriceService.AdjustedNordPoolPrice(inputPrice);
|
|
|
|
// Assert
|
|
inputPrice.Prices[0].Price.Should().Be(81.0);
|
|
result.Prices.Should().NotBeSameAs(inputPrice.Prices);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region UpdateCurrentPrice
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WithValidPriceAtIndex_ShouldReturnCorrectPrice()
|
|
{
|
|
// Arrange
|
|
var now = DateTime.UtcNow;
|
|
var prices = new List<NordPoolDayPrice>();
|
|
|
|
for (var i = 0; i < 96; i++)
|
|
prices.Add(new NordPoolDayPrice
|
|
{
|
|
Start = now.AddMinutes(i * 15),
|
|
End = now.AddMinutes((i + 1) * 15),
|
|
Price = 5.0 + i * 0.22
|
|
});
|
|
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
|
|
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
var result = _sut.GetCurrentPrice();
|
|
|
|
// Assert
|
|
result.Should().BeGreaterThan(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WhenNordPoolPriceIsNull_ShouldReturnZero()
|
|
{
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
var result = _sut.GetCurrentPrice();
|
|
|
|
// Assert
|
|
result.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WhenPricesListIsEmpty_ShouldReturnZero()
|
|
{
|
|
// Arrange
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
|
|
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
var result = _sut.GetCurrentPrice();
|
|
|
|
// Assert
|
|
result.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WhenPricesListIsNull_ShouldReturnZero()
|
|
{
|
|
// Arrange
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = null };
|
|
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
var result = _sut.GetCurrentPrice();
|
|
|
|
// Assert
|
|
result.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WhenSameQuarterIndex_ShouldReturnCachedPrice()
|
|
{
|
|
// Arrange
|
|
var expectedIndex = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
|
|
|
|
var prices = new List<NordPoolDayPrice>();
|
|
for (var i = 0; i < 96; i++)
|
|
prices.Add(new NordPoolDayPrice { Price = 4.0 + i * 0.23 });
|
|
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
|
|
_sut.DayQuartersIndex = expectedIndex;
|
|
_sut.CurrentPrice = 17.91;
|
|
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
var result = _sut.GetCurrentPrice();
|
|
|
|
// Assert
|
|
result.Should().Be(17.91);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WhenPriceAtIndexIsNull_ShouldReturnPreviousCurrentPrice()
|
|
{
|
|
// Arrange
|
|
var prices = new List<NordPoolDayPrice>();
|
|
for (var i = 0; i < 96; i++)
|
|
prices.Add(new NordPoolDayPrice { Price = null });
|
|
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
|
|
_sut.CurrentPrice = 17.91;
|
|
_sut.DayQuartersIndex = -1;
|
|
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
var result = _sut.GetCurrentPrice();
|
|
|
|
// Assert
|
|
result.Should().Be(17.91);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateCurrentPrice_WhenNewQuarterIndex_ShouldUpdateDayQuartersIndex()
|
|
{
|
|
// Arrange
|
|
var prices = new List<NordPoolDayPrice>();
|
|
for (var i = 0; i < 96; i++)
|
|
prices.Add(new NordPoolDayPrice { Price = 5.0 + i * 0.22 });
|
|
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = prices };
|
|
_sut.DayQuartersIndex = -1;
|
|
|
|
// Act
|
|
_sut.UpdateCurrentPrice();
|
|
|
|
// Assert
|
|
_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(_ => 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
|
|
|
|
private static NordPoolPrice CreatePrices(params double?[] values)
|
|
{
|
|
return new NordPoolPrice
|
|
{
|
|
Prices = [.. values.Select(price => new NordPoolDayPrice { Price = price })]
|
|
};
|
|
}
|
|
|
|
/// <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). xUnit cannot serialize
|
|
/// <see cref="NordPoolPrice" /> rows, so the matrix runs inside a single test.
|
|
/// </summary>
|
|
[Fact]
|
|
public void ShouldFetchNewPrices_ShouldMatchScenario()
|
|
{
|
|
var today = DateOnly.FromDateTime(DateTime.Now);
|
|
|
|
(string Scenario, DateOnly? LastFetchedDate, NordPoolPrice? TodayPrice, NordPoolPrice? TomorrowPrice, bool
|
|
Expected)[] cases =
|
|
[
|
|
("no fetch date yet", null, CreatePrices(12.50), CreatePrices(18.71), true),
|
|
("last fetch was yesterday", today.AddDays(-1), CreatePrices(12.50), CreatePrices(18.71), true),
|
|
("last fetch date is in the future", today.AddDays(1), CreatePrices(12.50), CreatePrices(18.71), false),
|
|
("today prices are null", today, new NordPoolPrice { Prices = null }, CreatePrices(18.71), true),
|
|
("today prices are empty", today, new NordPoolPrice { Prices = [] }, CreatePrices(18.71), true),
|
|
("today and tomorrow prices present", today, CreatePrices(12.50), CreatePrices(18.71), false),
|
|
("price values are null but entries exist", today, CreatePrices((double?)null),
|
|
CreatePrices((double?)null), false),
|
|
("all price payloads are null", today, null, null, true)
|
|
];
|
|
|
|
foreach (var (scenario, lastFetchedDate, todayPrice, tomorrowPrice, expected) in cases)
|
|
{
|
|
// Arrange
|
|
_sut.LastFetchedDate = lastFetchedDate;
|
|
_sut.TodayNordPoolPrice = todayPrice;
|
|
_sut.TomorrowNordPoolPrice = tomorrowPrice;
|
|
|
|
// Act
|
|
var result = _sut.ShouldFetchNewPrices(DateOnly.FromDateTime(DateTime.Now));
|
|
|
|
// Assert
|
|
result.Should().Be(expected, $"scenario: {scenario}");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldFetchNewPrices_WhenTomorrowPricesAreNull_ShouldDependOnHourOfDay()
|
|
{
|
|
// 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;
|
|
_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().Be(DateTime.Now.Hour >= 13);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldFetchNewPrices_WhenTomorrowPricesAreEmpty_ShouldDependOnHourOfDay()
|
|
{
|
|
// Arrange
|
|
var today = DateOnly.FromDateTime(DateTime.Now);
|
|
_sut.LastFetchedDate = today;
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice
|
|
{
|
|
Prices = new List<NordPoolDayPrice> { new() { Price = 12.50 } }
|
|
};
|
|
_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
|
|
_sut.RollOverPrices(today);
|
|
|
|
// Assert
|
|
_sut.TodayNordPoolPrice.Should().BeSameAs(tomorrow);
|
|
_sut.TomorrowNordPoolPrice.Should().BeNull();
|
|
_sut.DayQuartersIndex.Should().Be(-1);
|
|
_sut.LastFetchedDate.Should().Be(today);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Rows are not serializable as theory data, so the three unusable payloads are exercised in one test.
|
|
/// </summary>
|
|
[Fact]
|
|
public void RollOverPrices_WithoutUsableTomorrowPrices_ShouldResetTodayToEmptyPrice()
|
|
{
|
|
NordPoolPrice?[] unusablePrices = [null, new() { Prices = null }, new() { Prices = [] }];
|
|
|
|
foreach (var tomorrow in unusablePrices)
|
|
{
|
|
// Arrange
|
|
_sut.TodayNordPoolPrice = new NordPoolPrice { Prices = [new NordPoolDayPrice { 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 RollOverPrices_WhenBothPricesAreNull_ShouldNotThrow()
|
|
{
|
|
// Arrange
|
|
_sut.TodayNordPoolPrice = null;
|
|
_sut.TomorrowNordPoolPrice = null;
|
|
var today = new DateOnly(2026, 4, 1);
|
|
|
|
// Act
|
|
var act = () => _sut.RollOverPrices(today);
|
|
|
|
// Assert
|
|
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
|
|
} |