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


dim = 128
index_file_size = 10
X
Xiaohai Xu 已提交
14
collection_id = "test_add"
J
JinHai-CN 已提交
15
ADD_TIMEOUT = 60
Z
zhenwu 已提交
16
tag = "1970-01-01"
17 18
add_interval_time = 1.5
nb = 6000
J
JinHai-CN 已提交
19

G
groot 已提交
20

J
JinHai-CN 已提交
21 22 23
class TestAddBase:
    """
    ******************************************************************
D
del-zhenwu 已提交
24
      The following cases are used to test `insert` function
J
JinHai-CN 已提交
25 26
    ******************************************************************
    """
Z
zhenwu 已提交
27 28
    @pytest.fixture(
        scope="function",
29
        params=gen_simple_index()
Z
zhenwu 已提交
30
    )
31
    def get_simple_index(self, request, connect):
G
groot 已提交
32
        if str(connect._cmd("mode")[1]) == "CPU":
Z
zhenwu 已提交
33
            if request.param["index_type"] == IndexType.IVF_SQ8H:
G
groot 已提交
34 35 36
                pytest.skip("sq8h not support in cpu mode")
        if request.param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip PQ Temporary")
Z
zhenwu 已提交
37
        return request.param
J
JinHai-CN 已提交
38

X
Xiaohai Xu 已提交
39
    def test_add_vector_create_collection(self, connect, collection):
J
JinHai-CN 已提交
40
        '''
X
Xiaohai Xu 已提交
41 42
        target: test add vector, then create collection again
        method: add vector and create collection
J
JinHai-CN 已提交
43 44 45
        expected: status not ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
46
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
47
        param = {'collection_name': collection,
J
JinHai-CN 已提交
48 49 50
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
51
        status = connect.create_collection(param)
J
JinHai-CN 已提交
52 53
        assert not status.OK()

X
Xiaohai Xu 已提交
54
    def test_add_vector_has_collection(self, connect, collection):
J
JinHai-CN 已提交
55
        '''
X
Xiaohai Xu 已提交
56 57 58
        target: test add vector, then check collection existence
        method: add vector and call Hascollection
        expected: collection exists, status ok
J
JinHai-CN 已提交
59 60
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
61
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
62
        assert assert_has_collection(connect, collection)
J
JinHai-CN 已提交
63 64

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
65
    def test_drop_collection_add_vector(self, connect, collection):
J
JinHai-CN 已提交
66
        '''
X
Xiaohai Xu 已提交
67 68
        target: test add vector after collection deleted
        method: delete collection and add vector
J
JinHai-CN 已提交
69 70
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
71
        status = connect.drop_collection(collection)
J
JinHai-CN 已提交
72
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
73
        status, ids = connect.insert(collection, vector)
J
JinHai-CN 已提交
74 75 76
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
77
    def test_drop_collection_add_vector_another(self, connect, collection):
J
JinHai-CN 已提交
78
        '''
X
Xiaohai Xu 已提交
79 80
        target: test add vector to collection_1 after collection_2 deleted
        method: delete collection_2 and add vector to collection_1
J
JinHai-CN 已提交
81 82
        expected: status ok
        '''
X
Xiaohai Xu 已提交
83
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
84 85 86
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
87 88
        status = connect.create_collection(param)
        status = connect.drop_collection(collection)
J
JinHai-CN 已提交
89
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
90
        status, ids = connect.insert(param['collection_name'], vector)
J
JinHai-CN 已提交
91 92 93
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
94
    def test_add_vector_drop_collection(self, connect, collection):
J
JinHai-CN 已提交
95
        '''
X
Xiaohai Xu 已提交
96 97
        target: test delete collection after add vector
        method: add vector and delete collection
J
JinHai-CN 已提交
98 99 100
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
101
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
102
        status = connect.drop_collection(collection)
J
JinHai-CN 已提交
103 104 105
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
106
    def test_add_vector_delete_another_collection(self, connect, collection):
J
JinHai-CN 已提交
107
        '''
X
Xiaohai Xu 已提交
108 109
        target: test delete collection_1 collection after add vector to collection_2
        method: add vector and delete collection
J
JinHai-CN 已提交
110 111
        expected: status ok
        '''
X
Xiaohai Xu 已提交
112
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
113 114 115
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
116
        status = connect.create_collection(param)
J
JinHai-CN 已提交
117
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
118
        status, ids = connect.insert(collection, vector)
J
JinHai-CN 已提交
119 120 121
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
122
    def test_add_vector_sleep_drop_collection(self, connect, collection):
J
JinHai-CN 已提交
123
        '''
X
Xiaohai Xu 已提交
124 125
        target: test delete collection after add vector for a while
        method: add vector, sleep, and delete collection
J
JinHai-CN 已提交
126 127 128
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
129
        status, ids = connect.insert(collection, vector)
130
        assert status.OK()
X
Xiaohai Xu 已提交
131 132
        connect.flush([collection])
        status = connect.drop_collection(collection)
J
JinHai-CN 已提交
133 134 135
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
136
    def test_add_vector_sleep_delete_another_collection(self, connect, collection):
J
JinHai-CN 已提交
137
        '''
X
Xiaohai Xu 已提交
138 139
        target: test delete collection_1 collection after add vector to collection_2 for a while
        method: add vector , sleep, and delete collection
J
JinHai-CN 已提交
140 141
        expected: status ok
        '''
X
Xiaohai Xu 已提交
142
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
143 144 145
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
146
        status = connect.create_collection(param)
J
JinHai-CN 已提交
147
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
148
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
149 150
        connect.flush([collection])
        status = connect.drop_collection(param['collection_name'])
J
JinHai-CN 已提交
151 152 153
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
154
    def test_create_index_add_vector(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
155 156 157 158 159
        '''
        target: test add vector after build index
        method: build index and add vector
        expected: status ok
        '''
160 161
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
162
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
163
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
164
        status, ids = connect.insert(collection, vector)
J
JinHai-CN 已提交
165 166 167
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
168
    def test_create_index_add_vector_another(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
169
        '''
X
Xiaohai Xu 已提交
170
        target: test add vector to collection_2 after build index for collection_1
J
JinHai-CN 已提交
171 172 173
        method: build index and add vector
        expected: status ok
        '''
174 175
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
176
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
177 178 179
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
180 181
        status = connect.create_collection(param)
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
182
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
183
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
184
        connect.drop_collection(param['collection_name'])
J
JinHai-CN 已提交
185 186 187
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
188
    def test_add_vector_create_index(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
189 190 191 192 193
        '''
        target: test build index add after vector
        method: add vector and build index
        expected: status ok
        '''
194 195
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
Z
zhenwu 已提交
196
        logging.getLogger().info(index_param)
J
JinHai-CN 已提交
197
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
198
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
199
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
200 201 202
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
203
    def test_add_vector_create_index_another(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
204
        '''
X
Xiaohai Xu 已提交
205
        target: test add vector to collection_2 after build index for collection_1
