-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EES-4389 - additional memory caching for the Themes endpoint that is …
…used by Find Stats page
- Loading branch information
1 parent
2f05dc7
commit dbcfb2a
Showing
4 changed files
with
86 additions
and
13 deletions.
There are no files selected for viewing
80 changes: 77 additions & 3 deletions
80
...n.ExploreEducationStatistics.Content.Api.Tests/Controllers/ThemeControllerCachingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
src/GovUk.Education.ExploreEducationStatistics.Content.Api/Cache/GetThemesCacheKey.cs
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/GovUk.Education.ExploreEducationStatistics.Content.Api/Cache/ListThemesCacheKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters