Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Handle tally cases where results is None #1132

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions api/catalog/api/controllers/search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ def search(
search_response, results, page_size, page
)

results_to_tally = results or []
max_result_depth = page * page_size
if max_result_depth <= 80:
# Applies when `page_size * page` could land "evenly" on 80
should_tally = True
results_to_tally = results
elif 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
Expand All @@ -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 = False

Expand Down
40 changes: 40 additions & 0 deletions api/test/unit/controllers/test_search_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,43 @@ def test_search_tallies_pages_less_than_5(
assert len(passed_results) == number_of_results_passed
else:
count_provider_occurrences_mock.assert_not_called()


@pytest.mark.parametrize(
"index",
(
"image",
"audio",
),
)
@mock.patch.object(
tallies, "count_provider_occurrences", wraps=tallies.count_provider_occurrences
)
@mock.patch(
"catalog.api.controllers.search_controller._post_process_results",
)
@pytest.mark.django_db
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()