Skip to content

Commit

Permalink
EES-4389 - additional memory caching for the Themes endpoint that is …
Browse files Browse the repository at this point in the history
…used by Find Stats page
  • Loading branch information
duncan-at-hiveit committed Jul 10, 2023
1 parent 2f05dc7 commit dbcfb2a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,97 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GovUk.Education.ExploreEducationStatistics.Common.Cache;
using GovUk.Education.ExploreEducationStatistics.Common.Tests.Extensions;
using GovUk.Education.ExploreEducationStatistics.Common.Tests.Fixtures;
using GovUk.Education.ExploreEducationStatistics.Content.Api.Cache;
using GovUk.Education.ExploreEducationStatistics.Content.Api.Controllers;
using GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces;
using GovUk.Education.ExploreEducationStatistics.Content.Services.Interfaces.Cache;
using GovUk.Education.ExploreEducationStatistics.Content.Services.ViewModels;
using Moq;
using NCrontab;
using Xunit;
using static GovUk.Education.ExploreEducationStatistics.Common.Cache.CronSchedules;
using static GovUk.Education.ExploreEducationStatistics.Common.Services.CollectionUtils;
using static GovUk.Education.ExploreEducationStatistics.Common.Tests.Utils.MockUtils;
using static Moq.MockBehavior;
using static Newtonsoft.Json.JsonConvert;

namespace GovUk.Education.ExploreEducationStatistics.Content.Api.Tests.Controllers;

[Collection(CacheServiceTests)]
public class ThemeControllerCachingTests : CacheServiceTestFixture
{
private readonly IList<ThemeViewModel> _themes = ListOf(
new ThemeViewModel(Guid.NewGuid(), "slug1", "title1", "summary1"),
new ThemeViewModel(Guid.NewGuid(), "slug2", "title2", "summary2"));

[Fact]
public async Task ListThemes_NoCachedEntryExists()
{
var themeService = new Mock<IThemeService>(Strict);

MemoryCacheService
.Setup(s => s.GetItem(
new ListThemesCacheKey(),
typeof(IList<ThemeViewModel>)))
.Returns(null);

var expectedCacheConfiguration = new MemoryCacheConfiguration(
10, CrontabSchedule.Parse(HalfHourlyExpirySchedule));

MemoryCacheService
.Setup(s => s.SetItem<object>(
new ListThemesCacheKey(),
_themes,
ItIs.DeepEqualTo(expectedCacheConfiguration),
null));

themeService
.Setup(s => s.ListThemes())
.ReturnsAsync(_themes);

var controller = BuildController(themeService.Object);

var result = await controller.ListThemes();

VerifyAllMocks(MemoryCacheService, themeService);

Assert.Equal(_themes, result);
}

[Fact]
public async Task ListThemes_CachedEntryExists()
{
MemoryCacheService
.Setup(s => s.GetItem(
new ListThemesCacheKey(),
typeof(IList<ThemeViewModel>)))
.Returns(_themes);

var controller = BuildController();

var result = await controller.ListThemes();

VerifyAllMocks(MemoryCacheService);

Assert.Equal(_themes, result);
}

[Fact]
public void ThemeViewModelList_SerializeAndDeserialize()
{
var themes = ListOf(new ThemeViewModel(Guid.NewGuid(), "slug", "title", "summary"));
var converted = DeserializeObject<List<ThemeViewModel>>(SerializeObject(themes));
converted.AssertDeepEqualTo(themes);
var converted = DeserializeObject<List<ThemeViewModel>>(SerializeObject(_themes));
converted.AssertDeepEqualTo(_themes);
}

private static ThemeController BuildController(IThemeService? themeService = null)
{
return new(
Mock.Of<IMethodologyCacheService>(),
themeService ?? Mock.Of<IThemeService>(Strict)
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using GovUk.Education.ExploreEducationStatistics.Common.Cache.Interfaces;

namespace GovUk.Education.ExploreEducationStatistics.Content.Api.Cache;

public record ListThemesCacheKey : IMemoryCacheKey
{
public string Key => GetType().Name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<ActionResult<List<AllMethodologiesThemeViewModel>>> GetMethodo
.HandleFailuresOrOk();
}

[MemoryCache(typeof(GetThemesCacheKey), durationInSeconds: 10, expiryScheduleCron: HalfHourlyExpirySchedule)]
[MemoryCache(typeof(ListThemesCacheKey), durationInSeconds: 10, expiryScheduleCron: HalfHourlyExpirySchedule)]
[HttpGet("themes")]
public async Task<IList<ThemeViewModel>> ListThemes()
{
Expand Down

0 comments on commit dbcfb2a

Please sign in to comment.