77458383d1
- 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
135 lines
5.9 KiB
C#
135 lines
5.9 KiB
C#
using System.Reactive.Concurrency;
|
|
using BoilerMonitor.Models;
|
|
using FluentAssertions;
|
|
using HomeAssistantGenerated;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NordPool.Models;
|
|
using NordPoolManager;
|
|
using NordPoolManager.Interface;
|
|
using NordPoolManager.Models;
|
|
|
|
namespace NordPool.Manager.Tests;
|
|
|
|
public class NordPoolInitTests
|
|
{
|
|
private readonly Mock<INordPoolBoilerConfigProvider> _boilerConfigProviderMock;
|
|
private readonly Mock<ICheapestIntervalsFinder> _cheapestIntervalsFinderMock;
|
|
private readonly Entities _entities;
|
|
private readonly Mock<INordPoolEntityService> _entityServiceMock;
|
|
private readonly Mock<IHaContext> _haContextMock;
|
|
private readonly Mock<ILogger<NordPoolInit>> _loggerMock;
|
|
private readonly Mock<INordPoolPriceService> _priceServiceMock;
|
|
private readonly Mock<IScheduler> _schedulerMock;
|
|
private readonly Services _services;
|
|
|
|
public NordPoolInitTests()
|
|
{
|
|
_loggerMock = new Mock<ILogger<NordPoolInit>>();
|
|
_priceServiceMock = new Mock<INordPoolPriceService>();
|
|
_entityServiceMock = new Mock<INordPoolEntityService>();
|
|
_schedulerMock = new Mock<IScheduler>();
|
|
_schedulerMock.Setup(s => s.Now).Returns(DateTimeOffset.UtcNow);
|
|
_cheapestIntervalsFinderMock = new Mock<ICheapestIntervalsFinder>();
|
|
_boilerConfigProviderMock = new Mock<INordPoolBoilerConfigProvider>();
|
|
_haContextMock = new Mock<IHaContext>();
|
|
_entities = new Entities(_haContextMock.Object);
|
|
_services = new Services(_haContextMock.Object);
|
|
|
|
_priceServiceMock
|
|
.Setup(p => p.FetchPricesAsync(It.IsAny<Func<DateOnly, Task<NordPoolPrice>>>()))
|
|
.Returns(Task.CompletedTask);
|
|
_entityServiceMock.Setup(e => e.CleanupEntitiesAsync(It.IsAny<CancellationToken>()))
|
|
.Returns(Task.CompletedTask);
|
|
_entityServiceMock.Setup(e => e.CreateEntitiesAsync(It.IsAny<string>())).Returns(Task.CompletedTask);
|
|
_entityServiceMock.Setup(e => e.ResetEntitiesAsync()).Returns(Task.CompletedTask);
|
|
_entityServiceMock.Setup(e => e.UpdateEntitiesAsync(It.IsAny<EntityUpdateContext>()))
|
|
.Returns(Task.CompletedTask);
|
|
_priceServiceMock.Setup(p => p.GetCurrentPrice()).Returns(0.0);
|
|
_priceServiceMock.Setup(p => p.ShouldFetchNewPrices(It.IsAny<DateOnly>())).Returns(false);
|
|
_priceServiceMock.SetupGet(p => p.DayQuartersIndex).Returns(0);
|
|
_priceServiceMock.SetupGet(p => p.TodayNordPoolPrice).Returns((NordPoolPrice?)null);
|
|
_priceServiceMock.SetupGet(p => p.TomorrowNordPoolPrice).Returns((NordPoolPrice?)null);
|
|
_priceServiceMock.SetupGet(p => p.CurrentPrice).Returns(0.0);
|
|
_priceServiceMock.SetupGet(p => p.LastUpdatedDate).Returns((DateTime?)null);
|
|
_cheapestIntervalsFinderMock
|
|
.Setup(c => c.IsCurrentIntervalCheap(It.IsAny<IReadOnlyList<CheapestIntervalResult>>(),
|
|
It.IsAny<DateTime>()))
|
|
.Returns(false);
|
|
_boilerConfigProviderMock.Setup(b => b.GetCurrentConfiguration()).Returns(new BoilerConfig
|
|
{
|
|
IntervalCount = 4,
|
|
PriceCap = double.MaxValue,
|
|
Deadline = DateTime.Today.AddDays(1),
|
|
SkipPriceCap = false
|
|
});
|
|
}
|
|
|
|
private NordPoolInit CreateSut(NordPoolOptions? nordPoolOptions = null)
|
|
{
|
|
var options = Options.Create(
|
|
nordPoolOptions ?? new NordPoolOptions { CleanupEntitiesOnStartup = false });
|
|
|
|
return new NordPoolInit(
|
|
_entities,
|
|
_services,
|
|
_haContextMock.Object,
|
|
_loggerMock.Object,
|
|
_priceServiceMock.Object,
|
|
_entityServiceMock.Object,
|
|
_schedulerMock.Object,
|
|
_cheapestIntervalsFinderMock.Object,
|
|
_boilerConfigProviderMock.Object,
|
|
options,
|
|
TimeProvider.System);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_ShouldInitializeCorrectly()
|
|
{
|
|
// Arrange & Act
|
|
var sut = CreateSut();
|
|
|
|
// Assert
|
|
sut.Should().NotBeNull();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true)]
|
|
[InlineData(false)]
|
|
public async Task InitializeAsync_ShouldRespectCleanupOnStartupSetting(bool cleanupEntitiesOnStartup)
|
|
{
|
|
var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = cleanupEntitiesOnStartup });
|
|
|
|
await sut.InitializeAsync();
|
|
|
|
// The constructor also starts the same init flow in the background, so calls may happen more than once.
|
|
_entityServiceMock.Verify(
|
|
es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()),
|
|
cleanupEntitiesOnStartup ? Times.AtLeastOnce() : Times.Never());
|
|
_entityServiceMock.Verify(es => es.CreateEntitiesAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()),
|
|
Times.AtLeastOnce);
|
|
_entityServiceMock.Verify(es => es.ResetEntitiesAsync(It.IsAny<CancellationToken>()), Times.AtLeastOnce);
|
|
_entityServiceMock.Verify(
|
|
es => es.UpdateEntitiesAsync(It.IsAny<EntityUpdateContext>(), It.IsAny<CancellationToken>()),
|
|
Times.AtLeastOnce);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InitializeAsync_WhenCreatingEntitiesFails_ShouldRethrow()
|
|
{
|
|
var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = false });
|
|
_entityServiceMock
|
|
.Setup(es => es.CreateEntitiesAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
.ThrowsAsync(new InvalidOperationException("create failed"));
|
|
|
|
var act = sut.InitializeAsync;
|
|
|
|
await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("create failed");
|
|
_entityServiceMock.Verify(es => es.ResetEntitiesAsync(It.IsAny<CancellationToken>()), Times.Never);
|
|
_entityServiceMock.Verify(
|
|
es => es.UpdateEntitiesAsync(It.IsAny<EntityUpdateContext>(), It.IsAny<CancellationToken>()), Times.Never);
|
|
}
|
|
} |