test_index.py 55.3 KB
Newer Older
紫晴 已提交
1
import pytest
2

3
from base.client_base import TestcaseBase
4
from base.index_wrapper import ApiIndexWrapper
紫晴 已提交
5
from utils.util_log import test_log as log
6 7
from common import common_func as cf
from common import common_type as ct
D
del-zhenwu 已提交
8
from common.common_type import CaseLabel, CheckTasks
9 10
from common.code_mapping import CollectionErrorMessage as clem
from common.code_mapping import IndexErrorMessage as iem
11

12
from utils.util_pymilvus import *
13 14
from common.constants import *

15 16 17 18 19
prefix = "index"
default_schema = cf.gen_default_collection_schema()
default_field_name = ct.default_float_vec_field_name
default_index_params = {"index_type": "IVF_SQ8", "metric_type": "L2", "params": {"nlist": 64}}

20 21
# copied from pymilvus
uid = "test_index"
B
binbin 已提交
22
# BUILD_TIMEOUT = 300
23 24
field_name = default_float_vec_field_name
binary_field_name = default_binary_vec_field_name
25
# query = gen_search_vectors_params(field_name, default_entities, default_top_k, 1)
26 27
default_index = {"index_type": "IVF_FLAT", "params": {"nlist": 128}, "metric_type": "L2"}

28

29
class TestIndexParams(TestcaseBase):
30 31 32
    """ Test case of index interface """

    @pytest.mark.tags(CaseLabel.L1)
33 34
    @pytest.mark.parametrize("collection", [None, "coll"])
    def test_index_non_collection(self, collection):
35
        """
D
del-zhenwu 已提交
36 37
        target: test index with None collection
        method: input none collection object
38 39 40
        expected: raise exception
        """
        self._connect()
41
        self.index_wrap.init_index(collection, default_field_name, default_index_params, check_task=CheckTasks.err_res,
42
                                   check_items={ct.err_code: 0, ct.err_msg: clem.CollectionType})
43 44

    @pytest.mark.tags(CaseLabel.L1)
45 46
    @pytest.mark.parametrize("field_name", ct.get_invalid_strs)
    def test_index_field_name_invalid(self, field_name):
47 48 49 50 51
        """
        target: test index with error field name
        method: input field name
        expected: raise exception
        """
52 53 54 55
        collection_name = cf.gen_unique_str(prefix)

        collection_w = self.init_collection_wrap(name=collection_name)

56
        log.error(iem.WrongFieldName % str(field_name))
57 58 59
        self.index_wrap.init_index(collection_w.collection, field_name, default_index_params,
                                   check_task=CheckTasks.err_res,
                                   check_items={ct.err_code: 1,
60
                                                ct.err_msg: iem.WrongFieldName % str(field_name)})
61 62 63 64 65 66 67 68 69 70

    @pytest.mark.tags(CaseLabel.L1)
    def test_index_field_name_not_existed(self):
        """
        target: test index with error field name
        method: input field name not created
        expected: raise exception
        """
        c_name = cf.gen_unique_str(prefix)
        f_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
71
        collection_w = self.init_collection_wrap(name=c_name)
72
        self.index_wrap.init_index(collection_w.collection, f_name, default_index_params, check_task=CheckTasks.err_res,
73 74
                                   check_items={ct.err_code: 1,
                                                ct.err_msg: f"cannot create index on non-existed field: {f_name}"})
75

B
binbin 已提交
76
    @pytest.mark.tags(CaseLabel.L0)
77 78 79
    # TODO (reason="pymilvus issue #677", raises=TypeError)
    @pytest.mark.parametrize("index_type", ct.get_invalid_strs)
    def test_index_type_invalid(self, index_type):
80 81 82 83 84 85
        """
        target: test index with error index type
        method: input invalid index type
        expected: raise exception
        """
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
86
        collection_w = self.init_collection_wrap(name=c_name)
87
        index_params = copy.deepcopy(default_index_params)
88
        index_params["index_type"] = index_type
89 90 91 92
        if not isinstance(index_params["index_type"], str):
            msg = "must be str"
        else:
            msg = "Invalid index_type"
93 94 95
        self.index_wrap.init_index(collection_w.collection, default_field_name, index_params,
                                   check_task=CheckTasks.err_res,
                                   check_items={ct.err_code: 1, ct.err_msg: msg})
96 97 98 99 100 101 102 103 104

    @pytest.mark.tags(CaseLabel.L1)
    def test_index_type_not_supported(self):
        """
        target: test index with error index type
        method: input unsupported index type
        expected: raise exception
        """
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
105
        collection_w = self.init_collection_wrap(name=c_name)
106 107
        index_params = copy.deepcopy(default_index_params)
        index_params["index_type"] = "IVFFFFFFF"
108 109 110
        self.index_wrap.init_index(collection_w.collection, default_field_name, index_params,
                                   check_task=CheckTasks.err_res,
                                   check_items={ct.err_code: 1, ct.err_msg: ""})
111 112 113 114 115 116 117 118 119

    @pytest.mark.tags(CaseLabel.L1)
    def test_index_params_invalid(self, get_invalid_index_params):
        """
        target: test index with error index params
        method: input invalid index params
        expected: raise exception
        """
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
120
        collection_w = self.init_collection_wrap(name=c_name)
121
        index_params = get_invalid_index_params
