test_diff.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import annotations
  2. from dataclasses import replace
  3. def test_diff_classifies_added_modified_deleted_and_unchanged():
  4. from app.core.knowledge.diff import diff_snapshots
  5. from app.core.knowledge.point_builder import build_knowledge_snapshot
  6. old = build_knowledge_snapshot(
  7. "DataFlow",
  8. {
  9. "uid": "01900000-0000-7000-8000-000000000601",
  10. "version": 1,
  11. "name": "sync",
  12. "purpose": "old purpose",
  13. "owner": "team-a",
  14. },
  15. )
  16. new = build_knowledge_snapshot(
  17. "DataFlow",
  18. {
  19. "uid": "01900000-0000-7000-8000-000000000601",
  20. "version": 2,
  21. "name": "sync",
  22. "purpose": "new purpose",
  23. "aliases": ["sync_job"],
  24. },
  25. )
  26. result = diff_snapshots(old, new)
  27. assert len(result.modified) == 1
  28. assert result.modified[0].point_key.endswith("/purpose")
  29. assert len(result.added) == 1
  30. assert "/aliases/" in result.added[0].point_key
  31. assert len(result.deleted) == 1
  32. assert result.deleted[0].point_key.endswith("/owner")
  33. assert any(change.point_key.endswith("/name") for change in result.unchanged)
  34. def test_permission_change_is_modified_even_when_content_is_same():
  35. from app.core.knowledge.diff import diff_snapshots
  36. from app.core.knowledge.point_builder import build_knowledge_snapshot
  37. old = build_knowledge_snapshot(
  38. "BusinessDomain",
  39. {
  40. "uid": "01900000-0000-7000-8000-000000000701",
  41. "version": 1,
  42. "definition": "customer",
  43. "permission_scope": {"business_domains": ["a", "b"]},
  44. },
  45. )
  46. new = replace(
  47. build_knowledge_snapshot(
  48. "BusinessDomain",
  49. {
  50. "uid": "01900000-0000-7000-8000-000000000701",
  51. "version": 2,
  52. "definition": "customer",
  53. "permission_scope": {"business_domains": ["a"]},
  54. },
  55. ),
  56. )
  57. result = diff_snapshots(old, new)
  58. assert not result.unchanged
  59. assert result.modified
  60. assert all(change.permission_changed for change in result.modified)
  61. def test_diff_rejects_different_source_objects():
  62. import pytest
  63. from app.core.knowledge.diff import diff_snapshots
  64. from app.core.knowledge.point_builder import build_knowledge_snapshot
  65. first = build_knowledge_snapshot(
  66. "Label", {"uid": "01900000-0000-7000-8000-000000000801", "name": "PII"}
  67. )
  68. second = build_knowledge_snapshot(
  69. "Label", {"uid": "01900000-0000-7000-8000-000000000802", "name": "PII"}
  70. )
  71. with pytest.raises(ValueError, match="same source"):
  72. diff_snapshots(first, second)