-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtest_cursor.py
388 lines (330 loc) · 11.3 KB
/
test_cursor.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
import pytest
from arango.exceptions import (
CursorCloseError,
CursorCountError,
CursorEmptyError,
CursorNextError,
CursorStateError,
)
from tests.helpers import clean_doc
@pytest.fixture(autouse=True)
def setup_collection(col, docs):
col.import_bulk(docs)
def test_cursor_from_execute_query(db, col, docs):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=2,
ttl=1000,
optimizer_rules=["+all"],
profile=2,
)
cursor_id = cursor.id
assert "Cursor" in repr(cursor)
assert cursor.type == "cursor"
assert cursor.has_more() is True
assert cursor.cached() is False
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 6
assert clean_doc(cursor.batch()) == docs[:2]
statistics = cursor.statistics()
assert statistics["modified"] == 0
assert statistics["filtered"] == 0
assert statistics["ignored"] == 0
assert statistics["execution_time"] > 0
assert "http_requests" in statistics
assert "scanned_full" in statistics
assert "scanned_index" in statistics
assert "nodes" in statistics
assert cursor.warnings() == []
profile = cursor.profile()
assert profile["initializing"] > 0
assert profile["parsing"] > 0
plan = cursor.plan()
expected_keys = {
"nodes",
"rules",
"collections",
"variables",
"estimatedCost",
"estimatedNrItems",
"isModificationQuery",
}
for key in expected_keys:
assert key in plan
assert clean_doc(cursor.next()) == docs[0]
assert cursor.id == cursor_id
assert cursor.has_more() is True
assert cursor.cached() is False
assert cursor.statistics() == statistics
assert cursor.profile() == profile
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 6
assert clean_doc(cursor.batch()) == [docs[1]]
assert clean_doc(cursor.next()) == docs[1]
assert cursor.id == cursor_id
assert cursor.has_more() is True
assert cursor.cached() is False
assert cursor.statistics() == statistics
assert cursor.profile() == profile
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 6
assert clean_doc(cursor.batch()) == []
assert clean_doc(cursor.next()) == docs[2]
assert cursor.id == cursor_id
assert cursor.has_more() is True
assert cursor.cached() is False
assert cursor.statistics() == statistics
assert cursor.profile() == profile
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 6
assert clean_doc(cursor.batch()) == [docs[3]]
assert clean_doc(cursor.next()) == docs[3]
assert clean_doc(cursor.next()) == docs[4]
assert clean_doc(cursor.next()) == docs[5]
assert cursor.id == cursor_id
assert cursor.has_more() is False
assert cursor.statistics() == statistics
assert cursor.profile() == profile
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 6
assert clean_doc(cursor.batch()) == []
with pytest.raises(StopIteration):
cursor.next()
assert cursor.close(ignore_missing=True) is False
def test_cursor_write_query(db, col, docs):
cursor = db.aql.execute(
"""
FOR d IN {col} FILTER d._key == @first OR d._key == @second
UPDATE {{_key: d._key, _val: @val }} IN {col}
RETURN NEW
""".format(
col=col.name
),
bind_vars={"first": "1", "second": "2", "val": 42},
count=True,
batch_size=1,
ttl=1000,
optimizer_rules=["+all"],
profile=1,
max_runtime=0.0,
)
cursor_id = cursor.id
assert "Cursor" in repr(cursor)
assert cursor.has_more() is True
assert cursor.cached() is False
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 2
assert clean_doc(cursor.batch()) == [docs[0]]
statistics = cursor.statistics()
assert statistics["modified"] == 2
assert statistics["filtered"] == 0
assert statistics["ignored"] == 0
assert statistics["execution_time"] > 0
assert "http_requests" in statistics
assert "scanned_full" in statistics
assert "scanned_index" in statistics
assert cursor.warnings() == []
profile = cursor.profile()
assert profile["initializing"] > 0
assert profile["parsing"] > 0
assert clean_doc(cursor.next()) == docs[0]
assert cursor.id == cursor_id
assert cursor.has_more() is True
assert cursor.cached() is False
assert cursor.statistics() == statistics
assert cursor.profile() == profile
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 2
assert clean_doc(cursor.batch()) == []
assert clean_doc(cursor.next()) == docs[1]
assert cursor.id == cursor_id
assert cursor.has_more() is False
assert cursor.cached() is False
assert cursor.statistics() == statistics
assert cursor.profile() == profile
assert cursor.warnings() == []
assert cursor.count() == len(cursor) == 2
assert clean_doc(cursor.batch()) == []
with pytest.raises(CursorCloseError) as err:
cursor.close(ignore_missing=False)
assert err.value.error_code == 1600
assert cursor.close(ignore_missing=True) is False
def test_cursor_invalid_id(db, col):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=2,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
)
# Set the cursor ID to "invalid" and assert errors
setattr(cursor, "_id", "invalid")
with pytest.raises(CursorNextError) as err:
list(cursor)
assert err.value.error_code == 1600
with pytest.raises(CursorCloseError) as err:
cursor.close(ignore_missing=False)
assert err.value.error_code == 1600
assert cursor.close(ignore_missing=True) is False
# Set the cursor ID to None and assert errors
setattr(cursor, "_id", None)
with pytest.raises(CursorStateError) as err:
cursor.next()
assert err.value.message == "cursor ID not set"
with pytest.raises(CursorStateError) as err:
cursor.fetch()
assert err.value.message == "cursor ID not set"
assert cursor.close() is None
def test_cursor_premature_close(db, col, docs):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=2,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
)
assert clean_doc(cursor.batch()) == docs[:2]
assert cursor.close() is True
with pytest.raises(CursorCloseError) as err:
cursor.close(ignore_missing=False)
assert err.value.error_code == 1600
assert cursor.close(ignore_missing=True) is False
def test_cursor_context_manager(db, col, docs):
with db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=2,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
) as cursor:
assert clean_doc(cursor.next()) == docs[0]
with pytest.raises(CursorCloseError) as err:
cursor.close(ignore_missing=False)
assert err.value.error_code == 1600
assert cursor.close(ignore_missing=True) is False
def test_cursor_manual_fetch_and_pop(db, col, docs):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=1,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
)
for size in range(2, 6):
result = cursor.fetch()
assert result["id"] == cursor.id
assert result["count"] == len(docs)
assert result["cached"] == cursor.cached()
assert result["has_more"] == cursor.has_more()
assert result["profile"] == cursor.profile()
assert result["warnings"] == cursor.warnings()
assert result["statistics"] == cursor.statistics()
assert len(result["batch"]) > 0
assert cursor.count() == len(docs)
assert cursor.has_more()
assert len(cursor.batch()) == size
cursor.fetch()
assert len(cursor.batch()) == 6
assert not cursor.has_more()
while not cursor.empty():
cursor.pop()
assert len(cursor.batch()) == 0
with pytest.raises(CursorEmptyError) as err:
cursor.pop()
assert err.value.message == "current batch is empty"
def test_cursor_retry_disabled(db, col, docs):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=1,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
allow_retry=False,
)
result = cursor.fetch()
assert result["id"] == cursor.id
while not cursor.empty():
cursor.pop()
# The next batch ID should have no effect
cursor._next_batch_id = "2"
result = cursor.fetch()
assert result["next_batch_id"] == "4"
doc = cursor.pop()
assert clean_doc(doc) == docs[2]
assert cursor.close(ignore_missing=True)
def test_cursor_retry(db, col, docs, db_version):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=True,
batch_size=1,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
allow_retry=True,
)
assert cursor.count() == len(docs)
doc = cursor.pop()
assert clean_doc(doc) == docs[0]
assert cursor.has_more()
result = cursor.fetch()
assert result["id"] == cursor.id
assert result["next_batch_id"] == "3"
doc = cursor.pop()
assert clean_doc(doc) == docs[1]
assert cursor.empty()
# Decrease the next batch ID as if the previous fetch failed
cursor._next_batch_id = "2"
result = cursor.fetch()
assert result["id"] == cursor.id
assert result["next_batch_id"] == "3"
doc = cursor.pop()
assert clean_doc(doc) == docs[1]
assert cursor.empty()
# Fetch the next batches normally
for batch in range(2, 5):
result = cursor.fetch()
assert result["id"] == cursor.id
assert result["next_batch_id"] == str(batch + 2)
doc = cursor.pop()
assert clean_doc(doc) == docs[batch]
result = cursor.fetch()
assert not cursor.has_more()
assert "id" not in result
assert "next_batch_id" not in result
doc = cursor.pop()
assert clean_doc(doc) == docs[-1]
# We should be able to fetch the last batch again
cursor.fetch()
doc = cursor.pop()
assert clean_doc(doc) == docs[-1]
assert cursor.close()
def test_cursor_no_count(db, col):
cursor = db.aql.execute(
f"FOR d IN {col.name} SORT d._key RETURN d",
count=False,
batch_size=2,
ttl=1000,
optimizer_rules=["+all"],
profile=True,
)
with pytest.raises(CursorCountError) as err:
_ = len(cursor)
assert err.value.message == "cursor count not enabled"
with pytest.raises(CursorCountError) as err:
_ = bool(cursor)
assert err.value.message == "cursor count not enabled"
while cursor.has_more():
assert cursor.count() is None
with pytest.raises(CursorCountError) as err:
_ = len(cursor)
assert err.value.message == "cursor count not enabled"
with pytest.raises(CursorCountError) as err:
_ = bool(cursor)
assert err.value.message == "cursor count not enabled"
assert cursor.fetch()