test_insert.py 38.3 KB
Newer Older
J
JinHai-CN 已提交
1
import time
D
del-zhenwu 已提交
2
import pdb
3
import copy
J
JinHai-CN 已提交
4 5 6 7
import threading
import logging
from multiprocessing import Pool, Process
import pytest
8
from milvus import DataType
J
JinHai-CN 已提交
9 10 11
from utils import *

dim = 128
12
segment_row_count = 5000
13
collection_id = "test_insert"
J
JinHai-CN 已提交
14
ADD_TIMEOUT = 60
Z
zhenwu 已提交
15
tag = "1970-01-01"
16
insert_interval_time = 1.5
17
nb = 6000
18
field_name = default_float_vec_field_name 
19 20 21 22 23 24 25 26
entity = gen_entities(1)
raw_vector, binary_entity = gen_binary_entities(1)
entities = gen_entities(nb)
raw_vectors, binary_entities = gen_binary_entities(nb)
default_fields = gen_default_fields()
default_single_query = {
    "bool": {
        "must": [
27
            {"vector": {field_name: {"topk": 10, "query": gen_vectors(1, dim),"metric_type":"L2",
28
                                     "params": {"nprobe": 10}}}}
29 30 31 32 33 34
        ]
    }
}


class TestInsertBase:
J
JinHai-CN 已提交
35 36
    """
    ******************************************************************
D
del-zhenwu 已提交
37
      The following cases are used to test `insert` function
J
JinHai-CN 已提交
38 39
    ******************************************************************
    """
40

Z
zhenwu 已提交
41 42
    @pytest.fixture(
        scope="function",
43
        params=gen_simple_index()
Z
zhenwu 已提交
44
    )
45
    def get_simple_index(self, request, connect):
46 47 48
        if str(connect._cmd("mode")) == "CPU":
            if request.param["index_type"] in index_cpu_not_support():
                pytest.skip("CPU not support index_type: ivf_sq8h")
Z
zhenwu 已提交
49
        return request.param
J
JinHai-CN 已提交
50

51 52 53 54 55 56
    @pytest.fixture(
        scope="function",
        params=gen_single_filter_fields()
    )
    def get_filter_field(self, request):
        yield request.param
J
JinHai-CN 已提交
57

58 59 60 61 62 63
    @pytest.fixture(
        scope="function",
        params=gen_single_vector_fields()
    )
    def get_vector_field(self, request):
        yield request.param
J
JinHai-CN 已提交
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    def test_add_vector_with_empty_vector(self, connect, collection):
        '''
        target: test add vectors with empty vectors list
        method: set empty vectors list as add method params
        expected: raises a Exception
        '''
        vector = []
        with pytest.raises(Exception) as e:
            status, ids = connect.insert(collection, vector)

    def test_add_vector_with_None(self, connect, collection):
        '''
        target: test add vectors with None
        method: set None as add method params
        expected: raises a Exception
        '''
        vector = None
        with pytest.raises(Exception) as e:
            status, ids = connect.insert(collection, vector)

J
JinHai-CN 已提交
85
    @pytest.mark.timeout(ADD_TIMEOUT)
86
    def test_insert_collection_not_existed(self, connect):
J
JinHai-CN 已提交
87
        '''
88 89 90
        target: test insert, with collection not existed
        method: insert entity into a random named collection
        expected: error raised 
J
JinHai-CN 已提交
91
        '''
92 93 94
        collection_name = gen_unique_str(collection_id)
        with pytest.raises(Exception) as e:
            connect.insert(collection_name, entities)
J
JinHai-CN 已提交
95 96

    @pytest.mark.timeout(ADD_TIMEOUT)
97
    def test_insert_drop_collection(self, connect, collection):
J
JinHai-CN 已提交
98
        '''
99 100 101
        target: test delete collection after insert vector
        method: insert vector and delete collection
        expected: no error raised
J
JinHai-CN 已提交
102
        '''
103 104 105
        ids = connect.insert(collection, entity)
        assert len(ids) == 1
        connect.drop_collection(collection)
J
JinHai-CN 已提交
106 107

    @pytest.mark.timeout(ADD_TIMEOUT)
108
    def test_insert_sleep_drop_collection(self, connect, collection):
J
JinHai-CN 已提交
109
        '''
110 111 112
        target: test delete collection after insert vector for a while
        method: insert vector, sleep, and delete collection
        expected: no error raised 
J
JinHai-CN 已提交
113
        '''
114 115 116 117
        ids = connect.insert(collection, entity)
        assert len(ids) == 1
        connect.flush([collection])
        connect.drop_collection(collection)
J
JinHai-CN 已提交
118 119

    @pytest.mark.timeout(ADD_TIMEOUT)
