"""The index document must stay in sync with the mapping - and hide PII.""" from __future__ import annotations import pytest from apps.search.indexing import INDEX_MAPPINGS, build_document pytestmark = pytest.mark.django_db def test_document_only_contains_mapped_fields(sample_profiles) -> None: # noqa: ANN001 """ The mapping is ``dynamic: strict``: an unmapped field makes Elasticsearch reject the whole document, so this assertion protects every reindex. """ document = build_document(sample_profiles[0]) assert set(document) <= set(INDEX_MAPPINGS["properties"]) def test_document_never_carries_contact_details(sample_profiles) -> None: # noqa: ANN001 profile = sample_profiles[0] profile.emails = [{"address": "sara@acme.com", "type": "professional"}] profile.phone_numbers = ["+491701234567"] profile.save(update_fields=["emails", "phone_numbers"]) document = build_document(profile) serialised = str(document) assert "emails" not in document assert "phone_numbers" not in document assert "sara@acme.com" not in serialised assert "+491701234567" not in serialised def test_document_denormalises_the_related_rows(sample_profiles) -> None: # noqa: ANN001 document = build_document(sample_profiles[0]) assert document["full_name"] == "sara python" assert document["company"] == "acme corp" assert "python" in document["skills"] assert document["years_experience"] == 9