test_semantic_query.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from __future__ import annotations
  2. import pytest
  3. class Repository:
  4. def __init__(self):
  5. self.calls = []
  6. def find_property_paths(self, property_uid, *, depth, limit, after_uid):
  7. self.calls.append((property_uid, depth, limit, after_uid))
  8. return [
  9. {
  10. "path_uid": "path-1",
  11. "ontology_uid": "ontology-1",
  12. "ontology_status": "published",
  13. "property_uid": property_uid,
  14. "data_element_uid": "element-1",
  15. "field_uid": "field-1",
  16. "evidence_uid": "evidence-1",
  17. "business_domain_uid": "sales",
  18. "password": "must-not-leak",
  19. },
  20. {
  21. "path_uid": "path-2",
  22. "ontology_uid": "ontology-draft",
  23. "ontology_status": "draft",
  24. "property_uid": property_uid,
  25. "business_domain_uid": "sales",
  26. },
  27. {
  28. "path_uid": "path-3",
  29. "ontology_uid": "ontology-hr",
  30. "ontology_status": "published",
  31. "property_uid": property_uid,
  32. "business_domain_uid": "hr",
  33. },
  34. ]
  35. def test_semantic_query_is_bounded_published_permission_filtered_and_redacted():
  36. from app.core.data_research.ontology.query import SemanticQueryService
  37. repository = Repository()
  38. result = SemanticQueryService(repository).trace_property(
  39. "property-1", allowed_domains={"sales"}, limit=10, after_uid=None
  40. )
  41. assert [item["path_uid"] for item in result["items"]] == ["path-1"]
  42. assert "password" not in repr(result).lower()
  43. assert repository.calls == [("property-1", 3, 11, None)]
  44. with pytest.raises(ValueError, match="limit"):
  45. SemanticQueryService(repository).trace_property(
  46. "property-1", allowed_domains={"sales"}, limit=51
  47. )
  48. def test_semantic_query_has_stable_cursor_and_no_arbitrary_cypher_interface():
  49. from app.core.data_research.ontology.query import SemanticQueryService
  50. service = SemanticQueryService(Repository())
  51. result = service.trace_property("property-1", allowed_domains={"*"}, limit=1)
  52. assert result["next_cursor"] == "path-1"
  53. assert not hasattr(service, "execute_cypher")