-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_render.py
More file actions
121 lines (99 loc) · 4.54 KB
/
test_render.py
File metadata and controls
121 lines (99 loc) · 4.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
import re
from pathlib import Path
from typing import Any
import pytest
from selenium import webdriver
from neo4j_viz import Node, Relationship, VisualizationGraph
from neo4j_viz.options import Layout, Renderer
render_cases = {
"default": {},
"force layout": {"layout": Layout.FORCE_DIRECTED},
"grid layout": {"layout": Layout.GRID},
"coordinate layout": {"layout": Layout.COORDINATE},
}
@pytest.mark.parametrize("render_option", render_cases.values(), ids=render_cases.keys())
def test_basic_render(render_option: dict[str, Any], tmp_path: Path) -> None:
nodes = [
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:0", caption="Person", x=1, y=10),
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:6", caption="Product", x=2, y=15),
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:11", caption="Product", x=3, pinned=True),
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:12", caption="Product", x=4),
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:1", caption="Person", x=5),
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:7", caption="Product", x=6),
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:8", caption="Product", x=7),
]
relationships = [
Relationship(
source="4:d09f48a4-5fca-421d-921d-a30a896c604d:0",
target="4:d09f48a4-5fca-421d-921d-a30a896c604d:6",
caption="BUYS",
),
Relationship(
source="4:d09f48a4-5fca-421d-921d-a30a896c604d:0",
target="4:d09f48a4-5fca-421d-921d-a30a896c604d:11",
caption="BUYS",
),
]
VG = VisualizationGraph(nodes=nodes, relationships=relationships)
html = VG.render(**render_option)
file_path = tmp_path / "basic_render.html"
with open(file_path, "w+") as the_file:
the_file.write(html.data)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless") # avoid browser window popping up
driver = webdriver.Chrome(options=chrome_options)
# wait for page to render
driver.implicitly_wait(3)
driver.get(f"file://{file_path}")
logs = driver.get_log("browser") # type: ignore[no-untyped-call]
severe_logs = [log for log in logs if log["level"] == "SEVERE"]
assert not severe_logs, f"Severe logs found: {severe_logs}, all logs: {logs}"
def test_unsupported_field_type() -> None:
with pytest.raises(
ValueError, match="A field of a node object is not supported: Object of type set is not JSON serializable"
):
nodes = [
Node(id="4:d09f48a4-5fca-421d-921d-a30a896c604d:0", caption="Person", unsupported=set([1, 2, 3])),
]
VG = VisualizationGraph(nodes=nodes, relationships=[])
VG.render()
with pytest.raises(
ValueError,
match="A field of a relationship object is not supported: Object of type set is not JSON serializable",
):
relationships = [
Relationship(
source="4:d09f48a4-5fca-421d-921d-a30a896c604d:0",
target="4:d09f48a4-5fca-421d-921d-a30a896c604d:6",
caption="BUYS",
unsupported=set([1, 2, 3]),
),
]
VG = VisualizationGraph(nodes=[], relationships=relationships)
VG.render()
def test_max_allowed_nodes_limit() -> None:
nodes = [Node(id=i) for i in range(10_001)]
VG = VisualizationGraph(nodes=nodes, relationships=[])
with pytest.raises(
ValueError,
match=re.escape(
"Too many nodes (10001) to render. Maximum allowed nodes is set to 10000 for performance reasons. "
"It can be increased by overriding `max_allowed_nodes`, but rendering could then take a long time"
),
):
VG.render(max_allowed_nodes=10_000)
def test_render_warnings() -> None:
nodes = [Node(id=i) for i in range(10_001)]
VG = VisualizationGraph(nodes=nodes, relationships=[])
with pytest.warns(
UserWarning,
match="To visualize more than 10.000 nodes, we recommend using the WebGL renderer instead of the "
"canvas renderer for better performance. You can set the renderer using the `renderer` parameter",
):
VG.render(max_allowed_nodes=20_000, renderer=Renderer.CANVAS)
with pytest.warns(
UserWarning,
match="Although better for performance, the WebGL renderer cannot render text, icons and arrowheads on "
"relationships. If you need these features, use the canvas renderer by setting the `renderer` parameter",
):
VG.render(max_allowed_nodes=20_000, renderer=Renderer.WEB_GL)