Skip to content

Commit

Permalink
Removed hard dependency on ISearchFilterExpressionsBuilder in SearchO…
Browse files Browse the repository at this point in the history
…ptionsBuilder (#56)

Co-authored-by: Spencer O'HEGARTY <Spencer.OHEGARTY@EDUCATION.GOV.UK>
  • Loading branch information
spanersoraferty and spencerohegartyDfE authored Oct 8, 2024
1 parent 075527e commit 76baa95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Dfe.Data.SearchPrototype.Infrastructure.Builders
public sealed class SearchOptionsBuilder : ISearchOptionsBuilder
{
private readonly SearchOptions _searchOptions;
private readonly ISearchFilterExpressionsBuilder _searchFilterExpressionsBuilder;
private readonly ISearchFilterExpressionsBuilder? _searchFilterExpressionsBuilder;

private SearchMode? _searchMode;
private int? _size;
Expand All @@ -29,9 +29,12 @@ public sealed class SearchOptionsBuilder : ISearchOptionsBuilder
/// <param name="searchFilterExpressionsBuilder">
/// Builds the search filter expression required by Azure AI Search
/// </param>
public SearchOptionsBuilder(ISearchFilterExpressionsBuilder searchFilterExpressionsBuilder)
public SearchOptionsBuilder(ISearchFilterExpressionsBuilder? searchFilterExpressionsBuilder = null)
{
_searchFilterExpressionsBuilder = searchFilterExpressionsBuilder;
if (searchFilterExpressionsBuilder != null){
_searchFilterExpressionsBuilder = searchFilterExpressionsBuilder;
}

_searchOptions = new SearchOptions();
}

Expand Down Expand Up @@ -140,7 +143,7 @@ public SearchOptions Build()
_searchFields?.ToList().ForEach(_searchOptions.SearchFields.Add);
_facets?.ToList().ForEach(_searchOptions.Facets.Add);

if (_filters?.Count > 0)
if (_filters?.Count > 0 && _searchFilterExpressionsBuilder != null)
{
_searchOptions.Filter =
_searchFilterExpressionsBuilder.BuildSearchFilterExpressions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,23 @@ public void Build_WithFilters_CallsFilterBuilder_WithComposedFilterRequests()
matchingFilterRequest.FilterValues.Should().BeEquivalentTo(filterRequest.FilterValues);
}
}

[Fact]
public void Build_WithFiltersNullSearchFilterExpressionsBuilder_ByPassesFilterBuilderCall()
{
// arrange
var serviceAdapterInputFilterRequest =
new List<FilterRequest>() { FilterRequestFake.Create(), FilterRequestFake.Create() };

ISearchOptionsBuilder searchOptionsBuilder =
new SearchOptionsBuilder(searchFilterExpressionsBuilder: null);

// act
SearchOptions searchOptions =
searchOptionsBuilder.WithFilters(serviceAdapterInputFilterRequest).Build();

// assert
searchOptions.Filter.Should().BeNull();
}
}
}

0 comments on commit 76baa95

Please sign in to comment.