test_collection_count.py 28.8 KB
Newer Older
X
Xiaohai Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import pdb
import pytest
import logging
import itertools
from time import sleep
import threading
from multiprocessing import Process
from milvus import IndexType, MetricType
from utils import *

dim = 128
index_file_size = 10
add_time_interval = 3
tag = "1970-01-01"
nb = 6000

class TestCollectionCount:
    """
    params means different nb, the nb value may trigger merge, or not
    """
    @pytest.fixture(
        scope="function",
        params=[
            1,
            5000,
D
del-zhenwu 已提交
26
            20000,
X
Xiaohai Xu 已提交
27 28
        ],
    )
D
del-zhenwu 已提交
29
    def insert_nb(self, request):
X
Xiaohai Xu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        yield request.param

    """
    generate valid create_index params
    """
    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_simple_index(self, request, connect):
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in cpu mode")
        if request.param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip PQ Temporary")
        return request.param

D
del-zhenwu 已提交
47
    def test_collection_rows_count(self, connect, collection, insert_nb):
X
Xiaohai Xu 已提交
48 49 50
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
51
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
52 53
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
54
        nb = insert_nb
X
Xiaohai Xu 已提交
55
        vectors = gen_vectors(nb, dim)
D
del-zhenwu 已提交
56
        res = connect.insert(collection_name=collection, records=vectors)
X
Xiaohai Xu 已提交
57
        connect.flush([collection])
D
del-zhenwu 已提交
58
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
59 60
        assert res == nb

D
del-zhenwu 已提交
61
    def test_collection_rows_count_partition(self, connect, collection, insert_nb):
X
Xiaohai Xu 已提交
62 63 64
        '''
        target: test collection rows_count is correct or not
        method: create collection, create partition and add vectors in it,
D
del-zhenwu 已提交
65
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
66 67
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
68
        nb = insert_nb
X
Xiaohai Xu 已提交
69 70 71
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(collection, tag)
        assert status.OK()
D
del-zhenwu 已提交
72
        res = connect.insert(collection_name=collection, records=vectors, partition_tag=tag)
X
Xiaohai Xu 已提交
73
        connect.flush([collection])
D
del-zhenwu 已提交
74
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
75 76
        assert res == nb

D
del-zhenwu 已提交
77
    def test_collection_rows_count_multi_partitions_A(self, connect, collection, insert_nb):
X
Xiaohai Xu 已提交
78 79 80
        '''
        target: test collection rows_count is correct or not
        method: create collection, create partitions and add vectors in it,
D
del-zhenwu 已提交
81
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
82 83 84
        expected: the count is equal to the length of vectors
        '''
        new_tag = "new_tag"
D
del-zhenwu 已提交
85
        nb = insert_nb
X
Xiaohai Xu 已提交
86 87 88 89
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(collection, tag)
        status = connect.create_partition(collection, new_tag)
        assert status.OK()
D
del-zhenwu 已提交
90
        res = connect.insert(collection_name=collection, records=vectors)
X
Xiaohai Xu 已提交
91
        connect.flush([collection])
D
del-zhenwu 已提交
92
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
93 94
        assert res == nb

D
del-zhenwu 已提交
95
    def test_collection_rows_count_multi_partitions_B(self, connect, collection, insert_nb):
X
Xiaohai Xu 已提交
96 97 98
        '''
        target: test collection rows_count is correct or not
        method: create collection, create partitions and add vectors in one of the partitions,
D
del-zhenwu 已提交
99
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
100 101 102
        expected: the count is equal to the length of vectors
        '''
        new_tag = "new_tag"
D
del-zhenwu 已提交
103
        nb = insert_nb
X
Xiaohai Xu 已提交
104 105 106 107
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(collection, tag)
        status = connect.create_partition(collection, new_tag)
        assert status.OK()
D
del-zhenwu 已提交
108
        res = connect.insert(collection_name=collection, records=vectors, partition_tag=tag)
X
Xiaohai Xu 已提交
109
        connect.flush([collection])
D
del-zhenwu 已提交
110
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
111 112
        assert res == nb

D
del-zhenwu 已提交
113
    def test_collection_rows_count_multi_partitions_C(self, connect, collection, insert_nb):
X
Xiaohai Xu 已提交
114 115 116
        '''
        target: test collection rows_count is correct or not
        method: create collection, create partitions and add vectors in one of the partitions,
