Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Handle tally cases where `results` is `None` (#1132)
Madison Swain-Bowden3204b6e8f911
diff --git a/api/catalog/api/controllers/search_controller.py b/api/catalog/api/controllers/search_controller.pyindex 20920b0d5..e1a5a9889 100644--- a/api/catalog/api/controllers/search_controller.py+++ b/api/catalog/api/controllers/search_controller.py@@ -413,11 +413,11 @@ def search(search_response, results, page_size, page)+ results_to_tally = results or []max_result_depth = page * page_sizeif max_result_depth <= 80:# Applies when `page_size * page` could land "evenly" on 80should_tally = True- results_to_tally = resultselif max_result_depth - page_size < 80:# Applies when `page_size * page` could land beyond 80, but still# encompass some results on _this page_ that are at or below the 80th@@ -426,7 +426,7 @@ def search(# the first eight results in `results` that are below or at the 80th# position for the query.should_tally = True- results_to_tally = results[: 80 - (max_result_depth - page_size)]+ results_to_tally = results_to_tally[: 80 - (max_result_depth - page_size)]else:should_tally = Falsediff --git a/api/test/unit/controllers/test_search_controller.py b/api/test/unit/controllers/test_search_controller.pyindex bc81da56d..ac80a0e8a 100644--- a/api/test/unit/controllers/test_search_controller.py+++ b/api/test/unit/controllers/test_search_controller.py@@ -463,3 +463,43 @@ def test_search_tallies_pages_less_than_5(assert len(passed_results) == number_of_results_passedelse:count_provider_occurrences_mock.assert_not_called()+++ "index",+ (+ "image",+ "audio",+ ),+)+ tallies, "count_provider_occurrences", wraps=tallies.count_provider_occurrences+)+ "catalog.api.controllers.search_controller._post_process_results",+)+def test_search_tallies_handles_empty_page(+ mock_post_process_results,+ count_provider_occurrences_mock: mock.MagicMock,+ index,+ request_factory,+):+ mock_post_process_results.return_value = None++ serializer = MediaSearchRequestSerializer(data={"q": "dogs"})+ serializer.is_valid()++ search_controller.search(+ search_params=serializer,+ ip=0,+ index=index,+ # Force calculated result depth length to include results within 80th position and above+ # to force edge case where retrieved results are only partially tallied.+ page=1,+ page_size=100,+ request=request_factory.get("/"),+ filter_dead=True,+ )++ count_provider_occurrences_mock.assert_not_called()