-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added facets to models and some test refactoring * facet faker and failing test for mapper * Fixes to searchByKeywordResponse * encapsulate facet results and establishments results into class for use by Adapter and mapper * refactor mappers * FacetResult count is long not int * pass facets from search service out as part of use-case response * Add integration tests for AdapterAndMapper * fix warnings
- Loading branch information
Showing
24 changed files
with
696 additions
and
167 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
33 changes: 33 additions & 0 deletions
33
...ata.SearchPrototype/Infrastructure/Mappers/AzureFacetResultToEstablishmentFacetsMapper.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,33 @@ | ||
using Dfe.Data.SearchPrototype.Common.Mappers; | ||
using Dfe.Data.SearchPrototype.SearchForEstablishments.Models; | ||
|
||
namespace Dfe.Data.SearchPrototype.Infrastructure.Mappers; | ||
|
||
using AzureFacetResult = Azure.Search.Documents.Models.FacetResult; | ||
|
||
/// <summary> | ||
/// Maps from an Azure facet result to a collection of | ||
/// T:Dfe.Data.SearchPrototype.SearchForEstablishments.Models.EstablishmentFacet | ||
/// </summary> | ||
public class AzureFacetResultToEstablishmentFacetsMapper : IMapper<Dictionary<string, IList<AzureFacetResult>>, EstablishmentFacets> | ||
{ | ||
/// <summary> | ||
/// Map from an Azure facet result to a collection of | ||
/// T:Dfe.Data.SearchPrototype.SearchForEstablishments.Models.EstablishmentFacet | ||
/// </summary> | ||
/// <param name="facetResult">The Azure facet result</param> | ||
/// <returns></returns> | ||
public EstablishmentFacets MapFrom(Dictionary<string, IList<AzureFacetResult>> facetResult) | ||
{ | ||
var establishmentFacets = new List<EstablishmentFacet>(); | ||
|
||
foreach (var facetCategory in facetResult.Where(facet => facet.Value != null)) | ||
{ | ||
var values = facetCategory.Value.Select(f => new FacetResult((string)f.Value, f.Count)).ToList(); | ||
var establishmentFacet = new EstablishmentFacet(facetCategory.Key, values); | ||
|
||
establishmentFacets.Add(establishmentFacet); | ||
} | ||
return new EstablishmentFacets (establishmentFacets); | ||
} | ||
} |
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
125 changes: 125 additions & 0 deletions
125
Dfe.Data.SearchPrototype/Infrastructure/Tests/CognitiveSearchServiceAdapterAndMapperTests.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,125 @@ | ||
using Azure; | ||
using Azure.Search.Documents.Models; | ||
using Dfe.Data.SearchPrototype.Common.Mappers; | ||
using Dfe.Data.SearchPrototype.Infrastructure.Mappers; | ||
using Dfe.Data.SearchPrototype.Infrastructure.Options; | ||
using Dfe.Data.SearchPrototype.Infrastructure.Tests.TestDoubles; | ||
using Dfe.Data.SearchPrototype.SearchForEstablishments; | ||
using Dfe.Data.SearchPrototype.SearchForEstablishments.Models; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Dfe.Data.SearchPrototype.Infrastructure.Tests; | ||
|
||
public sealed class CognitiveSearchServiceAdapterAndMapperTests | ||
{ | ||
private ISearchOptionsFactory _mockSearchOptionsFactory; | ||
private IMapper<Pageable<SearchResult<Establishment>>, EstablishmentResults> _searchResponseMapper; | ||
private IMapper<Dictionary<string, IList<Azure.Search.Documents.Models.FacetResult>>, EstablishmentFacets> _facetsMapper; | ||
|
||
public CognitiveSearchServiceAdapterAndMapperTests() | ||
{ | ||
_mockSearchOptionsFactory = SearchOptionsFactoryTestDouble.MockSearchOptionsFactory(); | ||
_searchResponseMapper = new PageableSearchResultsToEstablishmentResultsMapper( | ||
new AzureSearchResultToEstablishmentMapper( | ||
new AzureSearchResultToAddressMapper())); | ||
_facetsMapper = new AzureFacetResultToEstablishmentFacetsMapper(); | ||
} | ||
|
||
[Fact] | ||
public async Task Search_WithValidSearchContext_ReturnsResults() | ||
{ | ||
// arrange | ||
var establishmentSearchResults = new SearchResultFakeBuilder() | ||
.WithSearchResults() | ||
.Create(); | ||
var facetResults = new FacetsResultsFakeBuilder() | ||
.WithAutoGeneratedFacets() | ||
.Create(); | ||
var mockService = new SearchServiceMockBuilder() | ||
.WithSearchOptions("SearchKeyword", "TargetCollection") | ||
.WithSearchResults(establishmentSearchResults) | ||
.WithFacets(facetResults) | ||
.Create(); | ||
|
||
ISearchServiceAdapter cognitiveSearchServiceAdapter = new CognitiveSearchServiceAdapter<Establishment>( | ||
mockService, | ||
_mockSearchOptionsFactory, | ||
_searchResponseMapper, | ||
_facetsMapper); | ||
|
||
// act | ||
SearchResults? response = | ||
await cognitiveSearchServiceAdapter.SearchAsync( | ||
new SearchContext( | ||
searchKeyword: "SearchKeyword", | ||
targetCollection: "TargetCollection")); | ||
|
||
// assert | ||
response.Should().NotBeNull(); | ||
response.Establishments.Should().NotBeNull(); | ||
response.Establishments!.Establishments.Count().Should().Be(establishmentSearchResults.Count); | ||
response.Facets.Should().NotBeNull(); | ||
response.Facets!.Facets.Count().Should().Be(facetResults.Count()); | ||
} | ||
|
||
[Fact] | ||
public async Task Search_WithNoFacetsReturned_ReturnsNullFacets() | ||
{ | ||
// arrange | ||
var mockService = new SearchServiceMockBuilder() | ||
.WithSearchOptions("SearchKeyword", "TargetCollection") | ||
.WithSearchResults( | ||
new SearchResultFakeBuilder() | ||
.WithSearchResults() | ||
.Create()) | ||
.Create(); | ||
var mockSearchOptionsFactory = SearchOptionsFactoryTestDouble.MockSearchOptionsFactory(); | ||
|
||
ISearchServiceAdapter cognitiveSearchServiceAdapter = new CognitiveSearchServiceAdapter<Establishment>( | ||
mockService, | ||
mockSearchOptionsFactory, | ||
_searchResponseMapper, | ||
_facetsMapper); | ||
|
||
// act | ||
SearchResults? response = | ||
await cognitiveSearchServiceAdapter.SearchAsync( | ||
new SearchContext( | ||
searchKeyword: "SearchKeyword", | ||
targetCollection: "TargetCollection")); | ||
|
||
// assert | ||
response.Should().NotBeNull(); | ||
response.Facets.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task Search_WithNoResultsReturned_ReturnsEmptyResults() | ||
{ | ||
// arrange | ||
var mockService = new SearchServiceMockBuilder() | ||
.WithSearchOptions("SearchKeyword", "TargetCollection") | ||
.WithSearchResults( | ||
new SearchResultFakeBuilder() | ||
.WithEmptySearchResult() | ||
.Create()) | ||
.Create(); | ||
|
||
ISearchServiceAdapter cognitiveSearchServiceAdapter = new CognitiveSearchServiceAdapter<Establishment>( | ||
mockService, | ||
_mockSearchOptionsFactory, | ||
_searchResponseMapper, | ||
_facetsMapper); | ||
|
||
// act. | ||
var response = await cognitiveSearchServiceAdapter.SearchAsync(new SearchContext( | ||
searchKeyword: "SearchKeyword", | ||
targetCollection: "TargetCollection")); | ||
|
||
// assert | ||
response.Should().NotBeNull(); | ||
response.Establishments.Should().NotBeNull(); | ||
response.Establishments!.Establishments.Should().BeEmpty(); | ||
} | ||
} |
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.