122 123 124
        self.index_wrap.init_index(collection_w.collection, default_field_name, index_params,
                                   check_task=CheckTasks.err_res,
                                   check_items={ct.err_code: 1, ct.err_msg: ""})
125

D
del-zhenwu 已提交
126
    # TODO: not supported
127
    @pytest.mark.tags(CaseLabel.L1)
128 129
    @pytest.mark.skip(reason='not supported')
    def test_index_name_invalid(self, get_invalid_index_name):
130 131 132 133 134 135 136
        """
        target: test index with error index name
        method: input invalid index name
        expected: raise exception
        """
        c_name = cf.gen_unique_str(prefix)
        index_name = get_invalid_index_name
D
del-zhenwu 已提交
137
        collection_w = self.init_collection_wrap(name=c_name)
138 139 140
        self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params,
                                   check_task=CheckTasks.err_res,
                                   check_items={ct.err_code: 1, ct.err_msg: ""})
紫晴 已提交
141 142


143
class TestIndexOperation(TestcaseBase):
紫晴 已提交
144 145
    """ Test case of index interface """

Z
zhuwenxing 已提交
146 147 148 149 150 151 152 153 154 155 156
    @pytest.mark.tags(CaseLabel.L1)
    def test_index_create_with_different_indexes(self):
        """
        target: test create index on one field, with two different type of index
        method: create two different indexes
        expected: only latest index can be created for a collection
        """
        c_name = cf.gen_unique_str(prefix)
        collection_w = self.init_collection_wrap(name=c_name)
        self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params)
        self.index_wrap.init_index(collection_w.collection, default_field_name, default_index)
157

Z
zhuwenxing 已提交
158 159 160
        assert len(collection_w.indexes) == 1
        assert collection_w.indexes[0].params["index_type"] == default_index["index_type"]

161 162 163 164 165 166 167 168
    @pytest.mark.tags(CaseLabel.L1)
    def test_index_collection_empty(self):
        """
        target: test index with empty collection
        method: Index on empty collection
        expected: no exception raised
        """
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
169
        collection_w = self.init_collection_wrap(name=c_name)
170
        index, _ = self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params)
171
        # TODO: assert index
D
del-zhenwu 已提交
172
        cf.assert_equal_index(index, collection_w.collection.indexes[0])
173 174

    @pytest.mark.tags(CaseLabel.L1)
175 176
    @pytest.mark.parametrize("index_param", [default_index_params])
    def test_index_params(self, index_param):
177 178 179 180 181 182
        """
        target: test index with all index type/params
        method: input valid params
        expected: no exception raised
        """
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
183
        collection_w = self.init_collection_wrap(name=c_name)
184
        data = cf.gen_default_list_data()
185
        collection_w.insert(data=data)
186
        index_params = index_param
187 188 189 190 191 192 193 194 195 196 197 198 199
        index, _ = self.index_wrap.init_index(collection_w.collection, default_field_name, index_params)
        # TODO: assert index
        cf.assert_equal_index(index, collection_w.collection.indexes[0])

    @pytest.mark.tags(CaseLabel.L1)
    def test_index_params_flush(self):
        """
        target: test index with all index type/params
        method: input valid params
        expected: no exception raised
        """
        c_name = cf.gen_unique_str(prefix)
        collection_w = self.init_collection_wrap(name=c_name)
200
        data = cf.gen_default_list_data()
201
        collection_w.insert(data=data)
202 203
        # flush
        collection_w.num_entities
204
        index, _ = self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params)
205
        # TODO: assert index
D
del-zhenwu 已提交
206
        cf.assert_equal_index(index, collection_w.collection.indexes[0])
207
        assert collection_w.num_entities == ct.default_nb
208

D
del-zhenwu 已提交
209
    # TODO: not support
210
    @pytest.mark.tags(CaseLabel.L1)
211 212
    @pytest.mark.skip(reason='not supported')
    def test_index_name_dup(self):
213 214 215 216 217
        """
        target: test index with duplicate index name
        method: create index with existed index name create by `collection.create_index`
        expected: no exception raised
        """
D
del-zhenwu 已提交
218
        c_name = cf.gen_unique_str(prefix)
219
        index_name = ct.default_index_name
D
del-zhenwu 已提交
220 221
        collection_w = self.init_collection_wrap(name=c_name)
        collection_w.collection.create_index(default_field_name, default_index_params, index_name=index_name)
222 223 224
        self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params,
                                   check_task=CheckTasks.err_res,
                                   check_items={ct.err_code: 1, ct.err_msg: ""})
225 226 227

    # TODO: server not supported
    @pytest.mark.tags(CaseLabel.L1)
228 229
    @pytest.mark.skip(reason='not supported')
    def test_index_field_names(self):
230 231 232 233 234 235 236 237 238
        """
        target: test index on one field, with two indexes
        method: create index with two different indexes
        expected: no exception raised
        """
        pass

    # TODO: server not supported
    @pytest.mark.tags(CaseLabel.L1)
239 240
    @pytest.mark.skip(reason='not supported')
    def test_index_fields(self):
241 242 243 244 245 246 247 248 249
        """
        target: test index on two fields, with the same name
        method: create the same index name with two different fields
        expected: exception raised
        """
        pass

    # TODO: server not supported
    @pytest.mark.tags(CaseLabel.L1)
250 251
    @pytest.mark.skip(reason='not supported')
    def test_index_fields_B(self):
