test_partition.py 16.1 KB
Newer Older
Z
zhenwu 已提交
1 2 3 4 5 6 7
import time
import random
import pdb
import threading
import logging
from multiprocessing import Pool, Process
import pytest
8
from milvus import IndexType, MetricType
Z
zhenwu 已提交
9 10 11 12 13
from utils import *


dim = 128
index_file_size = 10
X
Xiaohai Xu 已提交
14
collection_id = "test_partition"
Z
zhenwu 已提交
15 16 17 18 19 20 21 22 23 24 25 26
ADD_TIMEOUT = 60
nprobe = 1
tag = "1970-01-01"


class TestCreateBase:

    """
    ******************************************************************
      The following cases are used to test `create_partition` function 
    ******************************************************************
    """
X
Xiaohai Xu 已提交
27
    def test_create_partition(self, connect, collection):
Z
zhenwu 已提交
28 29 30 31 32
        '''
        target: test create partition, check status returned
        method: call function: create_partition
        expected: status ok
        '''
X
Xiaohai Xu 已提交
33
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
34 35
        assert status.OK()

D
del-zhenwu 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
    def _test_create_partition_limit(self, connect, collection):
        '''
        target: test create partitions, check status returned
        method: call function: create_partition for 4097 times
        expected: status not ok
        '''
        for i in range(4096):
            tag_tmp = gen_unique_str()
            status = connect.create_partition(collection, tag_tmp)
            assert status.OK()
        status = connect.create_partition(collection, tag)
        assert not status.OK()

X
Xiaohai Xu 已提交
49
    def test_create_partition_repeat(self, connect, collection):
Z
zhenwu 已提交
50 51 52 53 54
        '''
        target: test create partition, check status returned
        method: call function: create_partition
        expected: status ok
        '''
X
Xiaohai Xu 已提交
55
        status = connect.create_partition(collection, tag)
56
        assert status.OK()
X
Xiaohai Xu 已提交
57
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
58 59
        assert not status.OK()

X
Xiaohai Xu 已提交
60
    def test_create_partition_collection_not_existed(self, connect):
Z
zhenwu 已提交
61
        '''
X
Xiaohai Xu 已提交
62
        target: test create partition, its owner collection name not existed in db, check status returned
Z
zhenwu 已提交
63 64 65
        method: call function: create_partition
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
66 67
        collection_name = gen_unique_str()
        status = connect.create_partition(collection_name, tag)
Z
zhenwu 已提交
68 69
        assert not status.OK()

X
Xiaohai Xu 已提交
70
    def test_create_partition_tag_name_None(self, connect, collection):
Z
zhenwu 已提交
71 72 73 74 75 76
        '''
        target: test create partition, tag name set None, check status returned
        method: call function: create_partition
        expected: status ok
        '''
        tag_name = None
D
del-zhenwu 已提交
77 78
        with pytest.raises(Exception) as e:
            status = connect.create_partition(collection, tag_name)
Z
zhenwu 已提交
79

X
Xiaohai Xu 已提交
80
    def test_create_different_partition_tags(self, connect, collection):
Z
zhenwu 已提交
81
        '''
82 83 84
        target: test create partition twice with different names
        method: call function: create_partition, and again
        expected: status ok
