Init
This commit is contained in:
@@ -0,0 +1,415 @@
|
||||
using FluentAssertions;
|
||||
using HomeAssistant.Entities.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NordPool.Models;
|
||||
using NordPoolManager;
|
||||
using NordPoolManager.Models;
|
||||
|
||||
namespace NordPool.Manager.Tests;
|
||||
|
||||
public class NordPoolEntityServiceTests
|
||||
{
|
||||
private readonly Mock<IEntityManager> _entityManagerMock;
|
||||
private readonly Mock<ILogger<NordPoolEntityService>> _loggerMock;
|
||||
private readonly NordPoolEntityService _sut;
|
||||
|
||||
public NordPoolEntityServiceTests()
|
||||
{
|
||||
_entityManagerMock = new Mock<IEntityManager>();
|
||||
_loggerMock = new Mock<ILogger<NordPoolEntityService>>();
|
||||
_sut = new NordPoolEntityService(_entityManagerMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
#region GetInitEntitiesList
|
||||
|
||||
[Fact]
|
||||
public void GetInitEntitiesList_ShouldReturnAllEntities()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().HaveCount(6);
|
||||
result.Should().Contain(e => e.EntityId == "sensor.nordpool_last_update_time");
|
||||
result.Should().Contain(e => e.EntityId == "sensor.nordpool_current_price");
|
||||
result.Should().Contain(e => e.EntityId == "sensor.nordpool_today_prices");
|
||||
result.Should().Contain(e => e.EntityId == "sensor.nordpool_tomorrow_prices");
|
||||
result.Should().Contain(e => e.EntityId == "sensor.nordpool_cheapest_intervals");
|
||||
result.Should().Contain(e => e.EntityId == "sensor.nordpool_boiler_water_ready");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInitEntitiesList_ShouldHaveCorrectEntityProperties()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
var entity = result.First(e => e.EntityId == "sensor.nordpool_current_price");
|
||||
entity.Domain.Should().Be("sensor");
|
||||
entity.Identifier.Should().Be("nordpool_current_price");
|
||||
entity.Name.Should().Be("NordPool current price");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInitEntitiesList_CurrentPriceEntity_ShouldHaveMonetaryDeviceClassConfig()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
var currentPriceEntity = result.First(e => e.EntityId == "sensor.nordpool_current_price");
|
||||
currentPriceEntity.Options.Should().NotBeNull();
|
||||
currentPriceEntity.Options!.UniqueId.Should().Be("sensor.nordpool_current_price");
|
||||
currentPriceEntity.Options!.Name.Should().Be("NordPool current price");
|
||||
currentPriceEntity.AdditionalConfig.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInitEntitiesList_AllEntities_ShouldHaveSensorDomain()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
result.Should().OnlyContain(e => e.Domain == "sensor");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInitEntitiesList_AllEntities_ShouldHaveUniqueEntityIds()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
result.Select(e => e.EntityId).Should().OnlyHaveUniqueItems();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CreateEntitiesAsync
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntitiesAsync_ShouldCreateAllEntities()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntitiesAsync("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(6));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntitiesAsync_WhenOneEntityFails_ShouldContinueWithOthers()
|
||||
{
|
||||
// Arrange
|
||||
var callCount = 0;
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()))
|
||||
.Returns(() =>
|
||||
{
|
||||
callCount++;
|
||||
if (callCount == 2)
|
||||
throw new InvalidOperationException("Entity creation failed");
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntitiesAsync("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.CreateEntityAsync(It.IsAny<NordPoolEntity>()), Times.Exactly(6));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CleanupEntitiesAsync
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupEntitiesAsync_ShouldRemoveAllEntities()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CleanupEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_last_update_time"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_current_price"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_today_prices"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_tomorrow_prices"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_cheapest_intervals"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.RemoveEntity("sensor.nordpool_boiler_water_ready"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupEntitiesAsync_WhenRemoveThrows_ShouldRethrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Remove failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.CleanupEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
await act.Should().ThrowAsync<InvalidOperationException>().WithMessage("Remove failed");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetEntityStateSafe
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityStateSafe_ShouldCallSetEntityState()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.SetEntityStateSafe("sensor.test", "42");
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.test", "42"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityStateSafe_WhenExceptionThrown_ShouldNotRethrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.SetEntityStateSafe("sensor.test", "42");
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SetEntityAttributesSafe
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityAttributesSafe_ShouldCallSetEntityAttributes()
|
||||
{
|
||||
// Arrange
|
||||
var attributes = new { unit_of_measurement = "EUR/kWh" };
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.SetEntityAttributesSafe("sensor.test", attributes);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.test", It.IsAny<object>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetEntityAttributesSafe_WhenExceptionThrown_ShouldNotRethrow()
|
||||
{
|
||||
// Arrange
|
||||
var attributes = new { unit_of_measurement = "EUR/kWh" };
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Attributes update failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.SetEntityAttributesSafe("sensor.test", attributes);
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResetEntitiesAsync
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntitiesAsync_ShouldSetDefaultStateForAllEntities()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_last_update_time", "unknown"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_cheapest_intervals", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_boiler_water_ready", "unknown"), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntitiesAsync_ShouldSetDefaultAttributesForAllEntities()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_last_update_time", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_prices", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_tomorrow_prices", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_cheapest_intervals", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_boiler_water_ready", It.IsAny<object>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntitiesAsync_WhenOneEntityFails_ShouldNotThrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.SetEntityState("sensor.nordpool_current_price", It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("State update failed"));
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.Is<string>(id => id != "sensor.nordpool_current_price"), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
var act = () => _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntitiesAsync_ShouldCallSetStateTwelveTimesTotal()
|
||||
{
|
||||
// Arrange - 6 entities x (1 state + 1 attributes) = 12 calls total
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.ResetEntitiesAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(6));
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()), Times.Exactly(6));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UpdateEntitiesAsync
|
||||
|
||||
private static EntityUpdateContext CreateDefaultContext(
|
||||
double currentPrice = 12.5,
|
||||
NordPoolPrice? todayPrice = null,
|
||||
NordPoolPrice? tomorrowPrice = null)
|
||||
{
|
||||
return new EntityUpdateContext
|
||||
{
|
||||
CurrentPrice = currentPrice,
|
||||
UnitOfMeasurement = "EUR/kWh",
|
||||
DayQuartersIndex = 5,
|
||||
LastApiUpdateDate = DateTime.UtcNow,
|
||||
UsingCachedData = false,
|
||||
TodayNordPoolPrice = todayPrice ?? new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new() { Price = 10.0 } }
|
||||
},
|
||||
TomorrowNordPoolPrice = tomorrowPrice ?? new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new() { Price = 20.0 } }
|
||||
},
|
||||
CheapestIntervals = [],
|
||||
ConfiguredHours = 1.0,
|
||||
ConfiguredIntervalCount = 4,
|
||||
ConfiguredPriceCap = 100.0,
|
||||
ConfiguredReadyBy = "06:00",
|
||||
IsNowCheap = false,
|
||||
NextCheapIntervalStart = null,
|
||||
WaterReady = false,
|
||||
BoilerSwitchState = "off",
|
||||
BoilerPowerWatts = 0.0
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_ShouldSetStateForAllEntities()
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateDefaultContext();
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_last_update_time", It.IsAny<string>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_current_price", "12.50"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "1"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "1"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_cheapest_intervals", It.IsAny<string>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_boiler_water_ready", It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_ShouldSetAttributesForPriceEntities()
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateDefaultContext();
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_current_price", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_today_prices", It.IsAny<object>()), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityAttributes("sensor.nordpool_tomorrow_prices", It.IsAny<object>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntitiesAsync_WhenNoPrices_ShouldSetZeroCounts()
|
||||
{
|
||||
// Arrange
|
||||
var context = CreateDefaultContext(
|
||||
todayPrice: new NordPoolPrice { Prices = null },
|
||||
tomorrowPrice: new NordPoolPrice { Prices = null });
|
||||
|
||||
_entityManagerMock.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
_entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.UpdateEntitiesAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_today_prices", "0"), Times.Once);
|
||||
_entityManagerMock.Verify(em => em.SetEntityState("sensor.nordpool_tomorrow_prices", "0"), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user