-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtest_aql.py
485 lines (413 loc) · 15.9 KB
/
test_aql.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import pytest
from packaging import version
from arango.errno import FORBIDDEN
from arango.exceptions import (
AQLCacheClearError,
AQLCacheConfigureError,
AQLCacheEntriesError,
AQLCachePropertiesError,
AQLFunctionCreateError,
AQLFunctionDeleteError,
AQLFunctionListError,
AQLQueryClearError,
AQLQueryExecuteError,
AQLQueryExplainError,
AQLQueryKillError,
AQLQueryListError,
AQLQueryTrackingGetError,
AQLQueryTrackingSetError,
AQLQueryValidateError,
)
from tests.helpers import assert_raises, extract, generate_col_name
def test_aql_attributes(db, username):
assert db.context in ["default", "async", "batch", "transaction"]
assert db.username == username
assert db.db_name == db.name
assert repr(db.aql) == f"<AQL in {db.name}>"
assert repr(db.aql.cache) == f"<AQLQueryCache in {db.name}>"
def test_aql_query_management(db_version, db, bad_db, col, docs):
explain_fields = [
"estimatedNrItems",
"estimatedCost",
"rules",
"variables",
"collections",
"stats",
]
stats_fields = {
"0.0.0": [
"rulesExecuted",
"rulesSkipped",
"plansCreated",
],
"3.10.4": [
"peakMemoryUsage",
"executionTime",
],
}
# Test explain invalid query
with assert_raises(AQLQueryExplainError) as err:
db.aql.explain("INVALID QUERY")
assert err.value.error_code == 1501
# Test explain valid query with all_plans set to False
explain = db.aql.explain(
f"FOR d IN {col.name} RETURN d",
all_plans=False,
opt_rules=["-all", "+use-index-range"],
)
assert all(field in explain for field in explain_fields)
for v, fields in stats_fields.items():
if db_version >= version.parse(v):
assert all(field in explain["stats"] for field in fields)
else:
assert all(field not in explain["stats"] for field in fields)
# Test explain valid query with all_plans set to True
explanations = db.aql.explain(
f"FOR d IN {col.name} RETURN d",
all_plans=True,
opt_rules=["-all", "+use-index-range"],
max_plans=10,
)
for explain in explanations:
assert all(field in explain for field in explain_fields)
for v, fields in stats_fields.items():
if db_version >= version.parse(v):
assert all(field in explain["stats"] for field in fields)
else:
assert all(field not in explain["stats"] for field in fields)
assert len(explanations) < 10
# Test validate invalid query
with assert_raises(AQLQueryValidateError) as err:
db.aql.validate("INVALID QUERY")
assert err.value.error_code == 1501
# Test validate valid query
result = db.aql.validate(f"FOR d IN {col.name} RETURN d")
assert "ast" in result
assert "bind_vars" in result
assert "collections" in result
assert "parsed" in result
# Test execute invalid AQL query
with assert_raises(AQLQueryExecuteError) as err:
db.aql.execute("INVALID QUERY")
assert err.value.error_code == 1501
# Test execute valid query
db.collection(col.name).import_bulk(docs)
cursor = db.aql.execute(
"""
FOR d IN {col}
UPDATE {{_key: d._key, _val: @val }} IN {col}
RETURN NEW
""".format(
col=col.name
),
count=True,
# batch_size=1,
ttl=10,
bind_vars={"val": 42},
full_count=True,
max_plans=1000,
optimizer_rules=["+all"],
cache=True,
memory_limit=1000000,
fail_on_warning=False,
profile=True,
max_transaction_size=100000,
max_warning_count=10,
intermediate_commit_count=1,
intermediate_commit_size=1000,
satellite_sync_wait=False,
stream=False,
skip_inaccessible_cols=True,
max_runtime=0.0,
)
assert cursor.id is None
assert cursor.type == "cursor"
assert cursor.batch() is not None
assert cursor.has_more() is False
assert cursor.count() == len(col)
assert cursor.cached() is False
assert cursor.statistics() is not None
assert cursor.profile() is not None
assert cursor.warnings() == []
assert extract("_key", cursor) == extract("_key", docs)
assert cursor.close(ignore_missing=True) is None
# Test get tracking properties with bad database
with assert_raises(AQLQueryTrackingGetError) as err:
bad_db.aql.tracking()
assert err.value.error_code in {11, 1228}
# Test get tracking properties
tracking = db.aql.tracking()
assert isinstance(tracking["enabled"], bool)
assert isinstance(tracking["max_query_string_length"], int)
assert isinstance(tracking["max_slow_queries"], int)
assert isinstance(tracking["slow_query_threshold"], int)
assert isinstance(tracking["track_bind_vars"], bool)
assert isinstance(tracking["track_slow_queries"], bool)
# Test set tracking properties with bad database
with assert_raises(AQLQueryTrackingSetError) as err:
bad_db.aql.set_tracking(enabled=not tracking["enabled"])
assert err.value.error_code in {11, 1228}
assert db.aql.tracking()["enabled"] == tracking["enabled"]
# Test set tracking properties
new_tracking = db.aql.set_tracking(
enabled=not tracking["enabled"],
max_query_string_length=4000,
max_slow_queries=60,
slow_query_threshold=15,
track_bind_vars=not tracking["track_bind_vars"],
track_slow_queries=not tracking["track_slow_queries"],
)
assert new_tracking["enabled"] != tracking["enabled"]
assert new_tracking["max_query_string_length"] == 4000
assert new_tracking["max_slow_queries"] == 60
assert new_tracking["slow_query_threshold"] == 15
assert new_tracking["track_bind_vars"] != tracking["track_bind_vars"]
assert new_tracking["track_slow_queries"] != tracking["track_slow_queries"]
# Make sure to revert the properties
new_tracking = db.aql.set_tracking(
enabled=True, track_bind_vars=True, track_slow_queries=True
)
assert new_tracking["enabled"] is True
assert new_tracking["track_bind_vars"] is True
assert new_tracking["track_slow_queries"] is True
# Kick off some long-lasting queries in the background
db.begin_async_execution().aql.execute("RETURN SLEEP(100)")
db.begin_async_execution().aql.execute("RETURN SLEEP(50)")
# Test list queries
queries = db.aql.queries()
for query in queries:
assert "id" in query
assert "query" in query
assert "started" in query
assert "state" in query
assert "bind_vars" in query
assert "runtime" in query
assert "peak_memory_usage" in query
assert len(queries) == 2
# Test list queries with bad database
with assert_raises(AQLQueryListError) as err:
bad_db.aql.queries()
assert err.value.error_code in {11, 1228}
# Test kill queries
query_id_1, query_id_2 = extract("id", queries)
assert db.aql.kill(query_id_1) is True
while len(queries) > 1:
queries = db.aql.queries()
assert query_id_1 not in extract("id", queries)
assert db.aql.kill(query_id_2) is True
while len(queries) > 0:
queries = db.aql.queries()
assert query_id_2 not in extract("id", queries)
# Test kill missing queries
with assert_raises(AQLQueryKillError) as err:
db.aql.kill(query_id_1)
assert err.value.error_code == 1591
with assert_raises(AQLQueryKillError) as err:
db.aql.kill(query_id_2)
assert err.value.error_code == 1591
# Test list slow queries
assert db.aql.slow_queries() == []
# Test list slow queries with bad database
with assert_raises(AQLQueryListError) as err:
bad_db.aql.slow_queries()
assert err.value.error_code in {11, 1228}
# Test clear slow queries
assert db.aql.clear_slow_queries() is True
# Test clear slow queries with bad database
with assert_raises(AQLQueryClearError) as err:
bad_db.aql.clear_slow_queries()
assert err.value.error_code in {11, 1228}
def test_aql_query_force_one_shard_attribute_value(db, db_version, enterprise, cluster):
if not enterprise or not cluster:
return
name = generate_col_name()
col = db.create_collection(name, shard_fields=["foo"], shard_count=3)
doc = {"foo": "bar"}
col.insert(doc)
cursor = db.aql.execute(
"FOR d IN @@c RETURN d",
bind_vars={"@c": name},
force_one_shard_attribute_value="bar",
)
results = [doc for doc in cursor]
assert len(results) == 1
assert results[0]["foo"] == "bar"
cursor = db.aql.execute(
"FOR d IN @@c RETURN d",
bind_vars={"@c": name},
force_one_shard_attribute_value="ooo",
)
results = [doc for doc in cursor]
assert len(results) == 0
def test_aql_function_management(db, bad_db):
fn_group = "functions::temperature"
fn_name_1 = "functions::temperature::celsius_to_fahrenheit"
fn_body_1 = "function (celsius) { return celsius * 1.8 + 32; }"
fn_name_2 = "functions::temperature::fahrenheit_to_celsius"
fn_body_2 = "function (fahrenheit) { return (fahrenheit - 32) / 1.8; }"
bad_fn_name = "functions::temperature::should_not_exist"
bad_fn_body = "function (celsius) { invalid syntax }"
# Test list AQL functions
assert db.aql.functions() == []
# Test list AQL functions with bad database
with assert_raises(AQLFunctionListError) as err:
bad_db.aql.functions()
assert err.value.error_code in {11, 1228}
# Test create invalid AQL function
with assert_raises(AQLFunctionCreateError) as err:
db.aql.create_function(bad_fn_name, bad_fn_body)
assert err.value.error_code == 1581
# Test create AQL function one
assert db.aql.create_function(fn_name_1, fn_body_1) == {"is_new": True}
functions = db.aql.functions()
assert len(functions) == 1
assert functions[0]["name"] == fn_name_1
assert functions[0]["code"] == fn_body_1
assert "is_deterministic" in functions[0]
# Test create AQL function one again (idempotency)
assert db.aql.create_function(fn_name_1, fn_body_1) == {"is_new": False}
functions = db.aql.functions()
assert len(functions) == 1
assert functions[0]["name"] == fn_name_1
assert functions[0]["code"] == fn_body_1
assert "is_deterministic" in functions[0]
# Test create AQL function two
assert db.aql.create_function(fn_name_2, fn_body_2) == {"is_new": True}
functions = sorted(db.aql.functions(), key=lambda x: x["name"])
assert len(functions) == 2
assert functions[0]["name"] == fn_name_1
assert functions[0]["code"] == fn_body_1
assert functions[1]["name"] == fn_name_2
assert functions[1]["code"] == fn_body_2
assert "is_deterministic" in functions[0]
assert "is_deterministic" in functions[1]
# Test delete AQL function one
assert db.aql.delete_function(fn_name_1) == {"deleted": 1}
functions = db.aql.functions()
assert len(functions) == 1
assert functions[0]["name"] == fn_name_2
assert functions[0]["code"] == fn_body_2
# Test delete missing AQL function
with assert_raises(AQLFunctionDeleteError) as err:
db.aql.delete_function(fn_name_1)
assert err.value.error_code == 1582
assert db.aql.delete_function(fn_name_1, ignore_missing=True) is False
functions = db.aql.functions()
assert len(functions) == 1
assert functions[0]["name"] == fn_name_2
assert functions[0]["code"] == fn_body_2
# Test delete AQL function group
assert db.aql.delete_function(fn_group, group=True) == {"deleted": 1}
assert db.aql.functions() == []
def test_cache_results_management(db, bad_db, col, docs, cluster):
if cluster:
pytest.skip("Cluster mode does not support query result cache management")
aql = db.aql
cache = aql.cache
# Sanity check, just see if the response is OK.
_ = cache.properties()
with pytest.raises(AQLCachePropertiesError) as err:
_ = bad_db.aql.cache.properties()
assert err.value.error_code == FORBIDDEN
# Turn on caching
result = cache.configure(mode="on")
assert result["mode"] == "on"
result = cache.properties()
assert result["mode"] == "on"
with pytest.raises(AQLCacheConfigureError) as err:
_ = bad_db.aql.cache.configure(mode="on")
assert err.value.error_code == FORBIDDEN
# Run a simple query to use the cache
col.insert(docs[0])
_ = aql.execute(
query="FOR doc IN @@collection RETURN doc",
bind_vars={"@collection": col.name},
cache=True,
)
# Check the entries
entries = cache.entries()
assert isinstance(entries, list)
assert len(entries) > 0
with pytest.raises(AQLCacheEntriesError) as err:
_ = bad_db.aql.cache.entries()
assert err.value.error_code == FORBIDDEN
# Clear the cache
cache.clear()
entries = cache.entries()
assert len(entries) == 0
with pytest.raises(AQLCacheClearError) as err:
bad_db.aql.cache.clear()
assert err.value.error_code == FORBIDDEN
def test_cache_plan_management(db, bad_db, col, docs, db_version):
if db_version < version.parse("3.12.4"):
pytest.skip("Query plan cache is supported in ArangoDB 3.12.4+")
aql = db.aql
cache = aql.cache
# Run a simple query to use the cache
col.insert(docs[0])
_ = aql.execute(
query="FOR doc IN @@collection RETURN doc",
bind_vars={"@collection": col.name},
use_plan_cache=True,
)
# Check the entries
entries = cache.plan_entries()
assert isinstance(entries, list)
assert len(entries) > 0
with pytest.raises(AQLCacheEntriesError) as err:
_ = bad_db.aql.cache.plan_entries()
assert err.value.error_code == FORBIDDEN
# Clear the cache
cache.clear_plan()
entries = cache.plan_entries()
assert len(entries) == 0
with pytest.raises(AQLCacheClearError) as err:
bad_db.aql.cache.clear_plan()
assert err.value.error_code == FORBIDDEN
def test_aql_cache_management(db, bad_db):
# Test get AQL cache properties
properties = db.aql.cache.properties()
assert "mode" in properties
assert "max_results" in properties
assert "max_results_size" in properties
assert "max_entry_size" in properties
assert "include_system" in properties
# Test get AQL cache properties with bad database
with assert_raises(AQLCachePropertiesError):
bad_db.aql.cache.properties()
# Test get AQL cache configure properties
properties = db.aql.cache.configure(
mode="on",
max_results=100,
max_results_size=10000,
max_entry_size=10000,
include_system=True,
)
assert properties["mode"] == "on"
assert properties["max_results"] == 100
assert properties["max_results_size"] == 10000
assert properties["max_entry_size"] == 10000
assert properties["include_system"] is True
properties = db.aql.cache.properties()
assert properties["mode"] == "on"
assert properties["max_results"] == 100
assert properties["max_results_size"] == 10000
assert properties["max_entry_size"] == 10000
assert properties["include_system"] is True
# Test get AQL cache configure properties with bad database
with assert_raises(AQLCacheConfigureError):
bad_db.aql.cache.configure(mode="on")
# Test get AQL cache entries
result = db.aql.cache.entries()
assert isinstance(result, list)
# Test get AQL cache entries with bad database
with assert_raises(AQLCacheEntriesError) as err:
bad_db.aql.cache.entries()
assert err.value.error_code in {11, 1228}
# Test get AQL cache clear
result = db.aql.cache.clear()
assert isinstance(result, bool)
# Test get AQL cache clear with bad database
with assert_raises(AQLCacheClearError) as err:
bad_db.aql.cache.clear()
assert err.value.error_code in {11, 1228}