Z
zhenwu 已提交
85
        '''
X
Xiaohai Xu 已提交
86
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
87
        assert status.OK()
88
        tag_name = gen_unique_str()
X
Xiaohai Xu 已提交
89
        status = connect.create_partition(collection, tag_name)
90
        assert status.OK()
D
del-zhenwu 已提交
91
        status, res = connect.list_partitions(collection)
92 93 94 95 96 97 98
        assert status.OK()
        tag_list = []
        for item in res:
            tag_list.append(item.tag)
        assert tag in tag_list
        assert tag_name in tag_list
        assert "_default" in tag_list
Z
zhenwu 已提交
99

D
del-zhenwu 已提交
100
    def test_create_partition_insert_default(self, connect, collection):
Z
zhenwu 已提交
101 102 103 104 105
        '''
        target: test create partition, and insert vectors, check status returned
        method: call function: create_partition
        expected: status ok
        '''
X
Xiaohai Xu 已提交
106
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
107 108 109 110
        assert status.OK()
        nq = 100
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
X
Xiaohai Xu 已提交
111
        status, ids = connect.insert(collection, vectors, ids)
Z
zhenwu 已提交
112 113
        assert status.OK()

X
Xiaohai Xu 已提交
114
    def test_create_partition_insert_with_tag(self, connect, collection):
Z
zhenwu 已提交
115 116 117 118 119
        '''
        target: test create partition, and insert vectors, check status returned
        method: call function: create_partition
        expected: status ok
        '''
X
Xiaohai Xu 已提交
120
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
121 122 123 124
        assert status.OK()
        nq = 100
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
X
Xiaohai Xu 已提交
125
        status, ids = connect.insert(collection, vectors, ids, partition_tag=tag)
Z
zhenwu 已提交
126 127
        assert status.OK()

X
Xiaohai Xu 已提交
128
    def test_create_partition_insert_with_tag_not_existed(self, connect, collection):
Z
zhenwu 已提交
129 130 131 132 133 134
        '''
        target: test create partition, and insert vectors, check status returned
        method: call function: create_partition
        expected: status not ok
        '''
        tag_new = "tag_new"
X
Xiaohai Xu 已提交
135
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
136 137 138 139
        assert status.OK()
        nq = 100
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
X
Xiaohai Xu 已提交
140
        status, ids = connect.insert(collection, vectors, ids, partition_tag=tag_new)
Z
zhenwu 已提交
141 142
        assert not status.OK()

X
Xiaohai Xu 已提交
143
    def test_create_partition_insert_same_tags(self, connect, collection):
Z
zhenwu 已提交
144 145 146 147 148
        '''
        target: test create partition, and insert vectors, check status returned
        method: call function: create_partition
        expected: status ok
        '''
X
Xiaohai Xu 已提交
149
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
150 151 152 153
        assert status.OK()
        nq = 100
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
X
Xiaohai Xu 已提交
154
        status, ids = connect.insert(collection, vectors, ids, partition_tag=tag)
Z
zhenwu 已提交
155
        ids = [(i+100) for i in range(nq)]
X
Xiaohai Xu 已提交
156
        status, ids = connect.insert(collection, vectors, ids, partition_tag=tag)
Z
zhenwu 已提交
157
        assert status.OK()
X
Xiaohai Xu 已提交
158
        status = connect.flush([collection])
159
        assert status.OK()
D
del-zhenwu 已提交
160
        status, res = connect.count_entities(collection)
Z
zhenwu 已提交
161 162
        assert res == nq * 2

X
Xiaohai Xu 已提交
163
    def test_create_partition_insert_same_tags_two_collections(self, connect, collection):
Z
zhenwu 已提交
164
        '''
X
Xiaohai Xu 已提交
165
        target: test create two partitions, and insert vectors with the same tag to each collection, check status returned
Z
zhenwu 已提交
166
        method: call function: create_partition
X
Xiaohai Xu 已提交
167
        expected: status ok, collection length is correct
Z
zhenwu 已提交
168
        '''
X
Xiaohai Xu 已提交
169
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
170
        assert status.OK()
X
Xiaohai Xu 已提交
171 172
        collection_new = gen_unique_str()
        param = {'collection_name': collection_new,
173 174 175
            'dimension': dim,
            'index_file_size': index_file_size,
            'metric_type': MetricType.L2}
X
Xiaohai Xu 已提交
176 177
        status = connect.create_collection(param)
        status = connect.create_partition(collection_new, tag)
Z
zhenwu 已提交
178 179 180
        nq = 100
        vectors = gen_vectors(nq, dim)
        ids = [i for i in range(nq)]
X
Xiaohai Xu 已提交
181
        status, ids = connect.insert(collection, vectors, ids, partition_tag=tag)
Z
zhenwu 已提交
182
        ids = [(i+100) for i in range(nq)]
X
Xiaohai Xu 已提交
183 184
        status, ids = connect.insert(collection_new, vectors, ids, partition_tag=tag)
        status = connect.flush([collection, collection_new])
Z
zhenwu 已提交
185
        assert status.OK()
D
del-zhenwu 已提交
186
        status, res = connect.count_entities(collection)
187
        assert res == nq
D
del-zhenwu 已提交
188
        status, res = connect.count_entities(collection_new)
Z
zhenwu 已提交
189 190 191 192 193 194 195
        assert res == nq


class TestShowBase:

    """
    ******************************************************************
D
del-zhenwu 已提交
196
      The following cases are used to test `list_partitions` function 
Z
zhenwu 已提交
197 198
    ******************************************************************
    """
D
del-zhenwu 已提交
199
    def test_list_partitions(self, connect, collection):
Z
zhenwu 已提交
200 201
        '''
        target: test show partitions, check status and partitions returned
D
del-zhenwu 已提交
202
        method: create partition first, then call function: list_partitions
Z
zhenwu 已提交
203 204
        expected: status ok, partition correct
        '''
X
Xiaohai Xu 已提交
205
        status = connect.create_partition(collection, tag)
D
del-zhenwu 已提交
206
        status, res = connect.list_partitions(collection)
Z
zhenwu 已提交
207 208
        assert status.OK()

D
del-zhenwu 已提交
209
    def test_list_partitions_no_partition(self, connect, collection):
Z
zhenwu 已提交
210
        '''