120
    def test_insert_create_index(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
121
        '''
122 123 124
        target: test build index insert after vector
        method: insert vector and build index
        expected: no error raised
J
JinHai-CN 已提交
125
        '''
126 127 128
        ids = connect.insert(collection, entities)
        assert len(ids) == nb
        connect.flush([collection])
129
        connect.create_index(collection, field_name, get_simple_index)
J
JinHai-CN 已提交
130 131

    @pytest.mark.timeout(ADD_TIMEOUT)
132
    def test_insert_after_create_index(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
133
        '''
134 135 136
        target: test build index insert after vector
        method: insert vector and build index
        expected: no error raised
J
JinHai-CN 已提交
137
        '''
138
        connect.create_index(collection, field_name, get_simple_index)
139 140
        ids = connect.insert(collection, entities)
        assert len(ids) == nb
J
JinHai-CN 已提交
141 142

    @pytest.mark.timeout(ADD_TIMEOUT)
143
    def test_insert_search(self, connect, collection):
J
JinHai-CN 已提交
144
        '''
145 146 147
        target: test search vector after insert vector after a while
        method: insert vector, sleep, and search collection
        expected: no error raised 
J
JinHai-CN 已提交
148
        '''
149
        ids = connect.insert(collection, entities)
X
Xiaohai Xu 已提交
150
        connect.flush([collection])
151 152 153
        res = connect.search(collection, default_single_query)
        logging.getLogger().debug(res)
        assert res
J
JinHai-CN 已提交
154

155 156 157 158 159 160 161 162 163
    @pytest.fixture(
        scope="function",
        params=[
            1,
            6000
        ],
    )
    def insert_count(self, request):
        yield request.param
J
JinHai-CN 已提交
164 165

    @pytest.mark.timeout(ADD_TIMEOUT)
166
    def test_insert_ids(self, connect, id_collection, insert_count):
J
JinHai-CN 已提交
167
        '''
168 169 170
        target: test insert vectors in collection, use customize ids
        method: create collection and insert vectors in it, check the ids returned and the collection length after vectors inserted
        expected: the length of ids and the collection row count
J
JinHai-CN 已提交
171
        '''
172 173
        nb = insert_count
        ids = [i for i in range(nb)]
174 175
        res_ids = connect.insert(id_collection, gen_entities(nb), ids)
        connect.flush([id_collection])
176 177
        assert len(res_ids) == nb
        assert res_ids == ids
178
        res_count = connect.count_entities(id_collection)
179
        assert res_count == nb
J
JinHai-CN 已提交
180 181

    @pytest.mark.timeout(ADD_TIMEOUT)
182
    def test_insert_the_same_ids(self, connect, id_collection, insert_count):
J
JinHai-CN 已提交
183
        '''
184 185 186
        target: test insert vectors in collection, use customize the same ids
        method: create collection and insert vectors in it, check the ids returned and the collection length after vectors inserted
        expected: the length of ids and the collection row count
J
JinHai-CN 已提交
187
        '''
188 189
        nb = insert_count
        ids = [1 for i in range(nb)]
190 191
        res_ids = connect.insert(id_collection, gen_entities(nb), ids)
        connect.flush([id_collection])
192 193
        assert len(res_ids) == nb
        assert res_ids == ids
194
        res_count = connect.count_entities(id_collection)
195
        assert res_count == nb
J
JinHai-CN 已提交
196

197 198 199 200 201 202 203 204 205 206 207 208 209
    @pytest.mark.timeout(ADD_TIMEOUT)
    def test_insert_ids_fields(self, connect, get_filter_field, get_vector_field):
        '''
        target: test create normal collection with different fields, insert entities into id with ids
        method: create collection with diff fields: metric/field_type/..., insert, and count
        expected: row count correct
        '''
        nb = 5
        filter_field = get_filter_field
        vector_field = get_vector_field
        collection_name = gen_unique_str("test_collection")
        fields = {
            "fields": [filter_field, vector_field],
D
del-zhenwu 已提交
210 211
            "segment_row_count": segment_row_count,
            "auto_id": True
212 213 214 215 216 217 218 219 220 221
        }
        connect.create_collection(collection_name, fields)
        ids = [i for i in range(nb)]
        entities = gen_entities_by_fields(fields["fields"], nb, dim)
        res_ids = connect.insert(collection_name, entities, ids)
        assert res_ids == ids
        connect.flush([collection_name])
        res_count = connect.count_entities(collection_name)
        assert res_count == nb

222
    # TODO: assert exception && enable
D
del-zhenwu 已提交
223
    @pytest.mark.level(2)
J
JinHai-CN 已提交
224
    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
225
    def test_insert_twice_ids_no_ids(self, connect, id_collection):
J
JinHai-CN 已提交
226
        '''
227 228 229
        target: check the result of insert, with params ids and no ids
        method: test insert vectors twice, use customize ids first, and then use no ids
        expected:  error raised
J
JinHai-CN 已提交
230
        '''
231
        ids = [i for i in range(nb)]
D
del-zhenwu 已提交
232
        res_ids = connect.insert(id_collection, entities, ids)
233
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
234
            res_ids_new = connect.insert(id_collection, entities)
J
JinHai-CN 已提交
235

236
    # TODO: assert exception && enable
D
del-zhenwu 已提交
237
    @pytest.mark.level(2)
J
JinHai-CN 已提交
238
    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
239
    def test_insert_twice_not_ids_ids(self, connect, id_collection):
J
JinHai-CN 已提交
240
        '''
241 242 243
        target: check the result of insert, with params ids and no ids
        method: test insert vectors twice, use not ids first, and then use customize ids
        expected:  error raised
J
JinHai-CN 已提交
244
        '''
245
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
246
            res_ids = connect.insert(id_collection, entities)
J
JinHai-CN 已提交
247 248

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
249
    def test_insert_ids_length_not_match_batch(self, connect, id_collection):
J
JinHai-CN 已提交
250
        '''
251 252 253
        target: test insert vectors in collection, use customize ids, len(ids) != len(vectors)
        method: create collection and insert vectors in it
        expected: raise an exception
J
JinHai-CN 已提交
254
        '''
255 256 257
        ids = [i for i in range(1, nb)]
        logging.getLogger().info(len(ids))
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
258
            res_ids = connect.insert(id_collection, entities, ids)
J
JinHai-CN 已提交
259 260

    @pytest.mark.timeout(ADD_TIMEOUT)
261
    def test_insert_ids_length_not_match_single(self, connect, collection):
J
JinHai-CN 已提交
262
        '''
263 264 265
        target: test insert vectors in collection, use customize ids, len(ids) != len(vectors)
        method: create collection and insert vectors in it
        expected: raise an exception
J
JinHai-CN 已提交
266
        '''
267 268 269 270
        ids = [i for i in range(1, nb)]
        logging.getLogger().info(len(ids))
        with pytest.raises(Exception) as e:
            res_ids = connect.insert(collection, entity, ids)
J
JinHai-CN 已提交
271 272

    @pytest.mark.timeout(ADD_TIMEOUT)
273 274 275 276 277 278 279 280 281 282 283 284
    def test_insert_ids_fields(self, connect, get_filter_field, get_vector_field):
        '''
        target: test create normal collection with different fields, insert entities into id without ids
        method: create collection with diff fields: metric/field_type/..., insert, and count
        expected: row count correct
        '''
        nb = 5
        filter_field = get_filter_field
        vector_field = get_vector_field
        collection_name = gen_unique_str("test_collection")
        fields = {
            "fields": [filter_field, vector_field],
285
            "segment_row_count": segment_row_count
286 287 288 289 290 291 292
        }
        connect.create_collection(collection_name, fields)
        entities = gen_entities_by_fields(fields["fields"], nb, dim)
        res_ids = connect.insert(collection_name, entities)
        connect.flush([collection_name])
        res_count = connect.count_entities(collection_name)
        assert res_count == nb
J
JinHai-CN 已提交
293 294

    @pytest.mark.timeout(ADD_TIMEOUT)
295
    def test_insert_tag(self, connect, collection):
J
JinHai-CN 已提交
296
        '''
297 298 299
        target: test insert entities in collection created before
        method: create collection and insert entities in it, with the partition_tag param
        expected: the collection row count equals to nq
J
JinHai-CN 已提交
300
        '''
301 302 303
        connect.create_partition(collection, tag)
        ids = connect.insert(collection, entities, partition_tag=tag)
        assert len(ids) == nb
J
JinHai-CN 已提交
304 305

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
306
    def test_insert_tag_with_ids(self, connect, id_collection):
J
JinHai-CN 已提交
307
        '''
308 309 310
        target: test insert entities in collection created before, insert with ids
        method: create collection and insert entities in it, with the partition_tag param
        expected: the collection row count equals to nq
J
JinHai-CN 已提交
311
        '''
D
del-zhenwu 已提交
312
        connect.create_partition(id_collection, tag)
313
        ids = [i for i in range(nb)]
D
del-zhenwu 已提交
314
        res_ids = connect.insert(id_collection, entities, ids, partition_tag=tag)
315
        assert res_ids == ids
J
JinHai-CN 已提交
316 317

    @pytest.mark.timeout(ADD_TIMEOUT)
318
    def test_insert_default_tag(self, connect, collection):
J
JinHai-CN 已提交
319
        '''
320 321 322
        target: test insert entities into default partition
        method: create partition and insert info collection without tag params
        expected: the collection row count equals to nb
J
JinHai-CN 已提交
323
        '''
324 325
        connect.create_partition(collection, tag)
        ids = connect.insert(collection, entities)
X
Xiaohai Xu 已提交
326
        connect.flush([collection])
327 328 329
        assert len(ids) == nb
        res_count = connect.count_entities(collection)
        assert res_count == nb
J
JinHai-CN 已提交
330 331

    @pytest.mark.timeout(ADD_TIMEOUT)
332
    def test_insert_tag_not_existed(self, connect, collection):
J
JinHai-CN 已提交
333
        '''
334 335 336
        target: test insert entities in collection created before
        method: create collection and insert entities in it, with the not existed partition_tag param
        expected: error raised
J
JinHai-CN 已提交
337
        '''
338 339 340
        tag = gen_unique_str()
        with pytest.raises(Exception) as e:
            ids = connect.insert(collection, entities, partition_tag=tag)
J
JinHai-CN 已提交
341 342

    @pytest.mark.timeout(ADD_TIMEOUT)
343
    def test_insert_tag_existed(self, connect, collection):
J
JinHai-CN 已提交
344
        '''
345 346 347
        target: test insert entities in collection created before
        method: create collection and insert entities in it repeatly, with the partition_tag param
        expected: the collection row count equals to nq
J
JinHai-CN 已提交
348
        '''
349 350 351
        connect.create_partition(collection, tag)
        ids = connect.insert(collection, entities, partition_tag=tag)
        ids = connect.insert(collection, entities, partition_tag=tag)
X
Xiaohai Xu 已提交
352
        connect.flush([collection])
353 354
        res_count = connect.count_entities(collection)
        assert res_count == 2 * nb
J
JinHai-CN 已提交
355

356 357
    @pytest.mark.level(2)
    def test_insert_without_connect(self, dis_connect, collection):
J
JinHai-CN 已提交
358
        '''
359 360 361
        target: test insert entities without connection
        method: create collection and insert entities in it, check if inserted successfully
        expected: raise exception
J
JinHai-CN 已提交
362
        '''
363 364
        with pytest.raises(Exception) as e:
            ids = dis_connect.insert(collection, entities)
J
JinHai-CN 已提交
365

366
    def test_insert_collection_not_existed(self, connect):
J
JinHai-CN 已提交
367
        '''
368 369 370
        target: test insert entities in collection, which not existed before
        method: insert entities collection not existed, check the status
        expected: error raised
J
JinHai-CN 已提交
371 372
        '''
        with pytest.raises(Exception) as e:
373
            ids = connect.insert(gen_unique_str("not_exist_collection"), entities)
J
JinHai-CN 已提交
374

375
    def test_insert_dim_not_matched(self, connect, collection):
J
JinHai-CN 已提交
376
        '''
377 378 379
        target: test insert entities, the vector dimension is not equal to the collection dimension
        method: the entities dimension is half of the collection dimension, check the status
        expected: error raised
J
JinHai-CN 已提交
380
        '''
381 382 383 384 385
        vectors = gen_vectors(nb, int(dim) // 2)
        insert_entities = copy.deepcopy(entities)
        insert_entities[-1]["values"] = vectors
        with pytest.raises(Exception) as e:
            ids = connect.insert(collection, insert_entities)
J
JinHai-CN 已提交
386

387
    def test_insert_with_field_name_not_match(self, connect, collection):
J
JinHai-CN 已提交
388
        '''
389 390 391
        target: test insert entities, with the entity field name updated
        method: update entity field name
        expected: error raised
J
JinHai-CN 已提交
392
        '''
D
del-zhenwu 已提交
393
        tmp_entity = update_field_name(copy.deepcopy(entity), "int64", "int64new")
394 395
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
396

D
del-zhenwu 已提交
397 398
    @pytest.mark.level(2)
    def test_insert_with_field_type_not_match(self, connect, collection):
Z
zhenwu 已提交
399
        '''
400 401 402
        target: test insert entities, with the entity field type updated
        method: update entity field type
        expected: error raised
Z
zhenwu 已提交
403
        '''
D
del-zhenwu 已提交
404
        tmp_entity = update_field_type(copy.deepcopy(entity), "int64", DataType.FLOAT)
405 406
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
Z
zhenwu 已提交
407

D
del-zhenwu 已提交
408 409
    @pytest.mark.level(2)
    def test_insert_with_field_value_not_match(self, connect, collection):
Z
zhenwu 已提交
410
        '''
411 412 413
        target: test insert entities, with the entity field value updated
        method: update entity field value
        expected: error raised
Z
zhenwu 已提交
414
        '''
D
del-zhenwu 已提交
415
        tmp_entity = update_field_value(copy.deepcopy(entity), DataType.FLOAT, 's')
416 417
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
Z
zhenwu 已提交
418

419
    def test_insert_with_field_more(self, connect, collection):
Z
zhenwu 已提交
420
        '''
421 422 423
        target: test insert entities, with more fields than collection schema
        method: add entity field
        expected: error raised
Z
zhenwu 已提交
424
        '''
425 426 427
        tmp_entity = add_field(copy.deepcopy(entity))
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
Z
zhenwu 已提交
428

429
    def test_insert_with_field_vector_more(self, connect, collection):
Z
zhenwu 已提交
430
        '''
431 432 433
        target: test insert entities, with more fields than collection schema
        method: add entity vector field
        expected: error raised
Z
zhenwu 已提交
434
        '''
435 436 437
        tmp_entity = add_vector_field(nb, dim)
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
Z
zhenwu 已提交
438

439
    def test_insert_with_field_less(self, connect, collection):
D
del-zhenwu 已提交
440
        '''
441 442 443
        target: test insert entities, with less fields than collection schema
        method: remove entity field
        expected: error raised
D
del-zhenwu 已提交
444
        '''
445 446 447
        tmp_entity = remove_field(copy.deepcopy(entity))
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
448

449
    def test_insert_with_field_vector_less(self, connect, collection):
J
JinHai-CN 已提交
450
        '''
451 452 453
        target: test insert entities, with less fields than collection schema
        method: remove entity vector field
        expected: error raised
J
JinHai-CN 已提交
454
        '''
455 456 457
        tmp_entity = remove_vector_field(copy.deepcopy(entity))
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
458

459
    def test_insert_with_no_field_vector_value(self, connect, collection):
J
JinHai-CN 已提交
460
        '''
461 462 463
        target: test insert entities, with no vector field value
        method: remove entity vector field
        expected: error raised
J
JinHai-CN 已提交
464
        '''
465 466 467 468
        tmp_entity = copy.deepcopy(entity)
        del tmp_entity[-1]["values"]
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
469

470
    def test_insert_with_no_field_vector_type(self, connect, collection):
J
JinHai-CN 已提交
471
        '''
472 473 474
        target: test insert entities, with no vector field type
        method: remove entity vector field
        expected: error raised
J
JinHai-CN 已提交
475
        '''
476 477 478 479
        tmp_entity = copy.deepcopy(entity)
        del tmp_entity[-1]["type"]
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
480

481
    def test_insert_with_no_field_vector_name(self, connect, collection):
J
JinHai-CN 已提交
482
        '''
483 484 485
        target: test insert entities, with no vector field name
        method: remove entity vector field
        expected: error raised
J
JinHai-CN 已提交
486
        '''
487 488 489 490
        tmp_entity = copy.deepcopy(entity)
        del tmp_entity[-1]["field"]
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
491

492 493
    @pytest.mark.level(2)
    @pytest.mark.timeout(30)
494
    def test_collection_insert_rows_count_multi_threading(self, args, collection):
495
        '''
X
Xiaohai Xu 已提交
496
        target: test collection rows_count is correct or not with multi threading
497 498 499
        method: create collection and insert entities in it(idmap),
            assert the value returned by count_entities method is equal to length of entities
        expected: the count is equal to the length of entities
500
        '''
501 502
        if args["handler"] == "HTTP":
            pytest.skip("Skip test in http mode")
503 504
        thread_num = 8
        threads = []
505
        milvus = get_milvus(host=args["ip"], port=args["port"], handler=args["handler"], try_connect=False)
506

507
        def insert(thread_i):
508
            logging.getLogger().info("In thread-%d" % thread_i)
509 510 511
            res_ids = milvus.insert(collection, entities)
            milvus.flush([collection])

512
        for i in range(thread_num):
513
            x = threading.Thread(target=insert, args=(i,))
514 515 516 517
            threads.append(x)
            x.start()
        for th in threads:
            th.join()
518 519
        res_count = milvus.count_entities(collection)
        assert res_count == thread_num * nb
J
JinHai-CN 已提交
520

521

D
del-zhenwu 已提交
522
class TestInsertAsync:
Z
zw 已提交
523 524 525 526 527
    @pytest.fixture(scope="function", autouse=True)
    def skip_http_check(self, args):
        if args["handler"] == "HTTP":
            pytest.skip("skip in http mode")

D
del-zhenwu 已提交
528 529 530 531 532 533 534 535 536 537
    @pytest.fixture(
        scope="function",
        params=[
            1,
            1000
        ],
    )
    def insert_count(self, request):
        yield request.param

538
    def check_status(self, result):
D
del-zhenwu 已提交
539
        logging.getLogger().info("In callback check status")
540
        assert not result
D
del-zhenwu 已提交
541

D
del-zhenwu 已提交
542
    def check_result(self, result):
D
del-zhenwu 已提交
543
        logging.getLogger().info("In callback check status")
D
del-zhenwu 已提交
544
        assert result
D
del-zhenwu 已提交
545 546 547

    def test_insert_async(self, connect, collection, insert_count):
        '''
548 549
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
550 551 552
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
553 554
        future = connect.insert(collection, gen_entities(nb), _async=True)
        ids = future.result()
D
del-zhenwu 已提交
555 556 557 558 559 560
        connect.flush([collection])
        assert len(ids) == nb

    @pytest.mark.level(2)
    def test_insert_async_false(self, connect, collection, insert_count):
        '''
561 562
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
563 564 565
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
566 567
        ids = connect.insert(collection, gen_entities(nb), _async=False)
        # ids = future.result()
D
del-zhenwu 已提交
568 569 570 571 572
        connect.flush([collection])
        assert len(ids) == nb

    def test_insert_async_callback(self, connect, collection, insert_count):
        '''
573 574
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
575 576 577
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
578
        future = connect.insert(collection, gen_entities(nb), _async=True, _callback=self.check_status)
D
del-zhenwu 已提交
579 580
        future.done()

D
del-zhenwu 已提交
581 582
    @pytest.mark.level(2)
    def test_insert_async_long(self, connect, collection):
D
del-zhenwu 已提交
583
        '''
584 585
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
586 587 588
        expected: length of ids is equal to the length of vectors
        '''
        nb = 50000
D
del-zhenwu 已提交
589
        future = connect.insert(collection, gen_entities(nb), _async=True, _callback=self.check_result)
590
        result = future.result()
591
        assert len(result) == nb
D
del-zhenwu 已提交
592
        connect.flush([collection])
593
        count = connect.count_entities(collection)
D
del-zhenwu 已提交
594 595 596
        logging.getLogger().info(count)
        assert count == nb

D
del-zhenwu 已提交
597 598
    @pytest.mark.level(2)
    def test_insert_async_callback_timeout(self, connect, collection):
D
del-zhenwu 已提交
599
        '''
600 601
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
602 603
        expected: length of ids is equal to the length of vectors
        '''
D
del-zhenwu 已提交
604
        nb = 100000
605 606 607 608 609
        future = connect.insert(collection, gen_entities(nb), _async=True, _callback=self.check_status, timeout=1)
        with pytest.raises(Exception) as e:
            result = future.result()
        count = connect.count_entities(collection)
        assert count == 0
D
del-zhenwu 已提交
610

611
    def test_insert_async_invalid_params(self, connect):
D
del-zhenwu 已提交
612
        '''
613 614
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
615 616 617
        expected: length of ids is equal to the length of vectors
        '''
        collection_new = gen_unique_str()
618 619 620
        future = connect.insert(collection_new, entities, _async=True)
        with pytest.raises(Exception) as e:
            result = future.result()
D
del-zhenwu 已提交
621 622 623

    def test_insert_async_invalid_params_raise_exception(self, connect, collection):
        '''
624 625
        target: test insert vectors with different length of vectors
        method: set different vectors as insert method params
D
del-zhenwu 已提交
626 627
        expected: length of ids is equal to the length of vectors
        '''
628 629
        entities = []
        future = connect.insert(collection, entities, _async=True)
D
del-zhenwu 已提交
630
        with pytest.raises(Exception) as e:
631
            future.result()
632

D
del-zhenwu 已提交
633

634
class TestInsertMultiCollections:
J
JinHai-CN 已提交
635 636
    """
    ******************************************************************
637
      The following cases are used to test `insert` function
J
JinHai-CN 已提交
638 639
    ******************************************************************
    """
640

Z
zhenwu 已提交
641 642
    @pytest.fixture(
        scope="function",
643
        params=gen_simple_index()
Z
zhenwu 已提交
644
    )
645
    def get_simple_index(self, request, connect):
646 647 648 649
        logging.getLogger().info(request.param)
        if str(connect._cmd("mode")) == "CPU":
            if request.param["index_type"] in index_cpu_not_support():
                pytest.skip("sq8h not support in CPU mode")
Z
zhenwu 已提交
650
        return request.param
J
JinHai-CN 已提交
651

652
    def test_insert_vector_multi_collections(self, connect):
J
JinHai-CN 已提交
653
        '''
654 655 656
        target: test insert entities
        method: create 10 collections and insert entities into them in turn
        expected: row count
J
JinHai-CN 已提交
657
        '''
658 659 660 661 662 663 664 665 666 667 668
        collection_num = 10
        collection_list = []
        for i in range(collection_num):
            collection_name = gen_unique_str(collection_id)
            collection_list.append(collection_name)
            connect.create_collection(collection_name, default_fields)
            ids = connect.insert(collection_name, entities)
            connect.flush([collection_name])
            assert len(ids) == nb
            count = connect.count_entities(collection_name)
            assert count == nb
J
JinHai-CN 已提交
669 670

    @pytest.mark.timeout(ADD_TIMEOUT)
671
    def test_drop_collection_insert_vector_another(self, connect, collection):
J
JinHai-CN 已提交
672
        '''
673 674 675
        target: test insert vector to collection_1 after collection_2 deleted
        method: delete collection_2 and insert vector to collection_1
        expected: row count equals the length of entities inserted
J
JinHai-CN 已提交
676
        '''
677 678 679 680 681 682
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
        connect.drop_collection(collection)
        ids = connect.insert(collection_name, entity)
        connect.flush([collection_name])
        assert len(ids) == 1
J
JinHai-CN 已提交
683 684

    @pytest.mark.timeout(ADD_TIMEOUT)
685
    def test_create_index_insert_vector_another(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
686
        '''
687 688
        target: test insert vector to collection_2 after build index for collection_1
        method: build index and insert vector
J
JinHai-CN 已提交
689 690
        expected: status ok
        '''
691 692
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
693
        connect.create_index(collection, field_name, get_simple_index)
694 695
        ids = connect.insert(collection, entity)
        connect.drop_collection(collection_name)
J
JinHai-CN 已提交
696 697

    @pytest.mark.timeout(ADD_TIMEOUT)
698
    def test_insert_vector_create_index_another(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
699
        '''
700 701
        target: test insert vector to collection_2 after build index for collection_1
        method: build index and insert vector
J
JinHai-CN 已提交
702 703
        expected: status ok
        '''
704 705 706
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
        ids = connect.insert(collection, entity)
707
        connect.create_index(collection, field_name, get_simple_index)
708 709
        count = connect.count_entities(collection_name)
        assert count == 0
J
JinHai-CN 已提交
710 711

    @pytest.mark.timeout(ADD_TIMEOUT)
712
    def test_insert_vector_sleep_create_index_another(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
713
        '''
714 715
        target: test insert vector to collection_2 after build index for collection_1 for a while
        method: build index and insert vector
J
JinHai-CN 已提交
716 717
        expected: status ok
        '''
718 719 720 721
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
        ids = connect.insert(collection, entity)
        connect.flush([collection])
722
        connect.create_index(collection, field_name, get_simple_index)
723 724
        count = connect.count_entities(collection)
        assert count == 1
J
JinHai-CN 已提交
725 726

    @pytest.mark.timeout(ADD_TIMEOUT)
727
    def test_search_vector_insert_vector_another(self, connect, collection):
J
JinHai-CN 已提交
728
        '''
729 730
        target: test insert vector to collection_1 after search collection_2
        method: search collection and insert vector
J
JinHai-CN 已提交
731 732
        expected: status ok
        '''
733 734 735 736 737 738 739 740
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
        res = connect.search(collection, default_single_query)
        logging.getLogger().debug(res)
        ids = connect.insert(collection_name, entity)
        connect.flush()
        count = connect.count_entities(collection_name)
        assert count == 1
J
JinHai-CN 已提交
741 742

    @pytest.mark.timeout(ADD_TIMEOUT)
743
    def test_insert_vector_search_vector_another(self, connect, collection):
J
JinHai-CN 已提交
744
        '''
745 746
        target: test insert vector to collection_1 after search collection_2
        method: search collection and insert vector
J
JinHai-CN 已提交
747 748
        expected: status ok
        '''
749 750 751 752
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
        ids = connect.insert(collection, entity)
        result = connect.search(collection_name, default_single_query)
J
JinHai-CN 已提交
753 754

    @pytest.mark.timeout(ADD_TIMEOUT)
755
    def test_insert_vector_sleep_search_vector_another(self, connect, collection):
J
JinHai-CN 已提交
756
        '''
757 758
        target: test insert vector to collection_1 after search collection_2 a while
        method: search collection , sleep, and insert vector
J
JinHai-CN 已提交
759 760
        expected: status ok
        '''
761 762 763 764 765
        collection_name = gen_unique_str(collection_id)
        connect.create_collection(collection_name, default_fields)
        ids = connect.insert(collection, entity)
        connect.flush([collection])
        result = connect.search(collection_name, default_single_query)
J
JinHai-CN 已提交
766 767


768 769 770 771
class TestInsertInvalid(object):
    """
    Test inserting vectors with invalid collection names
    """
J
JinHai-CN 已提交
772

773 774 775 776 777 778
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_collection_name(self, request):
        yield request.param
J
JinHai-CN 已提交
779

780 781 782 783 784 785
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_tag_name(self, request):
        yield request.param
J
JinHai-CN 已提交
786

787 788 789 790 791 792
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_field_name(self, request):
        yield request.param
J
JinHai-CN 已提交
793

794 795 796 797 798 799
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_field_type(self, request):
        yield request.param
J
JinHai-CN 已提交
800

801 802 803 804 805 806
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_field_int_value(self, request):
        yield request.param
J
JinHai-CN 已提交
807

808 809 810 811 812 813
    @pytest.fixture(
        scope="function",
        params=gen_invalid_ints()
    )
    def get_entity_id(self, request):
        yield request.param
J
JinHai-CN 已提交
814

815 816 817 818 819 820
    @pytest.fixture(
        scope="function",
        params=gen_invalid_vectors()
    )
    def get_field_vectors_value(self, request):
        yield request.param
J
JinHai-CN 已提交
821

D
del-zhenwu 已提交
822
    def test_insert_ids_invalid(self, connect, id_collection, get_entity_id):
J
JinHai-CN 已提交
823
        '''
824 825 826
        target: test insert, with using customize ids, which are not int64
        method: create collection and insert entities in it
        expected: raise an exception
J
JinHai-CN 已提交
827
        '''
828 829
        entity_id = get_entity_id
        ids = [entity_id for _ in range(nb)]
830
        if isinstance(entity_id, int):
D
del-zhenwu 已提交
831
            connect.insert(id_collection, entities, ids)
832 833 834
        else:
            with pytest.raises(Exception):
                connect.insert(id_collection, entities, ids)
J
JinHai-CN 已提交
835

836 837 838 839
    def test_insert_with_invalid_collection_name(self, connect, get_collection_name):
        collection_name = get_collection_name
        with pytest.raises(Exception):
            connect.insert(collection_name, entity)
J
JinHai-CN 已提交
840

841 842 843 844 845 846 847 848
    def test_insert_with_invalid_tag_name(self, connect, collection, get_tag_name):
        tag_name = get_tag_name
        connect.create_partition(collection, tag)
        if tag_name is not None:
            with pytest.raises(Exception):
                connect.insert(collection, entity, partition_tag=tag_name)
        else:
            connect.insert(collection, entity, partition_tag=tag_name)
J
JinHai-CN 已提交
849

850 851
    def test_insert_with_invalid_field_name(self, connect, collection, get_field_name):
        field_name = get_field_name
D
del-zhenwu 已提交
852
        tmp_entity = update_field_name(copy.deepcopy(entity), "int64", get_field_name)
853 854
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
855

856 857 858 859 860
    def test_insert_with_invalid_field_type(self, connect, collection, get_field_type):
        field_type = get_field_type
        tmp_entity = update_field_type(copy.deepcopy(entity), 'float', field_type)
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
861

862 863
    def test_insert_with_invalid_field_value(self, connect, collection, get_field_int_value):
        field_value = get_field_int_value
D
del-zhenwu 已提交
864
        tmp_entity = update_field_type(copy.deepcopy(entity), 'int64', field_value)
865 866 867 868 869 870 871 872 873
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)

    def test_insert_with_invalid_field_vector_value(self, connect, collection, get_field_vectors_value):
        tmp_entity = copy.deepcopy(entity)
        src_vector = tmp_entity[-1]["values"]
        src_vector[0][1] = get_field_vectors_value
        with pytest.raises(Exception):
            connect.insert(collection, tmp_entity)
J
JinHai-CN 已提交
874

875 876

class TestInsertInvalidBinary(object):
J
JinHai-CN 已提交
877
    """
878
    Test inserting vectors with invalid collection names
J
JinHai-CN 已提交
879
    """
880

J
JinHai-CN 已提交
881 882
    @pytest.fixture(
        scope="function",
883
        params=gen_invalid_strs()
J
JinHai-CN 已提交
884
    )
X
Xiaohai Xu 已提交
885
    def get_collection_name(self, request):
J
JinHai-CN 已提交
886 887
        yield request.param

Z
zhenwu 已提交
888 889
    @pytest.fixture(
        scope="function",
890
        params=gen_invalid_strs()
Z
zhenwu 已提交
891 892 893 894
    )
    def get_tag_name(self, request):
        yield request.param

895 896 897 898 899 900
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_field_name(self, request):
        yield request.param
J
JinHai-CN 已提交
901

902 903 904 905 906 907
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_field_type(self, request):
        yield request.param
Z
zhenwu 已提交
908

909 910 911 912 913 914
    @pytest.fixture(
        scope="function",
        params=gen_invalid_strs()
    )
    def get_field_int_value(self, request):
        yield request.param
J
JinHai-CN 已提交
915

916 917 918 919 920 921
    @pytest.fixture(
        scope="function",
        params=gen_invalid_ints()
    )
    def get_entity_id(self, request):
        yield request.param
J
JinHai-CN 已提交
922 923 924 925 926

    @pytest.fixture(
        scope="function",
        params=gen_invalid_vectors()
    )
927
    def get_field_vectors_value(self, request):
J
JinHai-CN 已提交
928 929 930
        yield request.param

    @pytest.mark.level(2)
D
del-zhenwu 已提交
931
    def test_insert_ids_invalid(self, connect, binary_id_collection, get_entity_id):
932 933 934 935 936 937 938 939
        '''
        target: test insert, with using customize ids, which are not int64
        method: create collection and insert entities in it
        expected: raise an exception
        '''
        entity_id = get_entity_id
        ids = [entity_id for _ in range(nb)]
        with pytest.raises(Exception):
D
del-zhenwu 已提交
940
            connect.insert(binary_id_collection, binary_entities, ids)
J
JinHai-CN 已提交
941

Z
zhenwu 已提交
942
    @pytest.mark.level(2)
943
    def test_insert_with_invalid_tag_name(self, connect, binary_collection, get_tag_name):
944
        tag_name = get_tag_name
945
        connect.create_partition(binary_collection, tag)
946 947
        if tag_name is not None:
            with pytest.raises(Exception):
948
                connect.insert(binary_collection, binary_entity, partition_tag=tag_name)
949
        else:
950
            connect.insert(binary_collection, binary_entity, partition_tag=tag_name)
G
groot 已提交
951 952

    @pytest.mark.level(2)
953
    def test_insert_with_invalid_field_name(self, connect, binary_collection, get_field_name):
954
        field_name = get_field_name
D
del-zhenwu 已提交
955
        tmp_entity = update_field_name(copy.deepcopy(binary_entity), "int64", get_field_name)
956
        with pytest.raises(Exception):
957
            connect.insert(binary_collection, tmp_entity)
G
groot 已提交
958 959

    @pytest.mark.level(2)
960
    def test_insert_with_invalid_field_value(self, connect, binary_collection, get_field_int_value):
961
        field_value = get_field_int_value
D
del-zhenwu 已提交
962
        tmp_entity = update_field_type(copy.deepcopy(binary_entity), 'int64', field_value)
963
        with pytest.raises(Exception):
964
            connect.insert(binary_collection, tmp_entity)
965 966

    @pytest.mark.level(2)
967
    def test_insert_with_invalid_field_vector_value(self, connect, binary_collection, get_field_vectors_value):
968 969 970 971
        tmp_entity = copy.deepcopy(binary_entity)
        src_vector = tmp_entity[-1]["values"]
        src_vector[0][1] = get_field_vectors_value
        with pytest.raises(Exception):
972
            connect.insert(binary_collection, tmp_entity)
973 974

    @pytest.mark.level(2)
D
del-zhenwu 已提交
975
    def test_insert_ids_invalid(self, connect, binary_id_collection, get_entity_id):
976 977 978 979 980 981 982 983
        '''
        target: test insert, with using customize ids, which are not int64
        method: create collection and insert entities in it
        expected: raise an exception
        '''
        entity_id = get_entity_id
        ids = [entity_id for _ in range(nb)]
        with pytest.raises(Exception):
D
del-zhenwu 已提交
984
            connect.insert(binary_id_collection, binary_entities, ids)
985 986

    @pytest.mark.level(2)
987
    def test_insert_with_invalid_field_name(self, connect, binary_collection, get_field_name):
988
        field_name = get_field_name
D
del-zhenwu 已提交
989
        tmp_entity = update_field_name(copy.deepcopy(binary_entity), "int64", get_field_name)
990
        with pytest.raises(Exception):
991
            connect.insert(binary_collection, tmp_entity)
992 993

    @pytest.mark.level(2)
994
    def test_insert_with_invalid_field_type(self, connect, binary_collection, get_field_type):
995
        field_type = get_field_type
D
del-zhenwu 已提交
996
        tmp_entity = update_field_type(copy.deepcopy(binary_entity), 'int64', field_type)
997
        with pytest.raises(Exception):
998
            connect.insert(binary_collection, tmp_entity)
999 1000

    @pytest.mark.level(2)
1001
    def test_insert_with_invalid_field_value(self, connect, binary_collection, get_field_int_value):
1002
        field_value = get_field_int_value
D
del-zhenwu 已提交
1003
        tmp_entity = update_field_type(copy.deepcopy(binary_entity), 'int64', field_value)
1004
        with pytest.raises(Exception):
1005
            connect.insert(binary_collection, tmp_entity)
1006 1007

    @pytest.mark.level(2)
1008
    def test_insert_with_invalid_field_vector_value(self, connect, binary_collection, get_field_vectors_value):
1009 1010 1011 1012
        tmp_entity = copy.deepcopy(binary_entities)
        src_vector = tmp_entity[-1]["values"]
        src_vector[1] = get_field_vectors_value
        with pytest.raises(Exception):
1013
            connect.insert(binary_collection, tmp_entity)