252 253 254 255 256 257 258 259 260
        """
        target: test index on two fields, with the different name
        method: create the different index with two different fields
        expected: no exception raised
        """
        pass

    # TODO: server not supported
    @pytest.mark.tags(CaseLabel.L1)
261 262
    @pytest.mark.skip(reason='not supported')
    def test_index_field_names_eq_maximum(self):
263 264
        """
        target: test index on one field, with the different names, num of the names equal to the maximum num supported
265
        method: create the different indexes
266 267 268 269 270 271
        expected: no exception raised
        """
        pass

    # TODO: server not supported
    @pytest.mark.tags(CaseLabel.L1)
272 273
    @pytest.mark.skip(reason='not supported')
    def test_index_field_names_more_maximum(self):
274 275
        """
        target: test index on one field, with the different names, num of the names more than the maximum num supported
276
        method: create the different indexes
277 278 279 280 281
        expected: exception raised
        """
        pass

    @pytest.mark.tags(CaseLabel.L1)
282
    def test_index_drop_index(self):
283 284 285 286 287 288
        """
        target: test index.drop
        method: create index by `index`, and then drop it
        expected: no exception raised
        """
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
289
        collection_w = self.init_collection_wrap(name=c_name)
290
        index, _ = self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params)
D
del-zhenwu 已提交
291
        cf.assert_equal_index(index, collection_w.collection.indexes[0])
292
        self.index_wrap.drop()
D
del-zhenwu 已提交
293
        assert len(collection_w.collection.indexes) == 0
294 295

    @pytest.mark.tags(CaseLabel.L1)
296 297
    # TODO #7372
    def test_index_drop_repeatedly(self):
298 299 300 301 302
        """
        target: test index.drop
        method: create index by `index`, and then drop it twice
        expected: exception raised
        """
303
        c_name = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
304
        collection_w = self.init_collection_wrap(name=c_name)
305
        _, _ = self.index_wrap.init_index(collection_w.collection, default_field_name, default_index_params)
D
del-zhenwu 已提交
306 307
        self.index_wrap.drop()
        self.index_wrap.drop(check_task=CheckTasks.err_res,
308
                             check_items={ct.err_code: 1, ct.err_msg: "Index doesn't exist"})
309 310


311
class TestIndexAdvanced(TestcaseBase):
312 313 314 315 316 317 318 319 320 321 322
    """ Test case of index interface """

    @pytest.mark.tags(CaseLabel.L2)
    def test_index_drop_multi_collections(self):
        """
        target: test index.drop
        method: create indexes by `index`, and then drop it, assert there is one index left
        expected: exception raised
        """
        c_name = cf.gen_unique_str(prefix)
        c_name_2 = cf.gen_unique_str(prefix)
D
del-zhenwu 已提交
323 324 325
        cw = self.init_collection_wrap(name=c_name)
        cw2 = self.init_collection_wrap(name=c_name_2)
        iw_2 = ApiIndexWrapper()
326 327
        self.index_wrap.init_index(cw.collection, default_field_name, default_index_params)
        index_2, _ = iw_2.init_index(cw2.collection, default_field_name, default_index_params)
328
        self.index_wrap.drop()
D
del-zhenwu 已提交
329 330
        assert cf.assert_equal_index(index_2, cw2.collection.indexes[0])
        assert len(cw.collection.indexes) == 0
331 332

    @pytest.mark.tags(CaseLabel.L2)
333 334
    @pytest.mark.skip(reason='TODO')
    def test_index_drop_during_inserting(self):
335 336
        """
        target: test index.drop during inserting
337
        method: create indexes by `index`, and then drop it during inserting entities, make sure async insert
338 339 340 341 342
        expected: no exception raised, insert success
        """
        pass

    @pytest.mark.tags(CaseLabel.L2)
343 344
    @pytest.mark.skip(reason='TODO')
    def test_index_drop_during_searching(self):
345 346
        """
        target: test index.drop during searching
347
        method: create indexes by `index`, and then drop it during searching, make sure async search
348 349 350 351
        expected: no exception raised, search success
        """
        pass

Y
yanliang567 已提交
352
    @pytest.mark.tags(CaseLabel.L2)
353 354
    @pytest.mark.skip(reason='TODO')
    def test_index_recovery_after_restart(self):
355 356
        """
        target: test index still existed after server restart
357
        method: create index by `index`, and then restart server, assert index existed
358 359 360 361
        expected: index in collection.indexes
        """
        pass

Y
yanliang567 已提交
362
    @pytest.mark.tags(CaseLabel.L2)
363 364
    @pytest.mark.skip(reason='TODO')
    def test_index_building_after_restart(self):
365 366 367
        """
        target: index can still build if not finished before server restart
        method: create index by `index`, and then restart server, assert server is indexing
B
binbin 已提交
368
        expected: index build finished after server restart
369 370
        """
        pass
371 372 373 374 375 376 377 378 379 380 381 382 383 384

    """
    ******************************************************************
      The following classes are copied from pymilvus test
    ******************************************************************
    """


class TestIndexBase:
    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_simple_index(self, request, connect):
T
ThreadDao 已提交
385
        log.info(request.param)
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
        # if str(connect._cmd("mode")) == "CPU":
        #     if request.param["index_type"] in index_cpu_not_support():
        #         pytest.skip("sq8h not support in CPU mode")
        return copy.deepcopy(request.param)

    @pytest.fixture(
        scope="function",
        params=[
            1,
            10,
            1111
        ],
    )
    def get_nq(self, request):
        yield request.param

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

