Add braces to single-statement guards and refactor NordPool price fetching
- Expand inline guard, return and logger blocks across the monitor services, init classes and tests - Have INordPoolPriceService take a per-date fetch delegate and expose ParseAndAdjust, moving the HA fetch call into NordPoolInit so the service can be registered singleton - Record the NordPool and boiler service lifetimes in AGENTS.md - Drop the unused persisted-state argument from the boiler test data helper
This commit is contained in:
@@ -2,10 +2,8 @@
|
||||
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;
|
||||
@@ -20,11 +18,9 @@ public class NordPoolPriceServiceTests
|
||||
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);
|
||||
_sut = new NordPoolPriceService(_mapperMock.Object, loggerMock.Object);
|
||||
}
|
||||
|
||||
#region GetTimeQuarterIndex
|
||||
@@ -680,6 +676,61 @@ public class NordPoolPriceServiceTests
|
||||
|
||||
#endregion
|
||||
|
||||
#region FetchPricesAsync
|
||||
|
||||
[Fact]
|
||||
public async Task FetchPricesAsync_WhenNoCachedPrices_ShouldFetchTodayAndTomorrowViaDelegate()
|
||||
{
|
||||
// Arrange
|
||||
var requestedDates = new List<DateOnly>();
|
||||
|
||||
Task<NordPoolPrice> Fetch(DateOnly date)
|
||||
{
|
||||
requestedDates.Add(date);
|
||||
return Task.FromResult(CreatePrices(1.5));
|
||||
}
|
||||
|
||||
// Act
|
||||
await _sut.FetchPricesAsync(Fetch);
|
||||
|
||||
// Assert
|
||||
requestedDates.Should().HaveCount(2);
|
||||
requestedDates[1].Should().Be(requestedDates[0].AddDays(1));
|
||||
_sut.TodayNordPoolPrice.Should().NotBeNull();
|
||||
_sut.TomorrowNordPoolPrice.Should().NotBeNull();
|
||||
_sut.TodayNordPoolPrice!.Prices![0].Price.Should().Be(1.5);
|
||||
_sut.TomorrowNordPoolPrice!.Prices![0].Price.Should().Be(1.5);
|
||||
_sut.LastFetchedDate.Should().Be(requestedDates[0]);
|
||||
_sut.LastUpdatedDate.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FetchPricesAsync_WhenCachedPricesAlreadyPresent_ShouldNotCallDelegate()
|
||||
{
|
||||
// Arrange
|
||||
var cetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var todayCet = DateOnly.FromDateTime(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cetTimeZone));
|
||||
|
||||
_sut.TodayNordPoolPrice = CreatePrices(1.0);
|
||||
_sut.TomorrowNordPoolPrice = CreatePrices(2.0);
|
||||
_sut.LastFetchedDate = todayCet;
|
||||
var delegateCalled = false;
|
||||
|
||||
// Act
|
||||
await _sut.FetchPricesAsync(_ =>
|
||||
{
|
||||
delegateCalled = true;
|
||||
return Task.FromResult(CreatePrices(3.0));
|
||||
});
|
||||
|
||||
// Assert
|
||||
delegateCalled.Should().BeFalse();
|
||||
_sut.TodayNordPoolPrice!.Prices![0].Price.Should().Be(1.0);
|
||||
_sut.TomorrowNordPoolPrice!.Prices![0].Price.Should().Be(2.0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ShouldFetchNewPrices
|
||||
|
||||
private static NordPoolPrice CreatePrices(params double?[] values)
|
||||
|
||||
Reference in New Issue
Block a user