D
del-zhenwu 已提交
117
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
118 119 120
        expected: the collection count is equal to the length of vectors
        '''
        new_tag = "new_tag"
D
del-zhenwu 已提交
121
        nb = insert_nb
X
Xiaohai Xu 已提交
122 123 124 125
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(collection, tag)
        status = connect.create_partition(collection, new_tag)
        assert status.OK()
D
del-zhenwu 已提交
126 127
        res = connect.insert(collection_name=collection, records=vectors, partition_tag=tag)
        res = connect.insert(collection_name=collection, records=vectors, partition_tag=new_tag)
X
Xiaohai Xu 已提交
128
        connect.flush([collection])
D
del-zhenwu 已提交
129
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
130 131 132 133
        assert res == nb * 2

    def test_collection_rows_count_after_index_created(self, connect, collection, get_simple_index):
        '''
D
del-zhenwu 已提交
134 135 136
        target: test count_entities, after index have been created
        method: add vectors in db, and create index, then calling count_entities with correct params 
        expected: count_entities raise exception
X
Xiaohai Xu 已提交
137 138 139 140 141
        '''
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        nb = 100
        vectors = gen_vectors(nb, dim)
D
del-zhenwu 已提交
142
        res = connect.insert(collection_name=collection, records=vectors)
X
Xiaohai Xu 已提交
143 144
        connect.flush([collection])
        connect.create_index(collection, index_type, index_param)
D
del-zhenwu 已提交
145
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
146 147
        assert res == nb

D
del-zhenwu 已提交
148 149 150
    # @pytest.mark.level(2)
    # def test_count_without_connection(self, collection, dis_connect):
    #     '''
D
del-zhenwu 已提交
151 152 153
    #     target: test count_entities, without connection
    #     method: calling count_entities with correct params, with a disconnected instance
    #     expected: count_entities raise exception
D
del-zhenwu 已提交
154 155
    #     '''
    #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
156
    #         status = dis_connect.count_entities(collection)
X
Xiaohai Xu 已提交
157 158 159 160 161

    def test_collection_rows_count_no_vectors(self, connect, collection):
        '''
        target: test collection rows_count is correct or not, if collection is empty
        method: create collection and no vectors in it,
D
del-zhenwu 已提交
162
            assert the value returned by count_entities method is equal to 0
X
Xiaohai Xu 已提交
163 164 165 166 167 168 169
        expected: the count is equal to 0
        '''
        collection_name = gen_unique_str()
        param = {'collection_name': collection_name,
                 'dimension': dim,
                 'index_file_size': index_file_size}
        connect.create_collection(param)        
D
del-zhenwu 已提交
170
        status, res = connect.count_entities(collection)
X
Xiaohai Xu 已提交
171 172 173 174 175 176 177 178 179
        assert res == 0

    # TODO: enable
    @pytest.mark.level(2)
    @pytest.mark.timeout(20)
    def _test_collection_rows_count_multiprocessing(self, connect, collection, args):
        '''
        target: test collection rows_count is correct or not with multiprocess
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
180
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
181 182 183 184
        expected: the count is equal to the length of vectors
        '''
        nq = 2
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
185
        res = connect.insert(collection_name=collection, records=vectors)
X
Xiaohai Xu 已提交
186 187 188
        time.sleep(add_time_interval)

        def rows_count(milvus):
D
del-zhenwu 已提交
189
            status, res = milvus.count_entities(collection)
X
Xiaohai Xu 已提交
190 191 192 193 194 195
            logging.getLogger().info(status)
            assert res == nq

        process_num = 8
        processes = []
        for i in range(process_num):
D
del-zhenwu 已提交
196
            milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
