Skip to content

Commit

Permalink
#102 Added tests for caching service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Morris committed Jan 14, 2021
1 parent c41e00e commit d365bd5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public virtual byte[] GetCacheItem(string key, bool includeValue)
if (includeValue)
{
var result = _dataService.ExecuteFunction<DTO.Response.Caching.Item>(Schemas.Caching, Functions.GetCacheItem, parameters);
value = result.FirstOrDefault()?.Value;
value = result?.FirstOrDefault()?.Value;
}

return value;
Expand All @@ -130,7 +130,7 @@ public virtual async Task<byte[]> GetCacheItemAsync(string key, bool includeValu
if (includeValue)
{
var result = await _dataService.ExecuteFunctionAsync<DTO.Response.Caching.Item>(Schemas.Caching, Functions.GetCacheItem, parameters, cancellationToken);
value = result.FirstOrDefault()?.Value;
value = result?.FirstOrDefault()?.Value;
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using gpconnect_appointment_checker.DAL;
using gpconnect_appointment_checker.DAL.Caching;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Text;
using Xunit;

namespace gpconnect_appointment_checker.IntegrationTest
{
[Collection("CachingData")]
public class CachingServiceTests
{
private readonly CachingService _cachingService;
private readonly DataService _dataService;
private readonly DistributedCacheEntryOptions _cacheEntryOptions;

public CachingServiceTests()
{
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings:DefaultConnection");
var mockLoggerCachingService = new Mock<ILogger<CachingService>>();
var mockLoggerDataService = new Mock<ILogger<DataService>>();
var mockConfSection = new Mock<IConfigurationSection>();
mockConfSection.SetupGet(m => m[It.Is<string>(s => s == "DefaultConnection")]).Returns(connectionString);
var mockConfiguration = new Mock<IConfiguration>();
mockConfiguration.Setup(a => a.GetSection(It.Is<string>(s => s == "ConnectionStrings"))).Returns(mockConfSection.Object);
_dataService = new DataService(mockConfiguration.Object, mockLoggerDataService.Object);
_cachingService = new CachingService(mockConfiguration.Object, mockLoggerCachingService.Object, _dataService);
_cacheEntryOptions = new DistributedCacheEntryOptions
{
AbsoluteExpiration = new DateTimeOffset(DateTime.UtcNow),
AbsoluteExpirationRelativeToNow = new TimeSpan(DateTime.Now.Day, DateTime.Now.AddHours(1).Hour, DateTime.Now.Minute, DateTime.Now.Second),
SlidingExpiration = new TimeSpan(DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.AddMinutes(30).Minute, DateTime.Now.Second)
};
}

[Theory]
[InlineData("02443b12-70c2-4166-9b8b-a6b25b1ce8f2", "Test String Number 1 - She Sells Sea Shells on the Sea Shore")]
[InlineData("23310df5-c0e3-4f0b-a7ac-bb7a8286d66b", "Test String Number 2 - Peter Piper Picked a Peck of Pickled Peppers")]
public void SetAndGetCacheItem(string key, string value)
{
var byteValue = Encoding.ASCII.GetBytes(value);
_cachingService.SetCacheItem(key, byteValue, _cacheEntryOptions);
var cacheItem = _cachingService.GetCacheItem(key);
var cacheItemAsString = Encoding.ASCII.GetString(cacheItem);
Assert.NotEmpty(cacheItem);
Assert.Equal(value, cacheItemAsString);
}
}
}

0 comments on commit d365bd5

Please sign in to comment.