test_index.py 79.8 KB
Newer Older
J
JinHai-CN 已提交
1 2 3 4 5 6 7 8 9 10
"""
   For testing index operations, including `create_index`, `describe_index` and `drop_index` interfaces
"""
import logging
import pytest
import time
import pdb
import threading
from multiprocessing import Pool, Process
import numpy
Z
zhenwu 已提交
11
import sklearn.preprocessing
12
from milvus import IndexType, MetricType
J
JinHai-CN 已提交
13 14
from utils import *

G
groot 已提交
15
nb = 6000
J
JinHai-CN 已提交
16
dim = 128
G
groot 已提交
17
index_file_size = 10
J
JinHai-CN 已提交
18
vectors = gen_vectors(nb, dim)
Z
zhenwu 已提交
19
vectors = sklearn.preprocessing.normalize(vectors, axis=1, norm='l2')
J
JinHai-CN 已提交
20
vectors = vectors.tolist()
Z
zhenwu 已提交
21
BUILD_TIMEOUT = 300
J
JinHai-CN 已提交
22
nprobe = 1
Z
zhenwu 已提交
23
tag = "1970-01-01"
24 25
NLIST = 4046
INVALID_NLIST = 100000000
J
JinHai-CN 已提交
26 27 28 29 30


class TestIndexBase:
    @pytest.fixture(
        scope="function",
31
        params=gen_index()
J
JinHai-CN 已提交
32
    )
33
    def get_index(self, request, connect):
G
groot 已提交
34
        if str(connect._cmd("mode")[1]) == "CPU":
35
            if request.param["index_type"] == IndexType.IVF_SQ8H:
36
                pytest.skip("sq8h not support in CPU mode")
37 38 39
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
40
        return request.param
J
JinHai-CN 已提交
41 42 43

    @pytest.fixture(
        scope="function",
44
        params=gen_simple_index()
J
JinHai-CN 已提交
45
    )
46
    def get_simple_index(self, request, connect):
G
groot 已提交
47
        if str(connect._cmd("mode")[1]) == "CPU":
Z
zhenwu 已提交
48
            if request.param["index_type"] == IndexType.IVF_SQ8H:
49
                pytest.skip("sq8h not support in CPU mode")
50 51 52
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
Z
zhenwu 已提交
53
        return request.param
54