X
Xiaohai Xu 已提交
197 198 199 200 201 202 203 204 205 206 207
            p = Process(target=rows_count, args=(milvus, ))
            processes.append(p)
            p.start()
            logging.getLogger().info(p)
        for p in processes:
            p.join()

    def test_collection_rows_count_multi_collections(self, connect):
        '''
        target: test collection rows_count is correct or not with multiple collections of L2
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
208
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221
        expected: the count is equal to the length of vectors
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
        collection_list = []
        for i in range(20):
            collection_name = gen_unique_str()
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
            connect.create_collection(param)
D
del-zhenwu 已提交
222
            res = connect.insert(collection_name=collection_name, records=vectors)
X
Xiaohai Xu 已提交
223 224
        connect.flush(collection_list)
        for i in range(20):
D
del-zhenwu 已提交
225
            status, res = connect.count_entities(collection_list[i])
X
Xiaohai Xu 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239
            assert status.OK()
            assert res == nq


class TestCollectionCountIP:
    """
    params means different nb, the nb value may trigger merge, or not
    """

    @pytest.fixture(
        scope="function",
        params=[
            1,
            5000,
D
del-zhenwu 已提交
240
            20000,
X
Xiaohai Xu 已提交
241 242
        ],
    )
D
del-zhenwu 已提交
243
    def insert_nb(self, request):
X
Xiaohai Xu 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        yield request.param

    """
    generate valid create_index params
    """

    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_simple_index(self, request, connect):
        if str(connect._cmd("mode")[1]) == "CPU":
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in CPU mode")
        if request.param["index_type"] == IndexType.IVF_PQ:
            pytest.skip("Skip PQ Temporary")
        return request.param

D
del-zhenwu 已提交
262
    def test_collection_rows_count(self, connect, ip_collection, insert_nb):
X
Xiaohai Xu 已提交
263 264 265
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
266
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
267 268
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
269
        nb = insert_nb
X
Xiaohai Xu 已提交
270
        vectors = gen_vectors(nb, dim)
D
del-zhenwu 已提交
271
        res = connect.insert(collection_name=ip_collection, records=vectors)
X
Xiaohai Xu 已提交
272
        connect.flush([ip_collection])
D
del-zhenwu 已提交
273
        status, res = connect.count_entities(ip_collection)
X
Xiaohai Xu 已提交
274 275 276 277
        assert res == nb

    def test_collection_rows_count_after_index_created(self, connect, ip_collection, get_simple_index):
        '''
D
del-zhenwu 已提交
278 279 280
        target: test count_entities, after index have been created
        method: add vectors in db, and create index, then calling count_entities with correct params
        expected: count_entities raise exception
X
Xiaohai Xu 已提交
281 282 283 284 285
        '''
        index_param = get_simple_index["index_param"]
        index_type = get_simple_index["index_type"]
        nb = 100
        vectors = gen_vectors(nb, dim)
D
del-zhenwu 已提交
286
        res = connect.insert(collection_name=ip_collection, records=vectors)
X
Xiaohai Xu 已提交
287 288
        connect.flush([ip_collection])
        connect.create_index(ip_collection, index_type, index_param)
D
del-zhenwu 已提交
289
        status, res = connect.count_entities(ip_collection)
X
Xiaohai Xu 已提交
290 291
        assert res == nb

D
del-zhenwu 已提交
292 293 294
    # @pytest.mark.level(2)
    # def test_count_without_connection(self, ip_collection, dis_connect):
    #     '''
D
del-zhenwu 已提交
295 296 297
    #     target: test count_entities, without connection
    #     method: calling count_entities with correct params, with a disconnected instance
    #     expected: count_entities raise exception
D
del-zhenwu 已提交
298 299
    #     '''
    #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
300
    #         status = dis_connect.count_entities(ip_collection)
