|
1
|
|
|
import networkx as nx |
|
2
|
|
|
import pytest |
|
3
|
|
|
|
|
4
|
|
|
import graphinate |
|
5
|
|
|
import graphinate.builders |
|
6
|
|
|
from graphinate import GraphType |
|
7
|
|
|
|
|
8
|
|
|
graph_types = [ |
|
9
|
|
|
(nx.Graph(), GraphType.Graph), |
|
10
|
|
|
(nx.DiGraph(), GraphType.DiGraph), |
|
11
|
|
|
(nx.MultiGraph(), GraphType.MultiGraph), |
|
12
|
|
|
(nx.MultiDiGraph(), GraphType.MultiDiGraph) |
|
13
|
|
|
] |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
@pytest.mark.parametrize(('graph', 'expected_graph_type'), graph_types) |
|
17
|
|
|
def test_returns_graph_type_for_graph(graph, expected_graph_type): |
|
18
|
|
|
# Act |
|
19
|
|
|
actual_graph_type = GraphType.of(graph) |
|
20
|
|
|
|
|
21
|
|
|
# Assert |
|
22
|
|
|
assert actual_graph_type == expected_graph_type |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_networkx_builder__empty_model(): |
|
26
|
|
|
# arrange |
|
27
|
|
|
name = "" |
|
28
|
|
|
graph_model = graphinate.model(name=name) |
|
29
|
|
|
|
|
30
|
|
|
# act |
|
31
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
32
|
|
|
graph = builder.build() |
|
33
|
|
|
|
|
34
|
|
|
# assert |
|
35
|
|
|
assert isinstance(graph, nx.Graph) |
|
36
|
|
|
assert graph.graph['name'] == name |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
@pytest.mark.parametrize('graph_type', list(graphinate.GraphType)) |
|
40
|
|
|
def test_networkx_builder__graph_type(graph_type): |
|
41
|
|
|
# arrange |
|
42
|
|
|
name = str(graph_type) |
|
43
|
|
|
graph_model = graphinate.model(name=name) |
|
44
|
|
|
|
|
45
|
|
|
@graph_model.edge() |
|
46
|
|
|
def edge(): |
|
47
|
|
|
for i in range(5): |
|
48
|
|
|
yield {'source': i, 'target': i + 1} |
|
49
|
|
|
if graph_type in (graphinate.GraphType.DiGraph, graphinate.GraphType.MultiDiGraph): |
|
50
|
|
|
yield {'source': i + 1, 'target': i} |
|
51
|
|
|
if graph_type in (graphinate.GraphType.MultiGraph, graphinate.GraphType.MultiDiGraph): |
|
52
|
|
|
yield {'source': i, 'target': i + 1} |
|
53
|
|
|
|
|
54
|
|
|
# act |
|
55
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model, graph_type=graph_type) |
|
56
|
|
|
graph = builder.build() |
|
57
|
|
|
|
|
58
|
|
|
# assert |
|
59
|
|
|
assert isinstance(graph, nx.Graph) |
|
60
|
|
|
assert graph.graph['name'] == name |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def test_networkx_builder_repeating_nodes(): |
|
64
|
|
|
# arrange |
|
65
|
|
|
name = 'Repeating Nodes' |
|
66
|
|
|
graph_model = graphinate.GraphModel(name=name) |
|
67
|
|
|
|
|
68
|
|
|
@graph_model.node() |
|
69
|
|
|
def node(): |
|
70
|
|
|
for i in range(5): |
|
71
|
|
|
yield i |
|
72
|
|
|
yield i |
|
73
|
|
|
|
|
74
|
|
|
# act |
|
75
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
76
|
|
|
graph: nx.Graph = builder.build() |
|
77
|
|
|
|
|
78
|
|
|
# assert |
|
79
|
|
|
assert isinstance(graph, nx.Graph) |
|
80
|
|
|
assert graph.graph['name'] == name |
|
81
|
|
|
assert all(graph.nodes[n]['magnitude'] == 2 for n in graph) |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
@pytest.mark.parametrize('weight', [1.0, 1.5]) |
|
85
|
|
|
def test_networkx_builder_repeating_edges(weight): |
|
86
|
|
|
# arrange |
|
87
|
|
|
name = 'Repeating Edges' |
|
88
|
|
|
graph_model = graphinate.GraphModel(name=name) |
|
89
|
|
|
|
|
90
|
|
|
@graph_model.edge(weight=weight) |
|
91
|
|
|
def edge(): |
|
92
|
|
|
for i in range(5): |
|
93
|
|
|
e = {'source': i, 'target': i + 1} |
|
94
|
|
|
yield e |
|
95
|
|
|
yield e |
|
96
|
|
|
|
|
97
|
|
|
# act |
|
98
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
99
|
|
|
graph = builder.build() |
|
100
|
|
|
|
|
101
|
|
|
# assert |
|
102
|
|
|
assert isinstance(graph, nx.Graph) |
|
103
|
|
|
assert graph.graph['name'] == name |
|
104
|
|
|
assert all(m == weight * 2 for *_, m in graph.edges.data('weight')) |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
def test_networkx_builder_simple_tuple(): |
|
108
|
|
|
# arrange |
|
109
|
|
|
name = 'Simple Tuple' |
|
110
|
|
|
graph_model = graphinate.GraphModel(name=name) |
|
111
|
|
|
|
|
112
|
|
|
@graph_model.edge() |
|
113
|
|
|
def edge(): |
|
114
|
|
|
for i in range(5): |
|
115
|
|
|
yield {'source': (i,), 'target': (i + 1,)} |
|
116
|
|
|
|
|
117
|
|
|
# act |
|
118
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
119
|
|
|
graph = builder.build() |
|
120
|
|
|
|
|
121
|
|
|
# assert |
|
122
|
|
|
assert isinstance(graph, nx.Graph) |
|
123
|
|
|
assert graph.graph['name'] == name |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
@pytest.mark.parametrize('execution_number', range(5)) |
|
127
|
|
|
def test_networkx_builder__map_graph_model(execution_number, map_graph_model): |
|
128
|
|
|
# arrange |
|
129
|
|
|
country_count, city_count, graph_model = map_graph_model |
|
130
|
|
|
person_count = city_count |
|
131
|
|
|
|
|
132
|
|
|
# act |
|
133
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
134
|
|
|
graph = builder.build() |
|
135
|
|
|
|
|
136
|
|
|
# assert |
|
137
|
|
|
assert graph.order() == country_count + city_count + person_count |
|
138
|
|
|
assert graph.graph['node_types']['country'] == country_count |
|
139
|
|
|
assert graph.graph['node_types']['city'] == city_count |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
@pytest.mark.parametrize('execution_number', range(5)) |
|
143
|
|
|
def test_d3_builder__map_graph_model(execution_number, map_graph_model): |
|
144
|
|
|
# arrange |
|
145
|
|
|
country_count, city_count, graph_model = map_graph_model |
|
146
|
|
|
person_count = city_count |
|
147
|
|
|
|
|
148
|
|
|
# act |
|
149
|
|
|
builder = graphinate.builders.D3Builder(graph_model) |
|
150
|
|
|
actual_graph = builder.build() |
|
151
|
|
|
|
|
152
|
|
|
# assert |
|
153
|
|
|
assert actual_graph['directed'] is False |
|
154
|
|
|
assert actual_graph['multigraph'] is False |
|
155
|
|
|
assert actual_graph['graph']['name'] == 'Map' |
|
156
|
|
|
assert actual_graph['graph']['node_types']['country'] == country_count |
|
157
|
|
|
assert actual_graph['graph']['node_types']['city'] == city_count |
|
158
|
|
|
assert len(actual_graph['nodes']) == country_count + city_count + person_count |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
def test_d3_builder__map_graph_model__both_specific_ids(map_graph_model): |
|
162
|
|
|
# arrange |
|
163
|
|
|
_, _, graph_model = map_graph_model |
|
164
|
|
|
|
|
165
|
|
|
# act |
|
166
|
|
|
builder = graphinate.builders.D3Builder(graph_model) |
|
167
|
|
|
actual_graph = builder.build(country_id="1", city_id="1") |
|
168
|
|
|
|
|
169
|
|
|
# assert |
|
170
|
|
|
assert actual_graph['directed'] is False |
|
171
|
|
|
assert actual_graph['multigraph'] is False |
|
172
|
|
|
assert actual_graph['graph']['name'] == 'Map' |
|
173
|
|
|
assert actual_graph['graph']['node_types'].get('city', 0) in (0, 1) |
|
174
|
|
|
assert actual_graph['graph']['node_types']['country'] == 1 |
|
175
|
|
|
assert len(actual_graph['nodes']) in (1, 3) |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
@pytest.mark.parametrize('execution_number', range(5)) |
|
179
|
|
|
def test_graphql_builder__map_graph_model(execution_number, map_graph_model, graphql_query): |
|
180
|
|
|
# arrange |
|
181
|
|
|
expected_country_count, expected_city_count, graph_model = map_graph_model |
|
182
|
|
|
expected_person_count = expected_city_count |
|
183
|
|
|
|
|
184
|
|
|
# act |
|
185
|
|
|
builder = graphinate.builders.GraphQLBuilder(graph_model) |
|
186
|
|
|
|
|
187
|
|
|
import strawberry |
|
188
|
|
|
schema: strawberry.Schema = builder.build() |
|
189
|
|
|
execution_result = schema.execute_sync(graphql_query) |
|
190
|
|
|
actual_graph = execution_result.data |
|
191
|
|
|
|
|
192
|
|
|
# assert |
|
193
|
|
|
assert actual_graph |
|
194
|
|
|
assert actual_graph['graph'] |
|
195
|
|
|
assert actual_graph['nodes'] |
|
196
|
|
|
assert actual_graph['edges'] |
|
197
|
|
|
assert actual_graph['graph']['name'] == 'Map' |
|
198
|
|
|
node_types_counts = {c['name']: c['value'] for c in actual_graph['graph']['nodeTypeCounts']} |
|
199
|
|
|
assert node_types_counts['country'] == expected_country_count |
|
200
|
|
|
assert node_types_counts['city'] == expected_city_count |
|
201
|
|
|
assert len(actual_graph['nodes']) == expected_country_count + expected_city_count + expected_person_count |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
graphql_operations_cases = [ |
|
205
|
|
|
("""{ |
|
206
|
|
|
empty: measure(measure: is_empty){...Details} |
|
207
|
|
|
directed: measure(measure: is_directed){...Details} |
|
208
|
|
|
planar: measure(measure: is_planar){...Details} |
|
209
|
|
|
connectivity: measure(measure: is_connected){...Details} |
|
210
|
|
|
node_connectivity: measure(measure: node_connectivity){...Details} |
|
211
|
|
|
threshold_graph: measure(measure: is_threshold_graph){...Details} |
|
212
|
|
|
} |
|
213
|
|
|
fragment Details on Measure {name value} |
|
214
|
|
|
""", { |
|
215
|
|
|
"empty": { |
|
216
|
|
|
"name": "is_empty", |
|
217
|
|
|
"value": 0 |
|
218
|
|
|
}, |
|
219
|
|
|
"directed": { |
|
220
|
|
|
"name": "is_directed", |
|
221
|
|
|
"value": 0 |
|
222
|
|
|
}, |
|
223
|
|
|
"planar": { |
|
224
|
|
|
"name": "is_planar", |
|
225
|
|
|
"value": 1 |
|
226
|
|
|
}, |
|
227
|
|
|
"connectivity": { |
|
228
|
|
|
"name": "is_connected", |
|
229
|
|
|
"value": 1 |
|
230
|
|
|
}, |
|
231
|
|
|
"node_connectivity": { |
|
232
|
|
|
"name": "node_connectivity", |
|
233
|
|
|
"value": 2 |
|
234
|
|
|
}, |
|
235
|
|
|
"threshold_graph": { |
|
236
|
|
|
"name": "is_threshold_graph", |
|
237
|
|
|
"value": 0 |
|
238
|
|
|
} |
|
239
|
|
|
}), |
|
240
|
|
|
("""query Graph {nodes(nodeId: "WzBd") {type label} edges(edgeId: "WyJXekJkIiwgIld6RmQiXQ==") {type label}}""", |
|
241
|
|
|
# noqa: E501 |
|
242
|
|
|
{ |
|
243
|
|
|
"nodes": [ |
|
244
|
|
|
{ |
|
245
|
|
|
"type": "node", |
|
246
|
|
|
"label": "0" |
|
247
|
|
|
} |
|
248
|
|
|
], |
|
249
|
|
|
"edges": [ |
|
250
|
|
|
{ |
|
251
|
|
|
"type": "edge", |
|
252
|
|
|
"label": "0 ⟹ 1" |
|
253
|
|
|
} |
|
254
|
|
|
] |
|
255
|
|
|
}), |
|
256
|
|
|
("mutation {refresh}", {'refresh': True}) |
|
257
|
|
|
] |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
@pytest.mark.parametrize(('graphql_query', 'expected_response'), graphql_operations_cases) |
|
261
|
|
|
def test_graphql_builder_query(octagonal_graph_model, graphql_query, expected_response): |
|
262
|
|
|
# act |
|
263
|
|
|
builder = graphinate.builders.GraphQLBuilder(octagonal_graph_model) |
|
264
|
|
|
|
|
265
|
|
|
import strawberry |
|
266
|
|
|
schema: strawberry.Schema = builder.build( |
|
267
|
|
|
default_node_attributes=graphinate.builders.Builder.default_node_attributes |
|
268
|
|
|
) |
|
269
|
|
|
execution_result = schema.execute_sync(graphql_query) |
|
270
|
|
|
actual_response = execution_result.data |
|
271
|
|
|
|
|
272
|
|
|
# assert |
|
273
|
|
|
assert actual_response == expected_response |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
def test_graphql_builder__ast_model__graph_query(ast_graph_model, graphql_query): |
|
277
|
|
|
# act |
|
278
|
|
|
builder = graphinate.builders.GraphQLBuilder(ast_graph_model) |
|
279
|
|
|
import strawberry |
|
280
|
|
|
schema: strawberry.Schema = builder.build() |
|
281
|
|
|
execution_result = schema.execute_sync(graphql_query) |
|
282
|
|
|
actual_graph = execution_result.data |
|
283
|
|
|
|
|
284
|
|
|
# assert |
|
285
|
|
|
assert actual_graph |
|
286
|
|
|
assert actual_graph['graph'] |
|
287
|
|
|
assert actual_graph['nodes'] |
|
288
|
|
|
assert actual_graph['edges'] |
|
289
|
|
|
assert actual_graph['graph']['name'] == 'AST Graph' |
|
290
|
|
|
node_types_counts = {c['name']: c['value'] for c in actual_graph['graph']['nodeTypeCounts']} |
|
291
|
|
|
assert node_types_counts |
|
292
|
|
|
|