Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add test coverage for vespa sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
olaughter committed Mar 25, 2024
1 parent 4cf2bda commit 32beab1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 55 deletions.
21 changes: 20 additions & 1 deletion tests/test_search_adaptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import pytest

from cpr_data_access.search_adaptors import VespaSearchAdapter
from cpr_data_access.models.search import SearchParameters, SearchResponse
from cpr_data_access.models.search import (
SearchParameters,
SearchResponse,
sort_fields,
)

from conftest import VESPA_TEST_SEARCH_URL

Expand Down Expand Up @@ -326,3 +330,18 @@ def test_vespa_search_adaptor__continuation_tokens__families_and_passages(
!= sorted([h.text_block_id for h in response_three.families[0].hits])
!= sorted([h.text_block_id for h in response_four.families[0].hits])
)


@pytest.mark.parametrize("sort_by", sort_fields.keys())
@pytest.mark.vespa
def test_vespa_search_adapter_sorting(fake_vespa_credentials, sort_by):
ascend = vespa_search(
fake_vespa_credentials,
SearchParameters(query_string="the", sort_by=sort_by, sort_order="ascending"),
)
descend = vespa_search(
fake_vespa_credentials,
SearchParameters(query_string="the", sort_by=sort_by, sort_order="descending"),
)

assert ascend != descend
16 changes: 15 additions & 1 deletion tests/test_search_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

from pydantic import ValidationError

from cpr_data_access.models.search import KeywordFilters, SearchParameters
from cpr_data_access.models.search import (
KeywordFilters,
SearchParameters,
sort_orders,
sort_fields,
)
from cpr_data_access.vespa import build_vespa_request_body
from cpr_data_access.exceptions import QueryError
from cpr_data_access.embedding import Embedder
Expand Down Expand Up @@ -158,6 +163,15 @@ def test_whether_an_invalid_sort_order_raises_a_queryerror():
assert "sort_order must be one of" in str(excinfo.value)


@pytest.mark.parametrize("sort_by", sort_fields.keys())
@pytest.mark.parametrize("sort_order", sort_orders.keys())
def test_computed_vespa_sort_fields(sort_by, sort_order):
params = SearchParameters(
query_string="test", sort_by=sort_by, sort_order=sort_order
)
assert params.vespa_sort_by and params.vespa_sort_order


@pytest.mark.parametrize(
"field",
["family_geography", "family_category", "document_languages", "family_source"],
Expand Down
52 changes: 0 additions & 52 deletions tests/test_search_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,58 +65,6 @@ def test_whether_an_invalid_vespa_response_raises_a_valueerror(
assert "Received status code 500" in str(excinfo.value)


def test_whether_sorting_by_ascending_date_works(valid_vespa_search_response):
request = SearchParameters(
query_string="test", sort_by="date", sort_order="ascending"
)
response = parse_vespa_response(
request=request, vespa_response=valid_vespa_search_response
)
for family_i, family_j in zip(response.families[:-1], response.families[1:]):
date_i = family_i.hits[0].family_publication_ts
date_j = family_j.hits[0].family_publication_ts
assert date_i <= date_j


def test_whether_sorting_by_descending_date_works(valid_vespa_search_response):
request = SearchParameters(
query_string="test", sort_by="date", sort_order="descending"
)
response = parse_vespa_response(
request=request, vespa_response=valid_vespa_search_response
)
for family_i, family_j in zip(response.families[:-1], response.families[1:]):
date_i = family_i.hits[0].family_publication_ts
date_j = family_j.hits[0].family_publication_ts
assert date_i >= date_j


def test_whether_sorting_by_ascending_name_works(valid_vespa_search_response):
request = SearchParameters(
query_string="test", sort_by="name", sort_order="ascending"
)
response = parse_vespa_response(
request=request, vespa_response=valid_vespa_search_response
)
for family_i, family_j in zip(response.families[:-1], response.families[1:]):
name_i = family_i.hits[0].family_name
name_j = family_j.hits[0].family_name
assert name_i <= name_j


def test_whether_sorting_by_descending_name_works(valid_vespa_search_response):
request = SearchParameters(
query_string="test", sort_by="name", sort_order="descending"
)
response = parse_vespa_response(
request=request, vespa_response=valid_vespa_search_response
)
for family_i, family_j in zip(response.families[:-1], response.families[1:]):
name_i = family_i.hits[0].family_name
name_j = family_j.hits[0].family_name
assert name_i >= name_j


def test_whether_continuation_token_is_returned_when_present(
valid_vespa_search_response,
):
Expand Down
23 changes: 22 additions & 1 deletion tests/test_yql_builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import pytest
from vespa.exceptions import VespaError

from cpr_data_access.models.search import KeywordFilters, SearchParameters
from cpr_data_access.models.search import (
KeywordFilters,
SearchParameters,
sort_fields,
sort_orders,
)
from cpr_data_access.vespa import VespaErrorDetails
from cpr_data_access.yql_builder import YQLBuilder

Expand Down Expand Up @@ -45,6 +50,22 @@ def test_whether_year_ranges_appear_in_yql(
assert exclude not in yql


@pytest.mark.parametrize("sort_by", sort_fields.keys())
@pytest.mark.parametrize("sort_order", sort_orders.keys())
def test_sorting_appears_in_yql(sort_by, sort_order):
params = SearchParameters(
query_string="test", sort_by=sort_by, sort_order=sort_order
)
assert "order" in YQLBuilder(params).to_str()


def test_sorting_does_not_appear_in_yql():
params = SearchParameters(query_string="test", sort_order="ascending")
assert "order" not in YQLBuilder(params).to_str()
params = SearchParameters(query_string="test")
assert "order" not in YQLBuilder(params).to_str()


def test_vespa_error_details():
# With invalid query parameter code
err_object = [
Expand Down

0 comments on commit 32beab1

Please sign in to comment.