J
JinHai-CN 已提交
206 207 208
        method: build index and add vector
        expected: status ok
        '''
209 210
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
211
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
212 213 214
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
215
        status = connect.create_collection(param)
J
JinHai-CN 已提交
216
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
217
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
218
        status = connect.create_index(param['collection_name'], index_type, index_param)
J
JinHai-CN 已提交
219 220 221
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
222
    def test_add_vector_sleep_create_index(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
223 224 225 226 227
        '''
        target: test build index add after vector for a while
        method: add vector and build index
        expected: status ok
        '''
228 229
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
J
JinHai-CN 已提交
230
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
231
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
232 233
        connect.flush([collection])
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
234 235 236
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
237
    def test_add_vector_sleep_create_index_another(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
238
        '''
X
Xiaohai Xu 已提交
239
        target: test add vector to collection_2 after build index for collection_1 for a while
J
JinHai-CN 已提交
240 241 242
        method: build index and add vector
        expected: status ok
        '''
243 244
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
245
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
246 247 248
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
249
        status = connect.create_collection(param)
J
JinHai-CN 已提交
250
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
251
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
252 253
        connect.flush([collection])
        status = connect.create_index(param['collection_name'], index_type, index_param)
J
JinHai-CN 已提交
254 255 256
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
257
    def test_search_vector_add_vector(self, connect, collection):
J
JinHai-CN 已提交
258
        '''
X
Xiaohai Xu 已提交
259 260
        target: test add vector after search collection
        method: search collection and add vector
J
JinHai-CN 已提交
261 262 263
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
264 265
        status, result = connect.search(collection, 1, vector)
        status, ids = connect.insert(collection, vector)
J
JinHai-CN 已提交
266 267 268
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
269
    def test_search_vector_add_vector_another(self, connect, collection):
J
JinHai-CN 已提交
270
        '''
X
Xiaohai Xu 已提交
271 272
        target: test add vector to collection_1 after search collection_2
        method: search collection and add vector
J
JinHai-CN 已提交
273 274
        expected: status ok
        '''
X
Xiaohai Xu 已提交
275
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
276 277 278
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
279
        status = connect.create_collection(param)
J
JinHai-CN 已提交
280
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
281 282
        status, result = connect.search(collection, 1, vector)
        status, ids = connect.insert(param['collection_name'], vector)
J
JinHai-CN 已提交
283 284 285
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
286
    def test_add_vector_search_vector(self, connect, collection):
J
JinHai-CN 已提交
287 288
        '''
        target: test search vector after add vector
X
Xiaohai Xu 已提交
289
        method: add vector and search collection
J
JinHai-CN 已提交
290 291 292
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
293
        status, ids = connect.insert(collection, vector)
294
        assert status.OK()
X
Xiaohai Xu 已提交
295
        connect.flush([collection])
D
del-zhenwu 已提交
296
        status, result = connect.search(collection, 1, vector)
J
JinHai-CN 已提交
297 298 299
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
300
    def test_add_vector_search_vector_another(self, connect, collection):
J
JinHai-CN 已提交
301
        '''
X
Xiaohai Xu 已提交
302 303
        target: test add vector to collection_1 after search collection_2
        method: search collection and add vector
J
JinHai-CN 已提交
304 305
        expected: status ok
        '''
X
Xiaohai Xu 已提交
306
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
307 308 309
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
310
        status = connect.create_collection(param)
J
JinHai-CN 已提交
311
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
312 313
        status, ids = connect.insert(collection, vector)
        status, result = connect.search(param['collection_name'], 1, vector)
J
JinHai-CN 已提交
314 315 316
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
317
    def test_add_vector_sleep_search_vector(self, connect, collection):
J
JinHai-CN 已提交
318 319
        '''
        target: test search vector after add vector after a while
X
Xiaohai Xu 已提交
320
        method: add vector, sleep, and search collection
J
JinHai-CN 已提交
321 322 323
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
324
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
325
        connect.flush([collection])
D
del-zhenwu 已提交
326
        status, result = connect.search(collection, 1, vector)
J
JinHai-CN 已提交
327 328 329
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
330
    def test_add_vector_sleep_search_vector_another(self, connect, collection):
J
JinHai-CN 已提交
331
        '''
X
Xiaohai Xu 已提交
332 333
        target: test add vector to collection_1 after search collection_2 a while
        method: search collection , sleep, and add vector
J
JinHai-CN 已提交
334 335
        expected: status ok
        '''
X
Xiaohai Xu 已提交
336
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
337 338 339
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
340
        status = connect.create_collection(param)
J
JinHai-CN 已提交
341
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
342
        status, ids = connect.insert(collection, vector)
X
Xiaohai Xu 已提交
343
        connect.flush([collection])
D
del-zhenwu 已提交
344
        status, result = connect.search(param['collection_name'], 1, vector)
J
JinHai-CN 已提交
345 346 347 348
        assert status.OK()

    """
    ******************************************************************
D
del-zhenwu 已提交
349
      The following cases are used to test `insert` function
J
JinHai-CN 已提交
350 351 352 353
    ******************************************************************
    """

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
354
    def test_insert_ids(self, connect, collection):
J
JinHai-CN 已提交
355
        '''
X
Xiaohai Xu 已提交
356 357 358
        target: test add vectors in collection, use customize ids
        method: create collection and add vectors in it, check the ids returned and the collection length after vectors added
        expected: the length of ids and the collection row count
J
JinHai-CN 已提交
359
        '''
360
        nq = 5; top_k = 1;
J
JinHai-CN 已提交
361 362
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
D
del-zhenwu 已提交
363
        status, ids = connect.insert(collection, vectors, ids)
X
Xiaohai Xu 已提交
364
        connect.flush([collection])
J
JinHai-CN 已提交
365 366
        assert status.OK()
        assert len(ids) == nq
D
del-zhenwu 已提交
367
        status, result = connect.search(collection, top_k, query_records=vectors)
J
JinHai-CN 已提交
368 369 370 371 372 373
        logging.getLogger().info(result)
        assert len(result) == nq
        for i in range(nq):
            assert result[i][0].id == i

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
374
    def test_insert_twice_ids_no_ids(self, connect, collection):
J
JinHai-CN 已提交
375
        '''
D
del-zhenwu 已提交
376
        target: check the result of insert, with params ids and no ids
J
JinHai-CN 已提交
377 378 379
        method: test add vectors twice, use customize ids first, and then use no ids
        expected: status not OK 
        '''
380
        nq = 5; top_k = 1;
J
JinHai-CN 已提交
381 382
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
D
del-zhenwu 已提交
383
        status, ids = connect.insert(collection, vectors, ids)
J
JinHai-CN 已提交
384
        assert status.OK()
D
del-zhenwu 已提交
385
        status, ids = connect.insert(collection, vectors)
J
JinHai-CN 已提交
386 387 388 389 390
        logging.getLogger().info(status)
        logging.getLogger().info(ids)
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
391
    def test_insert_twice_not_ids_ids(self, connect, collection):
J
JinHai-CN 已提交
392
        '''
D
del-zhenwu 已提交
393
        target: check the result of insert, with params ids and no ids
J
JinHai-CN 已提交
394 395 396
        method: test add vectors twice, use not ids first, and then use customize ids
        expected: status not OK 
        '''
397
        nq = 5; top_k = 1;
