test_ontology_knowledge_sync.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import annotations
  2. import pytest
  3. from tests.data_research.test_ontology_validation import base_graph
  4. class Repository:
  5. def __init__(self):
  6. self.documents = {}
  7. self.chunks = {}
  8. def upsert_document(self, document):
  9. key = (document["object_uid"], document["object_version"])
  10. existing = self.documents.get(key)
  11. self.documents[key] = document
  12. return existing is None or existing["content_hash"] != document["content_hash"]
  13. def replace_chunks(self, key, chunks):
  14. self.chunks[key] = list(chunks)
  15. def test_only_published_versions_sync_as_versioned_idempotent_documents():
  16. from app.core.data_research.ontology.knowledge_sync import OntologyKnowledgeSync
  17. repository = Repository()
  18. service = OntologyKnowledgeSync(repository, max_chunk_chars=80)
  19. payload = {
  20. "uid": "ontology-1",
  21. "name": "客户本体",
  22. "version": 3,
  23. "status": "published",
  24. "graph_document": base_graph(),
  25. "token": "must-not-sync",
  26. }
  27. first = service.sync(payload)
  28. second = service.sync(payload)
  29. assert first["changed"] is True
  30. assert second["changed"] is False
  31. assert first["document"]["object_version"] == 3
  32. assert "must-not-sync" not in first["document"]["content"]
  33. assert len(repository.chunks[("ontology-1", 3)]) > 1
  34. with pytest.raises(ValueError, match="published"):
  35. service.sync({**payload, "status": "draft"})