test_garden_layer.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import pytest
  2. import requests
  3. from garden_layer import GardenLayer
  4. from garden_layer.config import CLONE_OF, GRAPH, KEROSENE_ROOT
  5. def pytest_report_header(config):
  6. return "Garden layer tests: requires virtuoso_mcp running at GardenLayer.MCP_URL"
  7. @pytest.fixture
  8. def garden_layer():
  9. layer = GardenLayer()
  10. try:
  11. layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=1)
  12. except requests.RequestException as exc:
  13. pytest.skip(f"Cannot reach MCP server: {exc}")
  14. return layer
  15. def test_traverse_clone_tree(garden_layer):
  16. """Walk `cloneOf` incoming edges and ensure the root has clones to inspect."""
  17. result = garden_layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=5)
  18. assert isinstance(result, dict)
  19. bindings = result.get("results", {}).get("bindings", [])
  20. assert isinstance(bindings, list)
  21. assert bindings, "Expected at least one clone binding"
  22. print(f"Traverse of {KEROSENE_ROOT} returned {len(bindings)} clone candidates.")
  23. def test_describe_subject(garden_layer):
  24. """Describe the root plant and surface the top predicates the garden helpers can reuse."""
  25. summary = garden_layer.describe_subject(KEROSENE_ROOT, limit=5)
  26. assert isinstance(summary, dict)
  27. bindings = summary.get("results", {}).get("bindings", [])
  28. assert bindings, "Expected describe_subject to return bindings"
  29. triples = [f"{b['predicate']['value']} -> {b['object'].get('value', '<literal>')}" for b in bindings]
  30. print("describe_subject returned:")
  31. for triple in triples:
  32. print(" ", triple)
  33. def test_path_traverse_lineage(garden_layer):
  34. """Use the path helper to follow `cloneOf` and observe the lineage step."""
  35. path = garden_layer.path_traverse(KEROSENE_ROOT, [CLONE_OF], direction="incoming", limit=5)
  36. assert isinstance(path, dict)
  37. bindings = path.get("result", {}).get("results", {}).get("bindings", [])
  38. assert isinstance(bindings, list)
  39. assert bindings, "Path traverse should find at least one step"
  40. print(f"Property path {CLONE_OF} produced {len(bindings)} step bindings.")
  41. def test_property_usage_statistics(garden_layer):
  42. """Summarize how the `cloneOf` property is used so future helpers can rely on frequency data."""
  43. stats = garden_layer.property_usage_statistics(CLONE_OF, examples_limit=3)
  44. assert isinstance(stats, dict)
  45. count_bindings = stats.get("count", {}).get("results", {}).get("bindings", [])
  46. assert count_bindings, "Expected usage count bindings"
  47. usage_count = count_bindings[0].get("usageCount", {}).get("value")
  48. example_bindings = stats.get("examples", {}).get("results", {}).get("bindings", [])
  49. assert isinstance(example_bindings, list)
  50. print(f"cloneOf usage count: {usage_count}")
  51. print("example bindings:")
  52. for binding in example_bindings:
  53. subject = binding.get("subjectLabel", {}).get("value") or binding.get("subject", {}).get("value")
  54. object_ = binding.get("objectLabel", {}).get("value") or binding.get("object", {}).get("value")
  55. print(f" - {subject} -> {object_}")
  56. def test_batch_insert(garden_layer):
  57. """Batch-insert a TTL snippet and verify the query shape"""
  58. ttl = '<http://world.eu.org/example1#batch_test_subject> <http://www.w3.org/2000/01/rdf-schema#label> "garden batch" .'
  59. result = garden_layer.batch_insert(ttl=ttl, graph=GRAPH)
  60. assert isinstance(result, dict)
  61. assert "query" in result
  62. assert "INSERT DATA" in result.get("query", "").upper()
  63. print("batch_insert generated:")
  64. print(result.get("query", ""))