J
JinHai-CN 已提交
398 399
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
D
del-zhenwu 已提交
400
        status, ids = connect.insert(collection, vectors)
J
JinHai-CN 已提交
401
        assert status.OK()
D
del-zhenwu 已提交
402
        status, ids = connect.insert(collection, vectors, ids)
J
JinHai-CN 已提交
403 404 405 406 407
        logging.getLogger().info(status)
        logging.getLogger().info(ids)
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
408
    def test_insert_ids_length_not_match(self, connect, collection):
J
JinHai-CN 已提交
409
        '''
X
Xiaohai Xu 已提交
410 411
        target: test add vectors in collection, use customize ids, len(ids) != len(vectors)
        method: create collection and add vectors in it
J
JinHai-CN 已提交
412 413 414 415 416 417
        expected: raise an exception
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(1, nq)]
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
418
            status, ids = connect.insert(collection, vectors, ids)
J
JinHai-CN 已提交
419 420 421 422 423 424 425 426

    @pytest.fixture(
        scope="function",
        params=gen_invalid_vector_ids()
    )
    def get_vector_id(self, request):
        yield request.param

Z
zhenwu 已提交
427
    @pytest.mark.level(2)
D
del-zhenwu 已提交
428
    def test_insert_ids_invalid(self, connect, collection, get_vector_id):
J
JinHai-CN 已提交
429
        '''
X
Xiaohai Xu 已提交
430 431
        target: test add vectors in collection, use customize ids, which are not int64
        method: create collection and add vectors in it
J
JinHai-CN 已提交
432 433 434 435 436
        expected: raise an exception
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
        vector_id = get_vector_id
437 438
        ids = [vector_id for _ in range(nq)]
        with pytest.raises(Exception):
D
del-zhenwu 已提交
439
            connect.insert(collection, vectors, ids)
J
JinHai-CN 已提交
440 441

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
442
    def test_insert(self, connect, collection):
J
JinHai-CN 已提交
443
        '''
X
Xiaohai Xu 已提交
444 445 446
        target: test add vectors in collection created before
        method: create collection and add vectors in it, check the ids returned and the collection length after vectors added
        expected: the length of ids and the collection row count
J
JinHai-CN 已提交
447 448 449
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
450
        status, ids = connect.insert(collection, vectors)
J
JinHai-CN 已提交
451 452 453
        assert status.OK()
        assert len(ids) == nq

Z
zhenwu 已提交
454
    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
455
    def test_insert_tag(self, connect, collection):
Z
zhenwu 已提交
456
        '''
X
Xiaohai Xu 已提交
457 458 459
        target: test add vectors in collection created before
        method: create collection and add vectors in it, with the partition_tag param
        expected: the collection row count equals to nq
Z
zhenwu 已提交
460 461 462
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
463
        status = connect.create_partition(collection, tag)
D
del-zhenwu 已提交
464
        status, ids = connect.insert(collection, vectors, partition_tag=tag)
Z
zhenwu 已提交
465 466 467 468
        assert status.OK()
        assert len(ids) == nq

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
469
    def test_insert_tag_A(self, connect, collection):
Z
zhenwu 已提交
470
        '''
X
Xiaohai Xu 已提交
471
        target: test add vectors in collection created before
Z
zhenwu 已提交
472
        method: create partition and add vectors in it
X
Xiaohai Xu 已提交
473
        expected: the collection row count equals to nq
Z
zhenwu 已提交
474 475 476
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
477
        status = connect.create_partition(collection, tag)
D
del-zhenwu 已提交
478
        status, ids = connect.insert(collection, vectors, partition_tag=tag)
Z
zhenwu 已提交
479 480 481 482
        assert status.OK()
        assert len(ids) == nq

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
483
    def test_insert_tag_not_existed(self, connect, collection):
Z
zhenwu 已提交
484
        '''
X
Xiaohai Xu 已提交
485 486
        target: test add vectors in collection created before
        method: create collection and add vectors in it, with the not existed partition_tag param
Z
zhenwu 已提交
487 488 489 490
        expected: status not ok
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
491
        status, ids = connect.insert(collection, vectors, partition_tag=tag)
Z
zhenwu 已提交
492 493 494
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
495
    def test_insert_tag_not_existed_A(self, connect, collection):
Z
zhenwu 已提交
496
        '''
X
Xiaohai Xu 已提交
497
        target: test add vectors in collection created before
Z
zhenwu 已提交
498 499 500 501 502 503
        method: create partition, add vectors with the not existed partition_tag param
        expected: status not ok
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
        new_tag = "new_tag"
X
Xiaohai Xu 已提交
504
        status = connect.create_partition(collection, tag)
D
del-zhenwu 已提交
505
        status, ids = connect.insert(collection, vectors, partition_tag=new_tag)
Z
zhenwu 已提交
506 507 508
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
509
    def test_insert_tag_existed(self, connect, collection):
Z
zhenwu 已提交
510
        '''
X
Xiaohai Xu 已提交
511 512 513
        target: test add vectors in collection created before
        method: create collection and add vectors in it repeatly, with the partition_tag param
        expected: the collection row count equals to nq
Z
zhenwu 已提交
514 515 516
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
517
        status = connect.create_partition(collection, tag)
D
del-zhenwu 已提交
518
        status, ids = connect.insert(collection, vectors, partition_tag=tag)
Z
zhenwu 已提交
519
        for i in range(5):
D
del-zhenwu 已提交
520
            status, ids = connect.insert(collection, vectors, partition_tag=tag)
Z
zhenwu 已提交
521 522 523
            assert status.OK()
            assert len(ids) == nq

D
del-zhenwu 已提交
524 525 526 527 528 529 530 531 532 533 534
    @pytest.mark.level(2)
    def test_insert_without_connect(self, dis_connect, collection):
        '''
        target: test add vectors without connection
        method: create collection and add vectors in it, check if added successfully
        expected: raise exception
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
        with pytest.raises(Exception) as e:
            status, ids = dis_connect.insert(collection, vectors)
J
JinHai-CN 已提交
535

X
Xiaohai Xu 已提交
536
    def test_add_collection_not_existed(self, connect):
J
JinHai-CN 已提交
537
        '''
X
Xiaohai Xu 已提交
538 539
        target: test add vectors in collection, which not existed before
        method: add vectors collection not existed, check the status
J
JinHai-CN 已提交
540 541 542 543
        expected: status not ok
        '''
        nq = 5
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
544
        status, ids = connect.insert(gen_unique_str("not_exist_collection"), vector)
J
JinHai-CN 已提交
545 546 547
        assert not status.OK()
        assert not ids

X
Xiaohai Xu 已提交
548
    def test_add_vector_dim_not_matched(self, connect, collection):
J
JinHai-CN 已提交
549
        '''
X
Xiaohai Xu 已提交
550 551
        target: test add vector, the vector dimension is not equal to the collection dimension
        method: the vector dimension is half of the collection dimension, check the status
J
JinHai-CN 已提交
552 553 554
        expected: status not ok
        '''
        vector = gen_single_vector(int(dim)//2)
D
del-zhenwu 已提交
555
        status, ids = connect.insert(collection, vector)
J
JinHai-CN 已提交
556 557
        assert not status.OK()

D
del-zhenwu 已提交
558
    def test_insert_dim_not_matched(self, connect, collection):
J
JinHai-CN 已提交
559
        '''
