|
1
|
"""Tests for graph discovery (find_knowledge_graphs, describe_graph).""" |
|
2
|
|
|
3
|
import json |
|
4
|
|
|
5
|
from video_processor.integrators.graph_discovery import ( |
|
6
|
describe_graph, |
|
7
|
find_knowledge_graphs, |
|
8
|
find_nearest_graph, |
|
9
|
) |
|
10
|
|
|
11
|
|
|
12
|
class TestFindKnowledgeGraphs: |
|
13
|
def test_finds_db_in_current_dir(self, tmp_path): |
|
14
|
db = tmp_path / "knowledge_graph.db" |
|
15
|
db.write_bytes(b"") # placeholder |
|
16
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
17
|
assert db.resolve() in graphs |
|
18
|
|
|
19
|
def test_finds_in_results_subdir(self, tmp_path): |
|
20
|
results = tmp_path / "results" |
|
21
|
results.mkdir() |
|
22
|
db = results / "knowledge_graph.db" |
|
23
|
db.write_bytes(b"") |
|
24
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
25
|
assert db.resolve() in graphs |
|
26
|
|
|
27
|
def test_finds_in_output_subdir(self, tmp_path): |
|
28
|
output = tmp_path / "output" |
|
29
|
output.mkdir() |
|
30
|
db = output / "knowledge_graph.db" |
|
31
|
db.write_bytes(b"") |
|
32
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
33
|
assert db.resolve() in graphs |
|
34
|
|
|
35
|
def test_walks_up_parents(self, tmp_path): |
|
36
|
db = tmp_path / "knowledge_graph.db" |
|
37
|
db.write_bytes(b"") |
|
38
|
child = tmp_path / "sub" / "deep" |
|
39
|
child.mkdir(parents=True) |
|
40
|
graphs = find_knowledge_graphs(child, walk_up=True) |
|
41
|
assert db.resolve() in graphs |
|
42
|
|
|
43
|
def test_returns_empty_when_none_found(self, tmp_path): |
|
44
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
45
|
assert graphs == [] |
|
46
|
|
|
47
|
def test_finds_json_fallback(self, tmp_path): |
|
48
|
jf = tmp_path / "knowledge_graph.json" |
|
49
|
jf.write_text('{"nodes":[], "relationships":[]}') |
|
50
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
51
|
assert jf.resolve() in graphs |
|
52
|
|
|
53
|
def test_db_before_json(self, tmp_path): |
|
54
|
db = tmp_path / "knowledge_graph.db" |
|
55
|
db.write_bytes(b"") |
|
56
|
jf = tmp_path / "knowledge_graph.json" |
|
57
|
jf.write_text('{"nodes":[], "relationships":[]}') |
|
58
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
59
|
assert graphs.index(db.resolve()) < graphs.index(jf.resolve()) |
|
60
|
|
|
61
|
def test_closest_first_ordering(self, tmp_path): |
|
62
|
# Deeper file |
|
63
|
deep = tmp_path / "a" / "b" |
|
64
|
deep.mkdir(parents=True) |
|
65
|
deep_db = deep / "knowledge_graph.db" |
|
66
|
deep_db.write_bytes(b"") |
|
67
|
# Closer file |
|
68
|
close_db = tmp_path / "knowledge_graph.db" |
|
69
|
close_db.write_bytes(b"") |
|
70
|
graphs = find_knowledge_graphs(tmp_path, walk_up=False) |
|
71
|
assert graphs.index(close_db.resolve()) < graphs.index(deep_db.resolve()) |
|
72
|
|
|
73
|
|
|
74
|
class TestFindNearestGraph: |
|
75
|
def test_returns_closest(self, tmp_path): |
|
76
|
db = tmp_path / "knowledge_graph.db" |
|
77
|
db.write_bytes(b"") |
|
78
|
result = find_nearest_graph(tmp_path) |
|
79
|
assert result == db.resolve() |
|
80
|
|
|
81
|
def test_returns_none_when_empty(self, tmp_path): |
|
82
|
assert find_nearest_graph(tmp_path) is None |
|
83
|
|
|
84
|
|
|
85
|
class TestDescribeGraph: |
|
86
|
def test_describe_json_graph(self, tmp_path): |
|
87
|
data = { |
|
88
|
"nodes": [ |
|
89
|
{"name": "Python", "type": "technology", "descriptions": ["A language"]}, |
|
90
|
{"name": "Django", "type": "technology", "descriptions": ["A framework"]}, |
|
91
|
{"name": "Alice", "type": "person", "descriptions": ["Engineer"]}, |
|
92
|
], |
|
93
|
"relationships": [ |
|
94
|
{"source": "Django", "target": "Python", "type": "uses"}, |
|
95
|
], |
|
96
|
} |
|
97
|
jf = tmp_path / "knowledge_graph.json" |
|
98
|
jf.write_text(json.dumps(data)) |
|
99
|
info = describe_graph(jf) |
|
100
|
assert info["entity_count"] == 3 |
|
101
|
assert info["relationship_count"] == 1 |
|
102
|
assert info["entity_types"]["technology"] == 2 |
|
103
|
assert info["entity_types"]["person"] == 1 |
|
104
|
assert info["store_type"] == "json" |
|
105
|
|