test_ontology_validation.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import annotations
  2. def base_graph():
  3. return {
  4. "classes": [{"uid": "customer", "name": "Customer"}],
  5. "properties": [
  6. {
  7. "uid": "customer-id",
  8. "name": "customerId",
  9. "class_uid": "customer",
  10. "required": True,
  11. "cardinality": "1",
  12. "data_element_uid": "element-1",
  13. }
  14. ],
  15. "relations": [],
  16. "constraints": [],
  17. "domain_links": [{"domain_uid": "domain-1", "role": "owner"}],
  18. "element_mappings": [{"property_uid": "customer-id", "data_element_uid": "element-1"}],
  19. }
  20. def test_valid_graph_has_no_errors():
  21. from app.core.data_research.ontology.validation import validate_graph
  22. assert validate_graph(base_graph()) == []
  23. def test_validation_reports_stable_codes_for_governance_failures():
  24. from app.core.data_research.ontology.validation import validate_graph
  25. graph = base_graph()
  26. graph["classes"] += [
  27. {"uid": "duplicate", "name": "Customer", "parent_uid": "missing"},
  28. {"uid": "cycle-a", "name": "CycleA", "parent_uid": "cycle-b"},
  29. {"uid": "cycle-b", "name": "CycleB", "parent_uid": "cycle-a"},
  30. ]
  31. graph["properties"][0]["cardinality"] = "many-to-sometimes"
  32. graph["properties"][0].pop("data_element_uid")
  33. graph["element_mappings"] = []
  34. graph["relations"] = [{"uid": "bad-edge", "from_class_uid": "customer", "to_class_uid": "gone"}]
  35. graph["domain_links"] = [{"domain_uid": "domain-1", "role": "consumer"}]
  36. codes = {issue.code for issue in validate_graph(graph)}
  37. assert codes == {
  38. "ONTOLOGY_DUPLICATE_NAME",
  39. "ONTOLOGY_DANGLING_EDGE",
  40. "ONTOLOGY_INHERITANCE_CYCLE",
  41. "ONTOLOGY_INVALID_CARDINALITY",
  42. "ONTOLOGY_OWNER_REQUIRED",
  43. "ONTOLOGY_REQUIRED_PROPERTY_UNMAPPED",
  44. }