Skip to content

Commit

Permalink
test(api): correct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Dec 30, 2024
1 parent cf90d7f commit 96407a1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/api/Features/Geocoding/AddressSystemFromPlace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ private bool IsPlaceNameMatch(string key, out List<GridLinkable> result) {
private async Task<(bool success, List<GridLinkable> result)> IsFuzzyMatchAsync(string key, CancellationToken token) {
var result = await _fusionCache.GetOrDefaultAsync<List<GridLinkable>>($"mapping/place/{key}", defaultValue: [], token: token);

if (result!.Count > 0) {
if (result is null) {
return (false, []);
}

if (result.Count > 0) {
LogCacheHit(key, "fusion");

return (true, result);
Expand Down
2 changes: 1 addition & 1 deletion src/api/Features/Geocoding/AddressSystemFromZipCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Task<IReadOnlyCollection<GridLinkable>> Handle(Computation request, Cance
_log?.Debug("Getting address system from zip {zip}", request._zip);

if (string.IsNullOrEmpty(request._zip)) {
return Task.FromResult((IReadOnlyCollection<GridLinkable>)Task.FromResult(Array.Empty<GridLinkable>()));
return Task.FromResult((IReadOnlyCollection<GridLinkable>)[]);
}

var result = _memoryCache.Get<List<GridLinkable>>($"mapping/zip/{request._zip}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using ugrc.api.Cache;
using Microsoft.Extensions.Caching.Memory;
using ugrc.api.Features.Geocoding;
using ugrc.api.Infrastructure;
using ugrc.api.Models.Linkables;
using ZiggyCreatures.Caching.Fusion;

namespace api.tests.Features.Geocoding;
public class AddressSystemFromPlaceTests {
public AddressSystemFromPlaceTests() {
var mockDb = new Mock<IDatabase>();
mockDb.Setup(x => x.StringGetAsync(It.Is<RedisKey>(p => p.Equals(new RedisKey("map/place/key"))), CommandFlags.None))
.ReturnsAsync(new RedisValue("grid,0"));
var memoryCache = new MemoryCache(new MemoryCacheOptions());
memoryCache.Set("mapping/place/key", new List<GridLinkable> { new PlaceGridLink("key", "grid", 0) });

var mockConnection = new Mock<IConnectionMultiplexer>();
mockConnection.Setup(x => x.GetDatabase(It.IsAny<int>(), It.IsAny<object>())).Returns(mockDb.Object);

var redisCache = new RedisCacheRepository(new Lazy<IConnectionMultiplexer>(() => mockConnection.Object));
var mockFusionCache = new Mock<IFusionCache>();
var mockFusionCacheProvider = new Mock<IFusionCacheProvider>();
mockFusionCacheProvider.Setup(x => x.GetCache(It.IsAny<string>())).Returns(mockFusionCache.Object);
var mockLogger = new Mock<ILogger>() { DefaultValue = DefaultValue.Mock };

_handler = new AddressSystemFromPlace.Handler(redisCache, mockLogger.Object);
_handler = new AddressSystemFromPlace.Handler(memoryCache, mockFusionCacheProvider.Object, mockLogger.Object);
}

internal static IComputationHandler<AddressSystemFromPlace.Computation, IReadOnlyCollection<GridLinkable>> _handler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ugrc.api.Cache;
using Microsoft.Extensions.Caching.Memory;
using ugrc.api.Features.Geocoding;
using ugrc.api.Infrastructure;
using ugrc.api.Models.Linkables;
Expand All @@ -8,17 +8,12 @@ public class AddressSystemFromZipTests {
internal static IComputationHandler<AddressSystemFromZipCode.Computation, IReadOnlyCollection<GridLinkable>> _handler;

public AddressSystemFromZipTests() {
var mockDb = new Mock<IDatabase>();
mockDb.Setup(x => x.StringGetAsync(It.Is<RedisKey>(p => p.Equals(new RedisKey("1"))), CommandFlags.None))
.ReturnsAsync(new RedisValue("grid,1"));
var memoryCache = new MemoryCache(new MemoryCacheOptions());
memoryCache.Set("mapping/zip/1", new List<GridLinkable> { new ZipGridLink(1, "grid", 0) });

var mockConnection = new Mock<IConnectionMultiplexer>();
mockConnection.Setup(x => x.GetDatabase(It.IsAny<int>(), It.IsAny<object>())).Returns(mockDb.Object);

var redisCache = new RedisCacheRepository(new Lazy<IConnectionMultiplexer>(() => mockConnection.Object));
var mockLogger = new Mock<ILogger>() { DefaultValue = DefaultValue.Mock };

_handler = new AddressSystemFromZipCode.Handler(redisCache, mockLogger.Object);
_handler = new AddressSystemFromZipCode.Handler(memoryCache, mockLogger.Object);
}

[Fact]
Expand Down

0 comments on commit 96407a1

Please sign in to comment.