test(search): dedupe next(f for f in PUBLIC_FIELDS...) lookups, collapse table tests

Both test_fields.py and test_registry.py repeated the same generator-next
lookup by field name. Add a module-level {name: field} dict in each and
use it instead.

Also collapse test_fields.py's five single-attribute tests
(document_type/storage_path aliases, tag's comma_values, notes/
custom_fields subpaths) into one parametrized test_field_attributes -
they were really one table-consistency check split into five copies of
the same three-line shape.
This commit is contained in:
Trenton Holmes
2026-08-18 13:57:26 -07:00
parent 86f3f3ea26
commit 5ae9276f1c
2 changed files with 42 additions and 25 deletions
+38 -19
View File
@@ -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,
+4 -6
View File
@@ -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"}