-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathtest_index.py
394 lines (323 loc) · 10.7 KB
/
test_index.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
import pytest
from packaging import version
from arango.exceptions import (
IndexCreateError,
IndexDeleteError,
IndexGetError,
IndexListError,
IndexLoadError,
)
from tests.helpers import assert_raises, extract
def test_list_indexes(icol, bad_col):
indexes = icol.indexes()
assert isinstance(indexes, list)
assert len(indexes) > 0
assert "id" in indexes[0]
assert "type" in indexes[0]
assert "fields" in indexes[0]
assert "selectivity" in indexes[0]
assert "sparse" in indexes[0]
assert "unique" in indexes[0]
with assert_raises(IndexListError) as err:
bad_col.indexes()
assert err.value.error_code in {11, 1228}
def test_get_index(icol, bad_col):
indexes = icol.indexes()
for index in indexes:
retrieved_index = icol.get_index(index["id"])
assert retrieved_index["id"] == index["id"]
assert retrieved_index["name"] == index["name"]
assert retrieved_index["type"] == index["type"]
assert retrieved_index["fields"] == index["fields"]
assert retrieved_index["sparse"] == index["sparse"]
assert retrieved_index["unique"] == index["unique"]
# TODO: Revisit
# assert retrieved_index["selectivity"] == index["selectivity"]
with assert_raises(IndexGetError) as err:
icol.get_index("bad_index")
assert err.value.error_code == 1212
def test_add_hash_index(icol):
icol = icol
fields = ["attr1", "attr2"]
result = icol.add_index(
{
"type": "hash",
"fields": fields,
"unique": True,
"sparse": True,
"deduplicate": True,
"name": "hash_index",
"inBackground": False,
}
)
expected_index = {
"sparse": True,
"type": "hash",
"fields": fields,
"unique": True,
"deduplicate": True,
"name": "hash_index",
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
# Clean up the index
icol.delete_index(result["id"])
def test_add_skiplist_index(icol):
fields = ["attr1", "attr2"]
result = icol.add_index(
{
"type": "skiplist",
"fields": fields,
"unique": True,
"sparse": True,
"deduplicate": True,
"name": "skiplist_index",
"inBackground": False,
}
)
expected_index = {
"sparse": True,
"type": "skiplist",
"fields": ["attr1", "attr2"],
"unique": True,
"deduplicate": True,
"name": "skiplist_index",
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
# Clean up the index
icol.delete_index(result["id"])
def test_add_geo_index(icol):
# Test add geo index with one attribute
result = icol.add_index(
{
"type": "geo",
"fields": ["attr1"],
"geoJson": True,
"name": "geo_index",
"inBackground": True,
}
)
expected_index = {
"sparse": True,
"type": "geo",
"fields": ["attr1"],
"unique": False,
"geoJson": True,
"name": "geo_index",
}
for key, value in expected_index.items():
assert result[key] == value, (key, value, result[key])
assert result["id"] in extract("id", icol.indexes())
# Test add geo index with two attributes
result = icol.add_index(
{
"type": "geo",
"fields": ["attr1", "attr2"],
"geoJson": False,
}
)
expected_index = {
"sparse": True,
"type": "geo",
"fields": ["attr1", "attr2"],
"unique": False,
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
# Test add geo index with more than two attributes (should fail)
with assert_raises(IndexCreateError) as err:
icol.add_index({"type": "geo", "fields": ["attr1", "attr2", "attr3"]})
assert err.value.error_code == 10
# Clean up the index
icol.delete_index(result["id"])
def test_add_fulltext_index(icol):
# Test add fulltext index with one attributes
result = icol.add_index(
{
"type": "fulltext",
"fields": ["attr1"],
"minLength": 10,
"name": "fulltext_index",
"inBackground": True,
}
)
expected_index = {
"sparse": True,
"type": "fulltext",
"fields": ["attr1"],
"minLength": 10,
"unique": False,
"name": "fulltext_index",
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
# Test add fulltext index with two attributes (should fail)
with assert_raises(IndexCreateError) as err:
icol.add_index({"type": "fulltext", "fields": ["attr1", "attr2"]})
assert err.value.error_code == 10
# Clean up the index
icol.delete_index(result["id"])
def test_add_persistent_index(icol):
# Test add persistent index with two attributes
result = icol.add_index(
{
"type": "persistent",
"fields": ["attr1", "attr2"],
"unique": True,
"sparse": True,
"name": "persistent_index",
"inBackground": True,
}
)
expected_index = {
"sparse": True,
"type": "persistent",
"fields": ["attr1", "attr2"],
"unique": True,
"name": "persistent_index",
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
# Clean up the index
icol.delete_index(result["id"])
def test_add_ttl_index(icol):
# Test add persistent index with two attributes
result = icol.add_index(
{
"type": "ttl",
"fields": ["attr1"],
"expireAfter": 1000,
"name": "ttl_index",
"inBackground": True,
}
)
expected_index = {
"type": "ttl",
"fields": ["attr1"],
"expireAfter": 1000,
"name": "ttl_index",
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
# Clean up the index
icol.delete_index(result["id"])
def test_add_inverted_index(icol, enterprise):
parameters = dict(
fields=[{"name": "attr1", "cache": True}],
name="c0_cached",
storedValues=[{"fields": ["a"], "compression": "lz4", "cache": True}],
includeAllFields=True,
analyzer="identity",
primarySort={"cache": True, "fields": [{"field": "a", "direction": "asc"}]},
)
expected_keys = ["primarySort", "analyzer", "includeAllFields", "searchField"]
if enterprise:
parameters["cache"] = True
parameters["primaryKeyCache"] = True
expected_keys.extend(["cache", "primaryKeyCache"])
result = icol.add_index({"type": "inverted", **parameters})
assert result["id"] in extract("id", icol.indexes())
for key in expected_keys:
assert key in result
icol.delete_index(result["id"])
def test_add_zkd_index(icol, db_version):
result = icol.add_index(
{
"type": "zkd",
"fields": ["x", "y", "z"],
"fieldValueTypes": "double",
"name": "zkd_index",
"inBackground": False,
"unique": False,
}
)
expected_index = {
"name": "zkd_index",
"type": "zkd",
"fields": ["x", "y", "z"],
"isNewlyCreated": True,
"unique": False,
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
with assert_raises(IndexCreateError) as err:
icol.add_index(
{"type": "zkd", "fieldValueTypes": "integer", "fields": ["x", "y", "z"]}
)
assert err.value.error_code == 10
icol.delete_index(result["id"])
def test_add_mdi_index(icol, db_version):
if db_version < version.parse("3.12.0"):
pytest.skip("MDI indexes are usable with 3.12+ only")
result = icol.add_index(
{
"type": "mdi",
"fields": ["x", "y", "z"],
"fieldValueTypes": "double",
"name": "mdi_index",
"inBackground": False,
"unique": True,
}
)
expected_index = {
"name": "mdi_index",
"type": "mdi",
"fields": ["x", "y", "z"],
"isNewlyCreated": True,
"unique": True,
}
for key, value in expected_index.items():
assert result[key] == value
assert result["id"] in extract("id", icol.indexes())
with assert_raises(IndexCreateError) as err:
icol.add_index(
{
"type": "mdi",
"fieldValueTypes": "integer",
"fields": ["x", "y", "z"],
}
)
assert err.value.error_code == 10
icol.delete_index(result["id"])
def test_delete_index(icol, bad_col):
old_indexes = set(extract("id", icol.indexes()))
hash_index = {"type": "hash", "fields": ["attr1", "attr2"], "unique": True}
icol.add_index(hash_index)
skiplist_Index = {"type": "skiplist", "fields": ["attr3", "attr4"], "unique": True}
icol.add_index(skiplist_Index)
fulltext_index = {"type": "fulltext", "fields": ["attr5"], "min_length": 10}
icol.add_index(fulltext_index)
new_indexes = set(extract("id", icol.indexes()))
assert new_indexes.issuperset(old_indexes)
indexes_to_delete = new_indexes - old_indexes
for index_id in indexes_to_delete:
assert icol.delete_index(index_id) is True
new_indexes = set(extract("id", icol.indexes()))
assert new_indexes == old_indexes
# Test delete missing indexes
for index_id in indexes_to_delete:
assert icol.delete_index(index_id, ignore_missing=True) is False
for index_id in indexes_to_delete:
with assert_raises(IndexDeleteError) as err:
icol.delete_index(index_id, ignore_missing=False)
assert err.value.error_code == 1212
# Test delete indexes with bad collection
for index_id in indexes_to_delete:
with assert_raises(IndexDeleteError) as err:
bad_col.delete_index(index_id, ignore_missing=False)
assert err.value.error_code in {11, 1228}
def test_load_indexes(icol, bad_col):
# Test load indexes
assert icol.load_indexes() is True
# Test load indexes with bad collection
with assert_raises(IndexLoadError) as err:
bad_col.load_indexes()
assert err.value.error_code in {11, 1228}