X
Xiaohai Xu 已提交
301 302 303 304 305

    def test_collection_rows_count_no_vectors(self, connect, ip_collection):
        '''
        target: test collection rows_count is correct or not, if collection is empty
        method: create collection and no vectors in it,
D
del-zhenwu 已提交
306
            assert the value returned by count_entities method is equal to 0
X
Xiaohai Xu 已提交
307 308 309 310 311 312 313
        expected: the count is equal to 0
        '''
        collection_name = gen_unique_str("test_collection")
        param = {'collection_name': collection_name,
                 'dimension': dim,
                 'index_file_size': index_file_size}
        connect.create_collection(param)
D
del-zhenwu 已提交
314
        status, res = connect.count_entities(ip_collection)
X
Xiaohai Xu 已提交
315 316 317 318 319 320 321 322
        assert res == 0

    # TODO: enable
    @pytest.mark.timeout(60)
    def _test_collection_rows_count_multiprocessing(self, connect, ip_collection, args):
        '''
        target: test collection rows_count is correct or not with multiprocess
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
323
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
324 325 326 327
        expected: the count is equal to the length of vectors
        '''
        nq = 2
        vectors = gen_vectors(nq, dim)
D
del-zhenwu 已提交
328
        res = connect.insert(collection_name=ip_collection, records=vectors)
X
Xiaohai Xu 已提交
329 330 331
        time.sleep(add_time_interval)

        def rows_count(milvus):
D
del-zhenwu 已提交
332
            status, res = milvus.count_entities(ip_collection)
X
Xiaohai Xu 已提交
333 334 335 336 337 338
            logging.getLogger().info(status)
            assert res == nq

        process_num = 8
        processes = []
        for i in range(process_num):
D
del-zhenwu 已提交
339
            milvus = get_milvus(args["ip"], args["port"], handler=args["handler"])
X
Xiaohai Xu 已提交
340 341 342 343 344 345 346 347 348 349 350
            p = Process(target=rows_count, args=(milvus,))
            processes.append(p)
            p.start()
            logging.getLogger().info(p)
        for p in processes:
            p.join()

    def test_collection_rows_count_multi_collections(self, connect):
        '''
        target: test collection rows_count is correct or not with multiple collections of IP
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
351
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364
        expected: the count is equal to the length of vectors
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
        collection_list = []
        for i in range(20):
            collection_name = gen_unique_str('test_collection_rows_count_multi_collections')
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
            connect.create_collection(param)
D
del-zhenwu 已提交
365
            res = connect.insert(collection_name=collection_name, records=vectors)
X
Xiaohai Xu 已提交
366 367
        connect.flush(collection_list)
        for i in range(20):
D
del-zhenwu 已提交
368
            status, res = connect.count_entities(collection_list[i])
X
Xiaohai Xu 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382
            assert status.OK()
            assert res == nq


class TestCollectionCountJAC:
    """
    params means different nb, the nb value may trigger merge, or not
    """

    @pytest.fixture(
        scope="function",
        params=[
            1,
            5000,
D
del-zhenwu 已提交
383
            20000,
X
Xiaohai Xu 已提交
384 385
        ],
    )
D
del-zhenwu 已提交
386
    def insert_nb(self, request):
X
Xiaohai Xu 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        yield request.param

    """
    generate valid create_index params
    """

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

D
del-zhenwu 已提交
404
    def test_collection_rows_count(self, connect, jac_collection, insert_nb):
X
Xiaohai Xu 已提交
405 406 407
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
408
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
409 410
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
411
        nb = insert_nb
X
Xiaohai Xu 已提交
412
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
413
        res = connect.insert(collection_name=jac_collection, records=vectors)
X
Xiaohai Xu 已提交
414
        connect.flush([jac_collection])
D
del-zhenwu 已提交
415
        status, res = connect.count_entities(jac_collection)
X
Xiaohai Xu 已提交
416 417 418 419
        assert res == nb

    def test_collection_rows_count_after_index_created(self, connect, jac_collection, get_jaccard_index):
        '''
D
del-zhenwu 已提交
420 421 422
        target: test count_entities, after index have been created
        method: add vectors in db, and create index, then calling count_entities with correct params
        expected: count_entities raise exception
