test_dataflow_create_saga_neo4j.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from __future__ import annotations
  2. import os
  3. import pytest
  4. from neo4j import GraphDatabase
  5. from app import create_app
  6. from app.core.common.identifiers import new_governance_uid
  7. from app.core.data_flow.dataflows import DataFlowService
  8. def test_real_neo4j_uid_constraint_merge_replay_and_conflict():
  9. uri = os.environ.get("DATA_RULE_NEO4J_ACCEPTANCE_URI")
  10. password = os.environ.get("DATA_RULE_NEO4J_ACCEPTANCE_PASSWORD")
  11. if not uri or not password:
  12. pytest.skip("real Neo4j acceptance connection is not configured")
  13. user = os.environ.get("DATA_RULE_NEO4J_ACCEPTANCE_USER", "neo4j")
  14. app = create_app()
  15. app.config.update(
  16. TESTING=True,
  17. NEO4J_URI=uri,
  18. NEO4J_USER=user,
  19. NEO4J_PASSWORD=password,
  20. NEO4J_ENCRYPTED=False,
  21. )
  22. uid = new_governance_uid()
  23. node = {
  24. "uid": uid,
  25. "name_zh": f"Saga验收-{uid}",
  26. "name_en": f"saga-{uid}",
  27. "script_type": "governed",
  28. "script_requirement": '{"dataflow_spec":{"schema_version":"2.0"}}',
  29. "script_path": "",
  30. }
  31. driver = GraphDatabase.driver(uri, auth=(user, password), encrypted=False)
  32. try:
  33. with app.app_context():
  34. first_id, first = DataFlowService._merge_governed_dataflow(node)
  35. second_id, second = DataFlowService._merge_governed_dataflow(node)
  36. assert first_id == second_id
  37. assert first == second
  38. with pytest.raises(ValueError, match="dataflow_uid_conflict"):
  39. DataFlowService._merge_governed_dataflow(
  40. {**node, "name_zh": f"篡改-{uid}"}
  41. )
  42. with driver.session() as session:
  43. count = session.run(
  44. "MATCH (n:DataFlow {uid: $uid}) RETURN count(n) AS count",
  45. {"uid": uid},
  46. ).single()["count"]
  47. constraints = [
  48. record["name"]
  49. for record in session.run(
  50. "SHOW CONSTRAINTS YIELD name WHERE name = 'data_flow_uid' "
  51. "RETURN name"
  52. )
  53. ]
  54. assert count == 1
  55. assert constraints == ["data_flow_uid"]
  56. finally:
  57. with driver.session() as session:
  58. session.run("MATCH (n:DataFlow {uid: $uid}) DETACH DELETE n", {"uid": uid})
  59. driver.close()