X
Xiaohai Xu 已提交
560 561
        target: test add vectors, the vector dimension is not equal to the collection dimension
        method: the vectors dimension is half of the collection dimension, check the status
J
JinHai-CN 已提交
562 563 564 565
        expected: status not ok
        '''
        nq = 5
        vectors = gen_vectors(nq, int(dim)//2)
D
del-zhenwu 已提交
566
        status, ids = connect.insert(collection, vectors)
J
JinHai-CN 已提交
567 568
        assert not status.OK()

X
Xiaohai Xu 已提交
569
    def test_add_vector_query_after_sleep(self, connect, collection):
J
JinHai-CN 已提交
570 571 572 573 574 575 576
        '''
        target: test add vectors, and search it after sleep
        method: set vector[0][1] as query vectors
        expected: status ok and result length is 1
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
577
        status, ids = connect.insert(collection, vectors)
X
Xiaohai Xu 已提交
578
        connect.flush([collection])
D
del-zhenwu 已提交
579
        status, result = connect.search(collection, 1, [vectors[0]])
J
JinHai-CN 已提交
580 581 582
        assert status.OK()
        assert len(result) == 1

583 584
    @pytest.mark.level(2)
    @pytest.mark.timeout(30)
585
    def test_collection_add_rows_count_multi_threading(self, args, collection):
586
        '''
X
Xiaohai Xu 已提交
587 588
        target: test collection rows_count is correct or not with multi threading
        method: create collection and add vectors in it(idmap),
D
del-zhenwu 已提交
589
            assert the value returned by count_entities method is equal to length of vectors
590 591
        expected: the count is equal to the length of vectors
        '''
592 593
        if args["handler"] == "HTTP":
            pytest.skip("Skip test in http mode")
594 595
        thread_num = 8
        threads = []
D
del-zhenwu 已提交
596
        milvus = get_milvus(host=args["ip"], port=args["port"], handler=args["handler"], try_connect=False)
597 598 599
        vectors = gen_vectors(nb, dim)
        def add(thread_i):
            logging.getLogger().info("In thread-%d" % thread_i)
D
del-zhenwu 已提交
600 601
            # milvus = get_milvus(host=args["ip"], port=args["port"], handler=args["handler"])
            # assert milvus
D
del-zhenwu 已提交
602
            status, result = milvus.insert(collection, records=vectors)
603
            assert status.OK()
X
Xiaohai Xu 已提交
604
            status = milvus.flush([collection])
605 606 607 608 609 610 611
            assert status.OK()
        for i in range(thread_num):
            x = threading.Thread(target=add, args=(i, ))
            threads.append(x)
            x.start()
        for th in threads:
            th.join()
D
del-zhenwu 已提交
612
        status, res = milvus.count_entities(collection)
613 614
        assert res == thread_num * nb

X
Xiaohai Xu 已提交
615
    def test_add_vector_multi_collections(self, connect):
J
JinHai-CN 已提交
616
        '''
X
Xiaohai Xu 已提交
617 618
        target: test add vectors is correct or not with multiple collections of L2
        method: create 50 collections and add vectors into them in turn
J
JinHai-CN 已提交
619 620 621 622
        expected: status ok
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
623
        collection_list = []
Z
zhenwu 已提交
624
        for i in range(20):
X
Xiaohai Xu 已提交
625 626 627
            collection_name = gen_unique_str('test_add_vector_multi_collections')
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
J
JinHai-CN 已提交
628 629 630
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
631
            connect.create_collection(param)
Z
zhenwu 已提交
632 633
        for j in range(5):
            for i in range(20):
D
del-zhenwu 已提交
634
                status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
J
JinHai-CN 已提交
635 636
                assert status.OK()

D
del-zhenwu 已提交
637
class TestAddAsync:
Z
zw 已提交
638 639 640 641 642
    @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 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    @pytest.fixture(
        scope="function",
        params=[
            1,
            1000
        ],
    )
    def insert_count(self, request):
        yield request.param

    def check_status(self, status, result):
        logging.getLogger().info("In callback check status")
        assert status.OK()

    def check_status_not_ok(self, status, result):
        logging.getLogger().info("In callback check status")
        assert not status.OK()


    def test_insert_async(self, connect, collection, insert_count):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
670
        future = connect.insert(collection, insert_vec_list, _async=True)
D
del-zhenwu 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684
        status, ids = future.result()
        connect.flush([collection])
        assert len(ids) == nb
        assert status.OK()

    @pytest.mark.level(2)
    def test_insert_async_false(self, connect, collection, insert_count):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
685
        status, ids = connect.insert(collection, insert_vec_list, _async=False)
D
del-zhenwu 已提交
686 687 688 689 690 691 692 693 694 695 696 697
        connect.flush([collection])
        assert len(ids) == nb
        assert status.OK()

    def test_insert_async_callback(self, connect, collection, insert_count):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
698
        future = connect.insert(collection, insert_vec_list, _async=True, _callback=self.check_status)
D
del-zhenwu 已提交
699 700 701 702 703 704 705 706 707 708 709
        future.done()

    @pytest.mark.level(2)
    def test_insert_async_long(self, connect, collection):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = 50000
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
710
        future = connect.insert(collection, insert_vec_list, _async=True, _callback=self.check_status)
D
del-zhenwu 已提交
711 712 713 714
        status, result = future.result()
        assert status.OK()
        assert len(result) == nb 
        connect.flush([collection])
D
del-zhenwu 已提交
715
        status, count = connect.count_entities(collection)
D
del-zhenwu 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728
        assert status.OK()
        logging.getLogger().info(status)
        logging.getLogger().info(count)
        assert count == nb

    def test_insert_async_callback_timeout(self, connect, collection):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = 100000
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
729
        future = connect.insert(collection, insert_vec_list, _async=True, _callback=self.check_status, timeout=1)
D
del-zhenwu 已提交
730 731 732 733 734 735 736 737 738 739
        future.done()

    def test_insert_async_invalid_params(self, connect, collection):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        insert_vec_list = gen_vectors(nb, dim)
        collection_new = gen_unique_str()
D
del-zhenwu 已提交
740
        future = connect.insert(collection_new, insert_vec_list, _async=True)
D
del-zhenwu 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753
        status, result = future.result()
        assert not status.OK()

    # TODO: add assertion
    def test_insert_async_invalid_params_raise_exception(self, connect, collection):
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        insert_vec_list = []
        collection_new = gen_unique_str()
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
754
            future = connect.insert(collection_new, insert_vec_list, _async=True)
D
del-zhenwu 已提交
755 756


J
JinHai-CN 已提交
757 758 759
class TestAddIP:
    """
    ******************************************************************
D
del-zhenwu 已提交
760
      The following cases are used to test `insert / index / search / delete` mixed function
J
JinHai-CN 已提交
761 762
    ******************************************************************
    """
Z
zhenwu 已提交
763 764
    @pytest.fixture(
        scope="function",
765
        params=gen_simple_index()
Z
zhenwu 已提交
766
    )
767
    def get_simple_index(self, request, connect):
G
groot 已提交
768
        if str(connect._cmd("mode")[1]) == "CPU":
Z
zhenwu 已提交
769
            if request.param["index_type"] == IndexType.IVF_SQ8H:
G
groot 已提交
770 771 772
                pytest.skip("sq8h not support in cpu mode")
        if request.param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip PQ Temporary")
Z
zhenwu 已提交
773
        return request.param
J
JinHai-CN 已提交
774

X
Xiaohai Xu 已提交
775
    def test_add_vector_create_collection(self, connect, ip_collection):
J
JinHai-CN 已提交
776
        '''
