Init
This commit is contained in:
@@ -0,0 +1,285 @@
|
||||
using FluentAssertions;
|
||||
using HomeAssistant.Entities.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using PowerMonitor.Models;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class PowerMonitorEntityServiceTests
|
||||
{
|
||||
private readonly Mock<IEntityManager> _entityManagerMock;
|
||||
private readonly Mock<ILogger<PowerMonitorEntityService>> _loggerMock;
|
||||
private readonly PowerMonitorEntityService _sut;
|
||||
|
||||
public PowerMonitorEntityServiceTests()
|
||||
{
|
||||
_entityManagerMock = new Mock<IEntityManager>();
|
||||
_loggerMock = new Mock<ILogger<PowerMonitorEntityService>>();
|
||||
_sut = new PowerMonitorEntityService(_entityManagerMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
#region CreateEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntityAsync_ShouldCreateMqttEntity()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CreateEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.CreateEntityAsync(It.Is<PowerMonitorEntity>(e =>
|
||||
e.EntityId == "sensor.other_power_usage" &&
|
||||
e.Domain == "sensor" &&
|
||||
e.Identifier == "other_power_usage")),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateEntityAsync_WhenCreateFails_ShouldNotThrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<PowerMonitorEntity>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Entity creation failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.CreateEntityAsync();
|
||||
|
||||
// Assert
|
||||
await act.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ResetEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntityAsync_ShouldSetStateToZero()
|
||||
{
|
||||
// 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.ResetEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.other_power_usage", "0"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ResetEntityAsync_ShouldSetDefaultAttributes()
|
||||
{
|
||||
// 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.ResetEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.other_power_usage", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UpdateEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntityAsync_ShouldSetStateWithFormattedPower()
|
||||
{
|
||||
// Arrange
|
||||
var context = new PowerUpdateContext
|
||||
{
|
||||
TotalHomePower = 3000.0,
|
||||
TotalMonitoredPower = 1500.0,
|
||||
OtherPower = 1500.0,
|
||||
MonitoredSensorCount = 21
|
||||
};
|
||||
|
||||
_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.UpdateEntityAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.other_power_usage", "1500.00"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntityAsync_ShouldSetAttributes()
|
||||
{
|
||||
// Arrange
|
||||
var context = new PowerUpdateContext
|
||||
{
|
||||
TotalHomePower = 5000.0,
|
||||
TotalMonitoredPower = 2000.0,
|
||||
OtherPower = 3000.0,
|
||||
MonitoredSensorCount = 15
|
||||
};
|
||||
|
||||
_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.UpdateEntityAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityAttributes("sensor.other_power_usage", It.IsAny<object>()),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateEntityAsync_WithZeroPower_ShouldSetZeroState()
|
||||
{
|
||||
// Arrange
|
||||
var context = new PowerUpdateContext
|
||||
{
|
||||
TotalHomePower = 0.0,
|
||||
TotalMonitoredPower = 0.0,
|
||||
OtherPower = 0.0,
|
||||
MonitoredSensorCount = 10
|
||||
};
|
||||
|
||||
_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.UpdateEntityAsync(context);
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.SetEntityState("sensor.other_power_usage", "0.00"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region CleanupEntityAsync
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupEntityAsync_ShouldRemoveEntity()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
// Act
|
||||
await _sut.CleanupEntityAsync();
|
||||
|
||||
// Assert
|
||||
_entityManagerMock.Verify(
|
||||
em => em.RemoveEntity("sensor.other_power_usage"),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupEntityAsync_WhenRemoveFails_ShouldRethrow()
|
||||
{
|
||||
// Arrange
|
||||
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Remove failed"));
|
||||
|
||||
// Act
|
||||
var act = () => _sut.CleanupEntityAsync();
|
||||
|
||||
// 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 = "W" };
|
||||
_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 = "W" };
|
||||
_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
|
||||
}
|
||||
Reference in New Issue
Block a user