Passed
Push — master ( b86822...b6e78f )
by
unknown
02:07
created

TestInsertInvalid.get_field_int_value()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import logging
2
import time
3
import pdb
4
import copy
5
import threading
6
from multiprocessing import Pool, Process
7
import pytest
8
from milvus import DataType
9
from utils import *
10
from constants import *
11
12
ADD_TIMEOUT = 60
13
uid = "test_insert"
14
field_name = default_float_vec_field_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_float_vec_field_name does not seem to be defined.
Loading history...
15
binary_field_name = default_binary_vec_field_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_binary_vec_field_name does not seem to be defined.
Loading history...
16
default_single_query = {
17
    "bool": {
18
        "must": [
19
            {"vector": {field_name: {"topk": 10, "query": gen_vectors(1, default_dim), "metric_type": "L2",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_dim does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gen_vectors does not seem to be defined.
Loading history...
20
                                     "params": {"nprobe": 10}}}}
21
        ]
22
    }
23
}
24
25
26
class TestInsertBase:
27
    """
28
    ******************************************************************
29
      The following cases are used to test `insert` function
30
    ******************************************************************
31
    """
32
33
    @pytest.fixture(
34
        scope="function",
35
        params=gen_simple_index()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_simple_index does not seem to be defined.
Loading history...
36
    )
37
    def get_simple_index(self, request, connect):
38
        if str(connect._cmd("mode")) == "CPU":
39
            if request.param["index_type"] in index_cpu_not_support():
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable index_cpu_not_support does not seem to be defined.
Loading history...
40
                pytest.skip("CPU not support index_type: ivf_sq8h")
41
        return request.param
42
43
    @pytest.fixture(
44
        scope="function",
45
        params=gen_single_filter_fields()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_single_filter_fields does not seem to be defined.
Loading history...
46
    )
47
    def get_filter_field(self, request):
48
        yield request.param
49
50
    @pytest.fixture(
51
        scope="function",
52
        params=gen_single_vector_fields()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_single_vector_fields does not seem to be defined.
Loading history...
53
    )
54
    def get_vector_field(self, request):
55
        yield request.param
56
57
    def test_add_vector_with_empty_vector(self, connect, collection):
58
        '''
59
        target: test add vectors with empty vectors list
60
        method: set empty vectors list as add method params
61
        expected: raises a Exception
62
        '''
63
        vector = []
64
        with pytest.raises(Exception) as e:
65
            status, ids = connect.insert(collection, vector)
66
67
    def test_add_vector_with_None(self, connect, collection):
68
        '''
69
        target: test add vectors with None
70
        method: set None as add method params
71
        expected: raises a Exception
72
        '''
73
        vector = None
74
        with pytest.raises(Exception) as e:
75
            status, ids = connect.insert(collection, vector)
76
77
    @pytest.mark.timeout(ADD_TIMEOUT)
78
    def test_insert_collection_not_existed(self, connect):
79
        '''
80
        target: test insert, with collection not existed
81
        method: insert entity into a random named collection
82
        expected: error raised 
83
        '''
84
        collection_name = gen_unique_str(uid)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_unique_str does not seem to be defined.
Loading history...
85
        with pytest.raises(Exception) as e:
86
            connect.insert(collection_name, default_entities_rows)
87
88
    @pytest.mark.timeout(ADD_TIMEOUT)
89
    def test_insert_drop_collection(self, connect, collection):
90
        '''
91
        target: test delete collection after insert vector
92
        method: insert vector and delete collection
93
        expected: no error raised
94
        '''
95
        ids = connect.insert(collection, default_entity_row)
96
        assert len(ids) == 1
97
        connect.drop_collection(collection)
98
99
    @pytest.mark.timeout(ADD_TIMEOUT)
100
    def test_insert_sleep_drop_collection(self, connect, collection):
101
        '''
102
        target: test delete collection after insert vector for a while
103
        method: insert vector, sleep, and delete collection
104
        expected: no error raised 
105
        '''
