Files
HomeAssistant/NordPool.Manager.Tests/NordPoolInitTests.cs
T
gitea_admin 17774b40b8 Convert test data sources to typed TheoryData
Replace untyped MemberData sources with TheoryData<...>, use collection
expressions for array and list literals, drop redundant casts and null
suppressions, and give log-assertion helpers an IsEnabled setup so guarded
log calls stay verifiable.
2026-11-29 21:42:41 +02:00

124 lines
5.6 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<ILogger<NordPoolInit>> _loggerMock;
private readonly Mock<INordPoolPriceService> _priceServiceMock;
private readonly Mock<INordPoolEntityService> _entityServiceMock;
private readonly Mock<IScheduler> _schedulerMock;
private readonly Mock<ICheapestIntervalsFinder> _cheapestIntervalsFinderMock;
private readonly Mock<INordPoolBoilerConfigProvider> _boilerConfigProviderMock;
private readonly Mock<IHaContext> _haContextMock;
private readonly Entities _entities;
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);
_priceServiceMock.Setup(p => p.FetchPricesAsync()).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,
_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);
}
}