adds unit tests to cover 80%
This commit is contained in:
+36
-12
@@ -57,21 +57,45 @@ public class PersistentAvailabilityServiceTests
|
||||
[Fact]
|
||||
public async Task CircuitBreaker_ShouldReturnCachedStatus_AfterDatabaseError()
|
||||
{
|
||||
// Deze test is lastig met in-memory DB omdat deze zelden 'failt'.
|
||||
// Maar we kunnen de logica testen door de context te disposen (simulatie van DB error).
|
||||
|
||||
// Arrange
|
||||
await _service.UpdateStatusAsync(AvailabilityStatus.Available, "Reset", "System");
|
||||
_context.Dispose(); // Forceer error op volgende DB call
|
||||
_context.Dispose(); // Force DB error on next call
|
||||
|
||||
// Act
|
||||
var status1 = await _service.IsAvailableAsync(); // Deze zou moeten failen en circuit breaker activeren
|
||||
var status1 = await _service.IsAvailableAsync();
|
||||
|
||||
status1.Should().Be(AvailabilityStatus.Available); // Fallback
|
||||
|
||||
// Assert
|
||||
status1.Should().Be(AvailabilityStatus.Available); // Fallback status
|
||||
|
||||
// Act 2: Circuit breaker is nu actief
|
||||
var status2 = await _service.IsAvailableAsync();
|
||||
status2.Should().Be(AvailabilityStatus.Available); // Direct uit cache
|
||||
status2.Should().Be(AvailabilityStatus.Available); // From cache (circuit breaker active)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatusDetailsAsync_ShouldReturnAvailable_WhenNoStateInDb()
|
||||
{
|
||||
var details = await _service.GetStatusDetailsAsync();
|
||||
|
||||
details.Status.Should().Be(AvailabilityStatus.Available);
|
||||
details.Message.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatusDetailsAsync_ShouldReturnCorrectStatusAndMessage()
|
||||
{
|
||||
await _service.UpdateStatusAsync(AvailabilityStatus.Maintenance, "Scheduled maintenance", "Admin");
|
||||
|
||||
var details = await _service.GetStatusDetailsAsync();
|
||||
|
||||
details.Status.Should().Be(AvailabilityStatus.Maintenance);
|
||||
details.Message.Should().Be("Scheduled maintenance");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatusDetailsAsync_ShouldReturnFallback_WhenDatabaseError()
|
||||
{
|
||||
await _service.UpdateStatusAsync(AvailabilityStatus.Available, null, "System");
|
||||
_context.Dispose(); // Force DB error
|
||||
|
||||
var details = await _service.GetStatusDetailsAsync();
|
||||
|
||||
details.Status.Should().Be(AvailabilityStatus.Available); // Fallback
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user