-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_widget.py
More file actions
283 lines (217 loc) · 9.75 KB
/
test_widget.py
File metadata and controls
283 lines (217 loc) · 9.75 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import datetime
from typing import Any
import pytest
from neo4j_viz import GraphWidget, Node, Relationship, VisualizationGraph
from neo4j_viz.options import Layout, RenderOptions
from neo4j_viz.widget import _serialize_entity
class TestSerializeEntity:
def test_serialize_node(self) -> None:
node = Node(id="n1", caption="Person", color="#ff0000")
result = _serialize_entity(node)
assert result["id"] == "n1"
assert result["caption"] == "Person"
assert result["color"] == "#ff0000"
def test_serialize_relationship(self) -> None:
rel = Relationship(source="n1", target="n2", caption="KNOWS")
result = _serialize_entity(rel)
assert result["from"] == "n1"
assert result["to"] == "n2"
assert result["caption"] == "KNOWS"
def test_serialize_non_json_serializable_property(self) -> None:
now = datetime.datetime.now()
node = Node(
id="n1",
properties={"timestamp": now, "name": "test"},
)
result = _serialize_entity(node)
# Non-serializable datetime should be converted to string
assert result["properties"]["timestamp"] == str(now)
# Regular properties should be unchanged
assert result["properties"]["name"] == "test"
def test_serialize_multiple_non_json_serializable_properties(self) -> None:
now = datetime.datetime.now()
custom_obj = object()
node = Node(
id="n1",
properties={
"timestamp": now,
"custom": custom_obj,
"normal": 123,
},
)
result = _serialize_entity(node)
assert result["properties"]["timestamp"] == str(now)
assert result["properties"]["custom"] == str(custom_obj)
assert result["properties"]["normal"] == 123
class TestGraphWidget:
def test_from_graph_data_basic(self) -> None:
nodes = [Node(id="n1", caption="A"), Node(id="n2", caption="B")]
rels = [Relationship(source="n1", target="n2", caption="LINKS")]
widget = GraphWidget.from_graph_data(nodes, rels)
assert len(widget.nodes) == 2
assert len(widget.relationships) == 1
assert widget.width == "100%"
assert widget.height == "600px"
assert widget.options == {}
def test_from_graph_data_with_options(self) -> None:
nodes = [Node(id="n1")]
rels: list[Relationship] = []
widget = GraphWidget.from_graph_data(
nodes,
rels,
width="800px",
height="400px",
options=RenderOptions(layout=Layout.FORCE_DIRECTED),
)
assert widget.width == "800px"
assert widget.height == "400px"
assert widget.options == {"layout": "d3Force"}
def test_widget_trait_defaults(self) -> None:
widget = GraphWidget()
assert widget.nodes == []
assert widget.relationships == []
assert widget.width == "100%"
assert widget.height == "600px"
assert widget.options == {}
class TestWidgetDataBinding:
"""Test traitlet data binding - modifications that would sync to JS."""
def test_update_nodes(self) -> None:
widget = GraphWidget(nodes=[{"id": "n1", "caption": "A"}])
assert len(widget.nodes) == 1
# Simulate adding a node (as JS or Python might do)
widget.nodes = [*widget.nodes, {"id": "n2", "caption": "B"}]
assert len(widget.nodes) == 2
assert widget.nodes[1]["id"] == "n2"
def test_update_relationships(self) -> None:
widget = GraphWidget(
nodes=[{"id": "n1"}, {"id": "n2"}],
relationships=[],
)
assert len(widget.relationships) == 0
widget.relationships = [{"from": "n1", "to": "n2", "caption": "LINKS"}]
assert len(widget.relationships) == 1
def test_update_options(self) -> None:
widget = GraphWidget(options={"layout": "d3Force"})
widget.options = {"layout": "hierarchical", "zoom": 2.0}
assert widget.options["layout"] == "hierarchical"
assert widget.options["zoom"] == 2.0
def test_update_dimensions(self) -> None:
widget = GraphWidget()
widget.width = "500px"
widget.height = "300px"
assert widget.width == "500px"
assert widget.height == "300px"
def test_observe_node_changes(self) -> None:
"""Test that traitlet observers fire on changes."""
widget = GraphWidget()
changes: list[dict[str, Any]] = []
def on_change(change: dict[str, Any]) -> None:
changes.append(change)
widget.observe(on_change, names=["nodes"])
widget.nodes = [{"id": "n1"}]
assert len(changes) == 1
assert changes[0]["name"] == "nodes"
assert changes[0]["new"] == [{"id": "n1"}]
def test_observe_multiple_traits(self) -> None:
"""Test observing multiple trait changes."""
widget = GraphWidget()
change_log: list[str] = []
def log_change(change: dict[str, Any]) -> None:
change_log.append(change["name"])
widget.observe(log_change, names=["nodes", "relationships", "options"])
widget.nodes = [{"id": "n1"}]
widget.relationships = [{"from": "n1", "to": "n1"}]
widget.options = {"zoom": 1.5}
assert change_log == ["nodes", "relationships", "options"]
def test_replace_all_data(self) -> None:
"""Test replacing entire graph data."""
nodes = [Node(id="n1"), Node(id="n2")]
rels = [Relationship(source="n1", target="n2")]
widget = GraphWidget.from_graph_data(nodes, rels)
# Replace with completely new data
new_nodes = [Node(id="x1"), Node(id="x2"), Node(id="x3")]
new_rels = [Relationship(source="x1", target="x2"), Relationship(source="x2", target="x3")]
widget.nodes = [_serialize_entity(n) for n in new_nodes]
widget.relationships = [_serialize_entity(r) for r in new_rels]
assert len(widget.nodes) == 3
assert len(widget.relationships) == 2
assert widget.nodes[0]["id"] == "x1"
def test_add_data(self) -> None:
"""Test adding new data to existing graph."""
nodes = [Node(id="n1"), Node(id="n2")]
rels = [Relationship(source="n1", target="n2")]
widget = GraphWidget.from_graph_data(nodes, rels)
widget.add_data(Node(id="x1"), Relationship(source="x1", target="x2"))
assert len(widget.nodes) == 3
assert len(widget.relationships) == 2
def test_remove_data(self) -> None:
"""Test removing data from the graph."""
node_1 = Node(id="n1")
nodes = [node_1, Node(id="n2"), Node(id="n3")]
rels = [
Relationship(source="n1", target="n2"),
Relationship(id=42, source="n2", target="n1"),
Relationship(source="n2", target="n1"), # detach delete
Relationship(id=43, source="n3", target="n3"),
]
widget = GraphWidget.from_graph_data(nodes, rels)
widget.remove_data(nodes=[node_1, "n2"], relationships=[rels[0], "42"])
assert {n["id"] for n in widget.nodes} == {"n3"}
assert {r["id"] for r in widget.relationships} == {"43"}
render_widget_cases = {
"default": {},
"force layout": {"layout": Layout.FORCE_DIRECTED},
"grid layout": {"layout": Layout.GRID},
"coordinate layout": {"layout": Layout.COORDINATE},
"hierarchical layout + options": {"layout": Layout.HIERARCHICAL, "layout_options": {"direction": "left"}},
"with layout options": {"layout_options": {"gravity": 0.1}},
}
class TestRenderWidget:
@pytest.mark.parametrize("render_option", render_widget_cases.values(), ids=render_widget_cases.keys())
def test_basic_render_widget(self, render_option: dict[str, Any]) -> None:
nodes = [
Node(id="n1", caption="Person", x=1, y=10),
Node(id="n2", caption="Product", x=2, y=15),
]
relationships = [
Relationship(source="n1", target="n2", caption="BUYS"),
]
VG = VisualizationGraph(nodes=nodes, relationships=relationships)
widget = VG.render_widget(**render_option)
assert isinstance(widget, GraphWidget)
assert len(widget.nodes) == 2
assert len(widget.relationships) == 1
def test_render_widget_max_allowed_nodes_limit(self) -> None:
nodes = [Node(id=i) for i in range(10_001)]
VG = VisualizationGraph(nodes=nodes, relationships=[])
with pytest.raises(
ValueError,
match="Too many nodes .* to render",
):
VG.render_widget(max_allowed_nodes=10_000)
def test_render_widget_custom_dimensions(self) -> None:
nodes = [Node(id="n1")]
VG = VisualizationGraph(nodes=nodes, relationships=[])
widget = VG.render_widget(width="800px", height="400px")
assert widget.width == "800px"
assert widget.height == "400px"
def test_render_widget_with_non_json_serializable(self) -> None:
now = datetime.datetime.now()
node = Node(id="n1", properties={"timestamp": now})
VG = VisualizationGraph(nodes=[node], relationships=[])
# Should not raise
widget = VG.render_widget()
assert widget.nodes[0]["properties"]["timestamp"] == str(now)
def test_render_widget_options_passed_through(self) -> None:
nodes = [Node(id="n1")]
VG = VisualizationGraph(nodes=nodes, relationships=[])
widget = VG.render_widget(
layout=Layout.HIERARCHICAL,
initial_zoom=2.0,
min_zoom=0.1,
max_zoom=5.0,
)
assert widget.options["layout"] == "hierarchical"
assert widget.options["zoom"] == 2.0
assert widget.options["nvlOptions"]["minZoom"] == 0.1
assert widget.options["nvlOptions"]["maxZoom"] == 5.0