|
1
|
|
|
import graphinate.builders |
|
2
|
|
|
import networkx as nx |
|
3
|
|
|
import pytest |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
@pytest.mark.parametrize('case', [0, None, "", False]) |
|
7
|
|
|
def test_label_converter__value__falsy(case): |
|
8
|
|
|
actual = graphinate.builders.label_converter(case, delimiter=graphinate.builders.DEFAULT_NODE_DELIMITER) |
|
9
|
|
|
assert actual == case |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def test_encoding(): |
|
13
|
|
|
expected_edge = (("parent_a", "child_a"), ("parent_b", "child_b")) |
|
14
|
|
|
|
|
15
|
|
|
edge_id = graphinate.builders.encode_edge_id(expected_edge) |
|
16
|
|
|
actual_edge = graphinate.builders.decode_edge_id(edge_id) |
|
17
|
|
|
|
|
18
|
|
|
assert actual_edge == expected_edge |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
def test_networkx_builder__empty_model(): |
|
22
|
|
|
# arrange |
|
23
|
|
|
name = "" |
|
24
|
|
|
graph_model = graphinate.model(name=name) |
|
25
|
|
|
|
|
26
|
|
|
# act |
|
27
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
28
|
|
|
graph = builder.build() |
|
29
|
|
|
|
|
30
|
|
|
# assert |
|
31
|
|
|
assert isinstance(graph, nx.Graph) |
|
32
|
|
|
assert graph.graph['name'] == name |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
@pytest.mark.parametrize('graph_type', list(graphinate.GraphType)) |
|
36
|
|
|
def test_networkx_builder__graph_type(graph_type): |
|
37
|
|
|
# arrange |
|
38
|
|
|
name = str(graph_type) |
|
39
|
|
|
graph_model = graphinate.model(name=name) |
|
40
|
|
|
|
|
41
|
|
|
@graph_model.edge() |
|
42
|
|
|
def edge(): |
|
43
|
|
|
for i in range(5): |
|
44
|
|
|
yield {'source': i, 'target': i + 1} |
|
45
|
|
|
if graph_type in (graphinate.GraphType.DiGraph, graphinate.GraphType.MultiDiGraph): |
|
46
|
|
|
yield {'source': i + 1, 'target': i} |
|
47
|
|
|
if graph_type in (graphinate.GraphType.MultiGraph, graphinate.GraphType.MultiDiGraph): |
|
48
|
|
|
yield {'source': i, 'target': i + 1} |
|
49
|
|
|
|
|
50
|
|
|
# act |
|
51
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model, graph_type=graph_type) |
|
52
|
|
|
graph = builder.build() |
|
53
|
|
|
|
|
54
|
|
|
# assert |
|
55
|
|
|
assert isinstance(graph, nx.Graph) |
|
56
|
|
|
assert graph.graph['name'] == name |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def test_networkx_builder_repeating_edges(): |
|
60
|
|
|
# arrange |
|
61
|
|
|
name = 'Repeating Nodes' |
|
62
|
|
|
graph_model = graphinate.GraphModel(name=name) |
|
63
|
|
|
|
|
64
|
|
|
@graph_model.edge() |
|
65
|
|
|
def edge(): |
|
66
|
|
|
for i in range(5): |
|
67
|
|
|
e = {'source': i, 'target': i + 1} |
|
68
|
|
|
yield e |
|
69
|
|
|
yield e |
|
70
|
|
|
|
|
71
|
|
|
# act |
|
72
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
73
|
|
|
graph = builder.build() |
|
74
|
|
|
|
|
75
|
|
|
# assert |
|
76
|
|
|
assert isinstance(graph, nx.Graph) |
|
77
|
|
|
assert graph.graph['name'] == name |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
def test_networkx_builder_simple_tuple(): |
|
|
|
|
|
|
81
|
|
|
# arrange |
|
82
|
|
|
name = 'Simple Tuple' |
|
83
|
|
|
graph_model = graphinate.GraphModel(name=name) |
|
84
|
|
|
|
|
85
|
|
|
@graph_model.edge() |
|
86
|
|
|
def edge(): |
|
87
|
|
|
for i in range(5): |
|
88
|
|
|
yield {'source': (i,), 'target': (i + 1,)} |
|
89
|
|
|
|
|
90
|
|
|
# act |
|
91
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
92
|
|
|
graph = builder.build() |
|
93
|
|
|
|
|
94
|
|
|
# assert |
|
95
|
|
|
assert isinstance(graph, nx.Graph) |
|
96
|
|
|
assert graph.graph['name'] == name |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
def test_networkx_builder__defaults(): |
|
|
|
|
|
|
100
|
|
|
# arrange |
|
101
|
|
|
name = 'Simple Tuple' |
|
102
|
|
|
graph_model = graphinate.GraphModel(name=name) |
|
103
|
|
|
|
|
104
|
|
|
@graph_model.edge() |
|
105
|
|
|
def edge(): |
|
106
|
|
|
for i in range(5): |
|
107
|
|
|
yield {'source': (i,), 'target': (i + 1,)} |
|
108
|
|
|
|
|
109
|
|
|
# act |
|
110
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
111
|
|
|
graph = builder.build() |
|
112
|
|
|
|
|
113
|
|
|
# assert |
|
114
|
|
|
assert isinstance(graph, nx.Graph) |
|
115
|
|
|
assert graph.graph['name'] == name |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
@pytest.mark.parametrize('execution_number', range(5)) |
|
119
|
|
|
def test_networkx_builder__map_graph_model(execution_number, map_graph_model): |
|
120
|
|
|
# arrange |
|
121
|
|
|
country_count, city_count, graph_model = map_graph_model |
|
122
|
|
|
person_count = city_count |
|
123
|
|
|
|
|
124
|
|
|
# act |
|
125
|
|
|
builder = graphinate.builders.NetworkxBuilder(graph_model) |
|
126
|
|
|
graph = builder.build() |
|
127
|
|
|
|
|
128
|
|
|
# assert |
|
129
|
|
|
assert graph.order() == country_count + city_count + person_count |
|
130
|
|
|
assert graph.graph['node_types']['country'] == country_count |
|
131
|
|
|
assert graph.graph['node_types']['city'] == city_count |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
@pytest.mark.parametrize('execution_number', range(5)) |
|
135
|
|
|
def test_d3_builder__map_graph_model(execution_number, map_graph_model): |
|
136
|
|
|
# arrange |
|
137
|
|
|
country_count, city_count, graph_model = map_graph_model |
|
138
|
|
|
person_count = city_count |
|
139
|
|
|
# act |
|
140
|
|
|
builder = graphinate.builders.D3Builder(graph_model) |
|
141
|
|
|
actual_graph = builder.build() |
|
142
|
|
|
|
|
143
|
|
|
# assert |
|
144
|
|
|
assert actual_graph['directed'] is False |
|
145
|
|
|
assert actual_graph['multigraph'] is False |
|
146
|
|
|
assert actual_graph['graph']['name'] == 'Map' |
|
147
|
|
|
assert actual_graph['graph']['node_types']['city'] == city_count |
|
148
|
|
|
assert actual_graph['graph']['node_types']['country'] == country_count |
|
149
|
|
|
assert len(actual_graph['nodes']) == country_count + city_count + person_count |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
@pytest.mark.parametrize('execution_number', range(5)) |
|
153
|
|
|
def test_graphql_builder__map_graph_model(execution_number, map_graph_model, graphql_query): |
|
154
|
|
|
# arrange |
|
155
|
|
|
country_count, city_count, graph_model = map_graph_model |
|
156
|
|
|
person_count = city_count |
|
157
|
|
|
|
|
158
|
|
|
# act |
|
159
|
|
|
builder = graphinate.builders.GraphQLBuilder(graph_model) |
|
160
|
|
|
|
|
161
|
|
|
import strawberry |
|
162
|
|
|
schema: strawberry.Schema = builder.build() |
|
163
|
|
|
execution_result = schema.execute_sync(graphql_query) |
|
164
|
|
|
actual_graph = execution_result.data |
|
165
|
|
|
|
|
166
|
|
|
# assert |
|
167
|
|
|
assert actual_graph |
|
168
|
|
|
assert actual_graph['graph'] |
|
169
|
|
|
assert actual_graph['nodes'] |
|
170
|
|
|
assert actual_graph['edges'] |
|
171
|
|
|
assert actual_graph['graph']['name'] == 'Map' |
|
172
|
|
|
node_types_counts = {c['name']: c['value'] for c in actual_graph['graph']['nodeTypeCounts']} |
|
173
|
|
|
assert node_types_counts['country'] == country_count |
|
174
|
|
|
assert node_types_counts['city'] == city_count |
|
175
|
|
|
assert len(actual_graph['nodes']) == country_count + city_count + person_count |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
def test_graphql_builder_measures(): |
|
179
|
|
|
# arrange |
|
180
|
|
|
|
|
181
|
|
|
graph_model = graphinate.model(name="Octagonal Graph") |
|
182
|
|
|
number_of_sides = 8 |
|
183
|
|
|
graphql_query = """{ |
|
184
|
|
|
empty: measure(measure: is_empty) { |
|
185
|
|
|
name |
|
186
|
|
|
value |
|
187
|
|
|
} |
|
188
|
|
|
directed: measure(measure: is_directed) { |
|
189
|
|
|
name |
|
190
|
|
|
value |
|
191
|
|
|
} |
|
192
|
|
|
planar: measure(measure: is_planar) { |
|
193
|
|
|
name |
|
194
|
|
|
value |
|
195
|
|
|
} |
|
196
|
|
|
connectivity: measure(measure: is_connected) { |
|
197
|
|
|
name |
|
198
|
|
|
value |
|
199
|
|
|
} |
|
200
|
|
|
node_connectivity: measure(measure: node_connectivity) { |
|
201
|
|
|
name |
|
202
|
|
|
value |
|
203
|
|
|
} |
|
204
|
|
|
threshold_graph: measure(measure: is_threshold_graph) { |
|
205
|
|
|
name |
|
206
|
|
|
value |
|
207
|
|
|
} |
|
208
|
|
|
}""" |
|
209
|
|
|
|
|
210
|
|
|
expected_response = { |
|
211
|
|
|
"empty": { |
|
212
|
|
|
"name": "is_empty", |
|
213
|
|
|
"value": 0 |
|
214
|
|
|
}, |
|
215
|
|
|
"directed": { |
|
216
|
|
|
"name": "is_directed", |
|
217
|
|
|
"value": 0 |
|
218
|
|
|
}, |
|
219
|
|
|
"planar": { |
|
220
|
|
|
"name": "is_planar", |
|
221
|
|
|
"value": 1 |
|
222
|
|
|
}, |
|
223
|
|
|
"connectivity": { |
|
224
|
|
|
"name": "is_connected", |
|
225
|
|
|
"value": 1 |
|
226
|
|
|
}, |
|
227
|
|
|
"node_connectivity": { |
|
228
|
|
|
"name": "node_connectivity", |
|
229
|
|
|
"value": 2 |
|
230
|
|
|
}, |
|
231
|
|
|
"threshold_graph": { |
|
232
|
|
|
"name": "is_threshold_graph", |
|
233
|
|
|
"value": 0 |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
# Register edges supplier function |
|
239
|
|
|
@graph_model.edge() |
|
240
|
|
|
def edge(): |
|
241
|
|
|
for i in range(number_of_sides): |
|
242
|
|
|
yield {'source': i, 'target': i + 1} |
|
243
|
|
|
yield {'source': number_of_sides, 'target': 0} |
|
244
|
|
|
|
|
245
|
|
|
# act |
|
246
|
|
|
builder = graphinate.builders.GraphQLBuilder(graph_model) |
|
247
|
|
|
|
|
248
|
|
|
import strawberry |
|
249
|
|
|
schema: strawberry.Schema = builder.build( |
|
250
|
|
|
default_node_attributes=graphinate.builders.Builder.default_node_attributes |
|
251
|
|
|
) |
|
252
|
|
|
execution_result = schema.execute_sync(graphql_query) |
|
253
|
|
|
actual_response = execution_result.data |
|
254
|
|
|
|
|
255
|
|
|
# assert |
|
256
|
|
|
assert actual_response == expected_response |
|
257
|
|
|
|