test_garden_layer.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import os
  2. import pytest
  3. import requests
  4. from typing import Any, Dict
  5. from garden_layer import GardenLayer
  6. from garden_layer.config import CLONE_OF, GRAPH, KEROSENE_ROOT, MCP_URL
  7. CYCLE_2026_3 = "http://world.eu.org/example1#ProductionCycle_21d96b6c-179"
  8. FIXTURE_FILES = ["ganja_test_fixture.ttl"]
  9. def pytest_report_header(config):
  10. return "Garden layer tests: requires virtuoso_mcp running at GardenLayer.MCP_URL"
  11. @pytest.fixture
  12. def garden_layer():
  13. layer = GardenLayer()
  14. try:
  15. layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=1)
  16. except requests.RequestException as exc:
  17. pytest.skip(f"Cannot reach MCP server: {exc}")
  18. if os.getenv("MCP_ALLOW_EXAMPLE_LOAD", "false").lower() == "true":
  19. try:
  20. layer.load_examples(files=FIXTURE_FILES, graph=GRAPH)
  21. except Exception as exc:
  22. pytest.skip(f"Cannot load garden fixtures: {exc}")
  23. result = layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=1)
  24. bindings = result.get("results", {}).get("bindings", [])
  25. if not bindings:
  26. pytest.skip("No garden fixture data available; set MCP_ALLOW_EXAMPLE_LOAD=true")
  27. return layer
  28. def test_traverse_clone_tree(garden_layer):
  29. """Walk `cloneOf` incoming edges and ensure the root has clones to inspect."""
  30. result = garden_layer.traverse(KEROSENE_ROOT, CLONE_OF, direction="incoming", limit=5)
  31. assert isinstance(result, dict)
  32. bindings = result.get("results", {}).get("bindings", [])
  33. assert isinstance(bindings, list)
  34. assert bindings, "Expected at least one clone binding"
  35. print(f"Traverse of {KEROSENE_ROOT} returned {len(bindings)} clone candidates.")
  36. def test_describe_subject(garden_layer):
  37. """Describe the root plant and surface the top predicates the garden helpers can reuse."""
  38. summary = garden_layer.describe_subject(KEROSENE_ROOT, limit=5)
  39. assert isinstance(summary, dict)
  40. bindings = summary.get("results", {}).get("bindings", [])
  41. assert bindings, "Expected describe_subject to return bindings"
  42. triples = [f"{b['predicate']['value']} -> {b['object'].get('value', '<literal>')}" for b in bindings]
  43. print("describe_subject returned:")
  44. for triple in triples:
  45. print(" ", triple)
  46. def test_path_traverse_lineage(garden_layer):
  47. """Use the path helper to follow `cloneOf` and observe the lineage step."""
  48. path = garden_layer.path_traverse(KEROSENE_ROOT, [CLONE_OF], direction="incoming", limit=5)
  49. assert isinstance(path, dict)
  50. bindings = path.get("result", {}).get("results", {}).get("bindings", [])
  51. assert isinstance(bindings, list)
  52. assert bindings, "Path traverse should find at least one step"
  53. print(f"Property path {CLONE_OF} produced {len(bindings)} step bindings.")
  54. def test_property_usage_statistics(garden_layer):
  55. """Summarize how the `cloneOf` property is used so future helpers can rely on frequency data."""
  56. stats = garden_layer.property_usage_statistics(CLONE_OF, examples_limit=3)
  57. assert isinstance(stats, dict)
  58. count_bindings = stats.get("count", {}).get("results", {}).get("bindings", [])
  59. assert count_bindings, "Expected usage count bindings"
  60. usage_count = count_bindings[0].get("usageCount", {}).get("value")
  61. example_bindings = stats.get("examples", {}).get("results", {}).get("bindings", [])
  62. assert isinstance(example_bindings, list)
  63. print(f"cloneOf usage count: {usage_count}")
  64. print("example bindings:")
  65. for binding in example_bindings:
  66. subject = binding.get("subjectLabel", {}).get("value") or binding.get("subject", {}).get("value")
  67. object_ = binding.get("objectLabel", {}).get("value") or binding.get("object", {}).get("value")
  68. print(f" - {subject} -> {object_}")
  69. def test_batch_insert(garden_layer):
  70. """Batch-insert a TTL snippet and verify the query shape"""
  71. ttl = '<http://world.eu.org/example1#batch_test_subject> <http://www.w3.org/2000/01/rdf-schema#label> "garden batch" .'
  72. result = garden_layer.batch_insert(ttl=ttl, graph=GRAPH)
  73. assert isinstance(result, dict)
  74. assert "query" in result
  75. assert "INSERT DATA" in result.get("query", "").upper()
  76. print("batch_insert generated:")
  77. print(result.get("query", ""))
  78. def call_mcp_tool(tool_name: str, payload: Dict[str, Any]) -> Any:
  79. response = requests.post(MCP_URL, json={"tool": tool_name, "input": payload}, timeout=10)
  80. response.raise_for_status()
  81. body = response.json()
  82. assert body.get("status") == "ok", f"{tool_name} failed: {body.get('detail') or 'unknown reason'}"
  83. return body["result"]
  84. def test_mcp_garden_describe_subject():
  85. result = call_mcp_tool(
  86. "garden_describe_subject",
  87. {"subject_uri": KEROSENE_ROOT, "limit": 5},
  88. )
  89. bindings = result.get("results", {}).get("bindings", [])
  90. assert bindings, "garden_describe_subject should return bindings"
  91. print("garden_describe_subject returned", len(bindings), "bindings via /mcp")
  92. def test_mcp_garden_property_usage_statistics():
  93. result = call_mcp_tool(
  94. "garden_property_usage_statistics",
  95. {"property_uri": CLONE_OF, "examples_limit": 2},
  96. )
  97. count_bindings = result.get("count", {}).get("results", {}).get("bindings", [])
  98. assert count_bindings, "Expected usage count bindings from the garden tool"
  99. examples = result.get("examples", {}).get("results", {}).get("bindings", [])
  100. assert isinstance(examples, list)
  101. print("garden_property_usage_statistics sample bindings:", len(examples))
  102. def test_mcp_garden_cycle_plants():
  103. result = call_mcp_tool(
  104. "garden_cycle_plants",
  105. {"cycle_uri": CYCLE_2026_3, "limit": 5},
  106. )
  107. bindings = result.get("results", {}).get("bindings", [])
  108. assert bindings, "Expected at least one plant binding from the cycle"
  109. print("garden_cycle_plants returned", len(bindings), "plants for cycle", CYCLE_2026_3)
  110. for binding in bindings:
  111. plant_uri = binding.get("plant", {}).get("value")
  112. assert plant_uri, "Each binding should include a plant URI"
  113. def test_mcp_garden_cycle_plants_latest_when_cycle_uri_missing():
  114. """MCP compliance: cycle_uri should be optional and default to latest cycle."""
  115. result = call_mcp_tool(
  116. "garden_cycle_plants",
  117. {"limit": 3},
  118. )
  119. bindings = result.get("results", {}).get("bindings", [])
  120. assert bindings, "Expected plant bindings from the latest cycle when cycle_uri is omitted"
  121. def test_mcp_garden_cycle_list_detailed_has_strain_info():
  122. """Ensure the detailed cycle list includes strain labels/descriptions and clone siblings."""
  123. result = call_mcp_tool(
  124. "garden_cycle_list_detailed",
  125. {"limit": 10},
  126. )
  127. bindings = result.get("results", {}).get("bindings", [])
  128. assert bindings, "Expected at least one binding from garden_cycle_list_detailed"
  129. # Check that at least one binding has strainDesc or cloneSiblings populated.
  130. any_strain_desc = any('strainDesc' in b and b.get('strainDesc', {}).get('value') for b in bindings)
  131. any_siblings = any('cloneSiblings' in b and b.get('cloneSiblings', {}).get('value') for b in bindings)
  132. assert any_strain_desc or any_siblings, "Expected strainDesc and/or cloneSiblings to be present"
  133. def test_load_examples_fixture(garden_layer):
  134. if os.getenv("MCP_ALLOW_EXAMPLE_LOAD", "false").lower() != "true":
  135. pytest.skip("MCP_ALLOW_EXAMPLE_LOAD must be true to load example fixtures")
  136. result = garden_layer.load_examples(files=["test_fixture.ttl"], graph=GRAPH)
  137. loaded = result.get("loaded", [])
  138. assert any(item.get("file") == "test_fixture.ttl" for item in loaded), "Expected fixture file to be loaded"