Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aw/pagination #58

Merged
merged 15 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public interface ISearchOptionsBuilder
/// </returns>
ISearchOptionsBuilder WithSize(int? size);

/// <summary>
/// Sets the value used to define how many
/// records are skipped in the search response (if any).
/// </summary>
/// <param name="offset">
/// The number of initial search results to skip.
/// </param>
RogerHowellDfE marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>
/// The updated builder instance.
/// </returns>
ISearchOptionsBuilder WithOffset(int? offset);

/// <summary>
/// Sets the mode of search to invoke, i.e. All or Any.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public sealed class SearchOptionsBuilder : ISearchOptionsBuilder

private SearchMode? _searchMode;
private int? _size;
private int? _offset;
private bool? _includeTotalCount;
private IList<string>? _searchFields;
private IList<string>? _facets;
Expand Down Expand Up @@ -53,6 +54,22 @@ public ISearchOptionsBuilder WithSize(int? size)
return this;
}

/// <summary>
/// Sets the value used to define how many
/// records are skipped in the search response (if any).
/// </summary>
/// <param name="offset">
/// The number of initial search results to skip.
/// </param>
/// <returns>
/// The updated builder instance.
/// </returns>
public ISearchOptionsBuilder WithOffset(int? offset)
{
_offset = offset;
return this;
}