J
JinHai-CN 已提交
55 56 57 58 59 60 61
    """
    ******************************************************************
      The following cases are used to test `create_index` function
    ******************************************************************
    """

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
62
    def test_create_index(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
63 64
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
65
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
66 67
        expected: return code equals to 0, and search success
        '''
68 69 70
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
71 72
        status, ids = connect.add_vectors(collection, vectors)
        status = connect.create_index(collection, index_type, index_param)
73
        assert status.OK()
J
JinHai-CN 已提交
74

Z
zhenwu 已提交
75
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
76
    def test_create_index_no_vectors(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
77 78
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
79
        method: create collection and add vectors in it, create index
Z
zhenwu 已提交
80 81
        expected: return code equals to 0, and search success
        '''
82 83 84
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
85
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
86 87
        assert status.OK()

Z
zhenwu 已提交
88
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
89
    def test_create_index_partition(self, connect, collection, get_simple_index):
Z
zhenwu 已提交
90 91
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
92
        method: create collection, create partition, and add vectors in it, create index
Z
zhenwu 已提交
93 94
        expected: return code equals to 0, and search success
        '''
95 96 97
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
98 99 100
        status = connect.create_partition(collection, tag)
        status, ids = connect.add_vectors(collection, vectors, partition_tag=tag)
        status = connect.create_index(collection, index_type, index_param)
101 102 103
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
104
    def test_create_index_partition_flush(self, connect, collection, get_simple_index):
105 106
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
107
        method: create collection, create partition, and add vectors in it, create index
108 109 110 111 112
        expected: return code equals to 0, and search success
        '''
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
113 114
        status = connect.create_partition(collection, tag)
        status, ids = connect.add_vectors(collection, vectors, partition_tag=tag)
115
        connect.flush()
X
Xiaohai Xu 已提交
116
        status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
117 118
        assert status.OK()

J
JinHai-CN 已提交
119
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
120
    def test_create_index_without_connect(self, dis_connect, collection):
J
JinHai-CN 已提交
121 122
        '''
        target: test create index without connection
X
Xiaohai Xu 已提交
123
        method: create collection and add vectors in it, check if added successfully
J
JinHai-CN 已提交
124 125
        expected: raise exception
        '''
126
        nlist = NLIST
127 128
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": nlist}
J
JinHai-CN 已提交
129
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
130
            status = dis_connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
131 132

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
133
    def test_create_index_search_with_query_vectors(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
134 135
        '''
        target: test create index interface, search with more query vectors
X
Xiaohai Xu 已提交
136
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
137 138
        expected: return code equals to 0, and search success
        '''
139 140 141
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
142 143 144
        status, ids = connect.add_vectors(collection, vectors)
        status = connect.create_index(collection, index_type, index_param)
        logging.getLogger().info(connect.describe_index(collection))
J
JinHai-CN 已提交
145 146
        query_vecs = [vectors[0], vectors[1], vectors[2]]
        top_k = 5
147
        search_param = get_search_param(index_type)
X
Xiaohai Xu 已提交
148
        status, result = connect.search_vectors(collection, top_k, query_vecs, params=search_param)
J
JinHai-CN 已提交
149 150 151 152
        assert status.OK()
        assert len(result) == len(query_vecs)
        logging.getLogger().info(result)

153 154
    @pytest.mark.timeout(BUILD_TIMEOUT)
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
155
    def test_create_index_multithread(self, connect, collection, args):
156 157
        '''
        target: test create index interface with multiprocess
X
Xiaohai Xu 已提交
158
        method: create collection and add vectors in it, create index
159 160
        expected: return code equals to 0, and search success
        '''
X
Xiaohai Xu 已提交
161
        status, ids = connect.add_vectors(collection, vectors)
162 163

        def build(connect):
X
Xiaohai Xu 已提交
164
            status = connect.create_index(collection, IndexType.IVFLAT, {"nlist": NLIST})
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            assert status.OK()

        threads_num = 8
        threads = []
        uri = "tcp://%s:%s" % (args["ip"], args["port"])

        for i in range(threads_num):
            m = get_milvus(args["handler"])
            m.connect(uri=uri)
            t = threading.Thread(target=build, args=(m,))
            threads.append(t)
            t.start()
            time.sleep(0.2)
        for t in threads:
            t.join()

        query_vec = [vectors[0]]
        top_k = 1
        search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
184
        status, result = connect.search_vectors(collection, top_k, query_vec, params=search_param)
185 186 187 188 189
        assert len(result) == 1
        assert len(result[0]) == top_k
        assert result[0][0].distance == 0.0

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
190
    def test_create_index_multithread_multicollection(self, connect, args):
191 192
        '''
        target: test create index interface with multiprocess
X
Xiaohai Xu 已提交
193
        method: create collection and add vectors in it, create index
194 195 196 197 198 199
        expected: return code equals to 0, and search success
        '''
        threads_num = 8
        loop_num = 8
        threads = []

X
Xiaohai Xu 已提交
200
        collection = []
201 202
        j = 0
        while j < (threads_num*loop_num):
X
Xiaohai Xu 已提交
203 204 205
            collection_name = gen_unique_str("test_create_index_multiprocessing")
            collection.append(collection_name)
            param = {'collection_name': collection_name,
206 207 208
                     'dimension': dim,
                     'index_type': IndexType.FLAT,
                     'store_raw_vector': False}
X
Xiaohai Xu 已提交
209
            connect.create_collection(param)
210 211 212 213 214
            j = j + 1

        def create_index():
            i = 0
            while i < loop_num:
X
Xiaohai Xu 已提交
215 216
                # assert connect.has_collection(collection[ids*process_num+i])
                status, ids = connect.add_vectors(collection[ids*threads_num+i], vectors)
217

X
Xiaohai Xu 已提交
218
                status = connect.create_index(collection[ids*threads_num+i], IndexType.IVFLAT, {"nlist": NLIST})
219 220 221 222
                assert status.OK()
                query_vec = [vectors[0]]
                top_k = 1
                search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
223
                status, result = connect.search_vectors(collection[ids*threads_num+i], top_k, query_vec, params=search_param)
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                assert len(result) == 1
                assert len(result[0]) == top_k
                assert result[0][0].distance == 0.0
                i = i + 1

        uri = "tcp://%s:%s" % (args["ip"], args["port"])

        for i in range(threads_num):
            m = get_milvus(args["handler"])
            m.connect(uri=uri)
            ids = i
            t = threading.Thread(target=create_index, args=(m,ids))
            threads.append(t)
            t.start()
            time.sleep(0.2)
        for t in threads:
            t.join()

    @pytest.mark.timeout(BUILD_TIMEOUT)
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
244 245
    def test_create_index_a_multithreads(self, connect, collection, args):
        status, ids = connect.add_vectors(collection, vectors)
246
        def build(connect):
X
Xiaohai Xu 已提交
247
            status = connect.create_index(collection, IndexType.IVFLAT, {"nlist": NLIST})
248 249
            assert status.OK()
        def count(connect):
X
Xiaohai Xu 已提交
250
            status, count = connect.count_collection(collection)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
            assert status.OK()
            assert count == nb

        threads_num = 8
        threads = []
        uri = "tcp://%s:%s" % (args["ip"], args["port"])
        for i in range(threads_num):
            m = get_milvus(args["handler"])
            m.connect(uri=uri)
            if(i % 2 == 0):
                p = threading.Thread(target=build, args=(m,))
            else:
                p = threading.Thread(target=count, args=(m,))
            threads.append(p)
            p.start()
            time.sleep(0.2)
        for p in threads:
            p.join()


# TODO: enable
J
JinHai-CN 已提交
272 273
    @pytest.mark.timeout(BUILD_TIMEOUT)
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
274
    def _test_create_index_multiprocessing(self, connect, collection, args):
J
JinHai-CN 已提交
275 276
        '''
        target: test create index interface with multiprocess
X
Xiaohai Xu 已提交
277
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
278 279
        expected: return code equals to 0, and search success
        '''
X
Xiaohai Xu 已提交
280
        status, ids = connect.add_vectors(collection, vectors)
J
JinHai-CN 已提交
281 282

        def build(connect):
X
Xiaohai Xu 已提交
283
            status = connect.create_index(collection, IndexType.IVFLAT, {"nlist": NLIST})
J
JinHai-CN 已提交
284 285 286 287 288 289 290
            assert status.OK()

        process_num = 8
        processes = []
        uri = "tcp://%s:%s" % (args["ip"], args["port"])

        for i in range(process_num):
291
            m = get_milvus(args["handler"])
J
JinHai-CN 已提交
292 293 294 295 296 297 298 299 300 301
            m.connect(uri=uri)
            p = Process(target=build, args=(m,))
            processes.append(p)
            p.start()
            time.sleep(0.2)
        for p in processes:
            p.join()

        query_vec = [vectors[0]]
        top_k = 1
302
        search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
303
        status, result = connect.search_vectors(collection, top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
304 305 306 307 308 309
        assert len(result) == 1
        assert len(result[0]) == top_k
        assert result[0][0].distance == 0.0

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
310
    def _test_create_index_multiprocessing_multicollection(self, connect, args):
J
JinHai-CN 已提交
311 312
        '''
        target: test create index interface with multiprocess
X
Xiaohai Xu 已提交
313
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
314 315 316 317 318 319
        expected: return code equals to 0, and search success
        '''
        process_num = 8
        loop_num = 8
        processes = []

X
Xiaohai Xu 已提交
320
        collection = []
J
JinHai-CN 已提交
321 322
        j = 0
        while j < (process_num*loop_num):
X
Xiaohai Xu 已提交
323 324 325
            collection_name = gen_unique_str("test_create_index_multiprocessing")
            collection.append(collection_name)
            param = {'collection_name': collection_name,
J
JinHai-CN 已提交
326 327 328
                    'dimension': dim,
                    'index_type': IndexType.FLAT,
                    'store_raw_vector': False}
X
Xiaohai Xu 已提交
329
            connect.create_collection(param)
J
JinHai-CN 已提交
330 331 332 333 334
            j = j + 1

        def create_index():
            i = 0
            while i < loop_num:
X
Xiaohai Xu 已提交
335 336
                # assert connect.has_collection(collection[ids*process_num+i])
                status, ids = connect.add_vectors(collection[ids*process_num+i], vectors)
J
JinHai-CN 已提交
337

X
Xiaohai Xu 已提交
338
                status = connect.create_index(collection[ids*process_num+i], IndexType.IVFLAT, {"nlist": NLIST})
J
JinHai-CN 已提交
339 340 341
                assert status.OK()
                query_vec = [vectors[0]]
                top_k = 1
342
                search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
343
                status, result = connect.search_vectors(collection[ids*process_num+i], top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
344 345 346 347 348 349 350 351
                assert len(result) == 1
                assert len(result[0]) == top_k
                assert result[0][0].distance == 0.0
                i = i + 1

        uri = "tcp://%s:%s" % (args["ip"], args["port"])

        for i in range(process_num):
352
            m = get_milvus(args["handler"])
J
JinHai-CN 已提交
353 354 355 356 357 358 359 360 361
            m.connect(uri=uri)
            ids = i
            p = Process(target=create_index, args=(m,ids))
            processes.append(p)
            p.start()
            time.sleep(0.2)
        for p in processes:
            p.join()

X
Xiaohai Xu 已提交
362
    def test_create_index_collection_not_existed(self, connect):
J
JinHai-CN 已提交
363
        '''
X
Xiaohai Xu 已提交
364 365 366
        target: test create index interface when collection name not existed
        method: create collection and add vectors in it, create index
            , make sure the collection name not in index
J
JinHai-CN 已提交
367 368
        expected: return code not equals to 0, create index failed
        '''
X
Xiaohai Xu 已提交
369
        collection_name = gen_unique_str(self.__class__.__name__)
370
        nlist = NLIST
371 372
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": nlist}
X
Xiaohai Xu 已提交
373
        status = connect.create_index(collection_name, index_type, index_param)
J
JinHai-CN 已提交
374 375
        assert not status.OK()

X
Xiaohai Xu 已提交
376
    def test_create_index_collection_None(self, connect):
J
JinHai-CN 已提交
377
        '''
X
Xiaohai Xu 已提交
378 379
        target: test create index interface when collection name is None
        method: create collection and add vectors in it, create index with an collection_name: None
J
JinHai-CN 已提交
380 381
        expected: return code not equals to 0, create index failed
        '''
X
Xiaohai Xu 已提交
382
        collection_name = None
383
        nlist = NLIST
384 385
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": nlist}
J
JinHai-CN 已提交
386
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
387
            status = connect.create_index(collection_name, index_type, index_param)
J
JinHai-CN 已提交
388 389

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
390
    def test_create_index_no_vectors_then_add_vectors(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
391
        '''
X
Xiaohai Xu 已提交
392 393
        target: test create index interface when there is no vectors in collection, and does not affect the subsequent process
        method: create collection and add no vectors in it, and then create index, add vectors in it
J
JinHai-CN 已提交
394 395
        expected: return code equals to 0
        '''
396 397
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
398 399
        status = connect.create_index(collection, index_type, index_param)
        status, ids = connect.add_vectors(collection, vectors)
J
JinHai-CN 已提交
400 401 402
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
403
    def test_create_same_index_repeatedly(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
404 405 406 407 408
        '''
        target: check if index can be created repeatedly, with the same create_index params
        method: create index after index have been built
        expected: return code success, and search ok
        '''
409 410
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
411 412
        status = connect.create_index(collection, index_type, index_param)
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
413 414 415
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
416
    def test_create_different_index_repeatedly(self, connect, collection):
J
JinHai-CN 已提交
417 418 419 420 421
        '''
        target: check if index can be created repeatedly, with the different create_index params
        method: create another index with different index_params after index have been built
        expected: return code 0, and describe index result equals with the second index params
        '''
422
        nlist = NLIST
X
Xiaohai Xu 已提交
423
        status, ids = connect.add_vectors(collection, vectors)
Z
zhenwu 已提交
424 425
        index_type_1 = IndexType.IVF_SQ8
        index_type_2 = IndexType.IVFLAT
426 427 428
        indexs = [{"index_type": index_type_1, "index_param": {"nlist": nlist}}, {"index_type": index_type_2, "index_param": {"nlist": nlist}}]
        logging.getLogger().info(indexs)
        for index in indexs:
X
Xiaohai Xu 已提交
429
            status = connect.create_index(collection, index["index_type"], index["index_param"])
Z
zhenwu 已提交
430
            assert status.OK()
X
Xiaohai Xu 已提交
431
        status, result = connect.describe_index(collection)
432
        assert result._params["nlist"] == nlist
X
Xiaohai Xu 已提交
433
        assert result._collection_name == collection
Z
zhenwu 已提交
434
        assert result._index_type == index_type_2
J
JinHai-CN 已提交
435 436 437 438 439 440 441

    """
    ******************************************************************
      The following cases are used to test `describe_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
442
    def test_describe_index(self, connect, collection, get_index):
J
JinHai-CN 已提交
443 444
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
445
        method: create collection and add vectors in it, create index, call describe index
J
JinHai-CN 已提交
446 447
        expected: return code 0, and index instructure
        '''
448 449 450
        index_param = get_index["index_param"]
        index_type = get_index["index_type"]
        logging.getLogger().info(get_index)
X
Xiaohai Xu 已提交
451 452 453
        # status, ids = connect.add_vectors(collection, vectors)
        status = connect.create_index(collection, index_type, index_param)
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
454
        logging.getLogger().info(result)
455
        assert result._params == index_param
X
Xiaohai Xu 已提交
456
        assert result._collection_name == collection
457
        assert result._index_type == index_type
J
JinHai-CN 已提交
458

X
Xiaohai Xu 已提交
459
    def test_describe_and_drop_index_multi_collections(self, connect, get_simple_index):
J
JinHai-CN 已提交
460
        '''
X
Xiaohai Xu 已提交
461 462
        target: test create, describe and drop index interface with multiple collections of L2
        method: create collections and add vectors in it, create index, call describe index
J
JinHai-CN 已提交
463 464 465 466
        expected: return code 0, and index instructure
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
467
        collection_list = []
J
JinHai-CN 已提交
468
        for i in range(10):
X
Xiaohai Xu 已提交
469 470 471
            collection_name = gen_unique_str()
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
J
JinHai-CN 已提交
472 473 474
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
475
            connect.create_collection(param)
476 477 478
            index_param = get_simple_index["index_param"]
            index_type = get_simple_index["index_type"]
            logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
479 480
            status, ids = connect.add_vectors(collection_name=collection_name, records=vectors)
            status = connect.create_index(collection_name, index_type, index_param)
J
JinHai-CN 已提交
481 482 483
            assert status.OK()

        for i in range(10):
X
Xiaohai Xu 已提交
484
            status, result = connect.describe_index(collection_list[i])
J
JinHai-CN 已提交
485
            logging.getLogger().info(result)
486
            assert result._params == index_param
X
Xiaohai Xu 已提交
487
            assert result._collection_name == collection_list[i]
488
            assert result._index_type == index_type
J
JinHai-CN 已提交
489 490

        for i in range(10):
X
Xiaohai Xu 已提交
491
            status = connect.drop_index(collection_list[i])
J
JinHai-CN 已提交
492
            assert status.OK()
X
Xiaohai Xu 已提交
493
            status, result = connect.describe_index(collection_list[i])
J
JinHai-CN 已提交
494
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
495
            assert result._collection_name == collection_list[i]
J
JinHai-CN 已提交
496 497 498
            assert result._index_type == IndexType.FLAT

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
499
    def test_describe_index_without_connect(self, dis_connect, collection):
J
JinHai-CN 已提交
500 501 502 503 504 505
        '''
        target: test describe index without connection
        method: describe index, and check if describe successfully
        expected: raise exception
        '''
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
506
            status = dis_connect.describe_index(collection)
J
JinHai-CN 已提交
507

X
Xiaohai Xu 已提交
508
    def test_describe_index_collection_not_existed(self, connect):
J
JinHai-CN 已提交
509
        '''
X
Xiaohai Xu 已提交
510 511 512
        target: test describe index interface when collection name not existed
        method: create collection and add vectors in it, create index
            , make sure the collection name not in index
J
JinHai-CN 已提交
513 514
        expected: return code not equals to 0, describe index failed
        '''
X
Xiaohai Xu 已提交
515 516
        collection_name = gen_unique_str(self.__class__.__name__)
        status, result = connect.describe_index(collection_name)
J
JinHai-CN 已提交
517 518
        assert not status.OK()

X
Xiaohai Xu 已提交
519
    def test_describe_index_collection_None(self, connect):
J
JinHai-CN 已提交
520
        '''
X
Xiaohai Xu 已提交
521 522
        target: test describe index interface when collection name is None
        method: create collection and add vectors in it, create index with an collection_name: None
J
JinHai-CN 已提交
523 524
        expected: return code not equals to 0, describe index failed
        '''
X
Xiaohai Xu 已提交
525
        collection_name = None
J
JinHai-CN 已提交
526
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
527
            status = connect.describe_index(collection_name)
J
JinHai-CN 已提交
528

X
Xiaohai Xu 已提交
529
    def test_describe_index_not_create(self, connect, collection):
J
JinHai-CN 已提交
530 531
        '''
        target: test describe index interface when index not created
X
Xiaohai Xu 已提交
532 533
        method: create collection and add vectors in it, create index
            , make sure the collection name not in index
J
JinHai-CN 已提交
534 535
        expected: return code not equals to 0, describe index failed
        '''
X
Xiaohai Xu 已提交
536 537
        status, ids = connect.add_vectors(collection, vectors)
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
538 539
        logging.getLogger().info(result)
        assert status.OK()
540
        # assert result._params["nlist"] == index_params["nlist"]
X
Xiaohai Xu 已提交
541
        # assert result._collection_name == collection
J
JinHai-CN 已提交
542 543 544 545 546 547 548 549
        # assert result._index_type == index_params["index_type"]

    """
    ******************************************************************
      The following cases are used to test `drop_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
550
    def test_drop_index(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
551 552
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
553
        method: create collection and add vectors in it, create index, call drop index
J
JinHai-CN 已提交
554 555
        expected: return code 0, and default index param
        '''
556 557
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
558 559
        # status, ids = connect.add_vectors(collection, vectors)
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
560
        assert status.OK()
X
Xiaohai Xu 已提交
561
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
562
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
563
        status = connect.drop_index(collection)
J
JinHai-CN 已提交
564
        assert status.OK()
X
Xiaohai Xu 已提交
565
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
566
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
567
        assert result._collection_name == collection
J
JinHai-CN 已提交
568 569
        assert result._index_type == IndexType.FLAT

X
Xiaohai Xu 已提交
570
    def test_drop_index_repeatly(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
571 572 573 574 575
        '''
        target: test drop index repeatly
        method: create index, call drop index, and drop again
        expected: return code 0
        '''
576 577
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
578 579
        # status, ids = connect.add_vectors(collection, vectors)
        status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
580
        assert status.OK()
X
Xiaohai Xu 已提交
581
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
582
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
583
        status = connect.drop_index(collection)
J
JinHai-CN 已提交
584
        assert status.OK()
X
Xiaohai Xu 已提交
585
        status = connect.drop_index(collection)
J
JinHai-CN 已提交
586
        assert status.OK()
X
Xiaohai Xu 已提交
587
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
588
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
589
        assert result._collection_name == collection
J
JinHai-CN 已提交
590 591 592
        assert result._index_type == IndexType.FLAT

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
593
    def test_drop_index_without_connect(self, dis_connect, collection):
J
JinHai-CN 已提交
594 595 596 597 598 599
        '''
        target: test drop index without connection
        method: drop index, and check if drop successfully
        expected: raise exception
        '''
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
600
            status = dis_connect.drop_index(collection)
J
JinHai-CN 已提交
601

X
Xiaohai Xu 已提交
602
    def test_drop_index_collection_not_existed(self, connect):
J
JinHai-CN 已提交
603
        '''
X
Xiaohai Xu 已提交
604 605 606
        target: test drop index interface when collection name not existed
        method: create collection and add vectors in it, create index
            , make sure the collection name not in index, and then drop it
J
JinHai-CN 已提交
607 608
        expected: return code not equals to 0, drop index failed
        '''
X
Xiaohai Xu 已提交
609 610
        collection_name = gen_unique_str(self.__class__.__name__)
        status = connect.drop_index(collection_name)
J
JinHai-CN 已提交
611 612
        assert not status.OK()

X
Xiaohai Xu 已提交
613
    def test_drop_index_collection_None(self, connect):
J
JinHai-CN 已提交
614
        '''
X
Xiaohai Xu 已提交
615 616
        target: test drop index interface when collection name is None
        method: create collection and add vectors in it, create index with an collection_name: None
J
JinHai-CN 已提交
617 618
        expected: return code not equals to 0, drop index failed
        '''
X
Xiaohai Xu 已提交
619
        collection_name = None
J
JinHai-CN 已提交
620
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
621
            status = connect.drop_index(collection_name)
J
JinHai-CN 已提交
622

X
Xiaohai Xu 已提交
623
    def test_drop_index_collection_not_create(self, connect, collection):
J
JinHai-CN 已提交
624 625
        '''
        target: test drop index interface when index not created
X
Xiaohai Xu 已提交
626
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
627 628
        expected: return code not equals to 0, drop index failed
        '''
X
Xiaohai Xu 已提交
629 630
        status, ids = connect.add_vectors(collection, vectors)
        status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
631 632
        logging.getLogger().info(result)
        # no create index
X
Xiaohai Xu 已提交
633
        status = connect.drop_index(collection)
J
JinHai-CN 已提交
634 635 636
        logging.getLogger().info(status)
        assert status.OK()

X
Xiaohai Xu 已提交
637
    def test_create_drop_index_repeatly(self, connect, collection, get_simple_index):
J
JinHai-CN 已提交
638 639 640 641 642
        '''
        target: test create / drop index repeatly, use the same index params
        method: create index, drop index, four times
        expected: return code 0
        '''
643 644
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
645
        # status, ids = connect.add_vectors(collection, vectors)
J
JinHai-CN 已提交
646
        for i in range(2):
X
Xiaohai Xu 已提交
647
            status = connect.create_index(collection, index_type, index_param)
J
JinHai-CN 已提交
648
            assert status.OK()
X
Xiaohai Xu 已提交
649
            status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
650
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
651
            status = connect.drop_index(collection)
J
JinHai-CN 已提交
652
            assert status.OK()
X
Xiaohai Xu 已提交
653
            status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
654
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
655
            assert result._collection_name == collection
J
JinHai-CN 已提交
656 657
            assert result._index_type == IndexType.FLAT

X
Xiaohai Xu 已提交
658
    def test_create_drop_index_repeatly_different_index_params(self, connect, collection):
J
JinHai-CN 已提交
659 660 661 662 663
        '''
        target: test create / drop index repeatly, use the different index params
        method: create index, drop index, four times, each tme use different index_params to create index
        expected: return code 0
        '''
664
        nlist = NLIST
665
        indexs = [{"index_type": IndexType.IVFLAT, "index_param": {"nlist": nlist}}, {"index_type": IndexType.IVF_SQ8, "index_param": {"nlist": nlist}}]
X
Xiaohai Xu 已提交
666
        # status, ids = connect.add_vectors(collection, vectors)
J
JinHai-CN 已提交
667
        for i in range(2):
X
Xiaohai Xu 已提交
668
            status = connect.create_index(collection, indexs[i]["index_type"], indexs[i]["index_param"])
J
JinHai-CN 已提交
669
            assert status.OK()
X
Xiaohai Xu 已提交
670
            status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
671
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
672
            status = connect.drop_index(collection)
J
JinHai-CN 已提交
673
            assert status.OK()
X
Xiaohai Xu 已提交
674
            status, result = connect.describe_index(collection)
J
JinHai-CN 已提交
675
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
676
            assert result._collection_name == collection
J
JinHai-CN 已提交
677 678 679 680 681 682
            assert result._index_type == IndexType.FLAT


class TestIndexIP:
    @pytest.fixture(
        scope="function",
683
        params=gen_index()
J
JinHai-CN 已提交
684
    )
685
    def get_index(self, request, connect):
G
groot 已提交
686
        if str(connect._cmd("mode")[1]) == "CPU":
687
            if request.param["index_type"] == IndexType.IVF_SQ8H:
688
                pytest.skip("sq8h not support in CPU mode")
689 690 691
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
D
del-zhenwu 已提交
692 693
        if request.param["index_type"] == IndexType.RNSG:
            pytest.skip("rnsg not support in ip")
694
        return request.param
J
JinHai-CN 已提交
695 696 697

    @pytest.fixture(
        scope="function",
698
        params=gen_simple_index()
J
JinHai-CN 已提交
699
    )
700
    def get_simple_index(self, request, connect):
G
groot 已提交
701
        if str(connect._cmd("mode")[1]) == "CPU":
Z
zhenwu 已提交
702
            if request.param["index_type"] == IndexType.IVF_SQ8H:
703
                pytest.skip("sq8h not support in CPU mode")
704 705 706
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
D
del-zhenwu 已提交
707 708
        if request.param["index_type"] == IndexType.RNSG:
            pytest.skip("rnsg not support in ip")
Z
zhenwu 已提交
709
        return request.param
J
JinHai-CN 已提交
710 711 712 713 714
    """
    ******************************************************************
      The following cases are used to test `create_index` function
    ******************************************************************
    """
Z
zhenwu 已提交
715
    @pytest.mark.level(2)
J
JinHai-CN 已提交
716
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
717
    def test_create_index(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
718 719
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
720
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
721 722
        expected: return code equals to 0, and search success
        '''
723 724 725
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
726 727
        status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
728
        assert status.OK()
J
JinHai-CN 已提交
729

Z
zhenwu 已提交
730
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
731
    def test_create_index_collection(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
732 733
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
734
        method: create collection, create partition, and add vectors in it, create index on collection
Z
zhenwu 已提交
735 736
        expected: return code equals to 0, and search success
        '''
737 738 739
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
740 741 742
        status = connect.create_partition(ip_collection, tag)
        status, ids = connect.add_vectors(ip_collection, vectors, partition_tag=tag)
        status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
743
        assert status.OK()
Z
zhenwu 已提交
744

J
JinHai-CN 已提交
745
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
746
    def test_create_index_without_connect(self, dis_connect, ip_collection):
J
JinHai-CN 已提交
747 748
        '''
        target: test create index without connection
X
Xiaohai Xu 已提交
749
        method: create collection and add vectors in it, check if added successfully
J
JinHai-CN 已提交
750 751
        expected: raise exception
        '''
752
        nlist = NLIST
753 754
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": nlist}
J
JinHai-CN 已提交
755
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
756
            status = dis_connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
757 758

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
759
    def test_create_index_search_with_query_vectors(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
760 761
        '''
        target: test create index interface, search with more query vectors
X
Xiaohai Xu 已提交
762
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
763 764
        expected: return code equals to 0, and search success
        '''
765 766 767
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
768 769 770
        status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(ip_collection, index_type, index_param)
        logging.getLogger().info(connect.describe_index(ip_collection))
Z
zhenwu 已提交
771 772
        query_vecs = [vectors[0], vectors[1], vectors[2]]
        top_k = 5
773
        search_param = get_search_param(index_type)
X
Xiaohai Xu 已提交
774
        status, result = connect.search_vectors(ip_collection, top_k, query_vecs, params=search_param)
Z
zhenwu 已提交
775
        logging.getLogger().info(result)
776
        assert status.OK()
Z
zhenwu 已提交
777
        assert len(result) == len(query_vecs)
J
JinHai-CN 已提交
778 779 780 781

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
782
    def _test_create_index_multiprocessing(self, connect, ip_collection, args):
J
JinHai-CN 已提交
783 784
        '''
        target: test create index interface with multiprocess
X
Xiaohai Xu 已提交
785
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
786 787
        expected: return code equals to 0, and search success
        '''
X
Xiaohai Xu 已提交
788
        status, ids = connect.add_vectors(ip_collection, vectors)
J
JinHai-CN 已提交
789 790

        def build(connect):
X
Xiaohai Xu 已提交
791
            status = connect.create_index(ip_collection, IndexType.IVFLAT, {"nlist": NLIST})
J
JinHai-CN 已提交
792 793 794 795 796 797 798
            assert status.OK()

        process_num = 8
        processes = []
        uri = "tcp://%s:%s" % (args["ip"], args["port"])

        for i in range(process_num):
799
            m = get_milvus(args["handler"])
J
JinHai-CN 已提交
800 801 802 803 804 805 806 807 808 809
            m.connect(uri=uri)
            p = Process(target=build, args=(m,))
            processes.append(p)
            p.start()
            time.sleep(0.2)
        for p in processes:
            p.join()

        query_vec = [vectors[0]]
        top_k = 1
810
        search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
811
        status, result = connect.search_vectors(ip_collection, top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
812 813 814 815 816 817
        assert len(result) == 1
        assert len(result[0]) == top_k
        assert result[0][0].distance == 0.0

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
818
    def _test_create_index_multiprocessing_multicollection(self, connect, args):
J
JinHai-CN 已提交
819 820
        '''
        target: test create index interface with multiprocess
X
Xiaohai Xu 已提交
821
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
822 823 824 825 826 827
        expected: return code equals to 0, and search success
        '''
        process_num = 8
        loop_num = 8
        processes = []

X
Xiaohai Xu 已提交
828
        collection = []
J
JinHai-CN 已提交
829 830
        j = 0
        while j < (process_num*loop_num):
X
Xiaohai Xu 已提交
831 832 833
            collection_name = gen_unique_str("test_create_index_multiprocessing")
            collection.append(collection_name)
            param = {'collection_name': collection_name,
J
JinHai-CN 已提交
834
                    'dimension': dim}
X
Xiaohai Xu 已提交
835
            connect.create_collection(param)
J
JinHai-CN 已提交
836 837 838 839 840
            j = j + 1

        def create_index():
            i = 0
            while i < loop_num:
X
Xiaohai Xu 已提交
841 842
                # assert connect.has_collection(collection[ids*process_num+i])
                status, ids = connect.add_vectors(collection[ids*process_num+i], vectors)
J
JinHai-CN 已提交
843

X
Xiaohai Xu 已提交
844
                status = connect.create_index(collection[ids*process_num+i], IndexType.IVFLAT, {"nlist": NLIST})
J
JinHai-CN 已提交
845 846 847
                assert status.OK()
                query_vec = [vectors[0]]
                top_k = 1
848
                search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
849
                status, result = connect.search_vectors(collection[ids*process_num+i], top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
850 851 852 853 854 855 856 857
                assert len(result) == 1
                assert len(result[0]) == top_k
                assert result[0][0].distance == 0.0
                i = i + 1

        uri = "tcp://%s:%s" % (args["ip"], args["port"])

        for i in range(process_num):
858
            m = get_milvus(args["handler"])
J
JinHai-CN 已提交
859 860 861 862 863 864 865 866 867
            m.connect(uri=uri)
            ids = i
            p = Process(target=create_index, args=(m,ids))
            processes.append(p)
            p.start()
            time.sleep(0.2)
        for p in processes:
            p.join()

X
Xiaohai Xu 已提交
868
    def test_create_index_no_vectors(self, connect, ip_collection):
J
JinHai-CN 已提交
869
        '''
X
Xiaohai Xu 已提交
870 871
        target: test create index interface when there is no vectors in collection
        method: create collection and add no vectors in it, and then create index
J
JinHai-CN 已提交
872 873
        expected: return code equals to 0
        '''
874
        nlist = NLIST
875 876
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": nlist}
X
Xiaohai Xu 已提交
877
        status = connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
878 879 880
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
881
    def test_create_index_no_vectors_then_add_vectors(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
882
        '''
X
Xiaohai Xu 已提交
883 884
        target: test create index interface when there is no vectors in collection, and does not affect the subsequent process
        method: create collection and add no vectors in it, and then create index, add vectors in it
J
JinHai-CN 已提交
885 886
        expected: return code equals to 0
        '''
887 888
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
889 890
        status = connect.create_index(ip_collection, index_type, index_param)
        status, ids = connect.add_vectors(ip_collection, vectors)
J
JinHai-CN 已提交
891 892 893
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
894
    def test_create_same_index_repeatedly(self, connect, ip_collection):
J
JinHai-CN 已提交
895 896 897 898 899
        '''
        target: check if index can be created repeatedly, with the same create_index params
        method: create index after index have been built
        expected: return code success, and search ok
        '''
900
        nlist = NLIST
X
Xiaohai Xu 已提交
901
        status, ids = connect.add_vectors(ip_collection, vectors)
902 903
        index_type = IndexType.IVF_SQ8
        index_param = {"nlist": nlist}
X
Xiaohai Xu 已提交
904 905
        status = connect.create_index(ip_collection, index_type, index_param)
        status = connect.create_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
906 907 908
        assert status.OK()
        query_vec = [vectors[0]]
        top_k = 1
909
        search_param = {"nprobe": nprobe}
X
Xiaohai Xu 已提交
910
        status, result = connect.search_vectors(ip_collection, top_k, query_vec, params=search_param)
J
JinHai-CN 已提交
911 912 913 914
        assert len(result) == 1
        assert len(result[0]) == top_k

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
915
    def test_create_different_index_repeatedly(self, connect, ip_collection):
J
JinHai-CN 已提交
916 917 918 919 920
        '''
        target: check if index can be created repeatedly, with the different create_index params
        method: create another index with different index_params after index have been built
        expected: return code 0, and describe index result equals with the second index params
        '''
921
        nlist = NLIST
X
Xiaohai Xu 已提交
922
        status, ids = connect.add_vectors(ip_collection, vectors)
Z
zhenwu 已提交
923 924
        index_type_1 = IndexType.IVF_SQ8
        index_type_2 = IndexType.IVFLAT
925 926 927
        indexs = [{"index_type": index_type_1, "index_param": {"nlist": nlist}}, {"index_type": index_type_2, "index_param": {"nlist": nlist}}]
        logging.getLogger().info(indexs)
        for index in indexs:
X
Xiaohai Xu 已提交
928
            status = connect.create_index(ip_collection, index["index_type"], index["index_param"])
Z
zhenwu 已提交
929
            assert status.OK()
X
Xiaohai Xu 已提交
930
        status, result = connect.describe_index(ip_collection)
931
        assert result._params["nlist"] == nlist
X
Xiaohai Xu 已提交
932
        assert result._collection_name == ip_collection
Z
zhenwu 已提交
933
        assert result._index_type == index_type_2
J
JinHai-CN 已提交
934 935 936 937 938 939 940

    """
    ******************************************************************
      The following cases are used to test `describe_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
941
    def test_describe_index(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
942 943
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
944
        method: create collection and add vectors in it, create index, call describe index
J
JinHai-CN 已提交
945 946
        expected: return code 0, and index instructure
        '''
947 948 949
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
D
del-zhenwu 已提交
950
        # status, ids = connect.add_vectors(ip_collection, vectors[:5000])
X
Xiaohai Xu 已提交
951 952
        status = connect.create_index(ip_collection, index_type, index_param)
        status, result = connect.describe_index(ip_collection)
J
JinHai-CN 已提交
953
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
954
        assert result._collection_name == ip_collection
Z
zhenwu 已提交
955
        status, mode = connect._cmd("mode")
956
        if str(mode) == "GPU" and index_type == IndexType.IVF_PQ:
Z
zhenwu 已提交
957
            assert result._index_type == IndexType.FLAT
958
            assert result._params["nlist"] == NLIST
Z
zhenwu 已提交
959
        else:
960 961
            assert result._index_type == index_type
            assert result._params == index_param
J
JinHai-CN 已提交
962

X
Xiaohai Xu 已提交
963
    def test_describe_index_partition(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
964 965
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
966
        method: create collection, create partition and add vectors in it, create index, call describe index
Z
zhenwu 已提交
967 968
        expected: return code 0, and index instructure
        '''
969 970 971
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
972 973 974 975
        status = connect.create_partition(ip_collection, tag)
        status, ids = connect.add_vectors(ip_collection, vectors, partition_tag=tag)
        status = connect.create_index(ip_collection, index_type, index_param)
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
976
        logging.getLogger().info(result)
977
        assert result._params == index_param
X
Xiaohai Xu 已提交
978
        assert result._collection_name == ip_collection
979
        assert result._index_type == index_type
Z
zhenwu 已提交
980

X
Xiaohai Xu 已提交
981
    def test_describe_index_partition_A(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
982 983
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
984
        method: create collection, create partitions and add vectors in it, create index on partitions, call describe index
Z
zhenwu 已提交
985 986 987
        expected: return code 0, and index instructure
        '''
        new_tag = "new_tag"
988 989 990
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
991 992 993 994 995 996
        status = connect.create_partition(ip_collection, tag)
        status = connect.create_partition(ip_collection, new_tag)
        status, ids = connect.add_vectors(ip_collection, vectors, partition_tag=tag)
        status, ids = connect.add_vectors(ip_collection, vectors, partition_tag=new_tag)
        status = connect.create_index(ip_collection, index_type, index_param)
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
997
        logging.getLogger().info(result)
998
        assert result._params == index_param
X
Xiaohai Xu 已提交
999
        assert result._collection_name == ip_collection
1000
        assert result._index_type == index_type
Z
zhenwu 已提交
1001

X
Xiaohai Xu 已提交
1002
    def test_describe_and_drop_index_multi_collections(self, connect, get_simple_index):
J
JinHai-CN 已提交
1003
        '''
X
Xiaohai Xu 已提交
1004 1005
        target: test create, describe and drop index interface with multiple collections of IP
        method: create collections and add vectors in it, create index, call describe index
J
JinHai-CN 已提交
1006 1007 1008 1009
        expected: return code 0, and index instructure
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
X
Xiaohai Xu 已提交
1010
        collection_list = []
J
JinHai-CN 已提交
1011
        for i in range(10):
X
Xiaohai Xu 已提交
1012 1013 1014
            collection_name = gen_unique_str()
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
J
JinHai-CN 已提交
1015 1016 1017
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
X
Xiaohai Xu 已提交
1018
            connect.create_collection(param)
1019 1020 1021
            index_param = get_simple_index["index_param"]
            index_type = get_simple_index["index_type"]
            logging.getLogger().info(get_simple_index)
X
Xiaohai Xu 已提交
1022 1023
            status, ids = connect.add_vectors(collection_name=collection_name, records=vectors)
            status = connect.create_index(collection_name, index_type, index_param)
J
JinHai-CN 已提交
1024 1025
            assert status.OK()
        for i in range(10):
X
Xiaohai Xu 已提交
1026
            status, result = connect.describe_index(collection_list[i])
J
JinHai-CN 已提交
1027
            logging.getLogger().info(result)
1028
            assert result._params == index_param
X
Xiaohai Xu 已提交
1029
            assert result._collection_name == collection_list[i]
1030
            assert result._index_type == index_type
J
JinHai-CN 已提交
1031
        for i in range(10):
X
Xiaohai Xu 已提交
1032
            status = connect.drop_index(collection_list[i])
J
JinHai-CN 已提交
1033
            assert status.OK()
X
Xiaohai Xu 已提交
1034
            status, result = connect.describe_index(collection_list[i])
J
JinHai-CN 已提交
1035
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1036
            assert result._collection_name == collection_list[i]
J
JinHai-CN 已提交
1037 1038 1039
            assert result._index_type == IndexType.FLAT

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1040
    def test_describe_index_without_connect(self, dis_connect, ip_collection):
J
JinHai-CN 已提交
1041 1042 1043 1044 1045 1046
        '''
        target: test describe index without connection
        method: describe index, and check if describe successfully
        expected: raise exception
        '''
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
1047
            status = dis_connect.describe_index(ip_collection)
J
JinHai-CN 已提交
1048

X
Xiaohai Xu 已提交
1049
    def test_describe_index_not_create(self, connect, ip_collection):
J
JinHai-CN 已提交
1050 1051
        '''
        target: test describe index interface when index not created
X
Xiaohai Xu 已提交
1052 1053
        method: create collection and add vectors in it, create index
            , make sure the collection name not in index
J
JinHai-CN 已提交
1054 1055
        expected: return code not equals to 0, describe index failed
        '''
X
Xiaohai Xu 已提交
1056 1057
        status, ids = connect.add_vectors(ip_collection, vectors)
        status, result = connect.describe_index(ip_collection)
J
JinHai-CN 已提交
1058 1059
        logging.getLogger().info(result)
        assert status.OK()
1060
        # assert result._params["nlist"] == index_params["nlist"]
X
Xiaohai Xu 已提交
1061
        # assert result._collection_name == collection
J
JinHai-CN 已提交
1062 1063 1064 1065 1066 1067 1068 1069
        # assert result._index_type == index_params["index_type"]

    """
    ******************************************************************
      The following cases are used to test `drop_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
1070
    def test_drop_index(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
1071 1072
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1073
        method: create collection and add vectors in it, create index, call drop index
J
JinHai-CN 已提交
1074 1075
        expected: return code 0, and default index param
        '''
1076 1077
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
1078 1079
        status, mode = connect._cmd("mode")
        assert status.OK()
X
Xiaohai Xu 已提交
1080 1081
        # status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(ip_collection, index_type, index_param)
1082
        if str(mode) == "GPU" and (index_type == IndexType.IVF_PQ):
1083 1084 1085
            assert not status.OK()
        else:
            assert status.OK()
X
Xiaohai Xu 已提交
1086
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1087
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1088
        status = connect.drop_index(ip_collection)
Z
zhenwu 已提交
1089
        assert status.OK()
X
Xiaohai Xu 已提交
1090
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1091
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1092
        assert result._collection_name == ip_collection
Z
zhenwu 已提交
1093
        assert result._index_type == IndexType.FLAT
J
JinHai-CN 已提交
1094

X
Xiaohai Xu 已提交
1095
    def test_drop_index_partition(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
1096 1097
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1098
        method: create collection, create partition and add vectors in it, create index on collection, call drop collection index
Z
zhenwu 已提交
1099 1100
        expected: return code 0, and default index param
        '''
1101 1102
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
1103 1104 1105
        status = connect.create_partition(ip_collection, tag)
        status, ids = connect.add_vectors(ip_collection, vectors, partition_tag=tag)
        status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
1106
        assert status.OK()
X
Xiaohai Xu 已提交
1107
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1108
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1109
        status = connect.drop_index(ip_collection)
Z
zhenwu 已提交
1110
        assert status.OK()
X
Xiaohai Xu 已提交
1111
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1112
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1113
        assert result._collection_name == ip_collection
Z
zhenwu 已提交
1114
        assert result._index_type == IndexType.FLAT
Z
zhenwu 已提交
1115

X
Xiaohai Xu 已提交
1116
    def test_drop_index_partition_C(self, connect, ip_collection, get_simple_index):
Z
zhenwu 已提交
1117 1118
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1119
        method: create collection, create partitions and add vectors in it, create index on partitions, call drop partition index
Z
zhenwu 已提交
1120 1121 1122
        expected: return code 0, and default index param
        '''
        new_tag = "new_tag"
1123 1124
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
1125 1126 1127 1128
        status = connect.create_partition(ip_collection, tag)
        status = connect.create_partition(ip_collection, new_tag)
        status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
1129
        assert status.OK()
X
Xiaohai Xu 已提交
1130
        status = connect.drop_index(ip_collection)
Z
zhenwu 已提交
1131
        assert status.OK()
X
Xiaohai Xu 已提交
1132
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1133
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1134
        assert result._collection_name == ip_collection
1135
        assert result._index_type == IndexType.FLAT
Z
zhenwu 已提交
1136

X
Xiaohai Xu 已提交
1137
    def test_drop_index_repeatly(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
1138 1139 1140 1141 1142
        '''
        target: test drop index repeatly
        method: create index, call drop index, and drop again
        expected: return code 0
        '''
1143 1144
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
1145
        # status, ids = connect.add_vectors(ip_collection, vectors)
Z
zhenwu 已提交
1146
        status, mode = connect._cmd("mode")
Z
zhenwu 已提交
1147
        assert status.OK()
X
Xiaohai Xu 已提交
1148 1149
        # status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(ip_collection, index_type, index_param)
1150
        if str(mode) == "GPU" and (index_type == IndexType.IVF_PQ):
Z
zhenwu 已提交
1151 1152 1153
            assert not status.OK()
        else:
            assert status.OK()        
X
Xiaohai Xu 已提交
1154
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1155
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1156
        status = connect.drop_index(ip_collection)
Z
zhenwu 已提交
1157
        assert status.OK()
X
Xiaohai Xu 已提交
1158
        status = connect.drop_index(ip_collection)
Z
zhenwu 已提交
1159
        assert status.OK()
X
Xiaohai Xu 已提交
1160
        status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1161
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1162
        assert result._collection_name == ip_collection
Z
zhenwu 已提交
1163
        assert result._index_type == IndexType.FLAT
J
JinHai-CN 已提交
1164 1165

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1166
    def test_drop_index_without_connect(self, dis_connect, ip_collection):
J
JinHai-CN 已提交
1167 1168 1169 1170 1171
        '''
        target: test drop index without connection
        method: drop index, and check if drop successfully
        expected: raise exception
        '''
1172
        nlist = NLIST
1173 1174
        index_type = IndexType.IVFLAT
        index_param = {"nlist": nlist}
J
JinHai-CN 已提交
1175
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
1176
            status = dis_connect.drop_index(ip_collection, index_type, index_param)
J
JinHai-CN 已提交
1177

X
Xiaohai Xu 已提交
1178
    def test_drop_index_collection_not_create(self, connect, ip_collection):
J
JinHai-CN 已提交
1179 1180
        '''
        target: test drop index interface when index not created
X
Xiaohai Xu 已提交
1181
        method: create collection and add vectors in it, create index
J
JinHai-CN 已提交
1182 1183
        expected: return code not equals to 0, drop index failed
        '''
X
Xiaohai Xu 已提交
1184 1185
        status, ids = connect.add_vectors(ip_collection, vectors)
        status, result = connect.describe_index(ip_collection)
J
JinHai-CN 已提交
1186 1187
        logging.getLogger().info(result)
        # no create index
X
Xiaohai Xu 已提交
1188
        status = connect.drop_index(ip_collection)
J
JinHai-CN 已提交
1189 1190 1191
        logging.getLogger().info(status)
        assert status.OK()

X
Xiaohai Xu 已提交
1192
    def test_create_drop_index_repeatly(self, connect, ip_collection, get_simple_index):
J
JinHai-CN 已提交
1193 1194 1195 1196 1197
        '''
        target: test create / drop index repeatly, use the same index params
        method: create index, drop index, four times
        expected: return code 0
        '''
1198 1199
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
X
Xiaohai Xu 已提交
1200
        status, ids = connect.add_vectors(ip_collection, vectors)
J
JinHai-CN 已提交
1201
        for i in range(2):
X
Xiaohai Xu 已提交
1202
            status = connect.create_index(ip_collection, index_type, index_param)
Z
zhenwu 已提交
1203
            assert status.OK()
X
Xiaohai Xu 已提交
1204
            status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1205
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1206
            status = connect.drop_index(ip_collection)
Z
zhenwu 已提交
1207
            assert status.OK()
X
Xiaohai Xu 已提交
1208
            status, result = connect.describe_index(ip_collection)
Z
zhenwu 已提交
1209
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1210
            assert result._collection_name == ip_collection
Z
zhenwu 已提交
1211
            assert result._index_type == IndexType.FLAT
J
JinHai-CN 已提交
1212

X
Xiaohai Xu 已提交
1213
    def test_create_drop_index_repeatly_different_index_params(self, connect, ip_collection):
J
JinHai-CN 已提交
1214 1215 1216 1217 1218
        '''
        target: test create / drop index repeatly, use the different index params
        method: create index, drop index, four times, each tme use different index_params to create index
        expected: return code 0
        '''
1219
        nlist = NLIST
1220
        indexs = [{"index_type": IndexType.IVFLAT, "index_param": {"nlist": nlist}}, {"index_type": IndexType.IVF_SQ8, "index_param": {"nlist": nlist}}]
X
Xiaohai Xu 已提交
1221
        status, ids = connect.add_vectors(ip_collection, vectors)
J
JinHai-CN 已提交
1222
        for i in range(2):
X
Xiaohai Xu 已提交
1223
            status = connect.create_index(ip_collection, indexs[i]["index_type"], indexs[i]["index_param"])
J
JinHai-CN 已提交
1224
            assert status.OK()
X
Xiaohai Xu 已提交
1225
            status, result = connect.describe_index(ip_collection)
1226
            assert result._params == indexs[i]["index_param"]
X
Xiaohai Xu 已提交
1227
            assert result._collection_name == ip_collection
1228
            assert result._index_type == indexs[i]["index_type"]
X
Xiaohai Xu 已提交
1229
            status, result = connect.describe_index(ip_collection)
J
JinHai-CN 已提交
1230
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1231
            status = connect.drop_index(ip_collection)
J
JinHai-CN 已提交
1232
            assert status.OK()
X
Xiaohai Xu 已提交
1233
            status, result = connect.describe_index(ip_collection)
J
JinHai-CN 已提交
1234
            logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1235
            assert result._collection_name == ip_collection
J
JinHai-CN 已提交
1236 1237 1238
            assert result._index_type == IndexType.FLAT


G
groot 已提交
1239 1240 1241 1242 1243
class TestIndexJAC:
    tmp, vectors = gen_binary_vectors(nb, dim)

    @pytest.fixture(
        scope="function",
1244
        params=gen_index()
G
groot 已提交
1245
    )
1246
    def get_index(self, request, connect):
G
groot 已提交
1247 1248
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] == IndexType.IVF_SQ8H:
1249
                pytest.skip("sq8h not support in CPU mode")
1250 1251 1252
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
G
groot 已提交
1253 1254 1255 1256
        return request.param

    @pytest.fixture(
        scope="function",
1257
        params=gen_simple_index()
G
groot 已提交
1258
    )
1259
    def get_simple_index(self, request, connect):
G
groot 已提交
1260 1261
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] == IndexType.IVF_SQ8H:
1262
                pytest.skip("sq8h not support in CPU mode")
1263 1264 1265
        if str(connect._cmd("mode")[1]) == "GPU":
            if request.param["index_type"] == IndexType.IVF_PQ:
                pytest.skip("ivfpq not support in GPU mode")
G
groot 已提交
1266 1267 1268 1269
        return request.param

    @pytest.fixture(
        scope="function",
1270
        params=gen_simple_index()
G
groot 已提交
1271
    )
1272
    def get_jaccard_index(self, request, connect):
G
groot 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
        logging.getLogger().info(request.param)
        if request.param["index_type"] == IndexType.IVFLAT or request.param["index_type"] == IndexType.FLAT:
            return request.param
        else:
            pytest.skip("Skip index Temporary")

    """
    ******************************************************************
      The following cases are used to test `create_index` function
    ******************************************************************
    """
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
1285
    def test_create_index(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1286 1287
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
1288
        method: create collection and add vectors in it, create index
G
groot 已提交
1289 1290
        expected: return code equals to 0, and search success
        '''
1291 1292 1293
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
        logging.getLogger().info(get_jaccard_index)
X
Xiaohai Xu 已提交
1294 1295
        status, ids = connect.add_vectors(jac_collection, self.vectors)
        status = connect.create_index(jac_collection, index_type, index_param)
1296
        if index_type != IndexType.FLAT and index_type != IndexType.IVFLAT:
G
groot 已提交
1297 1298 1299 1300 1301
            assert not status.OK()
        else:
            assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
1302
    def test_create_index_partition(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1303 1304
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
1305
        method: create collection, create partition, and add vectors in it, create index
G
groot 已提交
1306 1307
        expected: return code equals to 0, and search success
        '''
1308 1309 1310
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
        logging.getLogger().info(get_jaccard_index)
X
Xiaohai Xu 已提交
1311 1312 1313
        status = connect.create_partition(jac_collection, tag)
        status, ids = connect.add_vectors(jac_collection, self.vectors, partition_tag=tag)
        status = connect.create_index(jac_collection, index_type, index_param)
G
groot 已提交
1314 1315 1316
        assert status.OK()

    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1317
    def test_create_index_without_connect(self, dis_connect, jac_collection):
G
groot 已提交
1318 1319
        '''
        target: test create index without connection
X
Xiaohai Xu 已提交
1320
        method: create collection and add vectors in it, check if added successfully
G
groot 已提交
1321 1322
        expected: raise exception
        '''
1323
        nlist = NLIST
1324
        index_param = {"nlist": nlist}
G
groot 已提交
1325
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
1326
            status = dis_connect.create_index(jac_collection, IndexType.IVF_SQ8, index_param)
G
groot 已提交
1327 1328

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
1329
    def test_create_index_search_with_query_vectors(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1330 1331
        '''
        target: test create index interface, search with more query vectors
X
Xiaohai Xu 已提交
1332
        method: create collection and add vectors in it, create index
G
groot 已提交
1333 1334
        expected: return code equals to 0, and search success
        '''
1335 1336 1337
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
        logging.getLogger().info(get_jaccard_index)
X
Xiaohai Xu 已提交
1338 1339 1340
        status, ids = connect.add_vectors(jac_collection, self.vectors)
        status = connect.create_index(jac_collection, index_type, index_param)
        logging.getLogger().info(connect.describe_index(jac_collection))
G
groot 已提交
1341 1342
        query_vecs = [self.vectors[0], self.vectors[1], self.vectors[2]]
        top_k = 5
1343
        search_param = get_search_param(index_type)
X
Xiaohai Xu 已提交
1344
        status, result = connect.search_vectors(jac_collection, top_k, query_vecs, params=search_param)
G
groot 已提交
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result) == len(query_vecs)

    """
    ******************************************************************
      The following cases are used to test `describe_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
1355
    def test_describe_index(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1356 1357
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
1358
        method: create collection and add vectors in it, create index, call describe index
G
groot 已提交
1359 1360
        expected: return code 0, and index instructure
        '''
1361 1362 1363
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
        logging.getLogger().info(get_jaccard_index)
X
Xiaohai Xu 已提交
1364 1365 1366
        # status, ids = connect.add_vectors(jac_collection, vectors[:5000])
        status = connect.create_index(jac_collection, index_type, index_param)
        status, result = connect.describe_index(jac_collection)
G
groot 已提交
1367
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1368
        assert result._collection_name == jac_collection
1369 1370
        assert result._index_type == index_type
        assert result._params == index_param
G
groot 已提交
1371

X
Xiaohai Xu 已提交
1372
    def test_describe_index_partition(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1373 1374
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
1375
        method: create collection, create partition and add vectors in it, create index, call describe index
G
groot 已提交
1376 1377
        expected: return code 0, and index instructure
        '''
1378 1379 1380
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
        logging.getLogger().info(get_jaccard_index)
X
Xiaohai Xu 已提交
1381 1382 1383 1384
        status = connect.create_partition(jac_collection, tag)
        status, ids = connect.add_vectors(jac_collection, vectors, partition_tag=tag)
        status = connect.create_index(jac_collection, index_type, index_param)
        status, result = connect.describe_index(jac_collection)
G
groot 已提交
1385
        logging.getLogger().info(result)
1386
        assert result._params == index_param
X
Xiaohai Xu 已提交
1387
        assert result._collection_name == jac_collection
1388
        assert result._index_type == index_type
G
groot 已提交
1389 1390 1391 1392 1393 1394 1395

    """
    ******************************************************************
      The following cases are used to test `drop_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
1396
    def test_drop_index(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1397 1398
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1399
        method: create collection and add vectors in it, create index, call drop index
G
groot 已提交
1400 1401
        expected: return code 0, and default index param
        '''
1402 1403
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
G
groot 已提交
1404 1405
        status, mode = connect._cmd("mode")
        assert status.OK()
X
Xiaohai Xu 已提交
1406 1407
        # status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(jac_collection, index_type, index_param)
G
groot 已提交
1408
        assert status.OK()
X
Xiaohai Xu 已提交
1409
        status, result = connect.describe_index(jac_collection)
G
groot 已提交
1410
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1411
        status = connect.drop_index(jac_collection)
G
groot 已提交
1412
        assert status.OK()
X
Xiaohai Xu 已提交
1413
        status, result = connect.describe_index(jac_collection)
G
groot 已提交
1414
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1415
        assert result._collection_name == jac_collection
G
groot 已提交
1416 1417
        assert result._index_type == IndexType.FLAT

X
Xiaohai Xu 已提交
1418
    def test_drop_index_partition(self, connect, jac_collection, get_jaccard_index):
G
groot 已提交
1419 1420
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1421
        method: create collection, create partition and add vectors in it, create index on collection, call drop collection index
G
groot 已提交
1422 1423
        expected: return code 0, and default index param
        '''
1424 1425
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
X
Xiaohai Xu 已提交
1426 1427 1428
        status = connect.create_partition(jac_collection, tag)
        status, ids = connect.add_vectors(jac_collection, vectors, partition_tag=tag)
        status = connect.create_index(jac_collection, index_type, index_param)
G
groot 已提交
1429
        assert status.OK()
X
Xiaohai Xu 已提交
1430
        status, result = connect.describe_index(jac_collection)
G
groot 已提交
1431
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1432
        status = connect.drop_index(jac_collection)
G
groot 已提交
1433
        assert status.OK()
X
Xiaohai Xu 已提交
1434
        status, result = connect.describe_index(jac_collection)
G
groot 已提交
1435
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1436
        assert result._collection_name == jac_collection
G
groot 已提交
1437 1438 1439
        assert result._index_type == IndexType.FLAT


D
del-zhenwu 已提交
1440
class TestIndexBinary:
G
groot 已提交
1441 1442 1443 1444
    tmp, vectors = gen_binary_vectors(nb, dim)

    @pytest.fixture(
        scope="function",
1445
        params=gen_index()
G
groot 已提交
1446
    )
1447
    def get_index(self, request, connect):
G
groot 已提交
1448 1449
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] == IndexType.IVF_SQ8H:
1450 1451
                pytest.skip("sq8h not support in CPU mode")
        if request.param["index_type"] == IndexType.IVF_PQ or request.param["index_type"] == IndexType.HNSW:
G
groot 已提交
1452 1453 1454 1455 1456
            pytest.skip("Skip PQ Temporary")
        return request.param

    @pytest.fixture(
        scope="function",
1457
        params=gen_simple_index()
G
groot 已提交
1458
    )
1459
    def get_simple_index(self, request, connect):
G
groot 已提交
1460 1461
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] == IndexType.IVF_SQ8H:
1462 1463
                pytest.skip("sq8h not support in CPU mode")
        if request.param["index_type"] == IndexType.IVF_PQ or request.param["index_type"] == IndexType.HNSW:
G
groot 已提交
1464 1465 1466 1467 1468
            pytest.skip("Skip PQ Temporary")
        return request.param

    @pytest.fixture(
        scope="function",
1469
        params=gen_simple_index()
G
groot 已提交
1470
    )
1471
    def get_hamming_index(self, request, connect):
G
groot 已提交
1472 1473 1474 1475 1476 1477
        logging.getLogger().info(request.param)
        if request.param["index_type"] == IndexType.IVFLAT or request.param["index_type"] == IndexType.FLAT:
            return request.param
        else:
            pytest.skip("Skip index Temporary")

D
del-zhenwu 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_substructure_index(self, request, connect):
        logging.getLogger().info(request.param)
        if request.param["index_type"] == IndexType.FLAT:
            return request.param
        else:
            pytest.skip("Skip index Temporary")

    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_superstructure_index(self, request, connect):
        logging.getLogger().info(request.param)
        if request.param["index_type"] == IndexType.FLAT:
            return request.param
        else:
            pytest.skip("Skip index Temporary")

G
groot 已提交
1500 1501 1502 1503 1504 1505
    """
    ******************************************************************
      The following cases are used to test `create_index` function
    ******************************************************************
    """
    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
1506
    def test_create_index(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1507 1508
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
1509
        method: create collection and add vectors in it, create index
G
groot 已提交
1510 1511
        expected: return code equals to 0, and search success
        '''
1512 1513 1514
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
        logging.getLogger().info(get_hamming_index)
X
Xiaohai Xu 已提交
1515 1516
        status, ids = connect.add_vectors(ham_collection, self.vectors)
        status = connect.create_index(ham_collection, index_type, index_param)
1517
        if index_type != IndexType.FLAT and index_type != IndexType.IVFLAT:
G
groot 已提交
1518 1519 1520 1521 1522
            assert not status.OK()
        else:
            assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
1523
    def test_create_index_partition(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1524 1525
        '''
        target: test create index interface
X
Xiaohai Xu 已提交
1526
        method: create collection, create partition, and add vectors in it, create index
G
groot 已提交
1527 1528
        expected: return code equals to 0, and search success
        '''
1529 1530 1531
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
        logging.getLogger().info(get_hamming_index)
X
Xiaohai Xu 已提交
1532 1533 1534
        status = connect.create_partition(ham_collection, tag)
        status, ids = connect.add_vectors(ham_collection, self.vectors, partition_tag=tag)
        status = connect.create_index(ham_collection, index_type, index_param)
G
groot 已提交
1535
        assert status.OK()
X
Xiaohai Xu 已提交
1536
        status, res = connect.count_collection(ham_collection)
1537
        assert res == len(self.vectors)
G
groot 已提交
1538

D
del-zhenwu 已提交
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_partition_structure(self, connect, substructure_collection, get_substructure_index):
        '''
        target: test create index interface
        method: create collection, create partition, and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        index_param = get_substructure_index["index_param"]
        index_type = get_substructure_index["index_type"]
        logging.getLogger().info(get_substructure_index)
        status = connect.create_partition(substructure_collection, tag)
        status, ids = connect.add_vectors(substructure_collection, self.vectors, partition_tag=tag)
        status = connect.create_index(substructure_collection, index_type, index_param)
        assert status.OK()
        status, res = connect.count_collection(substructure_collection,)
        assert res == len(self.vectors)

G
groot 已提交
1556
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
1557
    def test_create_index_without_connect(self, dis_connect, ham_collection):
G
groot 已提交
1558 1559
        '''
        target: test create index without connection
X
Xiaohai Xu 已提交
1560
        method: create collection and add vectors in it, check if added successfully
G
groot 已提交
1561 1562
        expected: raise exception
        '''
1563
        nlist = NLIST
1564
        index_param = {"nlist": nlist}
G
groot 已提交
1565
        with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
1566
            status = dis_connect.create_index(ham_collection, IndexType.IVF_SQ8, index_param)
G
groot 已提交
1567 1568

    @pytest.mark.timeout(BUILD_TIMEOUT)
X
Xiaohai Xu 已提交
1569
    def test_create_index_search_with_query_vectors(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1570 1571
        '''
        target: test create index interface, search with more query vectors
X
Xiaohai Xu 已提交
1572
        method: create collection and add vectors in it, create index
G
groot 已提交
1573 1574
        expected: return code equals to 0, and search success
        '''
1575 1576 1577
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
        logging.getLogger().info(get_hamming_index)
X
Xiaohai Xu 已提交
1578 1579 1580
        status, ids = connect.add_vectors(ham_collection, self.vectors)
        status = connect.create_index(ham_collection,  index_type, index_param)
        logging.getLogger().info(connect.describe_index(ham_collection))
G
groot 已提交
1581 1582
        query_vecs = [self.vectors[0], self.vectors[1], self.vectors[2]]
        top_k = 5
1583
        search_param = get_search_param(index_type)
X
Xiaohai Xu 已提交
1584
        status, result = connect.search_vectors(ham_collection, top_k, query_vecs, params=search_param)
G
groot 已提交
1585 1586 1587 1588
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result) == len(query_vecs)

D
del-zhenwu 已提交
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_search_with_query_vectors_superstructure(self, connect, superstructure_collection, get_superstructure_index):
        '''
        target: test create index interface, search with more query vectors
        method: create collection and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        index_param = get_superstructure_index["index_param"]
        index_type = get_superstructure_index["index_type"]
        logging.getLogger().info(get_superstructure_index)
        status, ids = connect.add_vectors(superstructure_collection, self.vectors)
        status = connect.create_index(superstructure_collection, index_type, index_param)
        logging.getLogger().info(connect.describe_index(superstructure_collection))
        query_vecs = [self.vectors[0], self.vectors[1], self.vectors[2]]
        top_k = 5
        search_param = get_search_param(index_type)
        status, result = connect.search_vectors(superstructure_collection, top_k, query_vecs, params=search_param)
        logging.getLogger().info(result)
        assert status.OK()
        assert len(result) == len(query_vecs)

G
groot 已提交
1610 1611 1612 1613 1614 1615
    """
    ******************************************************************
      The following cases are used to test `describe_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
1616
    def test_describe_index(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1617 1618
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
1619
        method: create collection and add vectors in it, create index, call describe index
G
groot 已提交
1620 1621
        expected: return code 0, and index instructure
        '''
1622 1623 1624
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
        logging.getLogger().info(get_hamming_index)
X
Xiaohai Xu 已提交
1625 1626 1627
        # status, ids = connect.add_vectors(jac_collection, vectors[:5000])
        status = connect.create_index(ham_collection, index_type, index_param)
        status, result = connect.describe_index(ham_collection)
G
groot 已提交
1628
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1629
        assert result._collection_name == ham_collection
1630 1631
        assert result._index_type == index_type
        assert result._params == index_param
G
groot 已提交
1632

X
Xiaohai Xu 已提交
1633
    def test_describe_index_partition(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1634 1635
        '''
        target: test describe index interface
X
Xiaohai Xu 已提交
1636
        method: create collection, create partition and add vectors in it, create index, call describe index
G
groot 已提交
1637 1638
        expected: return code 0, and index instructure
        '''
1639 1640 1641
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
        logging.getLogger().info(get_hamming_index)
X
Xiaohai Xu 已提交
1642 1643 1644 1645
        status = connect.create_partition(ham_collection, tag)
        status, ids = connect.add_vectors(ham_collection, vectors, partition_tag=tag)
        status = connect.create_index(ham_collection, index_type, index_param)
        status, result = connect.describe_index(ham_collection)
G
groot 已提交
1646
        logging.getLogger().info(result)
1647
        assert result._params == index_param
X
Xiaohai Xu 已提交
1648
        assert result._collection_name == ham_collection
1649
        assert result._index_type == index_type
G
groot 已提交
1650

D
del-zhenwu 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
    def test_describe_index_partition_superstructrue(self, connect, superstructure_collection, get_superstructure_index):
        '''
        target: test describe index interface
        method: create collection, create partition and add vectors in it, create index, call describe index
        expected: return code 0, and index instructure
        '''
        index_param = get_superstructure_index["index_param"]
        index_type = get_superstructure_index["index_type"]
        logging.getLogger().info(get_superstructure_index)
        status = connect.create_partition(superstructure_collection, tag)
        status, ids = connect.add_vectors(superstructure_collection, vectors, partition_tag=tag)
        status = connect.create_index(superstructure_collection, index_type, index_param)
        status, result = connect.describe_index(superstructure_collection)
        logging.getLogger().info(result)
        assert result._params == index_param
        assert result._collection_name == superstructure_collection
        assert result._index_type == index_type

G
groot 已提交
1669 1670 1671 1672 1673 1674
    """
    ******************************************************************
      The following cases are used to test `drop_index` function
    ******************************************************************
    """

X
Xiaohai Xu 已提交
1675
    def test_drop_index(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1676 1677
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1678
        method: create collection and add vectors in it, create index, call drop index
G
groot 已提交
1679 1680
        expected: return code 0, and default index param
        '''
1681 1682
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
G
groot 已提交
1683 1684
        status, mode = connect._cmd("mode")
        assert status.OK()
X
Xiaohai Xu 已提交
1685 1686
        # status, ids = connect.add_vectors(ip_collection, vectors)
        status = connect.create_index(ham_collection, index_type, index_param)
G
groot 已提交
1687
        assert status.OK()
X
Xiaohai Xu 已提交
1688
        status, result = connect.describe_index(ham_collection)
G
groot 已提交
1689
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1690
        status = connect.drop_index(ham_collection)
G
groot 已提交
1691
        assert status.OK()
X
Xiaohai Xu 已提交
1692
        status, result = connect.describe_index(ham_collection)
G
groot 已提交
1693
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1694
        assert result._collection_name == ham_collection
G
groot 已提交
1695 1696
        assert result._index_type == IndexType.FLAT

D
del-zhenwu 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
    def test_drop_index_substructure(self, connect, substructure_collection, get_substructure_index):
        '''
        target: test drop index interface
        method: create collection and add vectors in it, create index, call drop index
        expected: return code 0, and default index param
        '''
        index_param = get_substructure_index["index_param"]
        index_type = get_substructure_index["index_type"]
        status, mode = connect._cmd("mode")
        assert status.OK()
        status = connect.create_index(substructure_collection, index_type, index_param)
        assert status.OK()
        status, result = connect.describe_index(substructure_collection)
        logging.getLogger().info(result)
        status = connect.drop_index(substructure_collection)
        assert status.OK()
        status, result = connect.describe_index(substructure_collection)
        logging.getLogger().info(result)
        assert result._collection_name == substructure_collection
        assert result._index_type == IndexType.FLAT

X
Xiaohai Xu 已提交
1718
    def test_drop_index_partition(self, connect, ham_collection, get_hamming_index):
G
groot 已提交
1719 1720
        '''
        target: test drop index interface
X
Xiaohai Xu 已提交
1721
        method: create collection, create partition and add vectors in it, create index on collection, call drop collection index
G
groot 已提交
1722 1723
        expected: return code 0, and default index param
        '''
1724 1725
        index_param = get_hamming_index["index_param"]
        index_type = get_hamming_index["index_type"]
X
Xiaohai Xu 已提交
1726 1727 1728
        status = connect.create_partition(ham_collection, tag)
        status, ids = connect.add_vectors(ham_collection, vectors, partition_tag=tag)
        status = connect.create_index(ham_collection, index_type, index_param)
G
groot 已提交
1729
        assert status.OK()
X
Xiaohai Xu 已提交
1730
        status, result = connect.describe_index(ham_collection)
G
groot 已提交
1731
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1732
        status = connect.drop_index(ham_collection)
G
groot 已提交
1733
        assert status.OK()
X
Xiaohai Xu 已提交
1734
        status, result = connect.describe_index(ham_collection)
G
groot 已提交
1735
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1736
        assert result._collection_name == ham_collection
G
groot 已提交
1737 1738
        assert result._index_type == IndexType.FLAT

S
sahuang 已提交
1739
class TestIndexCollectionInvalid(object):
J
JinHai-CN 已提交
1740
    """
X
Xiaohai Xu 已提交
1741
    Test create / describe / drop index interfaces with invalid collection names
J
JinHai-CN 已提交
1742 1743 1744
    """
    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
1745
        params=gen_invalid_collection_names()
J
JinHai-CN 已提交
1746
    )
X
Xiaohai Xu 已提交
1747
    def get_collection_name(self, request):
J
JinHai-CN 已提交
1748 1749
        yield request.param

Z
zhenwu 已提交
1750
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1751 1752
    def test_create_index_with_invalid_collectionname(self, connect, get_collection_name):
        collection_name = get_collection_name
1753
        nlist = NLIST
1754
        index_param = {"nlist": nlist}
X
Xiaohai Xu 已提交
1755
        status = connect.create_index(collection_name, IndexType.IVF_SQ8, index_param)
J
JinHai-CN 已提交
1756 1757
        assert not status.OK()

Z
zhenwu 已提交
1758
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1759 1760 1761
    def test_describe_index_with_invalid_collectionname(self, connect, get_collection_name):
        collection_name = get_collection_name
        status, result = connect.describe_index(collection_name)
J
JinHai-CN 已提交
1762 1763
        assert not status.OK()   

Z
zhenwu 已提交
1764
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1765 1766 1767
    def test_drop_index_with_invalid_collectionname(self, connect, get_collection_name):
        collection_name = get_collection_name
        status = connect.drop_index(collection_name)
J
JinHai-CN 已提交
1768 1769 1770 1771 1772
        assert not status.OK()


class TestCreateIndexParamsInvalid(object):
    """
X
Xiaohai Xu 已提交
1773
    Test Building index with invalid collection names, collection names not in db
J
JinHai-CN 已提交
1774 1775 1776
    """
    @pytest.fixture(
        scope="function",
1777
        params=gen_invalid_index()
J
JinHai-CN 已提交
1778
    )
1779
    def get_index(self, request):
J
JinHai-CN 已提交
1780 1781
        yield request.param

Z
zhenwu 已提交
1782
    @pytest.mark.level(1)
X
Xiaohai Xu 已提交
1783
    def test_create_index_with_invalid_index_params(self, connect, collection, get_index):
1784 1785 1786
        index_param = get_index["index_param"]
        index_type = get_index["index_type"]
        logging.getLogger().info(get_index)
X
Xiaohai Xu 已提交
1787
        # status, ids = connect.add_vectors(collection, vectors)
1788
        if (not index_type) or (not isinstance(index_type, IndexType)):
Z
zhenwu 已提交
1789
            with pytest.raises(Exception) as e:
X
Xiaohai Xu 已提交
1790
                status = connect.create_index(collection, index_type, index_param)
Z
zhenwu 已提交
1791
        else:
X
Xiaohai Xu 已提交
1792
            status = connect.create_index(collection, index_type, index_param)
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
            assert not status.OK()

    """
    Test Building index with invalid nlist
    """
    @pytest.fixture(
        scope="function",
        params=[IndexType.FLAT,IndexType.IVFLAT,IndexType.IVF_SQ8,IndexType.IVF_SQ8H]
    )
    def get_index_type(self, request):
        yield request.param

X
Xiaohai Xu 已提交
1805 1806 1807
    def test_create_index_with_invalid_nlist(self, connect, collection, get_index_type):
        status, ids = connect.add_vectors(collection, vectors)
        status = connect.create_index(collection, get_index_type, {"nlist": INVALID_NLIST})
1808 1809 1810 1811 1812 1813
        if get_index_type != IndexType.FLAT:
            assert not status.OK()

    '''
    Test Building index with empty params
    '''
X
Xiaohai Xu 已提交
1814
    def test_create_index_with_empty_param(self, connect, collection, get_index_type):
1815
        logging.getLogger().info(get_index_type)
X
Xiaohai Xu 已提交
1816
        status = connect.create_index(collection, get_index_type, {})
1817
        if get_index_type != IndexType.FLAT :
Z
zhenwu 已提交
1818
            assert not status.OK()
X
Xiaohai Xu 已提交
1819
        status, result = connect.describe_index(collection)
1820
        logging.getLogger().info(result)
X
Xiaohai Xu 已提交
1821
        assert result._collection_name == collection
1822 1823
        assert result._index_type == IndexType.FLAT