X
Xiaohai Xu 已提交
211
        target: test show partitions with collection name, check status and partitions returned
D
del-zhenwu 已提交
212
        method: call function: list_partitions
Z
zhenwu 已提交
213 214
        expected: status ok, partitions correct
        '''
D
del-zhenwu 已提交
215
        status, res = connect.list_partitions(collection)
Z
zhenwu 已提交
216 217
        assert status.OK()

X
Xiaohai Xu 已提交
218
    def test_show_multi_partitions(self, connect, collection):
Z
zhenwu 已提交
219 220
        '''
        target: test show partitions, check status and partitions returned
D
del-zhenwu 已提交
221
        method: create partitions first, then call function: list_partitions
Z
zhenwu 已提交
222 223
        expected: status ok, partitions correct
        '''
224
        tag_new = gen_unique_str()
X
Xiaohai Xu 已提交
225 226
        status = connect.create_partition(collection, tag)
        status = connect.create_partition(collection, tag_new)
D
del-zhenwu 已提交
227
        status, res = connect.list_partitions(collection)
Z
zhenwu 已提交
228 229 230
        assert status.OK()


D
del-zhenwu 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
class TestHasBase:

    """
    ******************************************************************
      The following cases are used to test `has_partition` function
    ******************************************************************
    """
    @pytest.fixture(
        scope="function",
        params=gen_invalid_collection_names()
    )
    def get_tag_name(self, request):
        yield request.param

    def test_has_partition(self, connect, collection):
        '''
        target: test has_partition, check status and result
        method: create partition first, then call function: has_partition
        expected: status ok, result true
        '''
        status = connect.create_partition(collection, tag)
        status, res = connect.has_partition(collection, tag)
        assert status.OK()
        logging.getLogger().info(res)
        assert res

    def test_has_partition_multi_partitions(self, connect, collection):
        '''
        target: test has_partition, check status and result
        method: create partition first, then call function: has_partition
        expected: status ok, result true
        '''
        for tag_name in [tag, "tag_new", "tag_new_new"]:
            status = connect.create_partition(collection, tag_name)
        for tag_name in [tag, "tag_new", "tag_new_new"]:
            status, res = connect.has_partition(collection, tag_name)
            assert status.OK()
            assert res

    def test_has_partition_tag_not_existed(self, connect, collection):
        '''
        target: test has_partition, check status and result
        method: then call function: has_partition, with tag not existed
        expected: status ok, result empty
        '''
        status, res = connect.has_partition(collection, tag)
        assert status.OK()
        logging.getLogger().info(res)
        assert not res

    def test_has_partition_collection_not_existed(self, connect, collection):
        '''
        target: test has_partition, check status and result
        method: then call function: has_partition, with collection not existed
        expected: status not ok
        '''
        status, res = connect.has_partition("not_existed_collection", tag)
        assert not status.OK()

    @pytest.mark.level(2)
    def test_has_partition_with_invalid_tag_name(self, connect, collection, get_tag_name):
        '''
        target: test has partition, with invalid tag name, check status returned
        method: call function: has_partition
        expected: status ok
        '''
        tag_name = get_tag_name
        status = connect.create_partition(collection, tag)
        status, res = connect.has_partition(collection, tag_name)
        assert status.OK()


Z
zhenwu 已提交
303 304 305 306 307 308 309
class TestDropBase:

    """
    ******************************************************************
      The following cases are used to test `drop_partition` function 
    ******************************************************************
    """
X
Xiaohai Xu 已提交
310
    def test_drop_partition(self, connect, collection):
Z
zhenwu 已提交
311 312 313 314 315
        '''
        target: test drop partition, check status and partition if existed
        method: create partitions first, then call function: drop_partition
        expected: status ok, no partitions in db
        '''
X
Xiaohai Xu 已提交
316 317
        status = connect.create_partition(collection, tag)
        status = connect.drop_partition(collection, tag)
Z
zhenwu 已提交
318
        assert status.OK()
D
del-zhenwu 已提交
319
        status, res = connect.list_partitions(collection)
320 321 322 323
        tag_list = []
        for item in res:
            tag_list.append(item.tag)
        assert tag not in tag_list
Z
zhenwu 已提交
324

X
Xiaohai Xu 已提交
325
    def test_drop_partition_tag_not_existed(self, connect, collection):
Z
zhenwu 已提交
326 327 328 329 330
        '''
        target: test drop partition, but tag not existed
        method: create partitions first, then call function: drop_partition
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
331
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
332
        new_tag = "new_tag"
X
Xiaohai Xu 已提交
333
        status = connect.drop_partition(collection, new_tag)
Z
zhenwu 已提交
334 335
        assert not status.OK()

