diff --git a/src/documents/tests/search/test_fields.py b/src/documents/tests/search/test_fields.py index da5286efe..210e09142 100644 --- a/src/documents/tests/search/test_fields.py +++ b/src/documents/tests/search/test_fields.py @@ -1,7 +1,10 @@ +import pytest from whoosh_compat import FieldKind from documents.search._fields import PUBLIC_FIELDS +BY_NAME = {f.name: f for f in PUBLIC_FIELDS} + class TestPublicFields: def test_every_field_has_a_whoosh_compat_kind(self) -> None: @@ -22,25 +25,41 @@ class TestPublicFields: if field.kind is not FieldKind.JSON: assert not field.subpaths - def test_document_type_alias_is_type(self) -> None: - field = next(f for f in PUBLIC_FIELDS if f.name == "document_type") - assert field.aliases == ("type",) - - def test_storage_path_alias_is_path(self) -> None: - field = next(f for f in PUBLIC_FIELDS if f.name == "storage_path") - assert field.aliases == ("path",) - - def test_tag_allows_comma_values(self) -> None: - field = next(f for f in PUBLIC_FIELDS if f.name == "tag") - assert field.comma_values is True - - def test_notes_subpaths(self) -> None: - field = next(f for f in PUBLIC_FIELDS if f.name == "notes") - assert set(field.subpaths) == {"user", "note"} - - def test_custom_fields_subpaths(self) -> None: - field = next(f for f in PUBLIC_FIELDS if f.name == "custom_fields") - assert set(field.subpaths) == {"name", "value"} + @pytest.mark.parametrize( + ("name", "attr", "expected"), + [ + pytest.param( + "document_type", + "aliases", + ("type",), + id="document_type-aliases", + ), + pytest.param( + "storage_path", + "aliases", + ("path",), + id="storage_path-aliases", + ), + pytest.param("tag", "comma_values", True, id="tag-comma_values"), + pytest.param( + "notes", + "subpaths", + {"user", "note"}, + id="notes-subpaths", + ), + pytest.param( + "custom_fields", + "subpaths", + {"name", "value"}, + id="custom_fields-subpaths", + ), + ], + ) + def test_field_attributes(self, name: str, attr: str, expected: object) -> None: + actual = getattr(BY_NAME[name], attr) + if attr == "subpaths": + actual = set(actual) + assert actual == expected def test_no_internal_id_fields_present(self) -> None: # tag_id/owner_id/viewer_id/etc. are permission-filter-only fields, diff --git a/src/documents/tests/search/test_registry.py b/src/documents/tests/search/test_registry.py index f7e9aee6f..6de41a00f 100644 --- a/src/documents/tests/search/test_registry.py +++ b/src/documents/tests/search/test_registry.py @@ -6,6 +6,8 @@ from whoosh_compat.fields import ResolvedField from documents.search._fields import PUBLIC_FIELDS from documents.search._registry import get_field_registry +_BY_NAME = {f.name: f for f in PUBLIC_FIELDS} + @pytest.fixture def registry() -> FieldRegistry: @@ -126,13 +128,9 @@ class TestJsonSubpathCoupling: # _backend.py's _build_tantivy_doc builds: # doc.add_json("notes", {"note": ..., "user": ...}) # These literal keys must match PUBLIC_FIELDS' "notes" subpaths exactly. - notes_field = next(f for f in PUBLIC_FIELDS if f.name == "notes") - assert set(notes_field.subpaths) == {"note", "user"} + assert set(_BY_NAME["notes"].subpaths) == {"note", "user"} def test_custom_fields_dict_keys_match_public_fields_subpaths(self) -> None: # _backend.py's _build_tantivy_doc builds: # doc.add_json("custom_fields", {"name": ..., "value": ...}) - custom_fields_field = next( - f for f in PUBLIC_FIELDS if f.name == "custom_fields" - ) - assert set(custom_fields_field.subpaths) == {"name", "value"} + assert set(_BY_NAME["custom_fields"].subpaths) == {"name", "value"}