public async Task PerformSearch_FirstSearchResultReturnedCorrectly() { // Arrange IDatasetsApiClient datasetClient = Substitute.For <IDatasetsApiClient>(); ILogger logger = Substitute.For <ILogger>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); IFeatureToggle featureToggle = CreateFeatureToggle(); IDatasetSearchService datasetSearchService = new DatasetSearchService(datasetClient, mapper, logger, featureToggle); int numberOfItems = 25; PagedResult <DatasetSearchResultItem> itemResult = GeneratePagedResult(numberOfItems); datasetClient .FindDatasets(Arg.Any <SearchFilterRequest>()) .Returns(itemResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act DatasetSearchResultViewModel results = await datasetSearchService.PerformSearch(request); // Assert DatasetSearchResultItemViewModel first = results.Datasets.First(); first.Should().NotBeNull(); first.Id.Should().Be("10"); first.Status.Should().Be("Unknown"); first.LastUpdated.Should().Be(new DateTime(2018, 2, 6, 15, 31, 0)); first.Name.Should().Be("Dataset 1"); }
public async Task PerformSearch_StartAndEndItemsNumbersDisplayedCorrectlyOnSecondPageOfItemsWithMorePagesAvailable() { // Arrange IDatasetsApiClient datasetClient = Substitute.For <IDatasetsApiClient>(); ILogger logger = Substitute.For <ILogger>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); IFeatureToggle featureToggle = CreateFeatureToggle(); IDatasetSearchService datasetSearchService = new DatasetSearchService(datasetClient, mapper, logger, featureToggle); int numberOfItems = 50; PagedResult <DatasetSearchResultItem> itemResult = GeneratePagedResult(numberOfItems); itemResult.PageNumber = 2; itemResult.PageSize = 50; itemResult.TotalItems = 175; datasetClient .FindDatasets(Arg.Any <SearchFilterRequest>()) .Returns(itemResult); SearchRequestViewModel request = new SearchRequestViewModel() { PageNumber = 2, }; // Act DatasetSearchResultViewModel results = await datasetSearchService.PerformSearch(request); // Assert results.StartItemNumber.Should().Be(51); results.EndItemNumber.Should().Be(100); }
public async Task PerformSearch_WhenFindDatasetsServiceReturnsNotFound_ThenNullReturned() { // Arrange IDatasetsApiClient datasetClient = Substitute.For <IDatasetsApiClient>(); ILogger logger = Substitute.For <ILogger>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); IFeatureToggle featureToggle = CreateFeatureToggle(); IDatasetSearchService datasetSearchService = new DatasetSearchService(datasetClient, mapper, logger, featureToggle); PagedResult <DatasetSearchResultItem> expectedServiceResult = null; datasetClient .FindDatasets(Arg.Any <SearchFilterRequest>()) .Returns(expectedServiceResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act DatasetSearchResultViewModel result = await datasetSearchService.PerformSearch(request); // Assert result.Should().BeNull(); }
public async Task <DatasetSearchResultViewModel> PerformSearch(SearchRequestViewModel request) { SearchFilterRequest requestOptions = new SearchFilterRequest() { Page = request.PageNumber.HasValue ? request.PageNumber.Value : 1, PageSize = request.PageSize.HasValue ? request.PageSize.Value : 50, SearchTerm = request.SearchTerm, IncludeFacets = request.IncludeFacets, Filters = request.Filters, SearchMode = _featureToggle.IsSearchModeAllEnabled() ? SearchMode.All : SearchMode.Any }; if (request.PageNumber.HasValue && request.PageNumber.Value > 0) { requestOptions.Page = request.PageNumber.Value; } PagedResult <DatasetSearchResultItem> searchRequestResult = await _datasetsClient.FindDatasets(requestOptions); if (searchRequestResult == null) { _logger.Error("Find datasets HTTP request failed"); return(null); } DatasetSearchResultViewModel result = new DatasetSearchResultViewModel { TotalResults = searchRequestResult.TotalItems, CurrentPage = searchRequestResult.PageNumber, }; List <SearchFacetViewModel> searchFacets = new List <SearchFacetViewModel>(); if (searchRequestResult.Facets != null) { foreach (SearchFacet facet in searchRequestResult.Facets) { searchFacets.Add(_mapper.Map <SearchFacetViewModel>(facet)); } } result.Facets = searchFacets.AsEnumerable(); List <DatasetSearchResultItemViewModel> itemResults = new List <DatasetSearchResultItemViewModel>(); foreach (DatasetSearchResultItem searchResult in searchRequestResult.Items) { itemResults.Add(_mapper.Map <DatasetSearchResultItemViewModel>(searchResult)); } result.Datasets = itemResults.AsEnumerable(); if (result.TotalResults == 0) { result.StartItemNumber = 0; result.EndItemNumber = 0; } else { result.StartItemNumber = ((requestOptions.Page - 1) * requestOptions.PageSize) + 1; result.EndItemNumber = result.StartItemNumber + requestOptions.PageSize - 1; } if (result.EndItemNumber > searchRequestResult.TotalItems) { result.EndItemNumber = searchRequestResult.TotalItems; } result.PagerState = new PagerState(requestOptions.Page, searchRequestResult.TotalPages); return(result); }
public async Task PerformSearch_FirstSearchResultWithFacets_EnsuresFacetsLoadedCorrectly() { // Arrange IDatasetsApiClient datasetClient = Substitute.For <IDatasetsApiClient>(); ILogger logger = Substitute.For <ILogger>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); IFeatureToggle featureToggle = CreateFeatureToggle(); IDatasetSearchService datasetSearchService = new DatasetSearchService(datasetClient, mapper, logger, featureToggle); int numberOfItems = 25; IEnumerable <SearchFacet> facets = new[] { new SearchFacet { Name = "facet 1", FacetValues = new[] { new SearchFacetValue { Name = "f1", Count = 5 } } }, new SearchFacet { Name = "facet 2", FacetValues = new[] { new SearchFacetValue { Name = "f2", Count = 11 }, new SearchFacetValue { Name = "f3", Count = 1 } } } }; PagedResult <DatasetSearchResultItem> itemResult = GeneratePagedResult(numberOfItems, facets); datasetClient .FindDatasets(Arg.Any <SearchFilterRequest>()) .Returns(itemResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act DatasetSearchResultViewModel results = await datasetSearchService.PerformSearch(request); // Assert DatasetSearchResultItemViewModel first = results.Datasets.First(); first.Should().NotBeNull(); first.Id.Should().Be("10"); first.LastUpdated.Should().Be(new DateTime(2018, 2, 6, 15, 31, 0)); first.Status.Should().Be("Unknown"); first.Name.Should().Be("Dataset 1"); results.Facets.Count().Should().Be(2); results.Facets.First().Name.Should().Be("facet 1"); results.Facets.First().FacetValues.Count().Should().Be(1); results.Facets.First().FacetValues.First().Name.Should().Be("f1"); results.Facets.First().FacetValues.First().Count.Should().Be(5); results.Facets.Last().Name.Should().Be("facet 2"); results.Facets.Last().FacetValues.Count().Should().Be(2); results.Facets.Last().FacetValues.First().Name.Should().Be("f2"); results.Facets.Last().FacetValues.First().Count.Should().Be(11); results.Facets.Last().FacetValues.Last().Name.Should().Be("f3"); results.Facets.Last().FacetValues.Last().Count.Should().Be(1); }