-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest_bulk_update.py
406 lines (350 loc) · 13.4 KB
/
test_bulk_update.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# -*- coding: utf-8 -*-
import csv
import os
import unittest
import redis
from click.testing import CliRunner
from redis import Redis
from redisgraph_bulk_loader.bulk_update import bulk_update
class TestBulkUpdate:
redis_con = redis.Redis(decode_responses=True)
@classmethod
def setup_class(cls):
cls.redis_con.flushall()
@classmethod
def teardown_class(cls):
"""Delete temporary files"""
os.unlink("/tmp/csv.tmp")
cls.redis_con.flushall()
def test_simple_updates(self):
"""Validate that bulk updates work on an empty graph."""
graphname = "tmpgraph1"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file)
out.writerow(["id", "name"])
out.writerow([0, "a"])
out.writerow([5, "b"])
out.writerow([3, "c"])
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"CREATE (:L {id: row[0], name: row[1]})",
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Labels added: 1" in res.output
assert "Nodes created: 3" in res.output
assert "Properties set: 6" in res.output
tmp_graph = self.redis_con.graph(graphname)
query_result = tmp_graph.query("MATCH (a) RETURN a.id, a.name ORDER BY a.id")
# Validate that the expected results are all present in the graph
expected_result = [[0, "a"], [3, "c"], [5, "b"]]
assert query_result.result_set == expected_result
# Attempt to re-insert the entities using MERGE.
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"MERGE (:L {id: row[0], name: row[1]})",
graphname,
],
catch_exceptions=False,
)
# No new entities should be created.
assert res.exit_code == 0
assert "Labels added" not in res.output
assert "Nodes created" not in res.output
assert "Properties set" not in res.output
def test_traversal_updates(self):
"""Validate that bulk updates can create edges and perform traversals."""
graphname = "tmpgraph1"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file)
out.writerow(["src", "dest_id", "name"])
out.writerow([0, 1, "a2"])
out.writerow([5, 2, "b2"])
out.writerow([3, 4, "c2"])
# Create a graph of the form:
# (a)-->(b)-->(c), (a)-->(c)
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"MATCH (src {id: row[0]}) CREATE (src)-[:R]->(dest:L {id: row[1], name: row[2]})",
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Nodes created: 3" in res.output
assert "Relationships created: 3" in res.output
assert "Properties set: 6" in res.output
tmp_graph = self.redis_con.graph(graphname)
query_result = tmp_graph.query(
"MATCH (a)-[:R]->(b) RETURN a.name, b.name ORDER BY a.name, b.name"
)
# Validate that the expected results are all present in the graph
expected_result = [["a", "a2"], ["b", "b2"], ["c", "c2"]]
assert query_result.result_set == expected_result
def test_datatypes(self):
"""Validate that all RedisGraph datatypes are supported by the bulk updater."""
graphname = "tmpgraph2"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file)
out.writerow([0, 1.5, "true", "string", "[1, 'nested_str']"])
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"CREATE (a:L) SET a.intval = row[0], a.doubleval = row[1], a.boolval = row[2], a.stringval = row[3], a.arrayval = row[4]",
"--no-header",
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Nodes created: 1" in res.output
assert "Properties set: 5" in res.output
tmp_graph = self.redis_con.graph(graphname)
query_result = tmp_graph.query(
"MATCH (a) RETURN a.intval, a.doubleval, a.boolval, a.stringval, a.arrayval"
)
# Validate that the expected results are all present in the graph
expected_result = [[0, 1.5, True, "string", "[1,'nested_str']"]]
assert query_result.result_set == expected_result
def test_custom_delimiter(self):
"""Validate that non-comma delimiters produce the correct results."""
graphname = "tmpgraph3"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file, delimiter="|")
out.writerow(["id", "name"])
out.writerow([0, "a"])
out.writerow([5, "b"])
out.writerow([3, "c"])
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"CREATE (:L {id: row[0], name: row[1]})",
"--separator",
"|",
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Labels added: 1" in res.output
assert "Nodes created: 3" in res.output
assert "Properties set: 6" in res.output
tmp_graph = self.redis_con.graph(graphname)
query_result = tmp_graph.query("MATCH (a) RETURN a.id, a.name ORDER BY a.id")
# Validate that the expected results are all present in the graph
expected_result = [[0, "a"], [3, "c"], [5, "b"]]
assert query_result.result_set == expected_result
# Attempt to re-insert the entities using MERGE.
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"MERGE (:L {id: row[0], name: row[1]})",
"--separator",
"|",
graphname,
],
catch_exceptions=False,
)
# No new entities should be created.
assert res.exit_code == 0
assert "Labels added" not in res.output
assert "Nodes created" not in res.output
assert "Properties set" not in res.output
def test_custom_variable_name(self):
"""Validate that the user can specify the name of the 'row' query variable."""
graphname = "variable_name"
runner = CliRunner()
csv_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "../example/"
)
person_file = os.path.join(csv_path, "Person.csv")
# Build the social graph again with a max token count of 1.
res = runner.invoke(
bulk_update,
[
"--csv",
person_file,
"--query",
"CREATE (p:Person) SET p.name = line[0], p.age = line[1], p.gender = line[2], p.status = line[3]",
"--variable-name",
"line",
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Labels added: 1" in res.output
assert "Nodes created: 14" in res.output
assert "Properties set: 56" in res.output
tmp_graph = self.redis_con.graph(graphname)
# Validate that the expected results are all present in the graph
query_result = tmp_graph.query(
"MATCH (p:Person) RETURN p.name, p.age, p.gender, p.status ORDER BY p.name"
)
expected_result = [
["Ailon Velger", 32, "male", "married"],
["Alon Fital", 32, "male", "married"],
["Boaz Arad", 31, "male", "married"],
["Gal Derriere", 26, "male", "single"],
["Jane Chernomorin", 31, "female", "married"],
["Lucy Yanfital", 30, "female", "married"],
["Mor Yesharim", 31, "female", "married"],
["Noam Nativ", 34, "male", "single"],
["Omri Traub", 33, "male", "single"],
["Ori Laslo", 32, "male", "married"],
["Roi Lipman", 32, "male", "married"],
["Shelly Laslo Rooz", 31, "female", "married"],
["Tal Doron", 32, "male", "single"],
["Valerie Abigail Arad", 31, "female", "married"],
]
assert query_result.result_set == expected_result
def test_no_header(self):
"""Validate that the '--no-header' option works properly."""
graphname = "tmpgraph4"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file)
out.writerow([0, "a"])
out.writerow([5, "b"])
out.writerow([3, "c"])
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"CREATE (:L {id: row[0], name: row[1]})",
"--no-header",
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Labels added: 1" in res.output
assert "Nodes created: 3" in res.output
assert "Properties set: 6" in res.output
tmp_graph = self.redis_con.graph(graphname)
query_result = tmp_graph.query("MATCH (a) RETURN a.id, a.name ORDER BY a.id")
# Validate that the expected results are all present in the graph
expected_result = [[0, "a"], [3, "c"], [5, "b"]]
assert query_result.result_set == expected_result
def test_batched_update(self):
"""Validate that updates performed over multiple batches produce the correct results."""
graphname = "batched_update"
prop_str = "Property value to be repeated 100 thousand times generating a multi-megabyte CSV"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file)
for i in range(100_000):
out.writerow([prop_str])
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"CREATE (:L {prop: row[0]})",
"--no-header",
"--max-token-size",
1,
graphname,
],
catch_exceptions=False,
)
assert res.exit_code == 0
assert "Labels added: 1" in res.output
assert "Nodes created: 100000" in res.output
assert "Properties set: 100000" in res.output
tmp_graph = self.redis_con.graph(graphname)
query_result = tmp_graph.query("MATCH (a) RETURN DISTINCT a.prop")
# Validate that the expected results are all present in the graph
expected_result = [[prop_str]]
assert query_result.result_set == expected_result
def test_runtime_error(self):
"""Validate that run-time errors are captured by the bulk updater."""
graphname = "tmpgraph5"
# Write temporary files
with open("/tmp/csv.tmp", mode="w") as csv_file:
out = csv.writer(csv_file)
out.writerow(["a"])
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"MERGE (:L {val: NULL})",
"--no-header",
graphname,
],
)
assert res.exit_code != 0
assert "Cannot merge node" in str(res.exception)
def test_compile_time_error(self):
"""Validate that malformed queries trigger an early exit from the bulk updater."""
graphname = "tmpgraph5"
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/csv.tmp",
"--query",
"CREATE (:L {val: row[0], val2: undefined_identifier})",
"--no-header",
graphname,
],
)
assert res.exit_code != 0
assert "undefined_identifier not defined" in str(res.exception)
def test_invalid_inputs(self):
"""Validate that the bulk updater handles invalid inputs incorrectly."""
graphname = "tmpgraph6"
# Attempt to insert a non-existent CSV file.
runner = CliRunner()
res = runner.invoke(
bulk_update,
[
"--csv",
"/tmp/fake_file.csv",
"--query",
"MERGE (:L {val: NULL})",
graphname,
],
)
assert res.exit_code != 0
assert "No such file" in str(res.exception)