-
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 #4159 from dfe-analytical-services/EES-4389-api-an…
…d-next-caching-for-11-july Ees 4389 api and next caching for 11 july
- Loading branch information
Showing
24 changed files
with
222 additions
and
24 deletions.
There are no files selected for viewing
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
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
7 changes: 7 additions & 0 deletions
7
src/GovUk.Education.ExploreEducationStatistics.Common/Cache/CronSchedules.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,7 @@ | ||
namespace GovUk.Education.ExploreEducationStatistics.Common.Cache; | ||
|
||
public static class CronSchedules | ||
{ | ||
public const string HourlyExpirySchedule = "0 * * * *"; | ||
public const string HalfHourlyExpirySchedule = "*/30 * * * *"; | ||
} |
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
130 changes: 130 additions & 0 deletions
130
...oreEducationStatistics.Content.Api.Tests/Controllers/PublicationControllerCachingTests.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,130 @@ | ||
#nullable enable | ||
using System; | ||
using System.Threading.Tasks; | ||
using GovUk.Education.ExploreEducationStatistics.Common.Cache; | ||
using GovUk.Education.ExploreEducationStatistics.Common.Model; | ||
using GovUk.Education.ExploreEducationStatistics.Common.Tests.Extensions; | ||
using GovUk.Education.ExploreEducationStatistics.Common.Tests.Fixtures; | ||
using GovUk.Education.ExploreEducationStatistics.Common.ViewModels; | ||
using GovUk.Education.ExploreEducationStatistics.Content.Api.Cache; | ||
using GovUk.Education.ExploreEducationStatistics.Content.Api.Controllers; | ||
using GovUk.Education.ExploreEducationStatistics.Content.Api.Requests; | ||
using GovUk.Education.ExploreEducationStatistics.Content.Model; | ||
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 PublicationControllerCachingTests : CacheServiceTestFixture | ||
{ | ||
private readonly PublicationsListRequest _query = new( | ||
ReleaseType.ExperimentalStatistics, | ||
ThemeId: Guid.Empty, | ||
Search: "", | ||
IPublicationService.PublicationsSortBy.Published, | ||
SortOrder.Asc, | ||
Page: 1, | ||
PageSize: 10 | ||
); | ||
|
||
private readonly PaginatedListViewModel<PublicationSearchResultViewModel> _publications = new( | ||
ListOf(new PublicationSearchResultViewModel | ||
{ | ||
Id = Guid.NewGuid(), | ||
Published = DateTime.UtcNow, | ||
Rank = 4, | ||
Slug = "slug", | ||
Summary = "summary", | ||
Theme = "theme", | ||
Title = "title", | ||
Type = ReleaseType.ExperimentalStatistics | ||
}), 5, 1, 10); | ||
|
||
[Fact] | ||
public async Task ListPublications_NoCachedEntryExists() | ||
{ | ||
var publicationService = new Mock<IPublicationService>(Strict); | ||
|
||
MemoryCacheService | ||
.Setup(s => s.GetItem( | ||
new GetPublicationListCacheKey(_query), | ||
typeof(PaginatedListViewModel<PublicationSearchResultViewModel>))) | ||
.Returns(null); | ||
|
||
var expectedCacheConfiguration = new MemoryCacheConfiguration( | ||
10, CrontabSchedule.Parse(HalfHourlyExpirySchedule)); | ||
|
||
MemoryCacheService | ||
.Setup(s => s.SetItem<object>( | ||
new GetPublicationListCacheKey(_query), | ||
_publications, | ||
ItIs.DeepEqualTo(expectedCacheConfiguration), | ||
null)); | ||
|
||
publicationService | ||
.Setup(s => s.ListPublications( | ||
_query.ReleaseType, | ||
_query.ThemeId, | ||
_query.Search, | ||
_query.Sort, | ||
_query.Order, | ||
_query.Page, | ||
_query.PageSize)) | ||
.ReturnsAsync(_publications); | ||
|
||
var controller = BuildController(publicationService.Object); | ||
|
||
var result = await controller.ListPublications(_query); | ||
|
||
VerifyAllMocks(MemoryCacheService, publicationService); | ||
|
||
result.AssertOkResult(_publications); | ||
} | ||
|
||
[Fact] | ||
public async Task ListPublications_CachedEntryExists() | ||
{ | ||
MemoryCacheService | ||
.Setup(s => s.GetItem( | ||
new GetPublicationListCacheKey(_query), | ||
typeof(PaginatedListViewModel<PublicationSearchResultViewModel>))) | ||
.Returns(_publications); | ||
|
||
var controller = BuildController(); | ||
|
||
var result = await controller.ListPublications(_query); | ||
|
||
VerifyAllMocks(MemoryCacheService); | ||
|
||
result.AssertOkResult(_publications); | ||
} | ||
|
||
[Fact] | ||
public void PublicationSearchResults_SerializeAndDeserialize() | ||
{ | ||
var converted = DeserializeObject<PaginatedListViewModel<PublicationSearchResultViewModel>>( | ||
SerializeObject(_publications)); | ||
|
||
converted.AssertDeepEqualTo(_publications); | ||
} | ||
|
||
private static PublicationController BuildController( | ||
IPublicationService? publicationService = null | ||
) | ||
{ | ||
return new( | ||
Mock.Of<IPublicationCacheService>(Strict), | ||
publicationService ?? Mock.Of<IPublicationService>(Strict) | ||
); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
...ovUk.Education.ExploreEducationStatistics.Content.Api/Cache/GetPublicationListCacheKey.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,9 @@ | ||
using GovUk.Education.ExploreEducationStatistics.Common.Cache.Interfaces; | ||
using GovUk.Education.ExploreEducationStatistics.Content.Api.Requests; | ||
|
||
namespace GovUk.Education.ExploreEducationStatistics.Content.Api.Cache; | ||
|
||
public record GetPublicationListCacheKey(PublicationsListRequest PublicationQuery) : IMemoryCacheKey | ||
{ | ||
public string Key => $"{GetType().Name}:{PublicationQuery}"; | ||
} |
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
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
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
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
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
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
3 changes: 2 additions & 1 deletion
3
src/explore-education-statistics-frontend/src/pages/find-statistics/[publication].tsx
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,4 +1,5 @@ | ||
export { | ||
default, | ||
getServerSideProps, | ||
getStaticProps, | ||
getStaticPaths, | ||
} from '@frontend/modules/find-statistics/PublicationReleasePage'; |
3 changes: 2 additions & 1 deletion
3
...plore-education-statistics-frontend/src/pages/find-statistics/[publication]/[release].tsx
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,4 +1,5 @@ | ||
export { | ||
default, | ||
getServerSideProps, | ||
getStaticProps, | ||
getStaticPaths, | ||
} from '@frontend/modules/find-statistics/PublicationReleasePage'; |
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
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
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
Oops, something went wrong.