test_index.py 51.1 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 17 18
dim = 128
index_file_size = 10
vectors = gen_vectors(nb, dim)
Z
zhenwu 已提交
19
vectors = sklearn.preprocessing.normalize(vectors, axis=1, norm='l2')
J
JinHai-CN 已提交
20 21 22
vectors = vectors.tolist()
BUILD_TIMEOUT = 60
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 54 55 56 57 58 59 60 61 62 63 64 65

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

    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index(self, connect, table, get_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_index_params
        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 78 79 80
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_partition(self, connect, table, get_index_params):
        '''
        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()
        index_params = get_index_params
        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 已提交
81 82 83 84 85 86 87
    @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 已提交
88 89
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
90
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
91
            status = dis_connect.create_index(table, index_param)
J
JinHai-CN 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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

    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_search_with_query_vectors(self, connect, table, get_index_params):
        '''
        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
        '''
        index_params = get_index_params
        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 已提交
204
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
205 206 207 208
            , 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 已提交
209 210 211
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(table_name, index_param)
J
JinHai-CN 已提交
212 213 214 215 216 217 218 219 220
        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 已提交
221 222
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
223
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
224
            status = connect.create_index(table_name, index_param)
J
JinHai-CN 已提交
225 226 227 228 229 230 231

    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 已提交
232 233 234
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
235 236 237
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
238
    def test_create_index_no_vectors_then_add_vectors(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
239 240 241 242 243
        '''
        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 已提交
244
        index_param = get_simple_index_params
Z
zhenwu 已提交
245
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
246 247 248 249
        status, ids = connect.add_vectors(table, vectors)
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
250
    def test_create_same_index_repeatedly(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
251 252 253 254 255 256
        '''
        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 已提交
257
        index_param = get_simple_index_params
Z
zhenwu 已提交
258 259
        status = connect.create_index(table, index_param)
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273
        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 已提交
274
        nlist = 16384
J
JinHai-CN 已提交
275
        status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
276 277 278
        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 已提交
279
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
280 281 282
        for index_param in index_params:
            status = connect.create_index(table, index_param)
            assert status.OK()
J
JinHai-CN 已提交
283
        status, result = connect.describe_index(table)
Z
zhenwu 已提交
284
        assert result._nlist == nlist
J
JinHai-CN 已提交
285
        assert result._table_name == table
Z
zhenwu 已提交
286
        assert result._index_type == index_type_2
J
JinHai-CN 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

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

    def test_describe_index(self, connect, table, get_index_params):
        '''
        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
        '''
        index_params = get_index_params
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(table, vectors)
        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 已提交
320
            table_name = gen_unique_str()
J
JinHai-CN 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
            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
            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 已提交
362
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
            , 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 已提交
383
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
            , 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
    ******************************************************************
    """

    def test_drop_index(self, connect, table, get_index_params):
        '''
        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 已提交
407
        index_param = get_index_params
J
JinHai-CN 已提交
408
        status, ids = connect.add_vectors(table, vectors)
Z
zhenwu 已提交
409
        status = connect.create_index(table, index_param)
J
JinHai-CN 已提交
410 411 412 413 414 415 416 417 418 419 420
        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 已提交
421
    def test_drop_index_repeatly(self, connect, table, get_index_params):
J
JinHai-CN 已提交
422 423 424 425 426
        '''
        target: test drop index repeatly
        method: create index, call drop index, and drop again
        expected: return code 0
        '''
Z
zhenwu 已提交
427
        index_param = get_index_params
J
JinHai-CN 已提交
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
        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 已提交
456
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
            , 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 已提交
480 481
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 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
        status, ids = connect.add_vectors(table, vectors)
        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 已提交
517 518
        nlist = 16384
        index_params = [{"index_type": IndexType.IVFLAT, "nlist": nlist}, {"index_type": IndexType.IVF_SQ8, "nlist": nlist}]
J
JinHai-CN 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
        status, ids = connect.add_vectors(table, vectors)
        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()
    )
539
    def get_index_params(self, request, args):
540
        if "internal" not in args:
541 542 543
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
544 545 546 547 548

    @pytest.fixture(
        scope="function",
        params=gen_simple_index_params()
    )
Z
zhenwu 已提交
549
    def get_simple_index_params(self, request, args):
Z
zhenwu 已提交
550 551 552 553
        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 已提交
554 555 556 557 558 559

    """
    ******************************************************************
      The following cases are used to test `create_index` function
    ******************************************************************
    """
Z
zhenwu 已提交
560
    @pytest.mark.level(2)
J
JinHai-CN 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index(self, connect, ip_table, get_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_index_params
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
        assert status.OK()

Z
zhenwu 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_partition(self, connect, ip_table, get_index_params):
        '''
        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()
        index_params = get_index_params
        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)
        assert status.OK()

J
JinHai-CN 已提交
589 590 591 592 593 594 595
    @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 已提交
596 597
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
J
JinHai-CN 已提交
598
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
599
            status = dis_connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
600 601 602 603 604 605 606 607 608 609 610 611

    @pytest.mark.timeout(BUILD_TIMEOUT)
    def test_create_index_search_with_query_vectors(self, connect, ip_table, get_index_params):
        '''
        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
        '''
        index_params = get_index_params
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
Z
zhenwu 已提交
612
        assert status.OK()
J
JinHai-CN 已提交
613 614 615 616
        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)
Z
zhenwu 已提交
617
        logging.getLogger().info(result)
J
JinHai-CN 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 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
        assert status.OK()
        assert len(result) == len(query_vecs)

    # 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 已提交
714 715 716
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
717 718 719
        assert status.OK()

    @pytest.mark.timeout(BUILD_TIMEOUT)
Z
zhenwu 已提交
720
    def test_create_index_no_vectors_then_add_vectors(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
721 722 723 724 725
        '''
        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 已提交
726
        index_param = get_simple_index_params
Z
zhenwu 已提交
727
        status = connect.create_index(ip_table, index_param)
J
JinHai-CN 已提交
728 729 730 731 732 733 734 735 736 737
        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 已提交
738
        nlist = 16384
J
JinHai-CN 已提交
739
        status, ids = connect.add_vectors(ip_table, vectors)
Z
zhenwu 已提交
740 741 742
        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 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756
        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 已提交
757
        nlist = 16384
J
JinHai-CN 已提交
758
        status, ids = connect.add_vectors(ip_table, vectors)
Z
zhenwu 已提交
759 760 761
        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 已提交
762
        logging.getLogger().info(index_params)
Z
zhenwu 已提交
763 764 765
        for index_param in index_params:
            status = connect.create_index(ip_table, index_param)
            assert status.OK()
J
JinHai-CN 已提交
766
        status, result = connect.describe_index(ip_table)
Z
zhenwu 已提交
767
        assert result._nlist == nlist
J
JinHai-CN 已提交
768
        assert result._table_name == ip_table
Z
zhenwu 已提交
769
        assert result._index_type == index_type_2
J
JinHai-CN 已提交
770 771 772 773 774 775 776

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

Z
zhenwu 已提交
777
    def test_describe_index(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
778 779 780 781 782
        '''
        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 已提交
783
        index_params = get_simple_index_params
J
JinHai-CN 已提交
784 785 786 787 788 789 790 791 792
        logging.getLogger().info(index_params)
        status, ids = connect.add_vectors(ip_table, vectors)
        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 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
    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
        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
        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
        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 已提交
867 868 869 870 871 872 873 874 875 876
    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 已提交
877
            table_name = gen_unique_str()
J
JinHai-CN 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
            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
            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 已提交
919
        method: create table and add vectors in it, create index
J
JinHai-CN 已提交
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 950 951 952 953 954 955 956
            , 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
    ******************************************************************
    """

    def test_drop_index(self, connect, ip_table, get_index_params):
        '''
        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
        '''
        index_params = get_index_params
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
        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 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    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
        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)
        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

    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
        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)
        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

    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
        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)
        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

    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
        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)
        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"]

J
JinHai-CN 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    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
        status, ids = connect.add_vectors(ip_table, vectors)
        status = connect.create_index(ip_table, index_params)
        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

    @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 已提交
1091 1092
        nlist = 16384
        index_param = {"index_type": IndexType.IVFLAT, "nlist": nlist}
J
JinHai-CN 已提交
1093
        with pytest.raises(Exception) as e:
Z
zhenwu 已提交
1094
            status = dis_connect.drop_index(ip_table, index_param)
J
JinHai-CN 已提交
1095 1096 1097 1098 1099 1100 1101

    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 已提交
1102 1103 1104
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        logging.getLogger().info(index_param)
J
JinHai-CN 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
        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
        status, ids = connect.add_vectors(ip_table, vectors)
        for i in range(2):
            status = connect.create_index(ip_table, index_params)
            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

    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 已提交
1140 1141
        nlist = 16384
        index_params = [{"index_type": IndexType.IVFLAT, "nlist": nlist}, {"index_type": IndexType.IVF_SQ8, "nlist": nlist}]
J
JinHai-CN 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
        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 已提交
1172
    @pytest.mark.level(2)
J
JinHai-CN 已提交
1173 1174
    def test_create_index_with_invalid_tablename(self, connect, get_table_name):
        table_name = get_table_name
Z
zhenwu 已提交
1175 1176 1177
        nlist = 16384
        index_param = {"index_type": IndexType.IVF_SQ8, "nlist": nlist}
        status = connect.create_index(table_name, index_param)
J
JinHai-CN 已提交
1178 1179
        assert not status.OK()

Z
zhenwu 已提交
1180
    @pytest.mark.level(2)
J
JinHai-CN 已提交
1181 1182 1183 1184 1185
    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 已提交
1186
    @pytest.mark.level(2)
J
JinHai-CN 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    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

    @pytest.mark.level(2)
    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)
        status, ids = connect.add_vectors(table, vectors)
        # if not isinstance(index_type, int) or not isinstance(nlist, int):
Y
yhz 已提交
1212
        try:
J
JinHai-CN 已提交
1213
            status = connect.create_index(table, index_params)
Y
yhz 已提交
1214 1215 1216 1217 1218 1219 1220 1221
            assert not status.OK()
            # no exception raised & status is OK. unexpected.
            assert False
        except (Exception, ):
            pass
        # with pytest.raises(Exception) as e:
        #     status = connect.create_index(table, index_params)
            # assert not status.OK()
J
JinHai-CN 已提交
1222 1223 1224
        # else:
        #     status = connect.create_index(table, index_params)
        #     assert not status.OK()