106
        ids = connect.insert(collection, default_entity_row)
107
        assert len(ids) == 1
108
        connect.flush([collection])
109
        connect.drop_collection(collection)
110
111
    @pytest.mark.timeout(ADD_TIMEOUT)
112
    def test_insert_create_index(self, connect, collection, get_simple_index):
113
        '''
114
        target: test build index insert after vector
115
        method: insert vector and build index
116
        expected: no error raised
117
        '''
118
        ids = connect.insert(collection, default_entities_rows)
119
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
120
        connect.flush([collection])
121
        connect.create_index(collection, field_name, get_simple_index)
122
        info = connect.get_collection_info(collection)
123
        fields = info["fields"]
124
        for field in fields:
125
            if field["name"] == field_name:
126
                assert field["indexes"][0] == get_simple_index
127
128 View Code Duplication
    @pytest.mark.timeout(ADD_TIMEOUT)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
129
    def test_insert_after_create_index(self, connect, collection, get_simple_index):
130
        '''
131
        target: test build index insert after vector
132
        method: insert vector and build index
133
        expected: no error raised
134
        '''
135
        connect.create_index(collection, field_name, get_simple_index)
136
        ids = connect.insert(collection, default_entities_rows)
137
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
138
        info = connect.get_collection_info(collection)
139
        fields = info["fields"]
140
        for field in fields:
141
            if field["name"] == field_name:
142
                assert field["indexes"][0] == get_simple_index
143
144
    @pytest.mark.timeout(ADD_TIMEOUT)
145
    def test_insert_search(self, connect, collection):
146
        '''
147
        target: test search vector after insert vector after a while
148
        method: insert vector, sleep, and search collection
149
        expected: no error raised 
150
        '''
151
        ids = connect.insert(collection, default_entities_rows)
152
        connect.flush([collection])
153
        res = connect.search(collection, default_single_query)
154
        logging.getLogger().debug(res)
155
        assert res
156
157
    def test_insert_segment_row_count(self, connect, collection):
158
        nb = default_segment_row_limit + 1
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_segment_row_limit does not seem to be defined.
Loading history...
159
        res_ids = connect.insert(collection, gen_entities_rows(nb))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_entities_rows does not seem to be defined.
Loading history...
160
        connect.flush([collection])
161
        assert len(res_ids) == nb
162
        stats = connect.get_collection_stats(collection)
163
        assert len(stats['partitions'][0]['segments']) == 2
164
        for segment in stats['partitions'][0]['segments']:
165
            assert segment['row_count'] in [default_segment_row_limit, 1]
166
167
    @pytest.fixture(
168
        scope="function",
169
        params=[
170
            1,
171
            2000
172
        ],
173
    )
174
    def insert_count(self, request):
175
        yield request.param
176
177
    @pytest.mark.timeout(ADD_TIMEOUT)
178
    def test_insert_ids_not_match(self, connect, id_collection, insert_count):
179
        '''
180
        target: test insert vectors in collection, use customize ids
181
        method: create collection and insert vectors in it, check the ids returned and the collection length after vectors inserted
182
        expected: the length of ids and the collection row count
183
        '''
184
        nb = insert_count
185
        with pytest.raises(Exception) as e:
186
            res_ids = connect.insert(id_collection, gen_entities_rows(nb))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_entities_rows does not seem to be defined.
Loading history...
187
188
    @pytest.mark.timeout(ADD_TIMEOUT)
189
    def test_insert_twice_ids_no_ids(self, connect, collection):
190
        '''
191
        target: check the result of insert, with params ids and no ids
192
        method: test insert vectors twice, use customize ids first, and then use no ids
193
        expected:  error raised
194
        '''
195
        with pytest.raises(Exception) as e:
196
            res_ids = connect.insert(collection, gen_entities_rows(default_nb, _id=False))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_entities_rows does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