X
Xiaohai Xu 已提交
423 424 425 426 427
        '''
        nb = 100
        index_param = get_jaccard_index["index_param"]
        index_type = get_jaccard_index["index_type"]
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
428
        res = connect.insert(collection_name=jac_collection, records=vectors)
X
Xiaohai Xu 已提交
429 430
        connect.flush([jac_collection])
        connect.create_index(jac_collection, index_type, index_param)
D
del-zhenwu 已提交
431
        status, res = connect.count_entities(jac_collection)
X
Xiaohai Xu 已提交
432 433
        assert res == nb

D
del-zhenwu 已提交
434 435 436
    # @pytest.mark.level(2)
    # def test_count_without_connection(self, jac_collection, dis_connect):
    #     '''
D
del-zhenwu 已提交
437 438 439
    #     target: test count_entities, without connection
    #     method: calling count_entities with correct params, with a disconnected instance
    #     expected: count_entities raise exception
D
del-zhenwu 已提交
440 441
    #     '''
    #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
442
    #         status = dis_connect.count_entities(jac_collection)
X
Xiaohai Xu 已提交
443 444 445 446 447

    def test_collection_rows_count_no_vectors(self, connect, jac_collection):
        '''
        target: test collection rows_count is correct or not, if collection is empty
        method: create collection and no vectors in it,
D
del-zhenwu 已提交
448
            assert the value returned by count_entities method is equal to 0
X
Xiaohai Xu 已提交
449 450 451 452 453 454 455
        expected: the count is equal to 0
        '''
        collection_name = gen_unique_str("test_collection")
        param = {'collection_name': collection_name,
                 'dimension': dim,
                 'index_file_size': index_file_size}
        connect.create_collection(param)
D
del-zhenwu 已提交
456
        status, res = connect.count_entities(jac_collection)
X
Xiaohai Xu 已提交
457 458 459 460 461 462
        assert res == 0

    def test_collection_rows_count_multi_collections(self, connect):
        '''
        target: test collection rows_count is correct or not with multiple collections of IP
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
463
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476
        expected: the count is equal to the length of vectors
        '''
        nq = 100
        tmp, vectors = gen_binary_vectors(nq, dim)
        collection_list = []
        for i in range(20):
            collection_name = gen_unique_str('test_collection_rows_count_multi_collections')
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.JACCARD}
            connect.create_collection(param)
D
del-zhenwu 已提交
477
            res = connect.insert(collection_name=collection_name, records=vectors)
X
Xiaohai Xu 已提交
478 479
        connect.flush(collection_list)
        for i in range(20):
D
del-zhenwu 已提交
480
            status, res = connect.count_entities(collection_list[i])
X
Xiaohai Xu 已提交
481 482 483
            assert status.OK()
            assert res == nq

D
del-zhenwu 已提交
484
class TestCollectionCountBinary:
X
Xiaohai Xu 已提交
485 486 487 488 489 490 491 492 493
    """
    params means different nb, the nb value may trigger merge, or not
    """

    @pytest.fixture(
        scope="function",
        params=[
            1,
            5000,
D
del-zhenwu 已提交
494
            20000,
X
Xiaohai Xu 已提交
495 496
        ],
    )
D
del-zhenwu 已提交
497
    def insert_nb(self, request):
X
Xiaohai Xu 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
        yield request.param

    """
    generate valid create_index params
    """

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

D
del-zhenwu 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    @pytest.fixture(
        scope="function",
        params=gen_simple_index()
    )
    def get_substructure_index(self, request, connect):
        logging.getLogger().info(request.param)
        if request.param["index_type"] == IndexType.FLAT:
            return request.param
        else:
            pytest.skip("Skip index Temporary")

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

D
del-zhenwu 已提交
537
    def test_collection_rows_count(self, connect, ham_collection, insert_nb):
X
Xiaohai Xu 已提交
538 539 540
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
541
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
542 543
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
544
        nb = insert_nb
X
Xiaohai Xu 已提交
545
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
546
        res = connect.insert(collection_name=ham_collection, records=vectors)
X
Xiaohai Xu 已提交
547
        connect.flush([ham_collection])
