test_index.py 53.2 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
J
JinHai-CN 已提交
12 13 14
from milvus import Milvus, IndexType, MetricType
from utils import *

Z
zhenwu 已提交
15
nb = 10000
J
JinHai-CN 已提交
16
dim = 128
Z
zhenwu 已提交
17
index_file_size = 20
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"
J
JinHai-CN 已提交
24 25 26 27 28 29 30


class TestIndexBase:
    @pytest.fixture(
        scope="function",
        params=gen_index_params()
    )
31
    def get_index_params(self, request, args):
32
        if "internal" not in args:
33 34 35
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
36 37 38 39 40

    @pytest.fixture(
        scope="function",
        params=gen_simple_index_params()
    )
Z
zhenwu 已提交
41
    def get_simple_index_params(self, request, args):
Z
zhenwu 已提交
42 43 44 45
        if "internal" not in args:
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
46 47 48 49 50 51 52 53

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

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
54
    def test_create_index(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
55 56 57 58 59
        '''
        target: test create index interface
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
Z
zhenwu 已提交
60
        index_params = get_simple_index_params
J
JinHai-CN 已提交
61 62 63 64 65
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(table, vectors)
        status = connect.create_index(table, index_params)
        assert status.OK()

Z
zhenwu 已提交
66 67 68 69 70 71 72 73 74 75 76 77
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_no_vectors(self, connect, table, get_simple_index_params):
        '''
        target: test create index interface
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        index_params = get_simple_index_params
        logging.getLogger().info(index_params)
        status = connect.create_index(table, index_params)
        assert status.OK()

Z
zhenwu 已提交
78
    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
79
    def test_create_index_partition(self, connect, table, get_simple_index_params):
Z
zhenwu 已提交
80 81 82 83 84 85
        '''
        target: test create index interface
        method: create table, create partition, and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        partition_name = gen_unique_str()
Z
zhenwu 已提交
86
        index_params = get_simple_index_params
Z
zhenwu 已提交
87 88
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
89 90 91 92 93 94
        logging.getLogger().info(index_params)
        status = connect.create_partition(table, partition_name, tag)
        status, ids = connect.add_vectors(table, vectors, partition_tag=tag)
        status = connect.create_index(table, index_params)
        assert status.OK()

J
JinHai-CN 已提交
95 96 97 98 99 100 101
    @pytest.mark.level(2)
    def test_create_index_without_connect(self, dis_connect, table):
        '''
        target: test create index without connection
        method: create table and add vectors in it, check if added successfully
        expected: raise exception
        '''
Z
zhenwu 已提交
102 103
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
104
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
105
            status = dis_connect.create_index(table, index_param)
J
JinHai-CN 已提交
106 107

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
108
    def test_create_index_search_with_query_vectors(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
109 110 111 112 113
        '''
        target: test create index interface, search with more query vectors
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
Z
zhenwu 已提交
114
        index_params = get_simple_index_params
J
JinHai-CN 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(table, vectors)
        status = connect.create_index(table, index_params)
        logging.getLogger().info(connect.describe_index(table))
        query_vecs = [vectors[0], vectors[1], vectors[2]]
        top_k = 5
        status, result = connect.search_vectors(table, top_k, nprobe, query_vecs)
        assert status.OK()
        assert len(result) == len(query_vecs)
        logging.getLogger().info(result)

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
    @pytest.mark.level(2)
    def _test_create_index_multiprocessing(self, connect, table, args):
        '''
        target: test create index interface with multiprocess
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        status, ids = connect.add_vectors(table, vectors)

        def build(connect):
            status = connect.create_index(table)
            assert status.OK()

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

        for i in range(process_num):
            m = Milvus()
            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
        status, result = connect.search_vectors(table, top_k, nprobe, query_vec)
        assert len(result) == 1
        assert len(result[0]) == top_k
        assert result[0][0].distance == 0.0

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def _test_create_index_multiprocessing_multitable(self, connect, args):
        '''
        target: test create index interface with multiprocess
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        process_num = 8
        loop_num = 8
        processes = []

        table = []
        j = 0
        while j < (process_num*loop_num):
            table_name = gen_unique_str("test_create_index_multiprocessing")
            table.append(table_name)
            param = {'table_name': table_name,
                    'dimension': dim,
                    'index_type': IndexType.FLAT,
                    'store_raw_vector': False}
            connect.create_table(param)
            j = j + 1

        def create_index():
            i = 0
            while i < loop_num:
                # assert connect.has_table(table[ids*process_num+i])
                status, ids = connect.add_vectors(table[ids*process_num+i], vectors)

                status = connect.create_index(table[ids*process_num+i])
                assert status.OK()
                query_vec = [vectors[0]]
                top_k = 1
                status, result = connect.search_vectors(table[ids*process_num+i], top_k, nprobe, query_vec)
                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):
            m = Milvus()
            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()

    def test_create_index_table_not_existed(self, connect):
        '''
        target: test create index interface when table name not existed
Z
zhenwu 已提交
218
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
219 220 221 222
            , make sure the table name not in index
        expected: return code not equals to 0, create index failed
        '''
        table_name = gen_unique_str(self.__class__.__name__)
