using adas_core.Application.Services.Caching;
namespace adas_core.Test.Services;
[TestFixture]
public class NoCacheServiceTest
{
private NoCacheService _noCacheService = null!;
///
/// Initializes a fresh instance and assigns it to _noCacheService
/// to provide a clean, dependency-free test fixture prior to executing each test.
///
[SetUp]
public void SetUp()
{
_noCacheService = new NoCacheService();
}
#region TC-27
[Test]
public async Task GetOrSetObjectAsync_AlwaysCallsFactory_NeverUsesCache()
{
// Arrange
var callCount = 0;
var expectedResult = new { Name = "TestPatient", Id = "123" };
Func> factory = () =>
{
callCount++;
return Task.FromResult(expectedResult);
};
var key = "patients:abc123";
var ttl = TimeSpan.FromMinutes(5);
// Act
var result1 = await _noCacheService.GetOrSetObjectAsync(key, factory, ttl);
var result2 = await _noCacheService.GetOrSetObjectAsync(key, factory, ttl);
// Assert
Assert.That(callCount, Is.EqualTo(2), "La factory debería llamarse siempre (no hay caché)");
Assert.That(result1, Is.EqualTo(expectedResult));
Assert.That(result2, Is.EqualTo(expectedResult));
}
[Test]
public async Task GetOrSetObjectAsync_CallsFactory_ForEachUniqueKey()
{
// Arrange
var callCount = 0;
Func> factory = () =>
{
callCount++;
return Task.FromResult($"result-{callCount}");
};
// Act
await _noCacheService.GetOrSetValueAsync("key1", factory);
await _noCacheService.GetOrSetValueAsync("key2", factory);
await _noCacheService.GetOrSetValueAsync("key3", factory);
// Assert
Assert.That(callCount, Is.EqualTo(3),
"La factory debería haber sido invocada 3 veces, una por cada clave única");
}
#endregion
#region TC-28
///
/// Verifies that the no-cache service implementation never persists data to any backend when GetOrSetObjectAsync is invoked, and confirms that a subsequent DeleteObjectAsync on the same key does not throw.
///
[Test]
public async Task GetOrSetObjectAsync_DoesNotStoreData_InAnyBackend()
{
var key = "patients:abc123";
var expectedResult = new { Name = "TestPatient" };
Func> factory = () => Task.FromResult(expectedResult);
await _noCacheService.GetOrSetObjectAsync(key, factory);
var retrieved = await _noCacheService.GetObjectAsync(key);
Assert.That(retrieved, Is.Null);
Func act = async () => await _noCacheService.DeleteObjectAsync(key);
Assert.That(act, Throws.Nothing);
}
#endregion
#region TC-29
///
/// Verifies that the no-cache service's DeleteByPatternAsync returns 0 and does not throw when invoked with arbitrary patterns, confirming the no-op delete behavior for any key pattern.
///
[Test]
public async Task DeleteByPatternAsync_ReturnsZero_DoesNotThrow()
{
// Act
var result = await _noCacheService.DeleteByPatternAsync("patients:*");
var result2 = await _noCacheService.DeleteByPatternAsync("any:pattern:*");
// Assert
Assert.That(result, Is.EqualTo(0));
Assert.That(result2, Is.EqualTo(0));
}
#endregion
#region TC-30
///
/// Verifies that calling CleanCache on the no-cache service is a safe no-op that does not throw,
/// and that subsequent calls to GetValue consistently return null for any key.
///
[Test]
public void CleanCache_IsNoOp_DoesNotThrow()
{
Action act = () => _noCacheService.CleanCache();
Assert.That(act, Throws.Nothing);
var result = _noCacheService.GetValue("any-key");
Assert.That(result, Is.Null);
}
///
/// Verifies that calling SetValue on the no-cache service is a no-op and does not throw any exception.
///
[Test]
public void SetValue_IsNoOp_DoesNotThrow()
{
Action act = () => _noCacheService.SetValue("key", "value");
Assert.That(act, Throws.Nothing);
}
///
/// Verifies that the no-cache service's GetValue method returns null for any key,
/// confirming that the no-op implementation does not return or simulate cached values.
///
[Test]
public void GetValue_IsNoOp_ReturnsNull()
{
var result = _noCacheService.GetValue("any-key");
Assert.That(result, Is.Null);
}
#endregion
}