Skip to content

Commit

Permalink
Feature/add concept filters field validation (#128)
Browse files Browse the repository at this point in the history
* Adding a pydantic validation and relating check to ensure that in browse mode concept filters are not set.

* Updating validation check and tests.

* Updating the version.

* Resolving failing test.

* updating the error message.

---------

Co-authored-by: Mark <[email protected]>
  • Loading branch information
THOR300 and Mark authored Oct 8, 2024
1 parent 1431551 commit 72c7c77
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/cpr_sdk/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ def validate(self):
)
return self

@model_validator(mode="after")
def concept_filters_not_set_if_documents_only(self) -> "SearchParameters":
"""Ensure concept_filters are not set if browse mode (documents_only) is set."""
if self.concept_filters is not None and self.documents_only is True:
raise ValueError(
"Cannot set concept_filters when only searching documents. This is as concept_filters are only applicable to passages."
)
return self

@field_validator("continuation_tokens")
def continuation_tokens_must_be_upper_strings(cls, continuation_tokens):
"""Validate continuation_tokens match the expected format"""
Expand Down
2 changes: 1 addition & 1 deletion src/cpr_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_MAJOR = "1"
_MINOR = "9"
_PATCH = "0"
_PATCH = "1"
_SUFFIX = ""

VERSION_SHORT = "{0}.{1}".format(_MAJOR, _MINOR)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_search_adaptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ def test_vespa_search_adaptor__corpus_type_name(
{"name": "id", "value": "concept_0_0"},
],
),
(
"",
[
{"name": "parent_concept_ids_flat", "value": "Q0,"},
{"name": "id", "value": "concept_0_0"},
],
),
],
)
def test_vespa_search_adaptor__concept_filter(
Expand All @@ -513,6 +520,7 @@ def test_vespa_search_adaptor__concept_filter(
ConceptFilter.model_validate(concept_filter)
for concept_filter in concept_filters
],
documents_only=False,
)
response = vespa_search(test_vespa, request)
assert response.total_family_hits > 0
Expand Down
47 changes: 47 additions & 0 deletions tests/test_search_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Optional

import pytest
from pydantic_core import ValidationError

from cpr_sdk.models.search import SearchParameters


@pytest.mark.parametrize(
"params,expect_error,error_message",
[
({"query_string": "the"}, False, None),
({"query_string": ""}, False, None),
(
{
"query_string": "",
"documents_only": True,
"all_results": True,
"concept_filters": [{"name": "name", "value": "environment"}],
},
True,
"Cannot set concept_filters when only searching documents.",
),
(
{
"query_string": "",
"documents_only": False,
"concept_filters": [{"name": "name", "value": "environment"}],
},
False,
None,
),
],
)
@pytest.mark.vespa
def test_vespa_search_parameters(
params: dict, expect_error: bool, error_message: Optional[str]
) -> None:
"""Test that we can correctly instantiate the SearchParameters object."""
if expect_error:
with pytest.raises(
ValidationError,
match=error_message,
):
SearchParameters.model_validate(params)
else:
SearchParameters.model_validate(params)

0 comments on commit 72c7c77

Please sign in to comment.