73 lines
3.5 KiB
Python
73 lines
3.5 KiB
Python
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parents[1]))
|
|
from graph_view import GraphView
|
|
|
|
|
|
class AtomicContractTests(unittest.TestCase):
|
|
def fixture(self):
|
|
nodes = [{"id": i, "kind": "point" if i == "child" else "article",
|
|
"resource_id": "code", "label": "Статья " + i}
|
|
for i in ("focus", "child", "incoming", "target", "far", "unrelated")]
|
|
edges = [
|
|
{"source": "focus", "target": "child", "relation": "contains"},
|
|
{"source": "incoming", "target": "focus", "relation": "references"},
|
|
{"source": "child", "target": "target", "relation": "references"},
|
|
{"source": "target", "target": "far", "relation": "references"},
|
|
]
|
|
return {"nodes": nodes, "edges": edges}
|
|
|
|
def test_family_expansion_does_not_walk_reference_neighbors(self):
|
|
for reverse in (False, True):
|
|
data = self.fixture()
|
|
if reverse:
|
|
data["edges"].reverse()
|
|
value = GraphView(data).network(level="atoms", node_id="focus")
|
|
self.assertEqual({n["id"] for n in value["nodes"]}, {"focus", "child", "incoming", "target"})
|
|
self.assertNotIn("far", {n["id"] for n in value["nodes"]})
|
|
|
|
def test_structure_toggle_never_removes_child_citations(self):
|
|
view = GraphView(self.fixture())
|
|
results = [view.network(level="atoms", node_id="focus", include_contains=flag)
|
|
for flag in (True, False)]
|
|
references = [{(e["source"], e["target"]) for e in result["edges"]
|
|
if e["relation"] == "references"} for result in results]
|
|
self.assertEqual(references[0], references[1])
|
|
self.assertIn(("child", "target"), references[0])
|
|
|
|
def test_parent_child_duplicate_requires_same_provenance(self):
|
|
data = self.fixture()
|
|
for e in data["edges"]:
|
|
if e["source"] == "child":
|
|
e.update(source_file="code.md", source_location="L40", evidence={"excerpt": "precise child mention"})
|
|
data["edges"].append({"source": "focus", "target": "target", "relation": "references",
|
|
"source_file": "code.md", "source_location": "L40"})
|
|
view = GraphView(data)
|
|
edges = view.network(level="atoms", node_id="focus")["edges"]
|
|
self.assertFalse(any(e["source"] == "focus" and e["target"] == "target" for e in edges))
|
|
self.assertTrue(any(e["source"] == "child" and e["target"] == "target" for e in edges))
|
|
self.assertEqual(len(view.edges), 5) # Raw/legacy graph is untouched.
|
|
data["edges"][-1]["source_location"] = "L15"
|
|
edges = GraphView(data).network(level="atoms", node_id="focus")["edges"]
|
|
self.assertTrue(any(e["source"] == "focus" and e["target"] == "target" for e in edges))
|
|
|
|
def test_overview_bound_keeps_counterpart_points_and_honest_totals(self):
|
|
data = self.fixture()
|
|
value = GraphView(data).network(level="atoms", max_nodes=2)
|
|
self.assertEqual(len(value["nodes"]), 2)
|
|
self.assertEqual(value["total_nodes"], 6)
|
|
self.assertEqual(value["total_edges"], 4)
|
|
self.assertTrue(value["edges"])
|
|
self.assertTrue(value["truncated"])
|
|
|
|
def test_missing_focus_never_returns_whole_code(self):
|
|
result = GraphView(self.fixture()).network(level="atoms", node_id="missing", resource_id="code")
|
|
self.assertEqual(result["status"], "focus_not_found")
|
|
self.assertEqual(result["nodes"], [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|