28eddc5dc0
- Introduced `CancellationToken` to service methods for improved task cancellation handling. - Centralized reusable constants (`MinutesPerInterval`, `WaterReadyPowerThresholdWatts`) in `NordPoolConsts`. - Refactored `ConfigureAwait` usage and improved logging within `NordPoolEntityService`. - Adjusted tests to accommodate cancellation token changes.
75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
using System.Reactive.Concurrency;
|
|
using FluentAssertions;
|
|
using HomeAssistantGenerated;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using NordPoolManager;
|
|
using NordPoolManager.Interface;
|
|
|
|
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<IBoilerControlService> _boilerControlServiceMock;
|
|
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>();
|
|
_boilerControlServiceMock = new Mock<IBoilerControlService>();
|
|
_haContextMock = new Mock<IHaContext>();
|
|
_entities = new Entities(_haContextMock.Object);
|
|
}
|
|
|
|
private NordPoolInit CreateSut()
|
|
{
|
|
return new NordPoolInit(
|
|
_entities,
|
|
_haContextMock.Object,
|
|
_loggerMock.Object,
|
|
_priceServiceMock.Object,
|
|
_entityServiceMock.Object,
|
|
_schedulerMock.Object,
|
|
_cheapestIntervalsFinderMock.Object,
|
|
_boilerControlServiceMock.Object
|
|
);
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|