-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_captions.py
More file actions
161 lines (118 loc) · 5.54 KB
/
test_captions.py
File metadata and controls
161 lines (118 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pytest
from neo4j_viz import Node, VisualizationGraph
def test_set_node_captions_from_property() -> None:
"""Test setting captions from a node property."""
nodes = [
Node(id="1", properties={"name": "Alice", "age": 30}),
Node(id="2", properties={"name": "Bob", "age": 25}),
Node(id="3", properties={"name": "Charlie", "age": 35}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(property="name")
assert VG.nodes[0].caption == "Alice"
assert VG.nodes[1].caption == "Bob"
assert VG.nodes[2].caption == "Charlie"
def test_set_node_captions_from_field() -> None:
"""Test setting captions from a node field."""
nodes = [
Node(id="node-1", properties={"name": "Alice"}),
Node(id="node-2", properties={"name": "Bob"}),
Node(id="node-3", properties={"name": "Charlie"}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(field="id")
assert VG.nodes[0].caption == "node-1"
assert VG.nodes[1].caption == "node-2"
assert VG.nodes[2].caption == "node-3"
def test_set_node_captions_override_true() -> None:
"""Test that override=True replaces existing captions."""
nodes = [
Node(id="1", caption="OldCaption1", properties={"name": "Alice"}),
Node(id="2", caption="OldCaption2", properties={"name": "Bob"}),
Node(id="3", properties={"name": "Charlie"}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(property="name", override=True)
assert VG.nodes[0].caption == "Alice"
assert VG.nodes[1].caption == "Bob"
assert VG.nodes[2].caption == "Charlie"
def test_set_node_captions_override_false() -> None:
"""Test that override=False preserves existing captions."""
nodes = [
Node(id="1", caption="ExistingCaption", properties={"name": "Alice"}),
Node(id="2", properties={"name": "Bob"}),
Node(id="3", caption="AnotherCaption", properties={"name": "Charlie"}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(property="name", override=False)
assert VG.nodes[0].caption == "ExistingCaption" # Not overridden
assert VG.nodes[1].caption == "Bob" # Set (was None)
assert VG.nodes[2].caption == "AnotherCaption" # Not overridden
def test_set_node_captions_missing_property() -> None:
"""Test behavior when property is missing from some nodes."""
nodes = [
Node(id="1", properties={"name": "Alice"}),
Node(id="2", properties={"age": 25}), # No "name" property
Node(id="3", properties={"name": "Charlie"}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(property="name")
assert VG.nodes[0].caption == "Alice"
assert VG.nodes[1].caption == "" # Empty string for missing property
assert VG.nodes[2].caption == "Charlie"
def test_set_node_captions_numeric_property() -> None:
"""Test setting captions from numeric properties."""
nodes = [
Node(id="1", properties={"score": 100}),
Node(id="2", properties={"score": 200}),
Node(id="3", properties={"score": 300}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(property="score")
assert VG.nodes[0].caption == "100"
assert VG.nodes[1].caption == "200"
assert VG.nodes[2].caption == "300"
def test_set_node_captions_field_and_property_both_provided() -> None:
"""Test that providing both field and property raises an error."""
nodes = [Node(id="1", properties={"name": "Alice"})]
VG = VisualizationGraph(nodes=nodes, relationships=[])
with pytest.raises(ValueError, match="Exactly one of the arguments"):
VG.set_node_captions(field="id", property="name")
def test_set_node_captions_neither_field_nor_property() -> None:
"""Test that providing neither field nor property raises an error."""
nodes = [Node(id="1", properties={"name": "Alice"})]
VG = VisualizationGraph(nodes=nodes, relationships=[])
with pytest.raises(ValueError, match="Exactly one of the arguments"):
VG.set_node_captions()
def test_set_node_captions_field_with_snake_case() -> None:
"""Test that field names are converted to snake_case."""
nodes = [
Node(id="1", caption_size=1, properties={"name": "Alice"}),
Node(id="2", caption_size=2, properties={"name": "Bob"}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(field="captionSize")
assert VG.nodes[0].caption == "1"
assert VG.nodes[1].caption == "2"
def test_set_node_captions_empty_graph() -> None:
"""Test setting captions on an empty graph."""
VG = VisualizationGraph(nodes=[], relationships=[])
# Should not raise an error
VG.set_node_captions(property="name")
assert len(VG.nodes) == 0
def test_set_node_captions_complex_property_values() -> None:
"""Test setting captions from properties with complex types."""
nodes = [
Node(id="1", properties={"tags": ["tag1", "tag2"]}),
Node(id="2", properties={"metadata": {"key": "value"}}),
Node(id="3", properties={"value": None}),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.set_node_captions(property="tags")
assert VG.nodes[0].caption == "['tag1', 'tag2']"
assert VG.nodes[1].caption == ""
assert VG.nodes[2].caption == ""
VG.set_node_captions(property="metadata")
assert VG.nodes[0].caption == ""
assert VG.nodes[1].caption == "{'key': 'value'}"
assert VG.nodes[2].caption == ""