Passed
Push — main ( 325744...be7884 )
by Eran
01:17
created

test_graphql_builder_specific_elements()   A

Complexity

Conditions 1

Size

Total Lines 35
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nop 1
dl 0
loc 35
rs 9.6
c 0
b 0
f 0
1
import networkx as nx
2
import pytest
3
4
import graphinate.builders
5
6
7
@pytest.mark.parametrize('case', [0, None, "", False])
8
def test_label_converter__value__falsy(case):
9
    actual = graphinate.builders.label_converter(case, delimiter=graphinate.builders.DEFAULT_NODE_DELIMITER)
10
    assert actual == case
11
12
13
def test_encoding():
14
    expected_edge = (("parent_a", "child_a"), ("parent_b", "child_b"))
15
16
    edge_id = graphinate.builders.encode_edge_id(expected_edge)
17
    actual_edge = graphinate.builders.decode_edge_id(edge_id)
18
19
    assert actual_edge == expected_edge
20
21
22
def test_networkx_builder__empty_model():
23
    # arrange
24
    name = ""
25
    graph_model = graphinate.model(name=name)
26
27
    # act
28
    builder = graphinate.builders.NetworkxBuilder(graph_model)
29
    graph = builder.build()
30
31
    # assert
32
    assert isinstance(graph, nx.Graph)
33
    assert graph.graph['name'] == name
34
35
36
@pytest.mark.parametrize('graph_type', list(graphinate.GraphType))
37
def test_networkx_builder__graph_type(graph_type):
38
    # arrange
39
    name = str(graph_type)
40
    graph_model = graphinate.model(name=name)
41
42
    @graph_model.edge()
43
    def edge():
44
        for i in range(5):
45
            yield {'source': i, 'target': i + 1}
46
            if graph_type in (graphinate.GraphType.DiGraph, graphinate.GraphType.MultiDiGraph):
47
                yield {'source': i + 1, 'target': i}
48
            if graph_type in (graphinate.GraphType.MultiGraph, graphinate.GraphType.MultiDiGraph):
49
                yield {'source': i, 'target': i + 1}
50
51
    # act
52
    builder = graphinate.builders.NetworkxBuilder(graph_model, graph_type=graph_type)
53
    graph = builder.build()
54
55
    # assert
56
    assert isinstance(graph, nx.Graph)
57
    assert graph.graph['name'] == name
58
59
60 View Code Duplication
def test_networkx_builder_repeating_nodes():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
61
    # arrange
62
    name = 'Repeating Nodes'
63
    graph_model = graphinate.GraphModel(name=name)
64
65
    @graph_model.node()
66
    def edge():
67
        for i in range(5):
68
            yield i
69
            yield i
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
def test_networkx_builder_repeating_edges():
81
    # arrange
82
    name = 'Repeating Edges'
83
    graph_model = graphinate.GraphModel(name=name)
84
85
    @graph_model.edge()
86
    def edge():
87
        for i in range(5):
88
            e = {'source': i, 'target': i + 1}
89
            yield e
90
            yield e
91
92
    # act
93
    builder = graphinate.builders.NetworkxBuilder(graph_model)
94
    graph = builder.build()
95
96
    # assert
97
    assert isinstance(graph, nx.Graph)
98
    assert graph.graph['name'] == name
99
100
101 View Code Duplication
def test_networkx_builder_simple_tuple():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
    # arrange
103
    name = 'Simple Tuple'
104
    graph_model = graphinate.GraphModel(name=name)
105
106
    @graph_model.edge()
107
    def edge():
108
        for i in range(5):
109
            yield {'source': (i,), 'target': (i + 1,)}
110
111
    # act
112
    builder = graphinate.builders.NetworkxBuilder(graph_model)
113
    graph = builder.build()
114
115
    # assert
116
    assert isinstance(graph, nx.Graph)
117
    assert graph.graph['name'] == name
118
119
120 View Code Duplication
def test_networkx_builder__defaults():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
121
    # arrange
122
    name = 'Simple Tuple'
123
    graph_model = graphinate.GraphModel(name=name)
124
125
    @graph_model.edge()
126
    def edge():
127
        for i in range(5):
128
            yield {'source': (i,), 'target': (i + 1,)}
129
130
    # act
131
    builder = graphinate.builders.NetworkxBuilder(graph_model)
132
    graph = builder.build()
133
134
    # assert
135
    assert isinstance(graph, nx.Graph)
136
    assert graph.graph['name'] == name
137
138
139
@pytest.mark.parametrize('execution_number', range(5))
140
def test_networkx_builder__map_graph_model(execution_number, map_graph_model):
141
    # arrange
142
    country_count, city_count, graph_model = map_graph_model
143
    person_count = city_count
144
145
    # act
146
    builder = graphinate.builders.NetworkxBuilder(graph_model)
147
    graph = builder.build()
148
149
    # assert
150
    assert graph.order() == country_count + city_count + person_count
151
    assert graph.graph['node_types']['country'] == country_count
152
    assert graph.graph['node_types']['city'] == city_count
153
154
155
@pytest.mark.parametrize('execution_number', range(5))
156
def test_d3_builder__map_graph_model(execution_number, map_graph_model):
157
    # arrange
158
    country_count, city_count, graph_model = map_graph_model
159
    person_count = city_count
160
    # act
161
    builder = graphinate.builders.D3Builder(graph_model)
162
    actual_graph = builder.build()
163
164
    # assert
165
    assert actual_graph['directed'] is False
166
    assert actual_graph['multigraph'] is False
167
    assert actual_graph['graph']['name'] == 'Map'