197
198
    @pytest.mark.timeout(ADD_TIMEOUT)
199
    def test_insert_tag(self, connect, collection):
200
        '''
201
        target: test insert entities in collection created before
202
        method: create collection and insert entities in it, with the partition_tag param
203
        expected: the collection row count equals to nq
204
        '''
205
        connect.create_partition(collection, default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_tag does not seem to be defined.
Loading history...
206
        ids = connect.insert(collection, default_entities_rows, partition_tag=default_tag)
207
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
208
        assert connect.has_partition(collection, default_tag)
209
210
    @pytest.mark.timeout(ADD_TIMEOUT)
211
    def test_insert_tag_with_ids(self, connect, id_collection):
212
        '''
213
        target: test insert entities in collection created before, insert with ids
214
        method: create collection and insert entities in it, with the partition_tag param
215
        expected: the collection row count equals to nq
216
        '''
217
        connect.create_partition(id_collection, default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_tag does not seem to be defined.
Loading history...
218
        ids = [i for i in range(default_nb)]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
219
        res_ids = connect.insert(id_collection, gen_entities_rows(default_nb, _id=False), partition_tag=default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_entities_rows does not seem to be defined.
Loading history...
220
        assert res_ids == ids
221
222
    @pytest.mark.timeout(ADD_TIMEOUT)
223
    def test_insert_tag_not_existed(self, connect, collection):
224
        '''
225
        target: test insert entities in collection created before
226
        method: create collection and insert entities in it, with the not existed partition_tag param
227
        expected: error raised
228
        '''
229
        tag = gen_unique_str()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_unique_str does not seem to be defined.
Loading history...
230
        with pytest.raises(Exception) as e:
231
            ids = connect.insert(collection, default_entities_rows, partition_tag=tag)
232
233
    @pytest.mark.timeout(ADD_TIMEOUT)
234
    def test_insert_tag_existed(self, connect, collection):
235
        '''
236
        target: test insert entities in collection created before
237
        method: create collection and insert entities in it repeatly, with the partition_tag param
238
        expected: the collection row count equals to nq
239
        '''
240
        connect.create_partition(collection, default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_tag does not seem to be defined.
Loading history...
241
        ids = connect.insert(collection, default_entities_rows, partition_tag=default_tag)
242
        ids = connect.insert(collection, default_entities_rows, partition_tag=default_tag)
243
        connect.flush([collection])
244
        res_count = connect.count_entities(collection)
245
        assert res_count == 2 * default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
246
247
    @pytest.mark.level(2)
248
    def test_insert_collection_not_existed(self, connect):
249
        '''
250
        target: test insert entities in collection, which not existed before
251
        method: insert entities collection not existed, check the status
252
        expected: error raised
253
        '''
254
        with pytest.raises(Exception) as e:
255
            ids = connect.insert(gen_unique_str("not_exist_collection"), default_entities_rows)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_unique_str does not seem to be defined.
Loading history...
256
257
    def test_insert_dim_not_matched(self, connect, collection):
258
        '''
259
        target: test insert entities, the vector dimension is not equal to the collection dimension
260
        method: the entities dimension is half of the collection dimension, check the status
261
        expected: error raised
262
        '''
263
        vectors = gen_vectors(default_nb, int(default_dim) // 2)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gen_vectors does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable default_dim does not seem to be defined.
Loading history...
264
        insert_entities = copy.deepcopy(default_entities_rows)
265
        insert_entities[-1][default_float_vec_field_name] = vectors
266
        with pytest.raises(Exception) as e:
267
            ids = connect.insert(collection, insert_entities)
268
269
270
class TestInsertBinary:
271
    @pytest.fixture(
272
        scope="function",
273
        params=gen_binary_index()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_binary_index does not seem to be defined.
Loading history...
274
    )
275
    def get_binary_index(self, request):
276
        request.param["metric_type"] = "JACCARD"
277
        return request.param
278
279
    def test_insert_binary_entities(self, connect, binary_collection):
280
        '''
281
        target: test insert entities in binary collection
282
        method: create collection and insert binary entities in it
283
        expected: the collection row count equals to nb
284
        '''
285
        ids = connect.insert(binary_collection, default_binary_entities_rows)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_binary_entities_rows does not seem to be defined.
Loading history...
286
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
287
        connect.flush()
288
        assert connect.count_entities(binary_collection) == default_nb
289
290
    def test_insert_binary_tag(self, connect, binary_collection):
291
        '''
292
        target: test insert entities and create partition tag
293
        method: create collection and insert binary entities in it, with the partition_tag param
294
        expected: the collection row count equals to nb
295
        '''
296
        connect.create_partition(binary_collection, default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_tag does not seem to be defined.
Loading history...
297
        ids = connect.insert(binary_collection, default_binary_entities_rows, partition_tag=default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_binary_entities_rows does not seem to be defined.
Loading history...
298
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
299
        assert connect.has_partition(binary_collection, default_tag)
300
301
    # TODO
302
    @pytest.mark.level(2)
303
    def test_insert_binary_multi_times(self, connect, binary_collection):
304
        '''
305
        target: test insert entities multi times and final flush
306
        method: create collection and insert binary entity multi and final flush
307
        expected: the collection row count equals to nb
308
        '''
309
        for i in range(default_nb):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
310
            ids = connect.insert(binary_collection, default_binary_entity_row)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_binary_entity_row does not seem to be defined.
Loading history...
311
            assert len(ids) == 1
312
        connect.flush([binary_collection])
313
        assert connect.count_entities(binary_collection) == default_nb
314
315 View Code Duplication
    def test_insert_binary_after_create_index(self, connect, binary_collection, get_binary_index):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
316
        '''
317
        target: test insert binary entities after build index
318
        method: build index and insert entities
319
        expected: no error raised
320
        '''
321
        connect.create_index(binary_collection, binary_field_name, get_binary_index)
322
        ids = connect.insert(binary_collection, default_binary_entities_rows)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_binary_entities_rows does not seem to be defined.
Loading history...
323
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
324
        connect.flush([binary_collection])
325
        info = connect.get_collection_info(binary_collection)
326
        fields = info["fields"]
327
        for field in fields:
328
            if field["name"] == binary_field_name:
329
                assert field["indexes"][0] == get_binary_index
330
331 View Code Duplication
    @pytest.mark.timeout(ADD_TIMEOUT)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
332
    def test_insert_binary_create_index(self, connect, binary_collection, get_binary_index):
333
        '''
334
        target: test build index insert after vector
335
        method: insert vector and build index
336
        expected: no error raised
337
        '''
338
        ids = connect.insert(binary_collection, default_binary_entities_rows)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_binary_entities_rows does not seem to be defined.
Loading history...
339
        assert len(ids) == default_nb
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
340
        connect.flush([binary_collection])
341
        connect.create_index(binary_collection, binary_field_name, get_binary_index)
342
        info = connect.get_collection_info(binary_collection)
343
        fields = info["fields"]
344
        for field in fields:
345
            if field["name"] == binary_field_name:
346
                assert field["indexes"][0] == get_binary_index
347
348
349
class TestInsertInvalid(object):
350
    """
351
    Test inserting vectors with invalid collection names
352
    """
353
354
    @pytest.fixture(
355
        scope="function",
356
        params=gen_invalid_strs()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_strs does not seem to be defined.
Loading history...
357
    )
358
    def get_collection_name(self, request):
359
        yield request.param
360
361
    @pytest.fixture(
362
        scope="function",
363
        params=gen_invalid_strs()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_strs does not seem to be defined.
Loading history...
364
    )
365
    def get_tag_name(self, request):
366
        yield request.param
367
368
    @pytest.fixture(
369
        scope="function",
370
        params=gen_invalid_strs()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_strs does not seem to be defined.
Loading history...
371
    )
372
    def get_field_name(self, request):
373
        yield request.param
374
375
    @pytest.fixture(
376
        scope="function",
377
        params=gen_invalid_strs()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_strs does not seem to be defined.
Loading history...
378
    )
379
    def get_field_type(self, request):
380
        yield request.param
381
382
    @pytest.fixture(
383
        scope="function",
384
        params=gen_invalid_strs()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_strs does not seem to be defined.
Loading history...
385
    )
386
    def get_field_int_value(self, request):
387
        yield request.param
388
389
    @pytest.fixture(
390
        scope="function",
391
        params=gen_invalid_ints()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_ints does not seem to be defined.
Loading history...
392
    )
393
    def get_entity_id(self, request):
394
        yield request.param
395
396
    @pytest.fixture(
397
        scope="function",
398
        params=gen_invalid_vectors()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gen_invalid_vectors does not seem to be defined.
Loading history...
399
    )
400
    def get_field_vectors_value(self, request):
401
        yield request.param
402
403
    def test_insert_field_name_not_match(self, connect, collection):
404
        '''
405
        target: test insert, with field name not matched
406
        method: create collection and insert entities in it
407
        expected: raise an exception
408
        '''
409
        tmp_entity = copy.deepcopy(default_entity_row)
410
        tmp_entity[0]["string"] = "string"
411
        with pytest.raises(Exception):
412
            connect.insert(collection, tmp_entity)
413
414
    def test_insert_with_invalid_collection_name(self, connect, get_collection_name):
415
        collection_name = get_collection_name
416
        with pytest.raises(Exception):
417
            connect.insert(collection_name, default_entity_row)
418
419
    def test_insert_with_invalid_tag_name(self, connect, collection, get_tag_name):
420
        tag_name = get_tag_name
421
        connect.create_partition(collection, default_tag)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_tag does not seem to be defined.
Loading history...
422
        if tag_name is not None:
423
            with pytest.raises(Exception):
424
                connect.insert(collection, default_entity_row, partition_tag=tag_name)
425
        else:
426
            connect.insert(collection, default_entity_row, partition_tag=tag_name)
427
428
    def test_insert_with_less_field(self, connect, collection):
429
        tmp_entity = copy.deepcopy(default_entity_row)
430
        tmp_entity[0].pop(default_float_vec_field_name)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_float_vec_field_name does not seem to be defined.
Loading history...
431
        with pytest.raises(Exception):
432
            connect.insert(collection, tmp_entity) 
433
434
    def test_insert_with_less_field_id(self, connect, id_collection):
435
        tmp_entity = copy.deepcopy(gen_entities_rows(default_nb, _id=False))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable default_nb does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gen_entities_rows does not seem to be defined.
Loading history...
436
        tmp_entity[0].pop("_id")
437
        with pytest.raises(Exception):
438
            connect.insert(id_collection, tmp_entity) 
439
440
    def test_insert_with_more_field(self, connect, collection):
441
        tmp_entity = copy.deepcopy(default_entity_row)
442
        tmp_entity[0]["new_field"] = 1
443
        with pytest.raises(Exception):
444
            connect.insert(collection, tmp_entity) 
445
446
    def test_insert_with_more_field_id(self, connect, collection):
447
        tmp_entity = copy.deepcopy(default_entity_row)
448
        tmp_entity[0]["_id"] = 1
449
        with pytest.raises(Exception):
450
            connect.insert(collection, tmp_entity)
451
452
    def test_insert_with_invalid_field_vector_value(self, connect, collection, get_field_vectors_value):
453
        tmp_entity = copy.deepcopy(default_entity_row)
454
        tmp_entity[0][default_float_vec_field_name][1] = get_field_vectors_value
455
        with pytest.raises(Exception):
456
            connect.insert(collection, tmp_entity)
457