D
del-zhenwu 已提交
548
        status, res = connect.count_entities(ham_collection)
X
Xiaohai Xu 已提交
549 550
        assert res == nb

D
del-zhenwu 已提交
551
    def test_collection_rows_count_substructure(self, connect, substructure_collection, insert_nb):
D
del-zhenwu 已提交
552 553 554
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
555
            assert the value returned by count_entities method is equal to length of vectors
D
del-zhenwu 已提交
556 557
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
558
        nb = insert_nb
D
del-zhenwu 已提交
559
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
560
        res = connect.insert(collection_name=substructure_collection, records=vectors)
D
del-zhenwu 已提交
561
        connect.flush([substructure_collection])
D
del-zhenwu 已提交
562
        status, res = connect.count_entities(substructure_collection)
D
del-zhenwu 已提交
563 564
        assert res == nb

D
del-zhenwu 已提交
565
    def test_collection_rows_count_superstructure(self, connect, superstructure_collection, insert_nb):
D
del-zhenwu 已提交
566 567 568
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
569
            assert the value returned by count_entities method is equal to length of vectors
D
del-zhenwu 已提交
570 571
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
572
        nb = insert_nb
D
del-zhenwu 已提交
573
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
574
        res = connect.insert(collection_name=superstructure_collection, records=vectors)
D
del-zhenwu 已提交
575
        connect.flush([superstructure_collection])
D
del-zhenwu 已提交
576
        status, res = connect.count_entities(superstructure_collection)
D
del-zhenwu 已提交
577 578
        assert res == nb

X
Xiaohai Xu 已提交
579 580
    def test_collection_rows_count_after_index_created(self, connect, ham_collection, get_hamming_index):
        '''
D
del-zhenwu 已提交
581 582 583
        target: test count_entities, after index have been created
        method: add vectors in db, and create index, then calling count_entities with correct params
        expected: count_entities raise exception
X
Xiaohai Xu 已提交
584 585 586 587 588
        '''
        nb = 100
        index_type = get_hamming_index["index_type"]
        index_param = get_hamming_index["index_param"]
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
589
        res = connect.insert(collection_name=ham_collection, records=vectors)
X
Xiaohai Xu 已提交
590 591
        connect.flush([ham_collection])
        connect.create_index(ham_collection, index_type, index_param)
D
del-zhenwu 已提交
592
        status, res = connect.count_entities(ham_collection)
X
Xiaohai Xu 已提交
593 594
        assert res == nb

D
del-zhenwu 已提交
595 596
    def test_collection_rows_count_after_index_created_substructure(self, connect, substructure_collection, get_substructure_index):
        '''
D
del-zhenwu 已提交
597 598 599
        target: test count_entities, after index have been created
        method: add vectors in db, and create index, then calling count_entities with correct params
        expected: count_entities raise exception
D
del-zhenwu 已提交
600 601 602 603 604
        '''
        nb = 100
        index_type = get_substructure_index["index_type"]
        index_param = get_substructure_index["index_param"]
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
605
        res = connect.insert(collection_name=substructure_collection, records=vectors)
D
del-zhenwu 已提交
606 607
        connect.flush([substructure_collection])
        connect.create_index(substructure_collection, index_type, index_param)
D
del-zhenwu 已提交
608
        status, res = connect.count_entities(substructure_collection)
D
del-zhenwu 已提交
609 610 611 612
        assert res == nb

    def test_collection_rows_count_after_index_created_superstructure(self, connect, superstructure_collection, get_superstructure_index):
        '''
D
del-zhenwu 已提交
613 614 615
        target: test count_entities, after index have been created
        method: add vectors in db, and create index, then calling count_entities with correct params
        expected: count_entities raise exception
D
del-zhenwu 已提交
616 617 618 619 620
        '''
        nb = 100
        index_type = get_superstructure_index["index_type"]
        index_param = get_superstructure_index["index_param"]
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
621
        res = connect.insert(collection_name=superstructure_collection, records=vectors)
D
del-zhenwu 已提交
622 623
        connect.flush([superstructure_collection])
        connect.create_index(superstructure_collection, index_type, index_param)
