Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew MORGAN authored and Drew MORGAN committed Mar 4, 2025
1 parent 39b10d2 commit 8f99f7b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public static RedisValue Compress(RedisValue redisValue)
/// <returns>True if decompressed, false if not.</returns>
public static bool Decompress(ref byte[] value)
{

if (value == null || value.Length == 0 || !value.Take(_gzipHeader.Length).SequenceEqual(_gzipHeader))
{
return false;
Expand Down
33 changes: 11 additions & 22 deletions src/Dfe.PlanTech.Infrastructure.Redis/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,22 @@ public RedisCache(IRedisConnectionManager connectionManager, ILogger<RedisCache>
var db = await _connectionManager.GetDatabaseAsync(databaseId);
var redisResult = await GetAsync<T>(db, key);

var existedInCache = redisResult.ExistedInCache == true && redisResult.CacheValue != null;
var hasContent = false;
if (redisResult.Errored)
{
return await action();
}

if (existedInCache)
if (redisResult.ExistedInCache == true && redisResult.CacheValue != null)

Check warning on line 55 in src/Dfe.PlanTech.Infrastructure.Redis/RedisCache.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)

Check warning on line 55 in src/Dfe.PlanTech.Infrastructure.Redis/RedisCache.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)
{
var resultType = redisResult.CacheValue!.GetType();
var hasContent = redisResult.CacheValue is IEnumerable<IContentComponent> cacheValue
? cacheValue.Any()
: redisResult.CacheValue != null;

Check warning on line 59 in src/Dfe.PlanTech.Infrastructure.Redis/RedisCache.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Use a comparison to 'default(T)' instead or add a constraint to 'T' so that it can't be a value type. (https://rules.sonarsource.com/csharp/RSPEC-2955)

if (typeof(IEnumerable<IContentComponent>).IsAssignableFrom(resultType))
if (hasContent)
{
var cacheValue = redisResult.CacheValue as IEnumerable<IContentComponent>;
hasContent = cacheValue != null && cacheValue.Any();
_logger.LogTrace("Cache item with key: {Key} found", key);
return redisResult.CacheValue;
}
else
{
var cacheValue = redisResult.CacheValue as IContentComponent;
hasContent = cacheValue != null;
}
}

if (existedInCache && hasContent)
{
_logger.LogTrace("Cache item with key: {Key} found", key);
return redisResult.CacheValue;
}
else if (redisResult.Errored)
{
return await action();
}

return await CreateAndCacheItemAsync(db, key, action, expiry, onCacheItemCreation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.PlanTech.Domain.Background;
using Dfe.PlanTech.Domain.Content.Models;
using NSubstitute;
using StackExchange.Redis;

Expand All @@ -7,7 +8,7 @@ namespace Dfe.PlanTech.Infrastructure.Redis.UnitTests;
public class RedisCacheTestsBase
{
protected const string Key = "testKey";
protected const string Value = """{"this":"is json"}""";
protected string Value = JsonSerialiser.Serialise(new InsetText { Text = "This is text" });

protected readonly IDatabase Database = Substitute.For<IDatabase>();
protected readonly IBackgroundTaskQueue BackgroundTaskQueue = Substitute.For<IBackgroundTaskQueue>();
Expand Down Expand Up @@ -37,9 +38,7 @@ protected virtual void MockDatabaseMethods()

if (keyArg == Key)
{
var redisValue = new RedisValue(Value);

return Task.FromResult(redisValue);
return Task.FromResult(new RedisValue(Value));
}

return Task.FromResult(new RedisValue());
Expand Down

0 comments on commit 8f99f7b

Please sign in to comment.