168
    assert actual_graph['graph']['node_types']['city'] == city_count
169
    assert actual_graph['graph']['node_types']['country'] == country_count
170
    assert len(actual_graph['nodes']) == country_count + city_count + person_count
171
172
173
@pytest.mark.parametrize('execution_number', range(5))
174
def test_graphql_builder__map_graph_model(execution_number, map_graph_model, graphql_query):
175
    # arrange
176
    country_count, city_count, graph_model = map_graph_model
177
    person_count = city_count
178
179
    # act
180
    builder = graphinate.builders.GraphQLBuilder(graph_model)
181
182
    import strawberry
183
    schema: strawberry.Schema = builder.build()
184
    execution_result = schema.execute_sync(graphql_query)
185
    actual_graph = execution_result.data
186
187
    # assert
188
    assert actual_graph
189
    assert actual_graph['graph']
190
    assert actual_graph['nodes']
191
    assert actual_graph['edges']
192
    assert actual_graph['graph']['name'] == 'Map'
193
    node_types_counts = {c['name']: c['value'] for c in actual_graph['graph']['nodeTypeCounts']}
194
    assert node_types_counts['country'] == country_count
195
    assert node_types_counts['city'] == city_count
196
    assert len(actual_graph['nodes']) == country_count + city_count + person_count
197
198
199
def test_graphql_builder_measures(octagonal_graph_model):
200
    # arrange
201
    measures_graphql_query = """{
202
      empty: measure(measure: is_empty) {
203
        name
204
        value
205
      }
206
      directed: measure(measure: is_directed) {
207
        name
208
        value
209
      }
210
      planar: measure(measure: is_planar) {
211
        name
212
        value
213
      }
214
      connectivity: measure(measure: is_connected) {
215
        name
216
        value
217
      }
218
      node_connectivity: measure(measure: node_connectivity) {
219
        name
220
        value
221
      }
222
      threshold_graph: measure(measure: is_threshold_graph) {
223
        name
224
        value
225
      }
226
    }"""
227
    expected_response = {
228
        "empty": {
229
            "name": "is_empty",
230
            "value": 0
231
        },
232
        "directed": {
233
            "name": "is_directed",
234
            "value": 0
235
        },
236
        "planar": {
237
            "name": "is_planar",
238
            "value": 1
239
        },
240
        "connectivity": {
241
            "name": "is_connected",
242
            "value": 1
243
        },
244
        "node_connectivity": {
245
            "name": "node_connectivity",
246
            "value": 2
247
        },
248
        "threshold_graph": {
249
            "name": "is_threshold_graph",
250
            "value": 0
251
        }
252
253
    }
254
255
    # act
256
    builder = graphinate.builders.GraphQLBuilder(octagonal_graph_model)
257
258
    import strawberry
259
    schema: strawberry.Schema = builder.build(
260
        default_node_attributes=graphinate.builders.Builder.default_node_attributes
261
    )
262
    execution_result = schema.execute_sync(measures_graphql_query)
263
    actual_response = execution_result.data
264
265
    # assert
266
    assert actual_response == expected_response
267
268
269
def test_graphql_builder_specific_elements(octagonal_graph_model):
270
    # arrange
271
    graphql_query = """
272
    query Graph {
273
      nodes(nodeId: "H4sIAFs7E2UC/2tgmcrKAAHeDK1T9ADQP1FHEAAAAA==") {type label}
274
      edges(edgeId: "H4sIAFs7E2UC/2tgmZrIAAE9Oh4mxZ6ObsXmrkahzvpGJem5yUXejo4eqS7ehiGWji6BAYZuHq6OIGBrOwW38iywcmfDcH9jfbjytil6AHhudC5sAAAA") {type label}
275
    }
276
    """
277
    expected_response = {
278
        "nodes": [
279
            {
280
                "type": "node",
281
                "label": "0"
282
            }
283
        ],
284
        "edges": [
285
            {
286
                "type": "edge",
287
                "label": "{'source': 0, 'target': 1}"
288
            }
289
        ]
290
    }
291
292
    # act
293
    builder = graphinate.builders.GraphQLBuilder(octagonal_graph_model)
294
295
    import strawberry
296
    schema: strawberry.Schema = builder.build(
297
        default_node_attributes=graphinate.builders.Builder.default_node_attributes
298
    )
299
    execution_result = schema.execute_sync(graphql_query)
300
    actual_response = execution_result.data
301
302
    # assert
303
    assert actual_response == expected_response
304
305
306
def test_graphql_builder__ast_model__graph_query(ast_graph_model, graphql_query):
307
    # act
308
    builder = graphinate.builders.GraphQLBuilder(ast_graph_model)
309
    import strawberry
310
    schema: strawberry.Schema = builder.build()
311
    execution_result = schema.execute_sync(graphql_query)
312
    actual_graph = execution_result.data
313
314
    # assert
315
    assert actual_graph
316
    assert actual_graph['graph']
317
    assert actual_graph['nodes']
318
    assert actual_graph['edges']
319
    assert actual_graph['graph']['name'] == 'AST Graph'
320
    node_types_counts = {c['name']: c['value'] for c in actual_graph['graph']['nodeTypeCounts']}
321
    assert node_types_counts
322
323
324
def test_graphql_builder__ast_model__refresh_mutation(ast_graph_model):
325
    # act
326
    builder = graphinate.builders.GraphQLBuilder(ast_graph_model)
327
    import strawberry
328
    schema: strawberry.Schema = builder.build()
329
    execution_result = schema.execute_sync("mutation {refresh}")
330
    actual = execution_result.data
331
332
    # assert
333
    assert actual
334