X
Xiaohai Xu 已提交
336
    def test_drop_partition_tag_not_existed_A(self, connect, collection):
Z
zhenwu 已提交
337
        '''
X
Xiaohai Xu 已提交
338
        target: test drop partition, but collection not existed
Z
zhenwu 已提交
339 340 341
        method: create partitions first, then call function: drop_partition
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
342 343 344
        status = connect.create_partition(collection, tag)
        new_collection = gen_unique_str()
        status = connect.drop_partition(new_collection, tag)
Z
zhenwu 已提交
345 346
        assert not status.OK()

D
del-zhenwu 已提交
347
    @pytest.mark.level(2)
X
Xiaohai Xu 已提交
348
    def test_drop_partition_repeatedly(self, connect, collection):
Z
zhenwu 已提交
349 350 351 352 353
        '''
        target: test drop partition twice, check status and partition if existed
        method: create partitions first, then call function: drop_partition
        expected: status not ok, no partitions in db
        '''
X
Xiaohai Xu 已提交
354 355 356
        status = connect.create_partition(collection, tag)
        status = connect.drop_partition(collection, tag)
        status = connect.drop_partition(collection, tag)
Z
zhenwu 已提交
357 358
        time.sleep(2)
        assert not status.OK()
D
del-zhenwu 已提交
359
        status, res = connect.list_partitions(collection)
360 361 362 363
        tag_list = []
        for item in res:
            tag_list.append(item.tag)
        assert tag not in tag_list
Z
zhenwu 已提交
364

X
Xiaohai Xu 已提交
365
    def test_drop_partition_create(self, connect, collection):
Z
zhenwu 已提交
366 367 368 369 370
        '''
        target: test drop partition, and create again, check status
        method: create partitions first, then call function: drop_partition, create_partition
        expected: status not ok, partition in db
        '''
X
Xiaohai Xu 已提交
371 372
        status = connect.create_partition(collection, tag)
        status = connect.drop_partition(collection, tag)
Z
zhenwu 已提交
373
        time.sleep(2)
X
Xiaohai Xu 已提交
374
        status = connect.create_partition(collection, tag)
Z
zhenwu 已提交
375
        assert status.OK()
D
del-zhenwu 已提交
376
        status, res = connect.list_partitions(collection)
377 378 379 380
        tag_list = []
        for item in res:
            tag_list.append(item.tag)
        assert tag in tag_list
Z
zhenwu 已提交
381 382 383 384 385


class TestNameInvalid(object):
    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
386
        params=gen_invalid_collection_names()
Z
zhenwu 已提交
387 388 389 390 391 392
    )
    def get_tag_name(self, request):
        yield request.param

    @pytest.fixture(
        scope="function",
X
Xiaohai Xu 已提交
393
        params=gen_invalid_collection_names()
Z
zhenwu 已提交
394
    )
X
Xiaohai Xu 已提交
395
    def get_collection_name(self, request):
Z
zhenwu 已提交
396 397
        yield request.param

X
Xiaohai Xu 已提交
398
    def test_drop_partition_with_invalid_collection_name(self, connect, collection, get_collection_name):
Z
zhenwu 已提交
399
        '''
X
Xiaohai Xu 已提交
400
        target: test drop partition, with invalid collection name, check status returned
Z
zhenwu 已提交
401 402 403
        method: call function: drop_partition
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
404 405 406
        collection_name = get_collection_name
        status = connect.create_partition(collection, tag)
        status = connect.drop_partition(collection_name, tag)
Z
zhenwu 已提交
407 408
        assert not status.OK()

X
Xiaohai Xu 已提交
409
    def test_drop_partition_with_invalid_tag_name(self, connect, collection, get_tag_name):
Z
zhenwu 已提交
410 411 412 413 414 415
        '''
        target: test drop partition, with invalid tag name, check status returned
        method: call function: drop_partition
        expected: status not ok
        '''
        tag_name = get_tag_name
X
Xiaohai Xu 已提交
416 417
        status = connect.create_partition(collection, tag)
        status = connect.drop_partition(collection, tag_name)
Z
zhenwu 已提交
418 419
        assert not status.OK()

D
del-zhenwu 已提交
420
    def test_list_partitions_with_invalid_collection_name(self, connect, collection, get_collection_name):
Z
zhenwu 已提交
421
        '''
X
Xiaohai Xu 已提交
422
        target: test show partitions, with invalid collection name, check status returned
D
del-zhenwu 已提交
423
        method: call function: list_partitions
Z
zhenwu 已提交
424 425
        expected: status not ok
        '''
X
Xiaohai Xu 已提交
426 427
        collection_name = get_collection_name
        status = connect.create_partition(collection, tag)
D
del-zhenwu 已提交
428
        status, res = connect.list_partitions(collection_name)
429
        assert not status.OK()