Z
zhenwu 已提交
223 224 225
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(table_name, index_param)
J
JinHai-CN 已提交
226 227 228 229 230 231 232 233 234
        assert not status.OK()

    def test_create_index_table_None(self, connect):
        '''
        target: test create index interface when table name is None
        method: create table and add vectors in it, create index with an table_name: None
        expected: return code not equals to 0, create index failed
        '''
        table_name = None
Z
zhenwu 已提交
235 236
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
237
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
238
            status = connect.create_index(table_name, index_param)
J
JinHai-CN 已提交
239 240 241 242 243 244 245

    def test_create_index_no_vectors(self, connect, table):
        '''
        target: test create index interface when there is no vectors in table
        method: create table and add no vectors in it, and then create index
        expected: return code equals to 0
        '''
Z
zhenwu 已提交
246 247 248
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
249 250 251
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
252
    def test_create_index_no_vectors_then_add_vectors(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
253 254 255 256 257
        '''
        target: test create index interface when there is no vectors in table, and does not affect the subsequent process
        method: create table and add no vectors in it, and then create index, add vectors in it
        expected: return code equals to 0
        '''
Z
zhenwu 已提交
258
        index_param = get_simple_index_params
Z
zhenwu 已提交
259 260
        if index_param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
261
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
262 263 264 265
        status, ids = connect.add_vectors(table, vectors)
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
266
    def test_create_same_index_repeatedly(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
267 268 269 270 271 272
        '''
        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
        '''
        status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
273
        index_param = get_simple_index_params
Z
zhenwu 已提交
274 275
        if index_param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
276 277
        status = connect.create_index(table, index_param)
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291
        assert status.OK()
        query_vec = [vectors[0]]
        top_k = 1
        status, result = connect.search_vectors(table, top_k, nprobe, query_vec)
        assert len(result) == 1
        assert len(result[0]) == top_k

    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_different_index_repeatedly(self, connect, table):
        '''
        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
        '''
Z
zhenwu 已提交
292
        nlist = 16384
J
JinHai-CN 已提交
293
        status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
294 295 296
        index_type_1 = IndexType.IVF_SQ8
        index_type_2 = IndexType.IVFLAT
        index_params = [{"index_type": index_type_1, "nlist": nlist}, {"index_type": index_type_2, "nlist": nlist}]
J
JinHai-CN 已提交
297
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
298 299 300
        for index_param in index_params:
            status = connect.create_index(table, index_param)
            assert status.OK()
J
JinHai-CN 已提交
301
        status, result = connect.describe_index(table)
Z
zhenwu 已提交
302
        assert result._nlist == nlist
J
JinHai-CN 已提交
303
        assert result._table_name == table
Z
zhenwu 已提交
304
        assert result._index_type == index_type_2
J
JinHai-CN 已提交
305 306 307 308 309 310 311

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

Z
zhenwu 已提交
312
    def test_describe_index(self, connect, table, get_index_params):
J
JinHai-CN 已提交
313 314 315 316 317
        '''
        target: test describe index interface
        method: create table and add vectors in it, create index, call describe index
        expected: return code 0, and index instructure
        '''
Z
zhenwu 已提交
318
        index_params = get_index_params
J
JinHai-CN 已提交
319
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
320
        # status, ids = connect.add_vectors(table, vectors)
J
JinHai-CN 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
        status = connect.create_index(table, index_params)
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == table
        assert result._index_type == index_params["index_type"]

    def test_describe_and_drop_index_multi_tables(self, connect, get_simple_index_params):
        '''
        target: test create, describe and drop index interface with multiple tables of L2
        method: create tables and add vectors in it, create index, call describe index
        expected: return code 0, and index instructure
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
        table_list = []
        for i in range(10):
Z
zhenwu 已提交
338
            table_name = gen_unique_str()
J
JinHai-CN 已提交
339 340 341 342 343 344 345
            table_list.append(table_name)
            param = {'table_name': table_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
            connect.create_table(param)
            index_params = get_simple_index_params
Z
zhenwu 已提交
346 347
            if index_params["index_type"] == IndexType.IVF_PQ:
                pytest.skip("Skip some PQ cases")
J
JinHai-CN 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            logging.getLogger().info(index_params)
            status, ids = connect.add_vectors(table_name=table_name, records=vectors)
            status = connect.create_index(table_name, index_params)
            assert status.OK()

        for i in range(10):
            status, result = connect.describe_index(table_list[i])
            logging.getLogger().info(result)
            assert result._nlist == index_params["nlist"]
            assert result._table_name == table_list[i]
            assert result._index_type == index_params["index_type"]

        for i in range(10):
            status = connect.drop_index(table_list[i])
            assert status.OK()
            status, result = connect.describe_index(table_list[i])
            logging.getLogger().info(result)
            assert result._nlist == 16384
            assert result._table_name == table_list[i]
            assert result._index_type == IndexType.FLAT

    @pytest.mark.level(2)
    def test_describe_index_without_connect(self, dis_connect, table):
        '''
        target: test describe index without connection
        method: describe index, and check if describe successfully
        expected: raise exception
        '''
        with pytest.raises(Exception) as e:
            status = dis_connect.describe_index(table)

    def test_describe_index_table_not_existed(self, connect):
        '''
        target: test describe index interface when table name not existed
Z
zhenwu 已提交
382
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
            , make sure the table name not in index
        expected: return code not equals to 0, describe index failed
        '''
        table_name = gen_unique_str(self.__class__.__name__)
        status, result = connect.describe_index(table_name)
        assert not status.OK()

    def test_describe_index_table_None(self, connect):
        '''
        target: test describe index interface when table name is None
        method: create table and add vectors in it, create index with an table_name: None
        expected: return code not equals to 0, describe index failed
        '''
        table_name = None
        with pytest.raises(Exception) as e:
            status = connect.describe_index(table_name)

    def test_describe_index_not_create(self, connect, table):
        '''
        target: test describe index interface when index not created
Z
zhenwu 已提交
403
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
            , make sure the table name not in index
        expected: return code not equals to 0, describe index failed
        '''
        status, ids = connect.add_vectors(table, vectors)
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        assert status.OK()
        # assert result._nlist == index_params["nlist"]
        # assert result._table_name == table
        # assert result._index_type == index_params["index_type"]

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

Z
zhenwu 已提交
421
    def test_drop_index(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
422 423 424 425 426
        '''
        target: test drop index interface
        method: create table and add vectors in it, create index, call drop index
        expected: return code 0, and default index param
        '''
Z
zhenwu 已提交
427
        index_param = get_simple_index_params
Z
zhenwu 已提交
428
        # status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
429
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
430 431 432 433 434 435 436 437 438 439 440
        assert status.OK()
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        status = connect.drop_index(table)
        assert status.OK()
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == table
        assert result._index_type == IndexType.FLAT

Z
zhenwu 已提交
441
    def test_drop_index_repeatly(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
442 443 444 445 446
        '''
        target: test drop index repeatly
        method: create index, call drop index, and drop again
        expected: return code 0
        '''
Z
zhenwu 已提交
447
        index_param = get_simple_index_params
Z
zhenwu 已提交
448
        # status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
449
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        assert status.OK()
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        status = connect.drop_index(table)
        assert status.OK()
        status = connect.drop_index(table)
        assert status.OK()
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == table
        assert result._index_type == IndexType.FLAT

    @pytest.mark.level(2)
    def test_drop_index_without_connect(self, dis_connect, table):
        '''
        target: test drop index without connection
        method: drop index, and check if drop successfully
        expected: raise exception
        '''
        with pytest.raises(Exception) as e:
            status = dis_connect.drop_index(table)

    def test_drop_index_table_not_existed(self, connect):
        '''
        target: test drop index interface when table name not existed
Z
zhenwu 已提交
476
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
            , make sure the table name not in index, and then drop it
        expected: return code not equals to 0, drop index failed
        '''
        table_name = gen_unique_str(self.__class__.__name__)
        status = connect.drop_index(table_name)
        assert not status.OK()

    def test_drop_index_table_None(self, connect):
        '''
        target: test drop index interface when table name is None
        method: create table and add vectors in it, create index with an table_name: None
        expected: return code not equals to 0, drop index failed
        '''
        table_name = None
        with pytest.raises(Exception) as e:
            status = connect.drop_index(table_name)

    def test_drop_index_table_not_create(self, connect, table):
        '''
        target: test drop index interface when index not created
        method: create table and add vectors in it, create index
        expected: return code not equals to 0, drop index failed
        '''
Z
zhenwu 已提交
500 501
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        status, ids = connect.add_vectors(table, vectors)
        status, result = connect.describe_index(table)
        logging.getLogger().info(result)
        # no create index
        status = connect.drop_index(table)
        logging.getLogger().info(status)
        assert status.OK()

    def test_create_drop_index_repeatly(self, connect, table, get_simple_index_params):
        '''
        target: test create / drop index repeatly, use the same index params
        method: create index, drop index, four times
        expected: return code 0
        '''
        index_params = get_simple_index_params
Z
zhenwu 已提交
517
        # status, ids = connect.add_vectors(table, vectors)
J
JinHai-CN 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
        for i in range(2):
            status = connect.create_index(table, index_params)
            assert status.OK()
            status, result = connect.describe_index(table)
            logging.getLogger().info(result)
            status = connect.drop_index(table)
            assert status.OK()
            status, result = connect.describe_index(table)
            logging.getLogger().info(result)
            assert result._nlist == 16384
            assert result._table_name == table
            assert result._index_type == IndexType.FLAT

    def test_create_drop_index_repeatly_different_index_params(self, connect, table):
        '''
        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
        '''
Z
zhenwu 已提交
537 538
        nlist = 16384
        index_params = [{"index_type": IndexType.IVFLAT, "nlist": nlist}, {"index_type": IndexType.IVF_SQ8, "nlist": nlist}]
Z
zhenwu 已提交
539
        # status, ids = connect.add_vectors(table, vectors)
J
JinHai-CN 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
        for i in range(2):
            status = connect.create_index(table, index_params[i])
            assert status.OK()
            status, result = connect.describe_index(table)
            logging.getLogger().info(result)
            status = connect.drop_index(table)
            assert status.OK()
            status, result = connect.describe_index(table)
            logging.getLogger().info(result)
            assert result._nlist == 16384
            assert result._table_name == table
            assert result._index_type == IndexType.FLAT


class TestIndexIP:
    @pytest.fixture(
        scope="function",
        params=gen_index_params()
    )
559
    def get_index_params(self, request, args):
560
        if "internal" not in args:
561 562 563
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
564 565 566 567 568

    @pytest.fixture(
        scope="function",
        params=gen_simple_index_params()
    )
Z
zhenwu 已提交
569
    def get_simple_index_params(self, request, args):
Z
zhenwu 已提交
570 571 572 573
        if "internal" not in args:
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
574 575 576 577 578 579

    """
    ******************************************************************
      The following cases are used to test `create_index` function
    ******************************************************************
    """
Z
zhenwu 已提交
580
    @pytest.mark.level(2)
J
JinHai-CN 已提交
581
    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
582
    def test_create_index(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
583 584 585 586 587
        '''
        target: test create index interface
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
Z
zhenwu 已提交
588
        index_params = get_simple_index_params
J
JinHai-CN 已提交
589 590 591
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
592
        assert status.OK()
J
JinHai-CN 已提交
593

Z
zhenwu 已提交
594
    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
595
    def test_create_index_partition(self, connect, ip_table, get_simple_index_params):
Z
zhenwu 已提交
596 597 598 599 600 601
        '''
        target: test create index interface
        method: create table, create partition, and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        partition_name = gen_unique_str()
Z
zhenwu 已提交
602
        index_params = get_simple_index_params
Z
zhenwu 已提交
603 604
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
605 606 607 608
        logging.getLogger().info(index_params)
        status = connect.create_partition(ip_table, partition_name, tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status = connect.create_index(partition_name, index_params)
Z
zhenwu 已提交
609
        assert status.OK()
Z
zhenwu 已提交
610

J
JinHai-CN 已提交
611 612 613 614 615 616 617
    @pytest.mark.level(2)
    def test_create_index_without_connect(self, dis_connect, ip_table):
        '''
        target: test create index without connection
        method: create table and add vectors in it, check if added successfully
        expected: raise exception
        '''
Z
zhenwu 已提交
618 619
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
620
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
621
            status = dis_connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
622 623

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
624
    def test_create_index_search_with_query_vectors(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
625 626 627 628 629
        '''
        target: test create index interface, search with more query vectors
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
Z
zhenwu 已提交
630
        index_params = get_simple_index_params
J
JinHai-CN 已提交
631 632 633
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
634 635 636 637 638
        logging.getLogger().info(connect.describe_index(ip_table))
        query_vecs = [vectors[0], vectors[1], vectors[2]]
        top_k = 5
        status, result = connect.search_vectors(ip_table, top_k, nprobe, query_vecs)
        logging.getLogger().info(result)
639
        assert status.OK()
Z
zhenwu 已提交
640
        assert len(result) == len(query_vecs)
J
JinHai-CN 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
    @pytest.mark.level(2)
    def _test_create_index_multiprocessing(self, connect, ip_table, args):
        '''
        target: test create index interface with multiprocess
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        status, ids = connect.add_vectors(ip_table, vectors)

        def build(connect):
            status = connect.create_index(ip_table)
            assert status.OK()

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

        for i in range(process_num):
            m = Milvus()
            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
        status, result = connect.search_vectors(ip_table, top_k, nprobe, query_vec)
        assert len(result) == 1
        assert len(result[0]) == top_k
        assert result[0][0].distance == 0.0

    # TODO: enable
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def _test_create_index_multiprocessing_multitable(self, connect, args):
        '''
        target: test create index interface with multiprocess
        method: create table and add vectors in it, create index
        expected: return code equals to 0, and search success
        '''
        process_num = 8
        loop_num = 8
        processes = []

        table = []
        j = 0
        while j < (process_num*loop_num):
            table_name = gen_unique_str("test_create_index_multiprocessing")
            table.append(table_name)
            param = {'table_name': table_name,
                    'dimension': dim}
            connect.create_table(param)
            j = j + 1

        def create_index():
            i = 0
            while i < loop_num:
                # assert connect.has_table(table[ids*process_num+i])
                status, ids = connect.add_vectors(table[ids*process_num+i], vectors)

                status = connect.create_index(table[ids*process_num+i])
                assert status.OK()
                query_vec = [vectors[0]]
                top_k = 1
                status, result = connect.search_vectors(table[ids*process_num+i], top_k, nprobe, query_vec)
                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):
            m = Milvus()
            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()

    def test_create_index_no_vectors(self, connect, ip_table):
        '''
        target: test create index interface when there is no vectors in table
        method: create table and add no vectors in it, and then create index
        expected: return code equals to 0
        '''
Z
zhenwu 已提交
735 736 737
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
738 739 740
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
741
    def test_create_index_no_vectors_then_add_vectors(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
742 743 744 745 746
        '''
        target: test create index interface when there is no vectors in table, and does not affect the subsequent process
        method: create table and add no vectors in it, and then create index, add vectors in it
        expected: return code equals to 0
        '''
Z
zhenwu 已提交
747
        index_param = get_simple_index_params
Z
zhenwu 已提交
748 749
        if index_param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
750
        status = connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
751 752 753 754 755 756 757 758 759 760
        status, ids = connect.add_vectors(ip_table, vectors)
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_same_index_repeatedly(self, connect, ip_table):
        '''
        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
        '''
Z
zhenwu 已提交
761
        nlist = 16384
J
JinHai-CN 已提交
762
        status, ids = connect.add_vectors(ip_table, vectors)
Z
zhenwu 已提交
763 764 765
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(ip_table, index_param)
        status = connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779
        assert status.OK()
        query_vec = [vectors[0]]
        top_k = 1
        status, result = connect.search_vectors(ip_table, top_k, nprobe, query_vec)
        assert len(result) == 1
        assert len(result[0]) == top_k

    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_different_index_repeatedly(self, connect, ip_table):
        '''
        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
        '''
Z
zhenwu 已提交
780
        nlist = 16384
J
JinHai-CN 已提交
781
        status, ids = connect.add_vectors(ip_table, vectors)
Z
zhenwu 已提交
782 783 784
        index_type_1 = IndexType.IVF_SQ8
        index_type_2 = IndexType.IVFLAT
        index_params = [{"index_type": index_type_1, "nlist": nlist}, {"index_type": index_type_2, "nlist": nlist}]
J
JinHai-CN 已提交
785
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
786 787 788
        for index_param in index_params:
            status = connect.create_index(ip_table, index_param)
            assert status.OK()
J
JinHai-CN 已提交
789
        status, result = connect.describe_index(ip_table)
Z
zhenwu 已提交
790
        assert result._nlist == nlist
J
JinHai-CN 已提交
791
        assert result._table_name == ip_table
Z
zhenwu 已提交
792
        assert result._index_type == index_type_2
J
JinHai-CN 已提交
793 794 795 796 797 798 799

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

Z
zhenwu 已提交
800
    def test_describe_index(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
801 802 803 804 805
        '''
        target: test describe index interface
        method: create table and add vectors in it, create index, call describe index
        expected: return code 0, and index instructure
        '''
Z
zhenwu 已提交
806
        index_params = get_simple_index_params
J
JinHai-CN 已提交
807
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
808
        # status, ids = connect.add_vectors(ip_table, vectors[:5000])
J
JinHai-CN 已提交
809 810 811 812 813 814 815
        status = connect.create_index(ip_table, index_params)
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == ip_table
        assert result._index_type == index_params["index_type"]

Z
zhenwu 已提交
816 817 818 819 820 821 822 823
    def test_describe_index_partition(self, connect, ip_table, get_simple_index_params):
        '''
        target: test describe index interface
        method: create table, create partition and add vectors in it, create index, call describe index
        expected: return code 0, and index instructure
        '''
        partition_name = gen_unique_str()
        index_params = get_simple_index_params
Z
zhenwu 已提交
824 825
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
        logging.getLogger().info(index_params)
        status = connect.create_partition(ip_table, partition_name, tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status = connect.create_index(ip_table, index_params)
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == ip_table
        assert result._index_type == index_params["index_type"]
        status, result = connect.describe_index(partition_name)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == partition_name
        assert result._index_type == index_params["index_type"]

    def test_describe_index_partition_A(self, connect, ip_table, get_simple_index_params):
        '''
        target: test describe index interface
        method: create table, create partition and add vectors in it, create index on partition, call describe index
        expected: return code 0, and index instructure
        '''
        partition_name = gen_unique_str()
        index_params = get_simple_index_params
Z
zhenwu 已提交
849 850
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
        logging.getLogger().info(index_params)
        status = connect.create_partition(ip_table, partition_name, tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status = connect.create_index(partition_name, index_params)
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
        status, result = connect.describe_index(partition_name)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == partition_name
        assert result._index_type == index_params["index_type"]

    def test_describe_index_partition_B(self, connect, ip_table, get_simple_index_params):
        '''
        target: test describe index interface
        method: create table, create partitions and add vectors in it, create index on partitions, call describe index
        expected: return code 0, and index instructure
        '''
        partition_name = gen_unique_str()
        new_partition_name = gen_unique_str()
        new_tag = "new_tag"
        index_params = get_simple_index_params
Z
zhenwu 已提交
876 877
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
        logging.getLogger().info(index_params)
        status = connect.create_partition(ip_table, partition_name, tag)
        status = connect.create_partition(ip_table, new_partition_name, new_tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=new_tag)
        status = connect.create_index(partition_name, index_params)
        status = connect.create_index(new_partition_name, index_params)
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
        status, result = connect.describe_index(new_partition_name)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == new_partition_name
        assert result._index_type == index_params["index_type"]

J
JinHai-CN 已提交
896 897 898 899 900 901 902 903 904 905
    def test_describe_and_drop_index_multi_tables(self, connect, get_simple_index_params):
        '''
        target: test create, describe and drop index interface with multiple tables of IP
        method: create tables and add vectors in it, create index, call describe index
        expected: return code 0, and index instructure
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
        table_list = []
        for i in range(10):
Z
zhenwu 已提交
906
            table_name = gen_unique_str()
J
JinHai-CN 已提交
907 908 909 910 911 912 913
            table_list.append(table_name)
            param = {'table_name': table_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
            connect.create_table(param)
            index_params = get_simple_index_params
Z
zhenwu 已提交
914 915
            if index_params["index_type"] == IndexType.IVF_PQ:
                pytest.skip("Skip some PQ cases")
J
JinHai-CN 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
            logging.getLogger().info(index_params)
            status, ids = connect.add_vectors(table_name=table_name, records=vectors)
            status = connect.create_index(table_name, index_params)
            assert status.OK()

        for i in range(10):
            status, result = connect.describe_index(table_list[i])
            logging.getLogger().info(result)
            assert result._nlist == index_params["nlist"]
            assert result._table_name == table_list[i]
            assert result._index_type == index_params["index_type"]

        for i in range(10):
            status = connect.drop_index(table_list[i])
            assert status.OK()
            status, result = connect.describe_index(table_list[i])
            logging.getLogger().info(result)
            assert result._nlist == 16384
            assert result._table_name == table_list[i]
            assert result._index_type == IndexType.FLAT

    @pytest.mark.level(2)
    def test_describe_index_without_connect(self, dis_connect, ip_table):
        '''
        target: test describe index without connection
        method: describe index, and check if describe successfully
        expected: raise exception
        '''
        with pytest.raises(Exception) as e:
            status = dis_connect.describe_index(ip_table)

    def test_describe_index_not_create(self, connect, ip_table):
        '''
        target: test describe index interface when index not created
Z
zhenwu 已提交
950
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
            , make sure the table name not in index
        expected: return code not equals to 0, describe index failed
        '''
        status, ids = connect.add_vectors(ip_table, vectors)
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert status.OK()
        # assert result._nlist == index_params["nlist"]
        # assert result._table_name == table
        # assert result._index_type == index_params["index_type"]

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

Z
zhenwu 已提交
968
    def test_drop_index(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
969 970 971 972 973
        '''
        target: test drop index interface
        method: create table and add vectors in it, create index, call drop index
        expected: return code 0, and default index param
        '''
Z
zhenwu 已提交
974
        index_params = get_simple_index_params
975 976
        status, mode = connect._cmd("mode")
        assert status.OK()
977
        # status, ids = connect.add_vectors(ip_table, vectors)
J
JinHai-CN 已提交
978
        status = connect.create_index(ip_table, index_params)
979
        if str(mode) == "GPU" and index_params["index_type"] == IndexType.IVF_PQ:
980 981 982
            assert not status.OK()
        else:
            assert status.OK()
Z
zhenwu 已提交
983 984 985 986 987 988 989 990 991
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        status = connect.drop_index(ip_table)
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
J
JinHai-CN 已提交
992

Z
zhenwu 已提交
993 994 995 996 997 998 999 1000
    def test_drop_index_partition(self, connect, ip_table, get_simple_index_params):
        '''
        target: test drop index interface
        method: create table, create partition and add vectors in it, create index on table, call drop table index
        expected: return code 0, and default index param
        '''
        partition_name = gen_unique_str()
        index_params = get_simple_index_params
Z
zhenwu 已提交
1001 1002
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
1003 1004 1005
        status = connect.create_partition(ip_table, partition_name, tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        status = connect.drop_index(ip_table)
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
Z
zhenwu 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024

    def test_drop_index_partition_A(self, connect, ip_table, get_simple_index_params):
        '''
        target: test drop index interface
        method: create table, create partition and add vectors in it, create index on partition, call drop table index
        expected: return code 0, and default index param
        '''
        partition_name = gen_unique_str()
        index_params = get_simple_index_params
Z
zhenwu 已提交
1025 1026
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
1027 1028 1029
        status = connect.create_partition(ip_table, partition_name, tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status = connect.create_index(partition_name, index_params)
Z
zhenwu 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
        assert status.OK()
        status = connect.drop_index(ip_table)
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
        status, result = connect.describe_index(partition_name)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == partition_name
        assert result._index_type == IndexType.FLAT
Z
zhenwu 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051

    def test_drop_index_partition_B(self, connect, ip_table, get_simple_index_params):
        '''
        target: test drop index interface
        method: create table, create partition and add vectors in it, create index on partition, call drop partition index
        expected: return code 0, and default index param
        '''
        partition_name = gen_unique_str()
        index_params = get_simple_index_params
Z
zhenwu 已提交
1052 1053
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
1054 1055 1056
        status = connect.create_partition(ip_table, partition_name, tag)
        status, ids = connect.add_vectors(ip_table, vectors, partition_tag=tag)
        status = connect.create_index(partition_name, index_params)
Z
zhenwu 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
        assert status.OK()
        status = connect.drop_index(partition_name)
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
        status, result = connect.describe_index(partition_name)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == partition_name
        assert result._index_type == IndexType.FLAT
Z
zhenwu 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

    def test_drop_index_partition_C(self, connect, ip_table, get_simple_index_params):
        '''
        target: test drop index interface
        method: create table, create partitions and add vectors in it, create index on partitions, call drop partition index
        expected: return code 0, and default index param
        '''
        partition_name = gen_unique_str()
        new_partition_name = gen_unique_str()
        new_tag = "new_tag"
        index_params = get_simple_index_params
Z
zhenwu 已提交
1081 1082
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
Z
zhenwu 已提交
1083 1084 1085 1086
        status = connect.create_partition(ip_table, partition_name, tag)
        status = connect.create_partition(ip_table, new_partition_name, new_tag)
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
        assert status.OK()
        status = connect.drop_index(new_partition_name)
        assert status.OK()
        status, result = connect.describe_index(new_partition_name)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == new_partition_name
        assert result._index_type == IndexType.FLAT
        status, result = connect.describe_index(partition_name)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == partition_name
        assert result._index_type == index_params["index_type"]
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == index_params["nlist"]
        assert result._table_name == ip_table
        assert result._index_type == index_params["index_type"]
Z
zhenwu 已提交
1105

J
JinHai-CN 已提交
1106 1107 1108 1109 1110 1111 1112
    def test_drop_index_repeatly(self, connect, ip_table, get_simple_index_params):
        '''
        target: test drop index repeatly
        method: create index, call drop index, and drop again
        expected: return code 0
        '''
        index_params = get_simple_index_params
Z
zhenwu 已提交
1113
        # status, ids = connect.add_vectors(ip_table, vectors)
J
JinHai-CN 已提交
1114
        status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        status = connect.drop_index(ip_table)
        assert status.OK()
        status = connect.drop_index(ip_table)
        assert status.OK()
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        assert result._nlist == 16384
        assert result._table_name == ip_table
        assert result._index_type == IndexType.FLAT
J
JinHai-CN 已提交
1127 1128 1129 1130 1131 1132 1133 1134

    @pytest.mark.level(2)
    def test_drop_index_without_connect(self, dis_connect, ip_table):
        '''
        target: test drop index without connection
        method: drop index, and check if drop successfully
        expected: raise exception
        '''
Z
zhenwu 已提交
1135 1136
        nlist = 16384
        index_param = {"index_type": IndexType.IVFLAT, "nlist": nlist}
J
JinHai-CN 已提交
1137
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
1138
            status = dis_connect.drop_index(ip_table, index_param)
J
JinHai-CN 已提交
1139 1140 1141 1142 1143 1144 1145

    def test_drop_index_table_not_create(self, connect, ip_table):
        '''
        target: test drop index interface when index not created
        method: create table and add vectors in it, create index
        expected: return code not equals to 0, drop index failed
        '''
Z
zhenwu 已提交
1146 1147 1148
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        logging.getLogger().info(index_param)
J
JinHai-CN 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
        status, ids = connect.add_vectors(ip_table, vectors)
        status, result = connect.describe_index(ip_table)
        logging.getLogger().info(result)
        # no create index
        status = connect.drop_index(ip_table)
        logging.getLogger().info(status)
        assert status.OK()

    def test_create_drop_index_repeatly(self, connect, ip_table, get_simple_index_params):
        '''
        target: test create / drop index repeatly, use the same index params
        method: create index, drop index, four times
        expected: return code 0
        '''
        index_params = get_simple_index_params
Z
zhenwu 已提交
1164 1165
        if index_params["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip some PQ cases")
J
JinHai-CN 已提交
1166 1167 1168
        status, ids = connect.add_vectors(ip_table, vectors)
        for i in range(2):
            status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
            assert status.OK()
            status, result = connect.describe_index(ip_table)
            logging.getLogger().info(result)
            status = connect.drop_index(ip_table)
            assert status.OK()
            status, result = connect.describe_index(ip_table)
            logging.getLogger().info(result)
            assert result._nlist == 16384
            assert result._table_name == ip_table
            assert result._index_type == IndexType.FLAT
J
JinHai-CN 已提交
1179 1180 1181 1182 1183 1184 1185

    def test_create_drop_index_repeatly_different_index_params(self, connect, ip_table):
        '''
        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
        '''
Z
zhenwu 已提交
1186 1187
        nlist = 16384
        index_params = [{"index_type": IndexType.IVFLAT, "nlist": nlist}, {"index_type": IndexType.IVF_SQ8, "nlist": nlist}]
J
JinHai-CN 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
        status, ids = connect.add_vectors(ip_table, vectors)
        for i in range(2):
            status = connect.create_index(ip_table, index_params[i])
            assert status.OK()
            status, result = connect.describe_index(ip_table)
            assert result._nlist == index_params[i]["nlist"]
            assert result._table_name == ip_table
            assert result._index_type == index_params[i]["index_type"]
            status, result = connect.describe_index(ip_table)
            logging.getLogger().info(result)
            status = connect.drop_index(ip_table)
            assert status.OK()
            status, result = connect.describe_index(ip_table)
            logging.getLogger().info(result)
            assert result._nlist == 16384
            assert result._table_name == ip_table
            assert result._index_type == IndexType.FLAT


class TestIndexTableInvalid(object):
    """
    Test create / describe / drop index interfaces with invalid table names
    """
    @pytest.fixture(
        scope="function",
        params=gen_invalid_table_names()
    )
    def get_table_name(self, request):
        yield request.param

Z
zhenwu 已提交
1218
    @pytest.mark.level(1)
J
JinHai-CN 已提交
1219 1220
    def test_create_index_with_invalid_tablename(self, connect, get_table_name):
        table_name = get_table_name
Z
zhenwu 已提交
1221 1222 1223
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(table_name, index_param)
J
JinHai-CN 已提交
1224 1225
        assert not status.OK()

Z
zhenwu 已提交
1226
    @pytest.mark.level(1)
J
JinHai-CN 已提交
1227 1228 1229 1230 1231
    def test_describe_index_with_invalid_tablename(self, connect, get_table_name):
        table_name = get_table_name
        status, result = connect.describe_index(table_name)
        assert not status.OK()   

Z
zhenwu 已提交
1232
    @pytest.mark.level(1)
J
JinHai-CN 已提交
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
    def test_drop_index_with_invalid_tablename(self, connect, get_table_name):
        table_name = get_table_name
        status = connect.drop_index(table_name)
        assert not status.OK()


class TestCreateIndexParamsInvalid(object):
    """
    Test Building index with invalid table names, table names not in db
    """
    @pytest.fixture(
        scope="function",
        params=gen_invalid_index_params()
    )
    def get_index_params(self, request):
        yield request.param

Z
zhenwu 已提交
1250
    @pytest.mark.level(1)
J
JinHai-CN 已提交
1251 1252 1253 1254 1255
    def test_create_index_with_invalid_index_params(self, connect, table, get_index_params):
        index_params = get_index_params
        index_type = index_params["index_type"]
        nlist = index_params["nlist"]
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
1256
        # status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
1257 1258 1259 1260
        if (not index_type) or (not nlist) or (not isinstance(index_type, IndexType)) or (not isinstance(nlist, int)):
            with pytest.raises(Exception) as e:
                status = connect.create_index(table, index_params)
        else:
J
JinHai-CN 已提交
1261
            status = connect.create_index(table, index_params)
Z
zhenwu 已提交
1262
            assert not status.OK()