408
    @pytest.mark.tags(CaseLabel.L0)
409
    # @pytest.mark.timeout(BUILD_TIMEOUT)
410 411 412 413 414 415
    def test_create_index(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
416
        connect.insert(collection, default_entities)
417 418 419 420 421 422
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

423
    @pytest.mark.tags(CaseLabel.L0)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    @pytest.mark.skip(reason="Repeat with test_index_field_name_not_existed")
    def test_create_index_on_field_not_existed(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index on field not existed
        expected: error raised
        """
        tmp_field_name = gen_unique_str()
        result = connect.insert(collection, default_entities)
        with pytest.raises(Exception) as e:
            connect.create_index(collection, tmp_field_name, get_simple_index)

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_on_field(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index on other field
        expected: error raised
        """
        tmp_field_name = "int64"
        result = connect.insert(collection, default_entities)
        with pytest.raises(Exception) as e:
            connect.create_index(collection, tmp_field_name, get_simple_index)

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
449
    # @pytest.mark.timeout(BUILD_TIMEOUT)
450 451 452 453 454 455 456 457 458 459 460 461 462
    def test_create_index_no_vectors(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
463
    # @pytest.mark.timeout(BUILD_TIMEOUT)
464 465 466 467 468 469 470 471 472 473 474 475 476 477
    def test_create_index_partition(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection, create partition, and add entities in it, create index
        expected: return search success
        """
        connect.create_partition(collection, default_tag)
        result = connect.insert(collection, default_entities, partition_name=default_tag)
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

478
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
479
    # @pytest.mark.timeout(BUILD_TIMEOUT)
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    def test_create_index_partition_flush(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection, create partition, and add entities in it, create index
        expected: return search success
        """
        connect.create_partition(collection, default_tag)
        result = connect.insert(collection, default_entities, partition_name=default_tag)
        connect.flush([collection])
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_without_connect(self, dis_connect, collection):
        """
        target: test create index without connection
        method: create collection and add entities in it, check if added successfully
        expected: raise exception
        """
        with pytest.raises(Exception) as e:
503
            dis_connect.create_index(collection, field_name, default_index)
504

505
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
506
    # @pytest.mark.timeout(BUILD_TIMEOUT)
507 508 509 510 511 512 513 514 515
    def test_create_index_search_with_query_vectors(self, connect, collection, get_simple_index, get_nq):
        """
        target: test create index interface, search with more query vectors
        method: create collection and add entities in it, create index
        expected: return search success
        """
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
        connect.create_index(collection, field_name, get_simple_index)
T
ThreadDao 已提交
516
        log.info(connect.describe_index(collection, ""))
517 518 519
        nq = get_nq
        index_type = get_simple_index["index_type"]
        search_param = get_search_param(index_type)
520 521
        params, _ = gen_search_vectors_params(field_name, default_entities, default_top_k, nq,
                                              search_params=search_param)
522
        connect.load_collection(collection)
523
        res = connect.search(collection, **params)
524 525
        assert len(res) == nq

B
binbin 已提交
526
    # @pytest.mark.timeout(BUILD_TIMEOUT)
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_multithread(self, connect, collection, args):
        """
        target: test create index interface with multiprocess
        method: create collection and add entities in it, create index
        expected: return search success
        """
        connect.insert(collection, default_entities)

        def build(connect):
            connect.create_index(collection, field_name, default_index)
            if default_index["index_type"] != "FLAT":
                index = connect.describe_index(collection, "")
                create_target_index(default_index, field_name)
                assert index == default_index

        threads_num = 8
        threads = []
        for i in range(threads_num):
            m = get_milvus(host=args["ip"], port=args["port"], handler=args["handler"])
            t = MyThread(target=build, args=(m,))
            threads.append(t)
            t.start()
            time.sleep(0.2)
        for t in threads:
            t.join()

554
    @pytest.mark.tags(CaseLabel.L0)
555 556 557
    def test_create_index_collection_not_existed(self, connect):
        """
        target: test create index interface when collection name not existed
B
binbin 已提交
558 559
        method: create collection and add entities in it, create index,
                make sure the collection name not in index
560 561 562 563 564 565 566
        expected: create index failed
        """
        collection_name = gen_unique_str(uid)
        with pytest.raises(Exception) as e:
            connect.create_index(collection_name, field_name, default_index)

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
567
    # @pytest.mark.timeout(BUILD_TIMEOUT)
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
    def test_create_index_insert_flush(self, connect, collection, get_simple_index):
        """
        target: test create index
        method: create collection and create index, add entities in it
        expected: create index ok, and count correct
        """
        connect.create_index(collection, field_name, get_simple_index)
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
        stats = connect.get_collection_stats(collection)
        assert stats["row_count"] == default_nb
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
585
    # @pytest.mark.timeout(BUILD_TIMEOUT)
586 587 588 589 590 591 592 593 594 595 596 597 598 599
    def test_create_same_index_repeatedly(self, connect, collection, get_simple_index):
        """
        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
        """
        connect.create_index(collection, field_name, get_simple_index)
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
600
    # @pytest.mark.timeout(BUILD_TIMEOUT)
601 602 603 604 605 606 607 608
    def test_create_different_index_repeatedly(self, connect, collection):
        """
        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
        """
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
609
        indexs = [default_index, {"metric_type": "L2", "index_type": "FLAT", "params": {"nlist": 1024}}]
610 611 612 613 614
        for index in indexs:
            connect.create_index(collection, field_name, index)
            connect.release_collection(collection)
            connect.load_collection(collection)
        index = connect.describe_index(collection, "")
615
        assert not index  # FLAT is the last index_type, drop all indexes in server
616 617

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
618
    # @pytest.mark.timeout(BUILD_TIMEOUT)
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    def test_create_different_index_repeatedly_B(self, connect, collection):
        """
        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
        """
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
        indexs = [default_index, {"metric_type": "L2", "index_type": "IVF_SQ8", "params": {"nlist": 1024}}]
        for index in indexs:
            connect.create_index(collection, field_name, index)
            connect.release_collection(collection)
            connect.load_collection(collection)
        index = connect.describe_index(collection, "")
        create_target_index(indexs[-1], field_name)
        assert index == indexs[-1]
        # assert not index  # FLAT is the last index_type, drop all indexes in server

637
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
638
    # @pytest.mark.timeout(BUILD_TIMEOUT)
639 640 641 642 643 644 645 646 647 648 649 650 651 652
    def test_create_index_ip(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        result = connect.insert(collection, default_entities)
        get_simple_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

653
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
654
    # @pytest.mark.timeout(BUILD_TIMEOUT)
655 656 657 658 659 660 661 662 663 664 665 666 667 668
    def test_create_index_no_vectors_ip(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        get_simple_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
669
    # @pytest.mark.timeout(BUILD_TIMEOUT)
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    def test_create_index_partition_ip(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection, create partition, and add entities in it, create index
        expected: return search success
        """
        connect.create_partition(collection, default_tag)
        result = connect.insert(collection, default_entities, partition_name=default_tag)
        get_simple_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

685
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
686
    # @pytest.mark.timeout(BUILD_TIMEOUT)
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    def test_create_index_partition_flush_ip(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection, create partition, and add entities in it, create index
        expected: return search success
        """
        connect.create_partition(collection, default_tag)
        result = connect.insert(collection, default_entities, partition_name=default_tag)
        connect.flush([collection])
        get_simple_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, get_simple_index)
        if get_simple_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(get_simple_index, field_name)
            assert index == get_simple_index

703
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
704
    # @pytest.mark.timeout(BUILD_TIMEOUT)
705 706 707 708 709 710 711 712 713 714 715 716
    def test_create_index_search_with_query_vectors_ip(self, connect, collection, get_simple_index, get_nq):
        """
        target: test create index interface, search with more query vectors
        method: create collection and add entities in it, create index
        expected: return search success
        """
        metric_type = "IP"
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
        get_simple_index["metric_type"] = metric_type
        connect.create_index(collection, field_name, get_simple_index)
        connect.load_collection(collection)
T
ThreadDao 已提交
717
        log.info(connect.describe_index(collection, ""))
718 719 720
        nq = get_nq
        index_type = get_simple_index["index_type"]
        search_param = get_search_param(index_type)
721 722 723
        params, _ = gen_search_vectors_params(field_name, default_entities, default_top_k, nq,
                                              metric_type=metric_type, search_params=search_param)
        res = connect.search(collection, **params)
724 725
        assert len(res) == nq

B
binbin 已提交
726
    # @pytest.mark.timeout(BUILD_TIMEOUT)
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_multithread_ip(self, connect, collection, args):
        """
        target: test create index interface with multiprocess
        method: create collection and add entities in it, create index
        expected: return search success
        """
        connect.insert(collection, default_entities)

        def build(connect):
            default_index["metric_type"] = "IP"
            connect.create_index(collection, field_name, default_index)
            if default_index["index_type"] != "FLAT":
                index = connect.describe_index(collection, "")
                create_target_index(default_index, field_name)
                assert index == default_index

        threads_num = 8
        threads = []
        for i in range(threads_num):
            m = get_milvus(host=args["ip"], port=args["port"], handler=args["handler"])
            t = MyThread(target=build, args=(m,))
            threads.append(t)
            t.start()
            time.sleep(0.2)
        for t in threads:
            t.join()

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_collection_not_existed_ip(self, connect, collection):
        """
        target: test create index interface when collection name not existed
B
binbin 已提交
759 760
        method: create collection and add entities in it, create index,
                make sure the collection name not in index
761 762 763 764 765 766 767
        expected: return code not equals to 0, create index failed
        """
        collection_name = gen_unique_str(uid)
        default_index["metric_type"] = "IP"
        with pytest.raises(Exception) as e:
            connect.create_index(collection_name, field_name, default_index)

768
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
769
    # @pytest.mark.timeout(BUILD_TIMEOUT)
770 771
    def test_create_index_no_vectors_insert_ip(self, connect, collection):
        """
772 773 774 775
        target: test create index interface when there is no vectors in collection,
                and does not affect the subsequent process
        method: create collection and add no vectors in it, and then create index,
                add entities in it
776 777 778 779 780 781 782 783 784 785 786 787 788 789
        expected: return code equals to 0
        """
        default_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, default_index)
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
        stats = connect.get_collection_stats(collection)
        assert stats["row_count"] == default_nb
        if default_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(default_index, field_name)
            assert index == default_index

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
790
    # @pytest.mark.timeout(BUILD_TIMEOUT)
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    def test_create_same_index_repeatedly_ip(self, connect, collection):
        """
        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
        """
        default_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, default_index)
        connect.create_index(collection, field_name, default_index)
        if default_index["index_type"] != "FLAT":
            index = connect.describe_index(collection, "")
            create_target_index(default_index, field_name)
            assert index == default_index

    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
806
    # @pytest.mark.timeout(BUILD_TIMEOUT)
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
    def test_create_different_index_repeatedly_ip(self, connect, collection):
        """
        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
        """
        result = connect.insert(collection, default_entities)
        connect.flush([collection])
        connect.load_collection(collection)
        stats = connect.get_collection_stats(collection)
        assert stats["row_count"] == default_nb
        default_index["metric_type"] = "IP"
        indexs = [default_index, {"index_type": "FLAT", "params": {"nlist": 1024}, "metric_type": "IP"}]
        for index in indexs:
            connect.create_index(collection, field_name, index)
            connect.release_collection(collection)
            connect.load_collection(collection)
        index = connect.describe_index(collection, "")
        # assert index == indexs[-1]
        assert not index

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

834
    @pytest.mark.tags(CaseLabel.L0)
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 867 868 869
    def test_drop_index(self, connect, collection, get_simple_index):
        """
        target: test drop index interface
        method: create collection and add entities in it, create index, call drop index
        expected: return code 0, and default index param
        """
        connect.create_index(collection, field_name, get_simple_index)
        connect.drop_index(collection, field_name)
        index = connect.describe_index(collection, "")
        assert not index

    @pytest.mark.tags(CaseLabel.L2)
    # TODO #7372
    def test_drop_index_repeatedly(self, connect, collection, get_simple_index):
        """
        target: test drop index repeatedly
        method: create index, call drop index, and drop again
        expected: return code 0
        """
        connect.create_index(collection, field_name, get_simple_index)
        connect.drop_index(collection, field_name)
        connect.drop_index(collection, field_name)
        index = connect.describe_index(collection, "")
        assert not index

    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index_without_connect(self, dis_connect, collection):
        """
        target: test drop index without connection
        method: drop index, and check if drop successfully
        expected: raise exception
        """
        with pytest.raises(Exception) as e:
            dis_connect.drop_index(collection, field_name)

870
    @pytest.mark.tags(CaseLabel.L0)
871 872 873
    def test_drop_index_collection_not_existed(self, connect):
        """
        target: test drop index interface when collection name not existed
B
binbin 已提交
874 875
        method: create collection and add entities in it, create index,
                make sure the collection name not in index, and then drop it
876 877 878 879 880 881
        expected: return code not equals to 0, drop index failed
        """
        collection_name = gen_unique_str(uid)
        with pytest.raises(Exception) as e:
            connect.drop_index(collection_name, field_name)

882
    @pytest.mark.tags(CaseLabel.L0)
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 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 950 951 952 953 954 955 956 957 958 959 960 961 962 963
    def test_drop_index_collection_not_create(self, connect, collection):
        """
        target: test drop index interface when index not created
        method: create collection and add entities in it, create index
        expected: return code not equals to 0, drop index failed
        """
        # no create index
        connect.drop_index(collection, field_name)

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_drop_index_repeatedly(self, connect, collection, get_simple_index):
        """
        target: test create / drop index repeatedly, use the same index params
        method: create index, drop index, four times
        expected: return code 0
        """
        for i in range(4):
            connect.create_index(collection, field_name, get_simple_index)
            connect.drop_index(collection, field_name)

    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index_ip(self, connect, collection, get_simple_index):
        """
        target: test drop index interface
        method: create collection and add entities in it, create index, call drop index
        expected: return code 0, and default index param
        """
        # result = connect.insert(collection, entities)
        get_simple_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, get_simple_index)
        connect.drop_index(collection, field_name)
        index = connect.describe_index(collection, "")
        assert not index

    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index_repeatedly_ip(self, connect, collection, get_simple_index):
        """
        target: test drop index repeatedly
        method: create index, call drop index, and drop again
        expected: return code 0
        """
        get_simple_index["metric_type"] = "IP"
        connect.create_index(collection, field_name, get_simple_index)
        connect.drop_index(collection, field_name)
        connect.drop_index(collection, field_name)
        index = connect.describe_index(collection, "")
        assert not index

    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index_without_connect_ip(self, dis_connect, collection):
        """
        target: test drop index without connection
        method: drop index, and check if drop successfully
        expected: raise exception
        """
        with pytest.raises(Exception) as e:
            dis_connect.drop_index(collection, field_name)

    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index_collection_not_create_ip(self, connect, collection):
        """
        target: test drop index interface when index not created
        method: create collection and add entities in it, create index
        expected: return code not equals to 0, drop index failed
        """
        # result = connect.insert(collection, entities)
        # no create index
        connect.drop_index(collection, field_name)

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_drop_index_repeatedly_ip(self, connect, collection, get_simple_index):
        """
        target: test create / drop index repeatedly, use the same index params
        method: create index, drop index, four times
        expected: return code 0
        """
        get_simple_index["metric_type"] = "IP"
        for i in range(4):
            connect.create_index(collection, field_name, get_simple_index)
            connect.drop_index(collection, field_name)

964
    @pytest.mark.tags(CaseLabel.L0)
965
    def test_create_PQ_without_nbits(self, connect, collection):
966 967 968 969 970
        """
        target: test create PQ index
        method: create PQ index without nbits
        expected: create successfully
        """
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
        PQ_index = {"index_type": "IVF_PQ", "params": {"nlist": 128, "m": 16}, "metric_type": "L2"}
        result = connect.insert(collection, default_entities)
        connect.create_index(collection, field_name, PQ_index)
        index = connect.describe_index(collection, "")
        create_target_index(PQ_index, field_name)
        assert index == PQ_index


class TestIndexBinary:
    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_simple_index(self, request, connect):
        # if str(connect._cmd("mode")) == "CPU":
        #     if request.param["index_type"] in index_cpu_not_support():
        #         pytest.skip("sq8h not support in CPU mode")
        return copy.deepcopy(request.param)

    @pytest.fixture(
        scope="function",
        params=gen_binary_index()
    )
    def get_jaccard_index(self, request, connect):
        if request.param["index_type"] in binary_support():
            request.param["metric_type"] = "JACCARD"
            return request.param
        else:
            pytest.skip("Skip index")

    @pytest.fixture(
        scope="function",
        params=gen_binary_index()
    )
    def get_l2_index(self, request, connect):
        request.param["metric_type"] = "L2"
        return request.param

    @pytest.fixture(
        scope="function",
        params=[
            1,
            10,
            1111
        ],
    )
    def get_nq(self, request):
        yield request.param

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

1026
    @pytest.mark.tags(CaseLabel.L2)
B
binbin 已提交
1027
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
    def test_create_index(self, connect, binary_collection, get_jaccard_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        result = connect.insert(binary_collection, default_binary_entities)
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        binary_index = connect.describe_index(binary_collection, "")
        create_target_index(get_jaccard_index, binary_field_name)
        assert binary_index == get_jaccard_index

1040
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
1041
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    def test_create_index_partition(self, connect, binary_collection, get_jaccard_index):
        """
        target: test create index interface
        method: create collection, create partition, and add entities in it, create index
        expected: return search success
        """
        connect.create_partition(binary_collection, default_tag)
        result = connect.insert(binary_collection, default_binary_entities, partition_name=default_tag)
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        binary_index = connect.describe_index(binary_collection, "")
        create_target_index(get_jaccard_index, binary_field_name)
        assert binary_index == get_jaccard_index

1055
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
1056
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    def test_create_index_search_with_query_vectors(self, connect, binary_collection, get_jaccard_index, get_nq):
        """
        target: test create index interface, search with more query vectors
        method: create collection and add entities in it, create index
        expected: return search success
        """
        nq = get_nq
        result = connect.insert(binary_collection, default_binary_entities)
        connect.flush([binary_collection])
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        connect.load_collection(binary_collection)
        search_param = get_search_param(get_jaccard_index["index_type"], metric_type="JACCARD")
1069 1070
        params, _ = gen_search_vectors_params(binary_field_name, default_binary_entities, default_top_k, nq,
                                              search_params=search_param, metric_type="JACCARD")
T
ThreadDao 已提交
1071
        log.info(params)
1072
        res = connect.search(binary_collection, **params)
1073 1074
        assert len(res) == nq

B
binbin 已提交
1075
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1076 1077 1078 1079
    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_invalid_metric_type_binary(self, connect, binary_collection, get_l2_index):
        """
        target: test create index interface with invalid metric type
B
binbin 已提交
1080
        method: add entities into binary collection, flush, create index with L2 metric type.
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        expected: return create_index failure
        """
        # insert 6000 vectors
        result = connect.insert(binary_collection, default_binary_entities)
        connect.flush([binary_collection])
        with pytest.raises(Exception) as e:
            res = connect.create_index(binary_collection, binary_field_name, get_l2_index)

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

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    @pytest.mark.skip("repeat with test_create_index binary")
    def _test_get_index_info(self, connect, binary_collection, get_jaccard_index):
        """
        target: test describe index interface
        method: create collection and add entities in it, create index, call describe index
        expected: return code 0, and index instructure
        """
        result = connect.insert(binary_collection, default_binary_entities)
        connect.flush([binary_collection])
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        stats = connect.get_collection_stats(binary_collection)
        assert stats["row_count"] == default_nb
        for partition in stats["partitions"]:
            segments = partition["segments"]
            if segments:
                for segment in segments:
                    for file in segment["files"]:
                        if "index_type" in file:
                            assert file["index_type"] == get_jaccard_index["index_type"]

    @pytest.mark.skip("repeat with test_create_index_partition binary")
    def _test_get_index_info_partition(self, connect, binary_collection, get_jaccard_index):
        """
        target: test describe index interface
        method: create collection, create partition and add entities in it, create index, call describe index
        expected: return code 0, and index instructure
        """
        connect.create_partition(binary_collection, default_tag)
        result = connect.insert(binary_collection, default_binary_entities, partition_name=default_tag)
        connect.flush([binary_collection])
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        stats = connect.get_collection_stats(binary_collection)
T
ThreadDao 已提交
1127
        log.info(stats)
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
        assert stats["row_count"] == default_nb
        assert len(stats["partitions"]) == 2
        for partition in stats["partitions"]:
            segments = partition["segments"]
            if segments:
                for segment in segments:
                    for file in segment["files"]:
                        if "index_type" in file:
                            assert file["index_type"] == get_jaccard_index["index_type"]

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

1144 1145 1146 1147 1148 1149 1150 1151 1152
    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index(self, connect, binary_collection, get_jaccard_index):
        """
        target: test drop index interface
        method: create collection and add entities in it, create index, call drop index
        expected: return code 0, and default index param
        """
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        stats = connect.get_collection_stats(binary_collection)
T
ThreadDao 已提交
1153
        log.info(stats)
1154 1155 1156 1157
        connect.drop_index(binary_collection, binary_field_name)
        binary_index = connect.describe_index(binary_collection, "")
        assert not binary_index

1158
    @pytest.mark.tags(CaseLabel.L0)
1159 1160 1161
    def test_drop_index_partition(self, connect, binary_collection, get_jaccard_index):
        """
        target: test drop index interface
B
binbin 已提交
1162 1163
        method: create collection, create partition and add entities in it,
                create index on collection, call drop collection index
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
        expected: return code 0, and default index param
        """
        connect.create_partition(binary_collection, default_tag)
        result = connect.insert(binary_collection, default_binary_entities, partition_name=default_tag)
        connect.flush([binary_collection])
        connect.create_index(binary_collection, binary_field_name, get_jaccard_index)
        connect.drop_index(binary_collection, binary_field_name)
        binary_index = connect.describe_index(binary_collection, "")
        assert not binary_index


class TestIndexInvalid(object):
    """
    Test create / describe / drop index interfaces with invalid collection names
    """

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

1187
    @pytest.mark.tags(CaseLabel.L0)
1188
    def test_create_index_with_invalid_collection_name(self, connect, get_collection_name):
1189 1190 1191 1192 1193
        """
        target: test create index interface for invalid scenario
        method: create index with invalid collection name
        expected: raise exception
        """
1194 1195 1196 1197 1198 1199
        collection_name = get_collection_name
        with pytest.raises(Exception) as e:
            connect.create_index(collection_name, field_name, default_index)

    @pytest.mark.tags(CaseLabel.L2)
    def test_drop_index_with_invalid_collection_name(self, connect, get_collection_name):
1200 1201 1202 1203 1204
        """
        target: test drop index interface for invalid scenario
        method: drop index with invalid collection name
        expected: raise exception
        """
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
        collection_name = get_collection_name
        with pytest.raises(Exception) as e:
            connect.drop_index(collection_name)

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

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_with_invalid_index_params(self, connect, collection, get_index):
1218 1219 1220 1221 1222
        """
        target: test create index interface for invalid scenario
        method: create index with invalid index params
        expected: raise exception
        """
T
ThreadDao 已提交
1223
        log.info(get_index)
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
        with pytest.raises(Exception) as e:
            connect.create_index(collection, field_name, get_index)


class TestIndexAsync:
    @pytest.fixture(scope="function", autouse=True)
    def skip_http_check(self, args):
        if args["handler"] == "HTTP":
            pytest.skip("skip in http mode")

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

    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_simple_index(self, request, connect):
        # if str(connect._cmd("mode")) == "CPU":
        #     if request.param["index_type"] in index_cpu_not_support():
        #         pytest.skip("sq8h not support in CPU mode")
        return copy.deepcopy(request.param)

    def check_result(self, res):
T
ThreadDao 已提交
1251 1252
        log.info("In callback check search result")
        log.info(res)
1253 1254 1255 1256 1257 1258

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

B
binbin 已提交
1260
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1261 1262 1263 1264 1265 1266 1267
    def test_create_index(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        result = connect.insert(collection, default_entities)
T
ThreadDao 已提交
1268
        log.info("start index")
1269
        future = connect.create_index(collection, field_name, get_simple_index, _async=True)
T
ThreadDao 已提交
1270
        log.info("before result")
1271 1272
        res = future.result()
        # TODO:
T
ThreadDao 已提交
1273
        log.info(res)
1274

1275
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
1276
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1277 1278 1279 1280 1281 1282 1283 1284 1285
    def test_create_index_drop(self, connect, collection):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        result = connect.insert(collection, default_entities)
        connect.create_index(collection, field_name, default_index, _async=True)
        connect.drop_collection(collection)
C
Cai Yudong 已提交
1286
        with pytest.raises(Exception, match=f'DescribeIndex failed: collection {collection} not found'):
1287 1288 1289 1290 1291 1292 1293 1294 1295
            connect.describe_index(collection, "")

    @pytest.mark.tags(CaseLabel.L2)
    def test_create_index_with_invalid_collection_name(self, connect):
        collection_name = " "
        with pytest.raises(Exception) as e:
            future = connect.create_index(collection_name, field_name, default_index, _async=True)
            res = future.result()

1296
    @pytest.mark.tags(CaseLabel.L0)
B
binbin 已提交
1297
    # @pytest.mark.timeout(BUILD_TIMEOUT)
1298 1299 1300 1301 1302 1303 1304
    def test_create_index_callback(self, connect, collection, get_simple_index):
        """
        target: test create index interface
        method: create collection and add entities in it, create index
        expected: return search success
        """
        result = connect.insert(collection, default_entities)
T
ThreadDao 已提交
1305
        log.info("start index")
1306 1307
        future = connect.create_index(collection, field_name, get_simple_index, _async=True,
                                      _callback=self.check_result)
T
ThreadDao 已提交
1308
        log.info("before result")
1309 1310
        res = future.result()
        # TODO:
T
ThreadDao 已提交
1311
        log.info(res)