D
del-zhenwu 已提交
624
        status, res = connect.count_entities(superstructure_collection)
D
del-zhenwu 已提交
625 626
        assert res == nb

D
del-zhenwu 已提交
627 628 629
    # @pytest.mark.level(2)
    # def test_count_without_connection(self, ham_collection, dis_connect):
    #     '''
D
del-zhenwu 已提交
630 631 632
    #     target: test count_entities, without connection
    #     method: calling count_entities with correct params, with a disconnected instance
    #     expected: count_entities raise exception
D
del-zhenwu 已提交
633 634
    #     '''
    #     with pytest.raises(Exception) as e:
D
del-zhenwu 已提交
635
    #         status = dis_connect.count_entities(ham_collection)
X
Xiaohai Xu 已提交
636 637 638 639 640

    def test_collection_rows_count_no_vectors(self, connect, ham_collection):
        '''
        target: test collection rows_count is correct or not, if collection is empty
        method: create collection and no vectors in it,
D
del-zhenwu 已提交
641
            assert the value returned by count_entities method is equal to 0
X
Xiaohai Xu 已提交
642 643 644 645 646 647 648
        expected: the count is equal to 0
        '''
        collection_name = gen_unique_str("test_collection")
        param = {'collection_name': collection_name,
                 'dimension': dim,
                 'index_file_size': index_file_size}
        connect.create_collection(param)
D
del-zhenwu 已提交
649
        status, res = connect.count_entities(ham_collection)
X
Xiaohai Xu 已提交
650 651 652 653 654 655
        assert res == 0

    def test_collection_rows_count_multi_collections(self, connect):
        '''
        target: test collection rows_count is correct or not with multiple collections of IP
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
656
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669
        expected: the count is equal to the length of vectors
        '''
        nq = 100
        tmp, vectors = gen_binary_vectors(nq, dim)
        collection_list = []
        for i in range(20):
            collection_name = gen_unique_str('test_collection_rows_count_multi_collections')
            collection_list.append(collection_name)
            param = {'collection_name': collection_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.HAMMING}
            connect.create_collection(param)
D
del-zhenwu 已提交
670
            res = connect.insert(collection_name=collection_name, records=vectors)
X
Xiaohai Xu 已提交
671 672
        connect.flush(collection_list)
        for i in range(20):
D
del-zhenwu 已提交
673
            status, res = connect.count_entities(collection_list[i])
X
Xiaohai Xu 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687
            assert status.OK()
            assert res == nq


class TestCollectionCountTANIMOTO:
    """
    params means different nb, the nb value may trigger merge, or not
    """

    @pytest.fixture(
        scope="function",
        params=[
            1,
            5000,
D
del-zhenwu 已提交
688
            20000,
X
Xiaohai Xu 已提交
689 690
        ],
    )
D
del-zhenwu 已提交
691
    def insert_nb(self, request):
X
Xiaohai Xu 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
        yield request.param

    """
    generate valid create_index params
    """

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

D
del-zhenwu 已提交
709
    def test_collection_rows_count(self, connect, tanimoto_collection, insert_nb):
X
Xiaohai Xu 已提交
710 711 712
        '''
        target: test collection rows_count is correct or not
        method: create collection and add vectors in it,
D
del-zhenwu 已提交
713
            assert the value returned by count_entities method is equal to length of vectors
X
Xiaohai Xu 已提交
714 715
        expected: the count is equal to the length of vectors
        '''
D
del-zhenwu 已提交
716
        nb = insert_nb
X
Xiaohai Xu 已提交
717
        tmp, vectors = gen_binary_vectors(nb, dim)
D
del-zhenwu 已提交
718
        res = connect.insert(collection_name=tanimoto_collection, records=vectors)
X
Xiaohai Xu 已提交
719
        connect.flush([tanimoto_collection])
D
del-zhenwu 已提交
720
        status, res = connect.count_entities(tanimoto_collection)
X
Xiaohai Xu 已提交
721 722
        assert status.OK()
        assert res == nb