From 8cbe4afbea6116d14e09f1957be8b7ef2d131257 Mon Sep 17 00:00:00 2001 From: cairnsj <51908793+cairnsj@users.noreply.github.com> Date: Wed, 15 Nov 2023 11:15:49 +0000 Subject: [PATCH 1/2] Update paging format string to include the sort order --- .../ResourcesDynamicTagsSearchStategyTests.cs | 41 +++++++++++++++++++ .../ResourcesFixedTagsSearchStrategyTests.cs | 40 ++++++++++++++++++ .../ResourcesDynamicTagsSearchStategy.cs | 10 +++-- .../ResourcesFixedTagsSearchStrategy.cs | 10 +++-- 4 files changed, 93 insertions(+), 8 deletions(-) diff --git a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs index 5530c3bd..ef93255c 100644 --- a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs @@ -174,4 +174,45 @@ public async Task Invalid_Tags_Are_Sanitised(string value) //assert actual.SelectedTags.Should().NotContain(value); } + + [TestCase(ResourceSortOrder.UpdatedNewest)] + [TestCase(ResourceSortOrder.UpdatedOldest)] + public async Task SearchAsync_Paging_Should_Respect_SortOrder(ResourceSortOrder sortOrder) + { + // arrange + var results = new ResponseType() + { + ContentCollection = new ContentCollection() + { + Total = 11, + Items = new Collection() + { + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + } + } + }; + SetSearchResults(results); + var query = new ResourcesQuery + { + Page = 2, + Tags = new string[] { "tag1" }, + SortOrder = sortOrder + }; + + // act + var actual = await _sut.SearchAsync(query); + + // assert + actual.PagingFormatString.Should().Be($"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}&tags=tag1"); + } } diff --git a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs index 80a62c27..049a443b 100644 --- a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs @@ -168,4 +168,44 @@ public async Task Invalid_Tags_Are_Sanitised(string value) //assert actual.SelectedTags.Should().NotContain(value); } + + [TestCase(ResourceSortOrder.UpdatedNewest)] + [TestCase(ResourceSortOrder.UpdatedOldest)] + public async Task SearchAsync_Paging_Should_Respect_SortOrder(ResourceSortOrder sortOrder) + { + // arrange + var results = new ResponseType() + { + ContentCollection = new ContentCollection() + { + Total = 11, + Items = new Collection() + { + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + new SearchResult(), + } + } + }; + SetSearchResults(results); + var query = new ResourcesQuery + { + Page = 2, + SortOrder = sortOrder + }; + + // act + var actual = await _sut.SearchAsync(query); + + // assert + actual.PagingFormatString.Should().Be($"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}"); + } } diff --git a/Childrens-Social-Care-CPD/Core/Resources/ResourcesDynamicTagsSearchStategy.cs b/Childrens-Social-Care-CPD/Core/Resources/ResourcesDynamicTagsSearchStategy.cs index 70bb49e3..6cf4a0c8 100644 --- a/Childrens-Social-Care-CPD/Core/Resources/ResourcesDynamicTagsSearchStategy.cs +++ b/Childrens-Social-Care-CPD/Core/Resources/ResourcesDynamicTagsSearchStategy.cs @@ -38,16 +38,18 @@ private static Tuple CalculatePageStats(SearchResourcesByTags.Res return Tuple.Create(totalResults, totalPages, Math.Min(page, totalPages)); } - private static string GetPagingFormatString(IEnumerable tags) + private static string GetPagingFormatString(IEnumerable tags, ResourceSortOrder sortOrder) { + var result = $"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}"; + if (tags.Any()) { var tagStrings = tags.Select(x => $"tags={x}"); var allTags = string.Join("&", tagStrings); - return $"/resources-learning?page={{0}}&{allTags}"; + result += $"&{allTags}"; } - return $"/resources-learning?page={{0}}"; + return result; } public async Task SearchAsync(ResourcesQuery query, CancellationToken cancellationToken = default) @@ -82,7 +84,7 @@ public async Task SearchAsync(ResourcesQuery query, Canc currentPage, totalPages, totalResults, - GetPagingFormatString(query.Tags) + GetPagingFormatString(query.Tags, query.SortOrder) ); } } \ No newline at end of file diff --git a/Childrens-Social-Care-CPD/Core/Resources/ResourcesFixedTagsSearchStrategy.cs b/Childrens-Social-Care-CPD/Core/Resources/ResourcesFixedTagsSearchStrategy.cs index e6680f8b..c866fe44 100644 --- a/Childrens-Social-Care-CPD/Core/Resources/ResourcesFixedTagsSearchStrategy.cs +++ b/Childrens-Social-Care-CPD/Core/Resources/ResourcesFixedTagsSearchStrategy.cs @@ -51,16 +51,18 @@ private static Tuple CalculatePageStats(SearchResourcesByTags.Res return Tuple.Create(totalResults, totalPages, Math.Min(page, totalPages)); } - private static string GetPagingFormatString(int[] tags) + private static string GetPagingFormatString(int[] tags, ResourceSortOrder sortOrder) { + var result = $"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}"; + if (tags.Any()) { var tagStrings = tags.Select(x => $"tags={x}"); var allTags = string.Join("&", tagStrings); - return $"/resources-learning?page={{0}}&{allTags}"; + result += $"&{allTags}"; } - return $"/resources-learning?page={{0}}"; + return result; } public async Task SearchAsync(ResourcesQuery query, CancellationToken cancellationToken = default) @@ -91,7 +93,7 @@ public async Task SearchAsync(ResourcesQuery query, Canc currentPage, totalPages, totalResults, - GetPagingFormatString(queryTags) + GetPagingFormatString(queryTags, query.SortOrder) ); } } \ No newline at end of file From c188a0c1914549e6e9d4ab9a8d69fccc3be94681 Mon Sep 17 00:00:00 2001 From: cairnsj <51908793+cairnsj@users.noreply.github.com> Date: Thu, 16 Nov 2023 08:49:19 +0000 Subject: [PATCH 2/2] Improve tests --- .../Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs | 2 +- .../Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs index ef93255c..5e029f18 100644 --- a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesDynamicTagsSearchStategyTests.cs @@ -213,6 +213,6 @@ public async Task SearchAsync_Paging_Should_Respect_SortOrder(ResourceSortOrder var actual = await _sut.SearchAsync(query); // assert - actual.PagingFormatString.Should().Be($"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}&tags=tag1"); + actual.PagingFormatString.Should().Contain($"sortOrder={(int)sortOrder}"); } } diff --git a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs index 049a443b..4149bdbd 100644 --- a/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs +++ b/Childrens-Social-Care-CPD-Tests/Core/Resources/ResourcesFixedTagsSearchStrategyTests.cs @@ -206,6 +206,6 @@ public async Task SearchAsync_Paging_Should_Respect_SortOrder(ResourceSortOrder var actual = await _sut.SearchAsync(query); // assert - actual.PagingFormatString.Should().Be($"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}"); + actual.PagingFormatString.Should().Contain($"sortOrder={(int)sortOrder}"); } }