124 lines
4.9 KiB
C#
124 lines
4.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<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.UpdateCurrentPrice()).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,
|
|
_loggerMock.Object,
|
|
_priceServiceMock.Object,
|
|
_entityServiceMock.Object,
|
|
_schedulerMock.Object,
|
|
_cheapestIntervalsFinderMock.Object,
|
|
_boilerConfigProviderMock.Object,
|
|
options);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_ShouldInitializeCorrectly()
|
|
{
|
|
// Arrange & Act
|
|
var sut = CreateSut();
|
|
|
|
// Assert
|
|
sut.Should().NotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CleanupEntitiesAsync_ShouldDelegateToEntityService()
|
|
{
|
|
// Arrange
|
|
var sut = CreateSut();
|
|
_entityServiceMock.Setup(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()))
|
|
.Returns(Task.CompletedTask);
|
|
|
|
// Act
|
|
await sut.CleanupEntitiesAsync();
|
|
|
|
// Assert
|
|
_entityServiceMock.Verify(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InitializeAsync_WhenCleanupEntitiesOnStartupIsTrue_ShouldCallEntityCleanup()
|
|
{
|
|
var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = true });
|
|
|
|
await sut.InitializeAsync();
|
|
|
|
_entityServiceMock.Verify(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()), Times.AtLeastOnce);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InitializeAsync_WhenCleanupEntitiesOnStartupIsFalse_ShouldNotCallEntityCleanup()
|
|
{
|
|
var sut = CreateSut(new NordPoolOptions { CleanupEntitiesOnStartup = false });
|
|
|
|
await sut.InitializeAsync();
|
|
|
|
_entityServiceMock.Verify(es => es.CleanupEntitiesAsync(It.IsAny<CancellationToken>()), Times.Never);
|
|
}
|
|
}
|