X
Xiaohai Xu 已提交
777 778
        target: test add vector, then create collection again
        method: add vector and create collection
J
JinHai-CN 已提交
779 780 781
        expected: status not ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
782
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
783
        param = {'collection_name': ip_collection,
J
JinHai-CN 已提交
784 785 786
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
787
        status = connect.create_collection(param)
J
JinHai-CN 已提交
788 789
        assert not status.OK()

X
Xiaohai Xu 已提交
790
    def test_add_vector_has_collection(self, connect, ip_collection):
J
JinHai-CN 已提交
791
        '''
X
Xiaohai Xu 已提交
792 793 794
        target: test add vector, then check collection existence
        method: add vector and call Hascollection
        expected: collection exists, status ok
J
JinHai-CN 已提交
795 796
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
797
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
798
        assert assert_has_collection(connect, ip_collection)
J
JinHai-CN 已提交
799 800

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
801
    def test_drop_collection_add_vector(self, connect, ip_collection):
J
JinHai-CN 已提交
802
        '''
X
Xiaohai Xu 已提交
803 804
        target: test add vector after collection deleted
        method: delete collection and add vector
J
JinHai-CN 已提交
805 806
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
807
        status = connect.drop_collection(ip_collection)
J
JinHai-CN 已提交
808
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
809
        status, ids = connect.insert(ip_collection, vector)
J
JinHai-CN 已提交
810 811 812
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
813
    def test_drop_collection_add_vector_another(self, connect, ip_collection):
J
JinHai-CN 已提交
814
        '''
X
Xiaohai Xu 已提交
815 816
        target: test add vector to collection_1 after collection_2 deleted
        method: delete collection_2 and add vector to collection_1
J
JinHai-CN 已提交
817 818
        expected: status ok
        '''
X
Xiaohai Xu 已提交
819
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
820 821 822
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
823 824
        status = connect.create_collection(param)
        status = connect.drop_collection(ip_collection)
J
JinHai-CN 已提交
825
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
826
        status, ids = connect.insert(param['collection_name'], vector)
J
JinHai-CN 已提交
827 828 829
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
830
    def test_add_vector_drop_collection(self, connect, ip_collection):
J
JinHai-CN 已提交
831
        '''
X
Xiaohai Xu 已提交
832 833
        target: test delete collection after add vector
        method: add vector and delete collection
J
JinHai-CN 已提交
834 835 836
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
837
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
838
        status = connect.drop_collection(ip_collection)
J
JinHai-CN 已提交
839 840 841
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
842
    def test_add_vector_delete_another_collection(self, connect, ip_collection):
J
JinHai-CN 已提交
843
        '''
X
Xiaohai Xu 已提交
844 845
        target: test delete collection_1 collection after add vector to collection_2
        method: add vector and delete collection
J
JinHai-CN 已提交
846 847
        expected: status ok
        '''
X
Xiaohai Xu 已提交
848
        param = {'collection_name': 'test_add_vector_delete_another_collection',
J
JinHai-CN 已提交
849 850 851
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
852
        status = connect.create_collection(param)
J
JinHai-CN 已提交
853
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
854
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
855
        status = connect.drop_collection(param['collection_name'])
J
JinHai-CN 已提交
856 857 858
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
859
    def test_add_vector_sleep_drop_collection(self, connect, ip_collection):
J
JinHai-CN 已提交
860
        '''
X
Xiaohai Xu 已提交
861 862
        target: test delete collection after add vector for a while
        method: add vector, sleep, and delete collection
J
JinHai-CN 已提交
863 864 865
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
866
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
867 868
        connect.flush([ip_collection])
        status = connect.drop_collection(ip_collection)
J
JinHai-CN 已提交
869 870 871
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
872
    def test_add_vector_sleep_delete_another_collection(self, connect, ip_collection):
J
JinHai-CN 已提交
873
        '''
X
Xiaohai Xu 已提交
874 875
        target: test delete collection_1 collection after add vector to collection_2 for a while
        method: add vector , sleep, and delete collection
J
JinHai-CN 已提交
876 877
        expected: status ok
        '''
X
Xiaohai Xu 已提交
878
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
879 880 881
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
882
        status = connect.create_collection(param)
J
JinHai-CN 已提交
883
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
884
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
885 886
        connect.flush([ip_collection])
        status = connect.drop_collection(param['collection_name'])
J
JinHai-CN 已提交
887 888 889
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
890
    def test_create_index_add_vector(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
891 892 893 894 895
        '''
        target: test add vector after build index
        method: build index and add vector
        expected: status ok
        '''
896 897
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
898
        status = connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
899
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
900
        status, ids = connect.insert(ip_collection, vector)
J
JinHai-CN 已提交
901 902 903
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
904
    def test_create_index_add_vector_another(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
905
        '''
X
Xiaohai Xu 已提交
906
        target: test add vector to collection_2 after build index for collection_1
J
JinHai-CN 已提交
907 908 909
        method: build index and add vector
        expected: status ok
        '''
910 911
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"] 
X
Xiaohai Xu 已提交
912
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
913 914 915
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
916 917
        status = connect.create_collection(param)
        status = connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
918
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
919
        status, ids = connect.insert(ip_collection, vector)
J
JinHai-CN 已提交
920 921 922
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
923
    def test_add_vector_create_index(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
924 925 926 927 928
        '''
        target: test build index add after vector
        method: add vector and build index
        expected: status ok
        '''
929 930
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
J
JinHai-CN 已提交
931
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
932
        status, ids = connect.insert(ip_collection, vector)
Z
zhenwu 已提交
933
        status, mode = connect._cmd("mode")
J
JinHai-CN 已提交
934
        assert status.OK()
X
Xiaohai Xu 已提交
935
        status = connect.create_index(ip_collection, index_type, index_param)
936
        if str(mode) == "GPU" and (index_type == IndexType.IVF_PQ):
Z
zhenwu 已提交
937 938 939
            assert not status.OK()
        else:
            assert status.OK()
J
JinHai-CN 已提交
940 941

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
942
    def test_add_vector_create_index_another(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
943
        '''
X
Xiaohai Xu 已提交
944
        target: test add vector to collection_2 after build index for collection_1
J
JinHai-CN 已提交
945 946 947
        method: build index and add vector
        expected: status ok
        '''
948 949
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
950
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
951 952 953
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
954
        status = connect.create_collection(param)
J
JinHai-CN 已提交
955
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
956
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
957
        status = connect.create_index(param['collection_name'], index_type, index_param)
J
JinHai-CN 已提交
958 959 960
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
961
    def test_add_vector_sleep_create_index(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
962 963 964 965 966
        '''
        target: test build index add after vector for a while
        method: add vector and build index
        expected: status ok
        '''
967 968 969
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        if index_type == IndexType.IVF_PQ:
Z
zhenwu 已提交
970
            pytest.skip("Skip some PQ cases")
J
JinHai-CN 已提交
971
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
972
        status, ids = connect.insert(ip_collection, vector)
973 974
        assert status.OK()
        time.sleep(add_interval_time)
X
Xiaohai Xu 已提交
975
        status = connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
976 977 978
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
979
    def test_add_vector_sleep_create_index_another(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
980
        '''
X
Xiaohai Xu 已提交
981
        target: test add vector to collection_2 after build index for collection_1 for a while
J
JinHai-CN 已提交
982 983 984
        method: build index and add vector
        expected: status ok
        '''
985 986
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
987
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
988 989 990
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
991
        status = connect.create_collection(param)
J
JinHai-CN 已提交
992
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
993
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
994 995
        connect.flush([ip_collection])
        status = connect.create_index(param['collection_name'], index_type, index_param)
J
JinHai-CN 已提交
996 997 998
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
999
    def test_search_vector_add_vector(self, connect, ip_collection):
J
JinHai-CN 已提交
1000
        '''
X
Xiaohai Xu 已提交
1001 1002
        target: test add vector after search collection
        method: search collection and add vector
J
JinHai-CN 已提交
1003 1004 1005
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
1006 1007
        status, result = connect.search(ip_collection, 1, vector)
        status, ids = connect.insert(ip_collection, vector)
J
JinHai-CN 已提交
1008 1009 1010
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
1011
    def test_search_vector_add_vector_another(self, connect, ip_collection):
J
JinHai-CN 已提交
1012
        '''
X
Xiaohai Xu 已提交
1013 1014
        target: test add vector to collection_1 after search collection_2
        method: search collection and add vector
J
JinHai-CN 已提交
1015 1016
        expected: status ok
        '''
X
Xiaohai Xu 已提交
1017
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
1018 1019 1020
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
1021
        status = connect.create_collection(param)
J
JinHai-CN 已提交
1022
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
1023 1024
        status, result = connect.search(ip_collection, 1, vector)
        status, ids = connect.insert(param['collection_name'], vector)
J
JinHai-CN 已提交
1025 1026 1027
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
1028
    def test_add_vector_search_vector(self, connect, ip_collection):
J
JinHai-CN 已提交
1029 1030
        '''
        target: test search vector after add vector
X
Xiaohai Xu 已提交
1031
        method: add vector and search collection
J
JinHai-CN 已提交
1032 1033 1034
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
1035
        status, ids = connect.insert(ip_collection, vector)
1036
        assert status.OK()
X
Xiaohai Xu 已提交
1037
        connect.flush([ip_collection])
D
del-zhenwu 已提交
1038
        status, result = connect.search(ip_collection, 1, vector)
J
JinHai-CN 已提交
1039 1040 1041
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
1042
    def test_add_vector_search_vector_another(self, connect, ip_collection):
J
JinHai-CN 已提交
1043
        '''
X
Xiaohai Xu 已提交
1044 1045
        target: test add vector to collection_1 after search collection_2
        method: search collection and add vector
J
JinHai-CN 已提交
1046 1047
        expected: status ok
        '''
X
Xiaohai Xu 已提交
1048
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
1049 1050 1051
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
1052
        status = connect.create_collection(param)
J
JinHai-CN 已提交
1053
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
1054
        status, ids = connect.insert(ip_collection, vector)
X
Xiaohai Xu 已提交
1055
        connect.flush([ip_collection])
D
del-zhenwu 已提交
1056
        status, result = connect.search(param['collection_name'], 1, vector)
J
JinHai-CN 已提交
1057 1058 1059
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
1060
    def test_add_vector_sleep_search_vector(self, connect, ip_collection):
J
JinHai-CN 已提交
1061 1062
        '''
        target: test search vector after add vector after a while
X
Xiaohai Xu 已提交
1063
        method: add vector, sleep, and search collection
J
JinHai-CN 已提交
1064 1065 1066
        expected: status ok
        '''
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
1067
        status, ids = connect.insert(ip_collection, vector)
1068
        time.sleep(add_interval_time)
D
del-zhenwu 已提交
1069
        status, result = connect.search(ip_collection, 1, vector)
J
JinHai-CN 已提交
1070 1071 1072
        assert status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
X
Xiaohai Xu 已提交
1073
    def test_add_vector_sleep_search_vector_another(self, connect, ip_collection):
J
JinHai-CN 已提交
1074
        '''
X
Xiaohai Xu 已提交
1075 1076
        target: test add vector to collection_1 after search collection_2 a while
        method: search collection , sleep, and add vector
J
JinHai-CN 已提交
1077 1078
        expected: status ok
        '''
X
Xiaohai Xu 已提交
1079
        param = {'collection_name': gen_unique_str(),
J
JinHai-CN 已提交
1080 1081 1082
                 'dimension': dim,
                 'index_file_size': index_file_size,
                 'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
1083
        status = connect.create_collection(param)
J
JinHai-CN 已提交
1084
        vector = gen_single_vector(dim)
D
del-zhenwu 已提交
1085
        status, ids = connect.insert(ip_collection, vector)
1086 1087
        assert status.OK()
        time.sleep(add_interval_time)
D
del-zhenwu 已提交
1088
        status, result = connect.search(param['collection_name'], 1, vector)
J
JinHai-CN 已提交
1089 1090 1091 1092
        assert status.OK()

    """
    ******************************************************************
D
del-zhenwu 已提交
1093
      The following cases are used to test `insert` function
J
JinHai-CN 已提交
1094 1095 1096 1097
    ******************************************************************
    """

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
1098
    def test_insert_ids(self, connect, ip_collection):
J
JinHai-CN 已提交
1099
        '''
X
Xiaohai Xu 已提交
1100 1101 1102
        target: test add vectors in collection, use customize ids
        method: create collection and add vectors in it, check the ids returned and the collection length after vectors added
        expected: the length of ids and the collection row count
J
JinHai-CN 已提交
1103
        '''
1104
        nq = 5; top_k = 1
J
JinHai-CN 已提交
1105 1106
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
D
del-zhenwu 已提交
1107
        status, ids = connect.insert(ip_collection, vectors, ids)
J
JinHai-CN 已提交
1108
        assert status.OK()
X
Xiaohai Xu 已提交
1109
        connect.flush([ip_collection])
J
JinHai-CN 已提交
1110 1111
        assert len(ids) == nq
        # check search result
D
del-zhenwu 已提交
1112
        status, result = connect.search(ip_collection, top_k, vectors)
J
JinHai-CN 已提交
1113 1114 1115 1116 1117 1118
        logging.getLogger().info(result)
        assert len(result) == nq
        for i in range(nq):
            assert result[i][0].id == i

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
1119
    def test_insert_twice_ids_no_ids(self, connect, ip_collection):
J
JinHai-CN 已提交
1120
        '''
D
del-zhenwu 已提交
1121
        target: check the result of insert, with params ids and no ids
J
JinHai-CN 已提交
1122 1123 1124
        method: test add vectors twice, use customize ids first, and then use no ids
        expected: status not OK 
        '''
1125
        nq = 5; top_k = 1
J
JinHai-CN 已提交
1126 1127
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
D
del-zhenwu 已提交
1128
        status, ids = connect.insert(ip_collection, vectors, ids)
J
JinHai-CN 已提交
1129
        assert status.OK()
D
del-zhenwu 已提交
1130
        status, ids = connect.insert(ip_collection, vectors)
J
JinHai-CN 已提交
1131 1132 1133 1134 1135
        logging.getLogger().info(status)
        logging.getLogger().info(ids)
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
1136
    def test_insert_twice_not_ids_ids(self, connect, ip_collection):
J
JinHai-CN 已提交
1137
        '''
D
del-zhenwu 已提交
1138
        target: check the result of insert, with params ids and no ids
J
JinHai-CN 已提交
1139 1140 1141
        method: test add vectors twice, use not ids first, and then use customize ids
        expected: status not OK 
        '''
1142
        nq = 5; top_k = 1
J
JinHai-CN 已提交
1143 1144
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
D
del-zhenwu 已提交
1145
        status, ids = connect.insert(ip_collection, vectors)
J
JinHai-CN 已提交
1146
        assert status.OK()
D
del-zhenwu 已提交
1147
        status, ids = connect.insert(ip_collection, vectors, ids)
J
JinHai-CN 已提交
1148 1149 1150 1151 1152
        logging.getLogger().info(status)
        logging.getLogger().info(ids)
        assert not status.OK()

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
1153
    def test_insert_ids_length_not_match(self, connect, ip_collection):
J
JinHai-CN 已提交
1154
        '''
X
Xiaohai Xu 已提交
1155 1156
        target: test add vectors in collection, use customize ids, len(ids) != len(vectors)
        method: create collection and add vectors in it
J
JinHai-CN 已提交
1157 1158 1159 1160 1161 1162
        expected: raise an exception
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(1, nq)]
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1163
            status, ids = connect.insert(ip_collection, vectors, ids)
J
JinHai-CN 已提交
1164 1165 1166 1167 1168 1169 1170 1171

    @pytest.fixture(
        scope="function",
        params=gen_invalid_vector_ids()
    )
    def get_vector_id(self, request):
        yield request.param

Z
zhenwu 已提交
1172
    @pytest.mark.level(2)
D
del-zhenwu 已提交
1173
    def test_insert_ids_invalid(self, connect, ip_collection, get_vector_id):
J
JinHai-CN 已提交
1174
        '''
X
Xiaohai Xu 已提交
1175 1176
        target: test add vectors in collection, use customize ids, which are not int64
        method: create collection and add vectors in it
J
JinHai-CN 已提交
1177 1178 1179 1180 1181 1182 1183
        expected: raise an exception
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
        vector_id = get_vector_id
        ids = [vector_id for i in range(nq)]
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1184
            status, ids = connect.insert(ip_collection, vectors, ids)
J
JinHai-CN 已提交
1185 1186

    @pytest.mark.timeout(ADD_TIMEOUT)
D
del-zhenwu 已提交
1187
    def test_insert(self, connect, ip_collection):
J
JinHai-CN 已提交
1188
        '''
X
Xiaohai Xu 已提交
1189 1190 1191
        target: test add vectors in collection created before
        method: create collection and add vectors in it, check the ids returned and the collection length after vectors added
        expected: the length of ids and the collection row count
J
JinHai-CN 已提交
1192 1193 1194
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
1195
        status, ids = connect.insert(ip_collection, vectors)
J
JinHai-CN 已提交
1196 1197 1198
        assert status.OK()
        assert len(ids) == nq

D
del-zhenwu 已提交
1199
    # @pytest.mark.level(2)
D
del-zhenwu 已提交
1200
    # def test_insert_without_connect(self, dis_connect, ip_collection):
D
del-zhenwu 已提交
1201 1202 1203 1204 1205 1206 1207 1208
    #     '''
    #     target: test add vectors without connection
    #     method: create collection and add vectors in it, check if added successfully
    #     expected: raise exception
    #     '''
    #     nq = 5
    #     vectors = gen_vectors(nq, dim)
    #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1209
    #         status, ids = dis_connect.insert(ip_collection, vectors)
J
JinHai-CN 已提交
1210

X
Xiaohai Xu 已提交
1211
    def test_add_vector_dim_not_matched(self, connect, ip_collection):
J
JinHai-CN 已提交
1212
        '''
X
Xiaohai Xu 已提交
1213 1214
        target: test add vector, the vector dimension is not equal to the collection dimension
        method: the vector dimension is half of the collection dimension, check the status
J
JinHai-CN 已提交
1215 1216 1217
        expected: status not ok
        '''
        vector = gen_single_vector(int(dim)//2)
D
del-zhenwu 已提交
1218
        status, ids = connect.insert(ip_collection, vector)
J
JinHai-CN 已提交
1219 1220
        assert not status.OK()

D
del-zhenwu 已提交
1221
    def test_insert_dim_not_matched(self, connect, ip_collection):
J
JinHai-CN 已提交
1222
        '''
X
Xiaohai Xu 已提交
1223 1224
        target: test add vectors, the vector dimension is not equal to the collection dimension
        method: the vectors dimension is half of the collection dimension, check the status
J
JinHai-CN 已提交
1225 1226 1227 1228
        expected: status not ok
        '''
        nq = 5
        vectors = gen_vectors(nq, int(dim)//2)
D
del-zhenwu 已提交
1229
        status, ids = connect.insert(ip_collection, vectors)
J
JinHai-CN 已提交
1230 1231
        assert not status.OK()

X
Xiaohai Xu 已提交
1232
    def test_add_vector_query_after_sleep(self, connect, ip_collection):
J
JinHai-CN 已提交
1233 1234 1235 1236 1237 1238 1239
        '''
        target: test add vectors, and search it after sleep
        method: set vector[0][1] as query vectors
        expected: status ok and result length is 1
        '''
        nq = 5
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
1240
        status, ids = connect.insert(ip_collection, vectors)
1241
        time.sleep(add_interval_time)
D
del-zhenwu 已提交
1242
        status, result = connect.search(ip_collection, 1, [vectors[0]])
J
JinHai-CN 已提交
1243 1244 1245
        assert status.OK()
        assert len(result) == 1

X
Xiaohai Xu 已提交
1246
    def test_add_vector_multi_collections(self, connect):
J
JinHai-CN 已提交
1247
        '''
X
Xiaohai Xu 已提交
1248 1249
        target: test add vectors is correct or not with multiple collections of IP
        method: create 50 collections and add vectors into them in turn
J
JinHai-CN 已提交
1250 1251 1252 1253
        expected: status ok
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
1254
        collection_list = []
Z
zhenwu 已提交
1255
        for i in range(20):
X
Xiaohai Xu 已提交
1256 1257 1258
            collection_name = gen_unique_str('test_add_vector_multi_collections')
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
J
JinHai-CN 已提交
1259 1260 1261
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
X
Xiaohai Xu 已提交
1262
            connect.create_collection(param)
J
JinHai-CN 已提交
1263
        for j in range(10):
Z
zhenwu 已提交
1264
            for i in range(20):
D
del-zhenwu 已提交
1265
                status, ids = connect.insert(collection_name=collection_list[i], records=vectors)
J
JinHai-CN 已提交
1266 1267 1268 1269 1270 1271 1272 1273
                assert status.OK()

class TestAddAdvance:
    @pytest.fixture(
        scope="function",
        params=[
            1,
            1000,
D
del-zhenwu 已提交
1274
            6000
J
JinHai-CN 已提交
1275 1276 1277 1278 1279
        ],
    )
    def insert_count(self, request):
        yield request.param

X
Xiaohai Xu 已提交
1280
    def test_insert_much(self, connect, collection, insert_count):
J
JinHai-CN 已提交
1281 1282 1283 1284 1285 1286 1287
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
1288
        status, ids = connect.insert(collection, insert_vec_list)
J
JinHai-CN 已提交
1289 1290 1291
        assert len(ids) == nb
        assert status.OK()

X
Xiaohai Xu 已提交
1292
    def test_insert_much_ip(self, connect, ip_collection, insert_count):
J
JinHai-CN 已提交
1293 1294 1295 1296 1297 1298 1299
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        insert_vec_list = gen_vectors(nb, dim)
D
del-zhenwu 已提交
1300
        status, ids = connect.insert(ip_collection, insert_vec_list)
J
JinHai-CN 已提交
1301 1302 1303
        assert len(ids) == nb
        assert status.OK()

X
Xiaohai Xu 已提交
1304
    def test_insert_much_jaccard(self, connect, jac_collection, insert_count):
G
groot 已提交
1305 1306 1307 1308 1309 1310 1311
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        tmp, insert_vec_list = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
1312
        status, ids = connect.insert(jac_collection, insert_vec_list)
G
groot 已提交
1313 1314 1315
        assert len(ids) == nb
        assert status.OK()

X
Xiaohai Xu 已提交
1316
    def test_insert_much_hamming(self, connect, ham_collection, insert_count):
G
groot 已提交
1317 1318 1319 1320 1321 1322 1323
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        tmp, insert_vec_list = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
1324
        status, ids = connect.insert(ham_collection, insert_vec_list)
G
groot 已提交
1325 1326 1327
        assert len(ids) == nb
        assert status.OK()

X
Xiaohai Xu 已提交
1328
    def test_insert_much_tanimoto(self, connect, tanimoto_collection, insert_count):
G
groot 已提交
1329 1330 1331 1332 1333 1334 1335
        '''
        target: test add vectors with different length of vectors
        method: set different vectors as add method params
        expected: length of ids is equal to the length of vectors
        '''
        nb = insert_count
        tmp, insert_vec_list = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
1336
        status, ids = connect.insert(tanimoto_collection, insert_vec_list)
G
groot 已提交
1337 1338 1339
        assert len(ids) == nb
        assert status.OK()

Z
zhenwu 已提交
1340 1341

class TestNameInvalid(object):
J
JinHai-CN 已提交
1342
    """
X
Xiaohai Xu 已提交
1343
    Test adding vectors with invalid collection names
J
JinHai-CN 已提交
1344 1345 1346
    """
    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
1347
        params=gen_invalid_collection_names()
J
JinHai-CN 已提交
1348
    )
X
Xiaohai Xu 已提交
1349
    def get_collection_name(self, request):
J
JinHai-CN 已提交
1350 1351
        yield request.param

Z
zhenwu 已提交
1352 1353
    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
1354
        params=gen_invalid_collection_names()
Z
zhenwu 已提交
1355 1356 1357 1358
    )
    def get_tag_name(self, request):
        yield request.param

J
JinHai-CN 已提交
1359
    @pytest.mark.level(2)
D
del-zhenwu 已提交
1360
    def test_insert_with_invalid_collection_name(self, connect, get_collection_name):
X
Xiaohai Xu 已提交
1361
        collection_name = get_collection_name
J
JinHai-CN 已提交
1362
        vectors = gen_vectors(1, dim)
D
del-zhenwu 已提交
1363
        status, result = connect.insert(collection_name, vectors)
J
JinHai-CN 已提交
1364 1365
        assert not status.OK()

Z
zhenwu 已提交
1366
    @pytest.mark.level(2)
D
del-zhenwu 已提交
1367
    def test_insert_with_invalid_tag_name(self, connect, get_collection_name, get_tag_name):
X
Xiaohai Xu 已提交
1368
        collection_name = get_collection_name
Z
zhenwu 已提交
1369 1370
        tag_name = get_tag_name
        vectors = gen_vectors(1, dim)
D
del-zhenwu 已提交
1371
        status, result = connect.insert(collection_name, vectors, partition_tag=tag_name)
Z
zhenwu 已提交
1372 1373
        assert not status.OK()

J
JinHai-CN 已提交
1374

S
sahuang 已提交
1375
class TestAddCollectionVectorsInvalid(object):
J
JinHai-CN 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
    single_vector = gen_single_vector(dim)
    vectors = gen_vectors(2, dim)

    """
    Test adding vectors with invalid vectors
    """
    @pytest.fixture(
        scope="function",
        params=gen_invalid_vectors()
    )
    def gen_vector(self, request):
        yield request.param

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1390
    def test_add_vector_with_invalid_vectors(self, connect, collection, gen_vector):
J
JinHai-CN 已提交
1391 1392 1393
        tmp_single_vector = copy.deepcopy(self.single_vector)
        tmp_single_vector[0][1] = gen_vector
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1394
            status, result = connect.insert(collection, tmp_single_vector)
J
JinHai-CN 已提交
1395

Z
zhenwu 已提交
1396
    @pytest.mark.level(2)
D
del-zhenwu 已提交
1397
    def test_insert_with_invalid_vectors(self, connect, collection, gen_vector):
J
JinHai-CN 已提交
1398 1399 1400
        tmp_vectors = copy.deepcopy(self.vectors)
        tmp_vectors[1][1] = gen_vector
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1401
            status, result = connect.insert(collection, tmp_vectors)
G
groot 已提交
1402 1403

    @pytest.mark.level(2)
D
del-zhenwu 已提交
1404
    def test_insert_with_invalid_vectors_jaccard(self, connect, jac_collection, gen_vector):
G
groot 已提交
1405 1406 1407
        tmp_vectors = copy.deepcopy(self.vectors)
        tmp_vectors[1][1] = gen_vector
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1408
            status, result = connect.insert(jac_collection, tmp_vectors)
G
groot 已提交
1409 1410

    @pytest.mark.level(2)
D
del-zhenwu 已提交
1411
    def test_insert_with_invalid_vectors_hamming(self, connect, ham_collection, gen_vector):
G
groot 已提交
1412 1413 1414
        tmp_vectors = copy.deepcopy(self.vectors)
        tmp_vectors[1][1] = gen_vector
        with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
1415
            status, result = connect.insert(ham_collection, tmp_vectors)