-
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.
Merge pull request #4164 from dfe-analytical-services/EES-4389-api-an…
…d-next-caching-for-11-july EES-4389 - memory-caching additional Themes endpoint
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...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 |
---|---|---|
@@ -0,0 +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 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) | ||
); | ||
} | ||
} |
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