/// <summary>
/// Sets the mode of search to invoke, i.e. All or Any.
/// </summary>
Expand Down Expand Up @@ -139,6 +156,7 @@ public SearchOptions Build()
{
_searchOptions.SearchMode = _searchMode;
_searchOptions.Size = _size;
_searchOptions.Skip = _offset;
_searchOptions.IncludeTotalCount = _includeTotalCount;
_searchFields?.ToList().ForEach(_searchOptions.SearchFields.Add);
_facets?.ToList().ForEach(_searchOptions.Facets.Add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Dfe.Data.SearchPrototype.Infrastructure;
public sealed class CognitiveSearchServiceAdapter<TSearchResult> : ISearchServiceAdapter where TSearchResult : class
{
private readonly ISearchByKeywordService _searchByKeywordService;
private readonly IMapper<Pageable<SearchResult<TSearchResult>>, EstablishmentResults> _searchResultMapper;
private readonly IMapper<(Pageable<SearchResult<TSearchResult>>, long?), EstablishmentResults> _searchResultMapper;
private readonly IMapper<Dictionary<string, IList<AzureModels.FacetResult>>, EstablishmentFacets> _facetsMapper;
private readonly AzureSearchOptions _azureSearchOptions;
private readonly ISearchOptionsBuilder _searchOptionsBuilder;
Expand All @@ -46,7 +46,7 @@ public sealed class CognitiveSearchServiceAdapter<TSearchResult> : ISearchServic
public CognitiveSearchServiceAdapter(
ISearchByKeywordService searchByKeywordService,
IOptions<AzureSearchOptions> azureSearchOptions,
IMapper<Pageable<SearchResult<TSearchResult>>, EstablishmentResults> searchResultMapper,
IMapper<(Pageable<SearchResult<TSearchResult>>, long?), EstablishmentResults> searchResultMapper,
IMapper<Dictionary<string, IList<AzureModels.FacetResult>>, EstablishmentFacets> facetsMapper,
ISearchOptionsBuilder searchOptionsBuilder)
{
Expand Down Expand Up @@ -82,6 +82,7 @@ public async Task<SearchResults> SearchAsync(SearchServiceAdapterRequest searchS
_searchOptionsBuilder
.WithSearchMode((SearchMode)_azureSearchOptions.SearchMode)
.WithSize(_azureSearchOptions.Size)
.WithOffset(searchServiceAdapterRequest.Offset)
.WithIncludeTotalCount(_azureSearchOptions.IncludeTotalCount)
.WithSearchFields(searchServiceAdapterRequest.SearchFields)
.WithFacets(searchServiceAdapterRequest.Facets)
Expand All @@ -100,7 +101,9 @@ await _searchByKeywordService.SearchAsync<TSearchResult>(

var results = new SearchResults()
{
Establishments = _searchResultMapper.MapFrom(searchResults.Value.GetResults()),
Establishments =
_searchResultMapper.MapFrom(
(searchResults.Value.GetResults(), searchResults.Value.TotalCount)),
Facets = searchResults.Value.Facets != null
? _facetsMapper.MapFrom(searchResults.Value.Facets.ToDictionary())
: null
Expand Down
2 changes: 1 addition & 1 deletion Dfe.Data.SearchPrototype/Infrastructure/CompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void AddCognitiveSearchAdaptorServices(this IServiceCollection ser

services.AddScoped(typeof(ISearchServiceAdapter), typeof(CognitiveSearchServiceAdapter<DataTransferObjects.Establishment>));
services.AddScoped<ISearchOptionsBuilder, SearchOptionsBuilder>();
services.AddSingleton(typeof(IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults>), typeof(PageableSearchResultsToEstablishmentResultsMapper));
services.AddSingleton(typeof(IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults>), typeof(PageableSearchResultsToEstablishmentResultsMapper));
services.AddSingleton<IMapper<Dictionary<string, IList<Azure.Search.Documents.Models.FacetResult>>, EstablishmentFacets>, AzureFacetResultToEstablishmentFacetsMapper>();
services.AddSingleton<IMapper<DataTransferObjects.Establishment, Address>, AzureSearchResultToAddressMapper>();
services.AddSingleton<IMapper<DataTransferObjects.Establishment, Establishment>, AzureSearchResultToEstablishmentMapper>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Dfe.Data.SearchPrototype.Infrastructure.Mappers;

/// <summary>
/// Facilitates mapping from the received <see cref="Models.SearchResults"/>
/// into the required <see cref="Models.EstablishmentResults"/> object.
/// into the required <see cref="Models.EstablishmentResults"/> object.
/// </summary>
public sealed class PageableSearchResultsToEstablishmentResultsMapper : IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, Models.EstablishmentResults>
public sealed class PageableSearchResultsToEstablishmentResultsMapper : IMapper<(Pageable<SearchResult<Establishment>>, long?), Models.EstablishmentResults>
{
private readonly IMapper<DataTransferObjects.Establishment, Models.Establishment> _azureSearchResultToEstablishmentMapper;
private readonly IMapper<Establishment, Models.Establishment> _azureSearchResultToEstablishmentMapper;

/// <summary>
/// The following mapping dependency provides the functionality to map from a raw Azure
Expand All @@ -22,7 +22,7 @@ public sealed class PageableSearchResultsToEstablishmentResultsMapper : IMapper<
/// <param name="azureSearchResultToEstablishmentMapper">
/// Mapper used to map from the raw Azure search result to a <see cref="Establishment"/> instance.
/// </param>
public PageableSearchResultsToEstablishmentResultsMapper(IMapper<DataTransferObjects.Establishment, Models.Establishment> azureSearchResultToEstablishmentMapper)
public PageableSearchResultsToEstablishmentResultsMapper(IMapper<Establishment, Models.Establishment> azureSearchResultToEstablishmentMapper)
{
_azureSearchResultToEstablishmentMapper = azureSearchResultToEstablishmentMapper;
}
Expand All @@ -44,23 +44,23 @@ public PageableSearchResultsToEstablishmentResultsMapper(IMapper<DataTransferObj
/// <exception cref="ArgumentException">
/// Exception thrown if the data cannot be mapped
/// </exception>
public Models.EstablishmentResults MapFrom(Pageable<SearchResult<DataTransferObjects.Establishment>> input)
public Models.EstablishmentResults MapFrom((Pageable<SearchResult<Establishment>>, long?) input)
RogerHowellDfE marked this conversation as resolved.
Show resolved Hide resolved
{
ArgumentNullException.ThrowIfNull(input);
Models.EstablishmentResults establishmentResults = new();

if (input.Any())
if (input.Item1.Any())
{
var mappedResults = input.Select(result =>
var mappedResults = input.Item1.Select(result =>
result.Document != null
? _azureSearchResultToEstablishmentMapper.MapFrom(result.Document)
: throw new InvalidOperationException(
"Search result document object cannot be null.")
);
return new Models.EstablishmentResults(mappedResults);
}
else
{
return new Models.EstablishmentResults();
"Search result document object cannot be null."));

establishmentResults =
new Models.EstablishmentResults(mappedResults, input.Item2);
}

return establishmentResults;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public void Build_WithSize_SearchOptionsWithCorrectSize()
searchOptions.Size.Should().Be(100);
}

[Fact]
public void Build_WithOfset_SearchOptionsWithCorrectOffset()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo ofset

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change made.

{
// arrange
ISearchFilterExpressionsBuilder mockSearchFilterExpressionsBuilder = new FilterExpressionBuilderTestDouble().Create();

ISearchOptionsBuilder searchOptionsBuilder = new SearchOptionsBuilder(mockSearchFilterExpressionsBuilder);

// act
SearchOptions searchOptions = searchOptionsBuilder.WithOffset(offset: 39).Build();

// assert
searchOptions.Should().NotBeNull();
searchOptions.Skip.Should().Be(39);
}

[Fact]
public void Build_WithSearchMode_SearchOptionsWithCorrectSearchMode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Dfe.Data.SearchPrototype.Infrastructure.Tests;

public sealed class CognitiveSearchServiceAdapterAndMapperTests
{
private readonly IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> _searchResponseMapper;
private readonly IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> _searchResponseMapper;
private readonly IMapper<Dictionary<string, IList<Azure.Search.Documents.Models.FacetResult>>, EstablishmentFacets> _facetsMapper;
private readonly ISearchOptionsBuilder _searchOptionsBuilder = SearchOptionsBuilderTestDouble.MockFor(new Azure.Search.Documents.SearchOptions());

Expand Down Expand Up @@ -57,7 +57,8 @@ await cognitiveSearchServiceAdapter.SearchAsync(
new SearchServiceAdapterRequest(
searchKeyword: "SearchKeyword",
searchFields: ["FIELD1", "FIELD2", "FIELD2"],
facets: ["FACET1", "FACET2", "FACET3"]));
facets: ["FACET1", "FACET2", "FACET3"],
offset: 99));

// assert
response.Should().NotBeNull();
Expand Down Expand Up @@ -94,7 +95,8 @@ await cognitiveSearchServiceAdapter.SearchAsync(
new SearchServiceAdapterRequest(
searchKeyword: "SearchKeyword",
searchFields: ["FIELD1", "FIELD2", "FIELD2"],
facets: ["FACET1", "FACET2", "FACET3"]));
facets: ["FACET1", "FACET2", "FACET3"],
offset: 0));

// assert
response.Should().NotBeNull();
Expand Down Expand Up @@ -128,7 +130,8 @@ await cognitiveSearchServiceAdapter.SearchAsync(
new SearchServiceAdapterRequest(
searchKeyword: "SearchKeyword",
searchFields: ["FIELD1", "FIELD2", "FIELD2"],
facets: ["FACET1", "FACET2", "FACET3"]));
facets: ["FACET1", "FACET2", "FACET3"],
offset: 0));

// assert
response.Should().NotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class CognitiveSearchServiceAdapterTests
private readonly IMapper<Dictionary<string, IList<Azure.Search.Documents.Models.FacetResult>>, EstablishmentFacets> _mockFacetsMapper
= AzureFacetResultToEstablishmentFacetsMapperTestDouble.DefaultMock();
private readonly AzureSearchOptions _options = AzureSearchOptionsTestDouble.Stub();
private readonly IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> _mockEstablishmentResultsMapper
private readonly IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> _mockEstablishmentResultsMapper
= PageableSearchResultsToEstablishmentResultsMapperTestDouble.DefaultMock();
private readonly ISearchByKeywordService _mockSearchService;
private readonly ISearchOptionsBuilder _mockSearchOptionsBuilder =
Expand All @@ -30,7 +30,7 @@ public sealed class CognitiveSearchServiceAdapterTests
private static CognitiveSearchServiceAdapter<DataTransferObjects.Establishment> CreateServiceAdapterWith(
ISearchByKeywordService searchByKeywordService,
IOptions<AzureSearchOptions> searchOptions,
IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> searchResponseMapper,
IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> searchResponseMapper,
IMapper<Dictionary<string, IList<Azure.Search.Documents.Models.FacetResult>>, EstablishmentFacets> facetsMapper,
ISearchOptionsBuilder searchOptionsBuilder
) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Azure;
using Azure.Search.Documents.Models;
using Dfe.Data.SearchPrototype.Common.Mappers;
using Dfe.Data.SearchPrototype.Infrastructure.DataTransferObjects;
using Dfe.Data.SearchPrototype.Infrastructure.Mappers;
using Dfe.Data.SearchPrototype.Infrastructure.Tests.TestDoubles;
using Dfe.Data.SearchPrototype.Infrastructure.Tests.TestHelpers;
Expand All @@ -13,7 +12,7 @@ namespace Dfe.Data.SearchPrototype.Infrastructure.Tests.Mappers;

public sealed class PageableSearchResultsToEstablishmentResultsMapperTests
{
IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> _searchResultsMapper;
IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> _searchResultsMapper;

public PageableSearchResultsToEstablishmentResultsMapperTests()
{
Expand All @@ -29,14 +28,16 @@ public void MapFrom_WithValidSearchResults_ReturnsConfiguredEstablishments()
// arrange
List<SearchResult<DataTransferObjects.Establishment>> searchResultDocuments =
SearchResultFake.SearchResults();

var pageableSearchResults = PageableTestDouble.FromResults(searchResultDocuments);

// act
EstablishmentResults? mappedResult = _searchResultsMapper.MapFrom(pageableSearchResults);
EstablishmentResults? mappedResult = _searchResultsMapper.MapFrom((pageableSearchResults, 100));

// assert
mappedResult.Should().NotBeNull();
mappedResult.Establishments.Should().HaveCount(searchResultDocuments.Count());
mappedResult.Establishments.Should().HaveCount(searchResultDocuments.Count);

foreach (var searchResult in searchResultDocuments)
{
searchResult.ShouldHaveMatchingMappedEstablishment(mappedResult);
Expand All @@ -47,7 +48,7 @@ public void MapFrom_WithValidSearchResults_ReturnsConfiguredEstablishments()
public void MapFrom_WithEmptySearchResults_ReturnsEmptyList()
{
// act
EstablishmentResults? result = _searchResultsMapper.MapFrom(PageableTestDouble.FromResults(SearchResultFake.EmptySearchResult()));
EstablishmentResults? result = _searchResultsMapper.MapFrom((PageableTestDouble.FromResults(SearchResultFake.EmptySearchResult()), 100));

// assert
result.Should().NotBeNull();
Expand All @@ -60,10 +61,10 @@ public void MapFrom_WithNullSearchResults_ThrowsArgumentNullException()
// act.
_searchResultsMapper
.Invoking(mapper =>
mapper.MapFrom(null!))
mapper.MapFrom((null!, 0)))
.Should()
.Throw<ArgumentNullException>()
.WithMessage("Value cannot be null. (Parameter 'input')");
.WithMessage("Value cannot be null. (Parameter 'source')");
}

[Fact]
Expand All @@ -77,7 +78,7 @@ public void MapFrom_WithANullSearchResult_ThrowsInvalidOperationException()
// act.
_searchResultsMapper
.Invoking(mapper =>
mapper.MapFrom(PageableTestDouble.FromResults(searchResultDocuments)))
mapper.MapFrom((PageableTestDouble.FromResults(searchResultDocuments), 100)))
.Should()
.Throw<InvalidOperationException>()
.WithMessage("Search result document object cannot be null.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ namespace Dfe.Data.SearchPrototype.Infrastructure.Tests.TestDoubles;

internal static class PageableSearchResultsToEstablishmentResultsMapperTestDouble
{
public static IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> DefaultMock() =>
Mock.Of<IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults>>();
public static IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> DefaultMock() =>
Mock.Of<IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults>>();

public static Expression<Func<IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults>, EstablishmentResults>> MapFrom() =>
mapper => mapper.MapFrom(It.IsAny<Pageable<SearchResult<DataTransferObjects.Establishment>>>());
public static Expression<Func<IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults>, EstablishmentResults>> MapFrom() =>
mapper => mapper.MapFrom(It.IsAny<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long ?)>());

public static IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> MockFor(EstablishmentResults establishments)
public static IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> MockFor(EstablishmentResults establishments)
{
var mapperMock = new Mock<IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults>>();
var mapperMock = new Mock<IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults>>();

mapperMock.Setup(MapFrom()).Returns(establishments);

return mapperMock.Object;
}

public static IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults> MockMapperThrowingArgumentException()
public static IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults> MockMapperThrowingArgumentException()
{
var mapperMock = new Mock<IMapper<Pageable<SearchResult<DataTransferObjects.Establishment>>, EstablishmentResults>>();
var mapperMock = new Mock<IMapper<(Pageable<SearchResult<DataTransferObjects.Establishment>>, long?), EstablishmentResults>>();

mapperMock.Setup(MapFrom()).Throws(new ArgumentException());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static ISearchOptionsBuilder MockFor(SearchOptions searchOptions)

mockSearchOptionsBuilder.Setup(searchOptionsBuilder =>
searchOptionsBuilder.WithSize(It.IsAny<int?>())).Returns(mockSearchOptionsBuilder.Object);
mockSearchOptionsBuilder.Setup(searchOptionsBuilder =>
searchOptionsBuilder.WithOffset(It.IsAny<int?>())).Returns(mockSearchOptionsBuilder.Object);
mockSearchOptionsBuilder.Setup(searchOptionsBuilder =>
searchOptionsBuilder.WithSearchMode(It.IsAny<SearchMode>())).Returns(mockSearchOptionsBuilder.Object);
mockSearchOptionsBuilder.Setup(searchOptionsBuilder =>
Expand Down
Loading