24729ee267
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
554 lines
20 KiB
C#
554 lines
20 KiB
C#
using System.Text.Json;
|
|
using BoilerMonitorManager;
|
|
using FluentAssertions;
|
|
using HomeAssistant.Entities.Interfaces;
|
|
using HomeAssistant.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NetDaemon.HassModel;
|
|
using static BoilerMonitor.Models.BoilerMonitorConsts;
|
|
|
|
namespace BoilerMonitor.Manager.Tests;
|
|
|
|
public class BoilerMonitorEntityServiceTests
|
|
{
|
|
/// <summary>Every entity that <c>CleanupEntitiesAsync</c> is expected to remove.</summary>
|
|
private static readonly string[] CleanableEntityIds =
|
|
[
|
|
ConfigPowerEntityId,
|
|
ConfigSwitchEntityId,
|
|
ConfigPollIntervalEntityId,
|
|
ConfigHeatingThresholdEntityId,
|
|
StatusEntityId,
|
|
WaterReadyEntityId,
|
|
SkipPriceCapEntityId,
|
|
TodayRuntimeEntityId,
|
|
RemainingTimeEntityId,
|
|
BudgetPercentageEntityId,
|
|
BudgetExhaustedEntityId,
|
|
SessionCountEntityId,
|
|
BudgetOverrideEntityId,
|
|
AutoManagementEntityId
|
|
];
|
|
|
|
private readonly Mock<IEntityManager> _entityManagerMock = new();
|
|
private readonly Mock<IHaContext> _haContextMock = new();
|
|
private readonly Mock<ILogger<BoilerMonitorEntityService>> _loggerMock = new();
|
|
|
|
public BoilerMonitorEntityServiceTests()
|
|
{
|
|
_entityManagerMock.Setup(em => em.CreateEntityAsync(It.IsAny<EntityCreateModel>()))
|
|
.Returns(Task.CompletedTask);
|
|
_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);
|
|
_entityManagerMock.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
|
.Returns(Task.CompletedTask);
|
|
}
|
|
|
|
private BoilerMonitorEntityService CreateSut()
|
|
{
|
|
return new BoilerMonitorEntityService(
|
|
_entityManagerMock.Object,
|
|
_haContextMock.Object,
|
|
_loggerMock.Object);
|
|
}
|
|
|
|
#region ReadPersistedState
|
|
|
|
[Theory]
|
|
[MemberData(nameof(BoilerMonitorTestData.RuntimeStateSamples), MemberType = typeof(BoilerMonitorTestData))]
|
|
public void ReadPersistedState_WhenRuntimeStateVaries_ShouldParseOrReject(
|
|
string? runtimeState,
|
|
TimeSpan? expectedRunTime)
|
|
{
|
|
_haContextMock.SetupEntityStates(
|
|
BoilerMonitorTestData.CreatePersistedEntityStates(runtimeState, "2"));
|
|
|
|
var result = CreateSut().ReadPersistedState();
|
|
|
|
if (expectedRunTime is null)
|
|
{
|
|
result.Should().BeNull();
|
|
return;
|
|
}
|
|
|
|
result.Should().NotBeNull();
|
|
result.AccumulatedRunTime.Should().Be(expectedRunTime.Value);
|
|
result.SessionCount.Should().Be(2);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(BoilerMonitorTestData.SessionCountStateSamples), MemberType = typeof(BoilerMonitorTestData))]
|
|
public void ReadPersistedState_WhenSessionCountStateVaries_ShouldParseOrReject(
|
|
string? sessionCountState,
|
|
int? expectedSessionCount)
|
|
{
|
|
_haContextMock.SetupEntityStates(
|
|
BoilerMonitorTestData.CreatePersistedEntityStates("3600", sessionCountState));
|
|
|
|
var result = CreateSut().ReadPersistedState();
|
|
|
|
if (expectedSessionCount is null)
|
|
{
|
|
result.Should().BeNull();
|
|
return;
|
|
}
|
|
|
|
result.Should().NotBeNull();
|
|
result.AccumulatedRunTime.Should().Be(TimeSpan.FromHours(1));
|
|
result.SessionCount.Should().Be(expectedSessionCount.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedState_WithValidState_ShouldReturnPersistedValues()
|
|
{
|
|
_haContextMock.SetupEntityStates(BoilerMonitorTestData.CreatePersistedEntityStates(
|
|
"7200", "3", "ON", "OFF"));
|
|
|
|
var result = CreateSut().ReadPersistedState();
|
|
|
|
result.Should().NotBeNull();
|
|
result.AccumulatedRunTime.Should().Be(TimeSpan.FromHours(2));
|
|
result.SessionCount.Should().Be(3);
|
|
result.SkipPriceCapState.Should().Be("ON");
|
|
result.BudgetOverrideState.Should().Be("OFF");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("ON")]
|
|
[InlineData("on")]
|
|
[InlineData(" On ")]
|
|
[InlineData("")]
|
|
[InlineData(null)]
|
|
public void ReadPersistedState_ShouldReturnRawSwitchStates(string? switchState)
|
|
{
|
|
_haContextMock.SetupEntityStates(BoilerMonitorTestData.CreatePersistedEntityStates(
|
|
skipPriceCap: switchState, budgetOverride: switchState));
|
|
|
|
var result = CreateSut().ReadPersistedState();
|
|
|
|
result.Should().NotBeNull();
|
|
result.SkipPriceCapState.Should().Be(switchState);
|
|
result.BudgetOverrideState.Should().Be(switchState);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedState_WhenBothRuntimeEntitiesDoNotExist_ShouldReturnNull()
|
|
{
|
|
_haContextMock.SetupEntityStates(
|
|
BoilerMonitorTestData.CreatePersistedEntityStates(null, null));
|
|
|
|
CreateSut().ReadPersistedState().Should().BeNull();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ReadPersistedCooldownTimestamp
|
|
|
|
[Fact]
|
|
public void ReadPersistedCooldownTimestamp_WithValidIsoString_ShouldReturnTimestamp()
|
|
{
|
|
var cooldownSince = BoilerMonitorTestData.BaseTime - TimeSpan.FromMinutes(30);
|
|
_haContextMock.SetupEntityState(
|
|
WaterReadyEntityId,
|
|
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown(cooldownSince.ToString("O")));
|
|
|
|
var result = CreateSut().ReadPersistedCooldownTimestamp();
|
|
|
|
result.Should().Be(cooldownSince);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedCooldownTimestamp_WithUtcDesignator_ShouldReturnTimestamp()
|
|
{
|
|
var cooldownSince = new DateTimeOffset(2026, 3, 31, 9, 15, 0, TimeSpan.Zero);
|
|
_haContextMock.SetupEntityState(
|
|
WaterReadyEntityId,
|
|
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown("2026-03-31T09:15:00Z"));
|
|
|
|
var result = CreateSut().ReadPersistedCooldownTimestamp();
|
|
|
|
result.Should().Be(cooldownSince);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedCooldownTimestamp_WhenEntityDoesNotExist_ShouldReturnNull()
|
|
{
|
|
_haContextMock.SetupEntityState(WaterReadyEntityId, null);
|
|
|
|
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedCooldownTimestamp_WhenEntityHasNoAttributes_ShouldReturnNull()
|
|
{
|
|
_haContextMock.SetupEntityState(WaterReadyEntityId, BoilerMonitorTestData.CreateEntityState("yes"));
|
|
|
|
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedCooldownTimestamp_WhenAttributeIsMissing_ShouldReturnNull()
|
|
{
|
|
_haContextMock.SetupEntityState(
|
|
WaterReadyEntityId,
|
|
BoilerMonitorTestData.CreateEntityStateWithAttributes(
|
|
new Dictionary<string, object?> { ["friendly_name"] = "Boiler water ready" }));
|
|
|
|
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData(42)]
|
|
[InlineData(true)]
|
|
[InlineData("")]
|
|
[InlineData(" ")]
|
|
[InlineData("not-a-timestamp")]
|
|
[InlineData("2026-13-45T99:99:99")]
|
|
[InlineData("unavailable")]
|
|
public void ReadPersistedCooldownTimestamp_WhenAttributeIsNotAValidTimestamp_ShouldReturnNull(object? cooldownSince)
|
|
{
|
|
_haContextMock.SetupEntityState(
|
|
WaterReadyEntityId,
|
|
BoilerMonitorTestData.CreateWaterReadyEntityStateWithCooldown(cooldownSince));
|
|
|
|
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadPersistedCooldownTimestamp_WhenHaContextThrows_ShouldReturnNull()
|
|
{
|
|
_haContextMock.Setup(h => h.GetState(WaterReadyEntityId))
|
|
.Throws(new InvalidOperationException("Home Assistant unavailable"));
|
|
|
|
CreateSut().ReadPersistedCooldownTimestamp().Should().BeNull();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region UpdateEntitiesAsync
|
|
|
|
[Theory]
|
|
[InlineData(true, "yes")]
|
|
[InlineData(false, "no")]
|
|
public async Task UpdateEntitiesAsync_ShouldWriteWaterReadyState(bool waterReady, string expectedState)
|
|
{
|
|
await CreateSut().UpdateEntitiesAsync(BoilerMonitorTestData.CreateUpdateContext(waterReady: waterReady));
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(WaterReadyEntityId, expectedState), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateEntitiesAsync_ShouldWriteAllRuntimeEntitiesWithExpectedFormatting()
|
|
{
|
|
var context = BoilerMonitorTestData.CreateUpdateContext(
|
|
TimeSpan.FromSeconds(3723.9),
|
|
TimeSpan.FromSeconds(42.9),
|
|
150.0,
|
|
true,
|
|
4,
|
|
waterReady: true);
|
|
|
|
await CreateSut().UpdateEntitiesAsync(context);
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(StatusEntityId, StatusConfigured), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(TodayRuntimeEntityId, "3723"), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(RemainingTimeEntityId, "42"), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(BudgetPercentageEntityId, "100"), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(BudgetExhaustedEntityId, "on"), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(SessionCountEntityId, "4"), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()),
|
|
Times.Exactly(5));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-0.1, "0")]
|
|
[InlineData(0.0, "0")]
|
|
[InlineData(49.9, "49")]
|
|
[InlineData(99.999, "99")]
|
|
[InlineData(100.0, "100")]
|
|
[InlineData(100.1, "100")]
|
|
[InlineData(1_000.0, "100")]
|
|
public async Task UpdateEntitiesAsync_ShouldClampAndTruncateBudgetPercentage(
|
|
double budgetPercentage,
|
|
string expectedState)
|
|
{
|
|
await CreateSut().UpdateEntitiesAsync(
|
|
BoilerMonitorTestData.CreateUpdateContext(budgetPercentage: budgetPercentage));
|
|
|
|
_entityManagerMock.Verify(
|
|
em => em.SetEntityState(BudgetPercentageEntityId, expectedState), Times.Once);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-60, "0")]
|
|
[InlineData(0, "0")]
|
|
[InlineData(1, "1")]
|
|
[InlineData(59, "59")]
|
|
[InlineData(3600, "3600")]
|
|
public async Task UpdateEntitiesAsync_ShouldClampAndTruncateRemainingTime(int seconds, string expectedState)
|
|
{
|
|
await CreateSut().UpdateEntitiesAsync(
|
|
BoilerMonitorTestData.CreateUpdateContext(remainingTime: TimeSpan.FromSeconds(seconds)));
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(RemainingTimeEntityId, expectedState), Times.Once);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0.0, "0")]
|
|
[InlineData(0.9, "0")]
|
|
[InlineData(59.9, "59")]
|
|
[InlineData(60.0, "60")]
|
|
[InlineData(86_400.0, "86400")]
|
|
public async Task UpdateEntitiesAsync_ShouldTruncateTodayRunTimeToWholeSeconds(double seconds, string expectedState)
|
|
{
|
|
await CreateSut().UpdateEntitiesAsync(
|
|
BoilerMonitorTestData.CreateUpdateContext(TimeSpan.FromSeconds(seconds)));
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(TodayRuntimeEntityId, expectedState), Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateEntitiesAsync_WhenSwitchStateIsNull_ShouldWriteUnknownWaterReadyAttributes()
|
|
{
|
|
object? capturedAttributes = null;
|
|
_entityManagerMock
|
|
.Setup(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()))
|
|
.Callback<string, object>((_, attributes) => capturedAttributes = attributes)
|
|
.Returns(Task.CompletedTask);
|
|
|
|
await CreateSut().UpdateEntitiesAsync(BoilerMonitorTestData.CreateUpdateContext(
|
|
waterReady: false,
|
|
boilerSwitchState: null,
|
|
boilerPowerWatts: 1234.5));
|
|
|
|
capturedAttributes.Should().NotBeNull();
|
|
var attributes = JsonSerializer.SerializeToElement(capturedAttributes!);
|
|
attributes.GetProperty("boiler_switch_state").GetString().Should().Be("unknown");
|
|
attributes.GetProperty("boiler_power_watts").GetDouble().Should().Be(1234.5);
|
|
attributes.GetProperty("cooldown_since").ValueKind.Should().Be(JsonValueKind.Null);
|
|
attributes.GetProperty("last_updated").GetString().Should().NotBeNullOrEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateEntitiesAsync_WhenCooldownIsActive_ShouldSerializeCooldownTimestamp()
|
|
{
|
|
var cooldownSince = BoilerMonitorTestData.BaseTime - TimeSpan.FromMinutes(15);
|
|
object? capturedAttributes = null;
|
|
_entityManagerMock
|
|
.Setup(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()))
|
|
.Callback<string, object>((_, attributes) => capturedAttributes = attributes)
|
|
.Returns(Task.CompletedTask);
|
|
|
|
await CreateSut().UpdateEntitiesAsync(
|
|
BoilerMonitorTestData.CreateUpdateContext(cooldownSince: cooldownSince));
|
|
|
|
var attributes = JsonSerializer.SerializeToElement(capturedAttributes!);
|
|
attributes.GetProperty("cooldown_since").GetString().Should().Be(cooldownSince.ToString("O"));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region CreateEntitiesAsync
|
|
|
|
[Fact]
|
|
public async Task CreateEntitiesAsync_ShouldCreateEveryBoilerMonitorEntityOnce()
|
|
{
|
|
var createdEntityIds = new List<string?>();
|
|
_entityManagerMock
|
|
.Setup(em => em.CreateEntityAsync(It.IsAny<EntityCreateModel>()))
|
|
.Callback<EntityCreateModel>(entity => createdEntityIds.Add(entity.EntityId))
|
|
.Returns(Task.CompletedTask);
|
|
|
|
await CreateSut().CreateEntitiesAsync();
|
|
|
|
createdEntityIds.Should().HaveCount(15).And.OnlyHaveUniqueItems();
|
|
createdEntityIds.Should().Contain(
|
|
[
|
|
ConfigPowerEntityId,
|
|
ConfigSwitchEntityId,
|
|
ConfigPollIntervalEntityId,
|
|
ConfigHeatingThresholdEntityId,
|
|
CleanupButtonEntityId,
|
|
WaterReadyEntityId,
|
|
TodayRuntimeEntityId,
|
|
BudgetExhaustedEntityId,
|
|
AutoManagementEntityId
|
|
]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateEntitiesAsync_WhenOneEntityFails_ShouldStillAttemptEveryEntity()
|
|
{
|
|
_entityManagerMock
|
|
.Setup(em =>
|
|
em.CreateEntityAsync(It.Is<EntityCreateModel>(entity => entity.EntityId == CleanupButtonEntityId)))
|
|
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
|
|
|
var act = () => CreateSut().CreateEntitiesAsync();
|
|
|
|
await act.Should().NotThrowAsync();
|
|
_entityManagerMock.Invocations
|
|
.Count(invocation => invocation.Method.Name == nameof(IEntityManager.CreateEntityAsync))
|
|
.Should().Be(15);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ResetEntitiesAsync
|
|
|
|
public static TheoryData<string, string> ResetEntityStates => new()
|
|
{
|
|
{ StatusEntityId, StatusAwaitingConfiguration },
|
|
{ WaterReadyEntityId, "unknown" },
|
|
{ SkipPriceCapEntityId, "OFF" },
|
|
{ TodayRuntimeEntityId, "0" },
|
|
{ RemainingTimeEntityId, "0" },
|
|
{ BudgetPercentageEntityId, "0" },
|
|
{ BudgetExhaustedEntityId, "off" },
|
|
{ SessionCountEntityId, "0" },
|
|
{ BudgetOverrideEntityId, "OFF" },
|
|
{ AutoManagementEntityId, "ON" }
|
|
};
|
|
|
|
[Theory]
|
|
[MemberData(nameof(ResetEntityStates))]
|
|
public async Task ResetEntitiesAsync_ShouldWriteExpectedDefaultState(string entityId, string expectedState)
|
|
{
|
|
await CreateSut().ResetEntitiesAsync();
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(entityId, expectedState), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(10));
|
|
_entityManagerMock.Verify(em => em.SetEntityAttributes(It.IsAny<string>(), It.IsAny<object>()),
|
|
Times.Exactly(5));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResetEntitiesAsync_WhenEntityManagerThrows_ShouldNotThrow()
|
|
{
|
|
_entityManagerMock
|
|
.Setup(em => em.SetEntityState(It.IsAny<string>(), It.IsAny<string>()))
|
|
.ThrowsAsync(new InvalidOperationException("MQTT unavailable"));
|
|
|
|
var act = () => CreateSut().ResetEntitiesAsync();
|
|
|
|
await act.Should().NotThrowAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ResetTransientEntitiesAsync
|
|
|
|
[Fact]
|
|
public async Task ResetTransientEntitiesAsync_ShouldResetStatusAndWaterReady()
|
|
{
|
|
await CreateSut().ResetTransientEntitiesAsync();
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(StatusEntityId, StatusAwaitingConfiguration), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityState(WaterReadyEntityId, "unknown"), Times.Once);
|
|
_entityManagerMock.Verify(em => em.SetEntityAttributes(WaterReadyEntityId, It.IsAny<object>()), Times.Once);
|
|
}
|
|
|
|
public static TheoryData<string> UntouchedByTransientResetEntityIds =>
|
|
[
|
|
TodayRuntimeEntityId,
|
|
SessionCountEntityId,
|
|
BudgetPercentageEntityId,
|
|
BudgetExhaustedEntityId,
|
|
RemainingTimeEntityId,
|
|
SkipPriceCapEntityId,
|
|
BudgetOverrideEntityId,
|
|
AutoManagementEntityId,
|
|
ConfigPowerEntityId,
|
|
ConfigSwitchEntityId
|
|
];
|
|
|
|
[Theory]
|
|
[MemberData(nameof(UntouchedByTransientResetEntityIds))]
|
|
public async Task ResetTransientEntitiesAsync_ShouldNotResetPersistentOrSwitchEntities(string entityId)
|
|
{
|
|
await CreateSut().ResetTransientEntitiesAsync();
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(entityId, It.IsAny<string>()), Times.Never);
|
|
_entityManagerMock.Verify(em => em.SetEntityAttributes(entityId, It.IsAny<object>()), Times.Never);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region CleanupEntitiesAsync
|
|
|
|
[Fact]
|
|
public async Task CleanupEntitiesAsync_ShouldRemoveEveryEntityExceptCleanupButton()
|
|
{
|
|
await CreateSut().CleanupEntitiesAsync();
|
|
|
|
foreach (var entityId in CleanableEntityIds)
|
|
_entityManagerMock.Verify(em => em.RemoveEntity(entityId), Times.Once);
|
|
|
|
_entityManagerMock.Verify(em => em.RemoveEntity(CleanupButtonEntityId), Times.Never);
|
|
_entityManagerMock.Verify(em => em.RemoveEntity(It.IsAny<string>()), Times.Exactly(CleanableEntityIds.Length));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CleanupEntitiesAsync_WhenRemoveFails_ShouldRethrow()
|
|
{
|
|
_entityManagerMock
|
|
.Setup(em => em.RemoveEntity(It.IsAny<string>()))
|
|
.Throws(new InvalidOperationException("MQTT unavailable"));
|
|
|
|
var act = () => CreateSut().CleanupEntitiesAsync();
|
|
|
|
await act.Should().ThrowAsync<InvalidOperationException>();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region SetStatusAsync and cancellation
|
|
|
|
[Fact]
|
|
public async Task SetStatusAsync_ShouldWriteStatusEntity()
|
|
{
|
|
await CreateSut().SetStatusAsync(StatusConfigured);
|
|
|
|
_entityManagerMock.Verify(em => em.SetEntityState(StatusEntityId, StatusConfigured), Times.Once);
|
|
}
|
|
|
|
public enum CancelableOperation
|
|
{
|
|
CreateEntities,
|
|
ResetEntities,
|
|
UpdateEntities,
|
|
ResetTransientEntities,
|
|
CleanupEntities
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(CancelableOperation.CreateEntities)]
|
|
[InlineData(CancelableOperation.ResetEntities)]
|
|
[InlineData(CancelableOperation.UpdateEntities)]
|
|
[InlineData(CancelableOperation.ResetTransientEntities)]
|
|
[InlineData(CancelableOperation.CleanupEntities)]
|
|
public async Task Operation_WhenCancellationIsAlreadyRequested_ShouldThrowAndNotTouchEntityManager(
|
|
CancelableOperation operation)
|
|
{
|
|
var canceledToken = new CancellationToken(true);
|
|
var sut = CreateSut();
|
|
Func<Task> act = operation switch
|
|
{
|
|
CancelableOperation.CreateEntities => () => sut.CreateEntitiesAsync(canceledToken),
|
|
CancelableOperation.ResetEntities => () => sut.ResetEntitiesAsync(canceledToken),
|
|
CancelableOperation.UpdateEntities => () => sut.UpdateEntitiesAsync(
|
|
BoilerMonitorTestData.CreateUpdateContext(), canceledToken),
|
|
CancelableOperation.ResetTransientEntities => () => sut.ResetTransientEntitiesAsync(canceledToken),
|
|
_ => () => sut.CleanupEntitiesAsync(canceledToken)
|
|
};
|
|
|
|
await act.Should().ThrowAsync<OperationCanceledException>();
|
|
_entityManagerMock.Invocations.Should().BeEmpty();
|
|
}
|
|
|
|
#endregion
|
|
} |