Passed
Push — master ( fd4969...54df52 )
by
unknown
01:50
created

TestCreateCollectionInvalid.test_create_collection_None()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import pdb
2
import copy
3
import logging
4
import itertools
5
from time import sleep
6
import threading
7
from multiprocessing import Process
8
import sklearn.preprocessing
9
10
import pytest
11
from milvus import IndexType, MetricType
12
from utils import *
13
14
nb = 1
15
dim = 128
16
collection_id = "create_collection"
17
default_segment_size = 1024
18
drop_collection_interval_time = 3
19
segment_size = 10
20
default_fields = gen_default_fields() 
21
entities = gen_entities(nb)
22
23
class TestCreateCollection:
24
25
    """
26
    ******************************************************************
27
      The following cases are used to test `create_collection` function
28
    ******************************************************************
29
    """
30
    @pytest.fixture(
31
        scope="function",
32
        params=gen_single_filter_fields()
33
    )
34
    def get_filter_field(self, request):
35
        yield request.param
36
37
    @pytest.fixture(
38
        scope="function",
39
        params=gen_single_vector_fields()
40
    )
41
    def get_vector_field(self, request):
42
        yield request.param
43
44
    @pytest.fixture(
45
        scope="function",
46
        params=gen_segment_sizes()
47
    )
48
    def get_segment_size(self, request):
49
        yield request.param
50
51
    def test_create_collection_fields(self, connect, get_filter_field, get_vector_field):
52
        '''
53
        target: test create normal collection with different fields
54
        method: create collection with diff fields: metric/field_type/...
55
        expected: no exception raised
56
        '''
57
        filter_field = get_filter_field
58
        logging.getLogger().info(filter_field)
59
        vector_field = get_vector_field
60
        collection_name = gen_unique_str(collection_id)
61
        fields = {
62
                "fields": [filter_field, vector_field],
63
                "segment_size": segment_size
64
        }
65
        logging.getLogger().info(fields)
66
        connect.create_collection(collection_name, fields)
67
        assert connect.has_collection(collection_name)
68
69
    # TODO
70
    def test_create_collection_fields_create_index(self, connect, get_filter_field, get_vector_field):
71
        '''
72
        target: test create normal collection with different fields
73
        method: create collection with diff fields: metric/field_type/...
74
        expected: no exception raised
75
        '''
76
        filter_field = get_filter_field
77
        vector_field = get_vector_field
78
        collection_name = gen_unique_str(collection_id)
79
        fields = {
80
                "fields": [filter_field, vector_field],
81
                "segment_size": segment_size
82
        }
83
        connect.create_collection(collection_name, fields)
84
        assert connect.has_collection(collection_name)
85
        
86
    def test_create_collection_segment_size(self, connect, get_segment_size):
87
        '''
88
        target: test create normal collection with different fields
89
        method: create collection with diff segment_size
90
        expected: no exception raised
91
        '''
92
        collection_name = gen_unique_str(collection_id)
93
        fields = copy.deepcopy(default_fields)
94
        fields["segment_size"] = get_segment_size
95
        connect.create_collection(collection_name, fields)
96
        assert connect.has_collection(collection_name)
97
98
    def test_create_collection_auto_flush_disabled(self, connect):
99
        '''
100
        target: test create normal collection, with large auto_flush_interval
101
        method: create collection with corrent params
102
        expected: create status return ok
103
        '''
104
        disable_flush(connect)
105
        collection_name = gen_unique_str(collection_id)
106
        try:
107
            connect.create_collection(collection_name, default_fields)
108
        finally:
109
            enable_flush(connect)
110
        # pdb.set_trace()
111
112
    def test_create_collection_after_insert(self, connect, collection):
113
        '''
114
        target: test insert vector, then create collection again
115
        method: insert vector and create collection
116
        expected: error raised
117
        '''
118
        # pdb.set_trace()
119
        connect.insert(collection, entities)
120
121
        with pytest.raises(Exception) as e:
122
            connect.create_collection(collection, default_fields)
123
124
    def test_create_collection_after_insert_flush(self, connect, collection):
125
        '''
126
        target: test insert vector, then create collection again
127
        method: insert vector and create collection
128
        expected: error raised
129
        '''
130
        connect.insert(collection, entities)
131
        connect.flush([collection])
132
        with pytest.raises(Exception) as e:
133
            connect.create_collection(collection, default_fields)
134
135
    # TODO: assert exception
136
    @pytest.mark.level(2)
137
    def test_create_collection_without_connection(self, dis_connect):
138
        '''
139
        target: test create collection, without connection
140
        method: create collection with correct params, with a disconnected instance
141
        expected: create raise exception
142
        '''
143
        collection_name = gen_unique_str(collection_id)
144
        with pytest.raises(Exception) as e:
145
            connect.create_collection(collection_name, default_fields)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable connect does not seem to be defined.
Loading history...
146
147
    def test_create_collection_existed(self, connect):
148
        '''
149
        target: test create collection but the collection name have already existed
150
        method: create collection with the same collection_name
151
        expected: create status return not ok
152
        '''
153
        collection_name = gen_unique_str(collection_id)
154
        connect.create_collection(collection_name, default_fields)
155
        with pytest.raises(Exception) as e:
156
            connect.create_collection(collection_name, default_fields)
157
158
    @pytest.mark.level(2)
159
    def test_create_collection_multithread(self, connect):
160
        '''
161
        target: test create collection with multithread
162
        method: create collection using multithread, 
163
        expected: collections are created
164
        '''
165
        threads_num = 8 
166
        threads = []
167
        collection_names = []
168
169
        def create():
170
            collection_name = gen_unique_str(collection_id)
171
            collection_names.append(collection_name)
172
            connect.create_collection(collection_name, default_fields)
173
        for i in range(threads_num):
174
            t = threading.Thread(target=create, args=())
175
            threads.append(t)
176
            t.start()
177
            time.sleep(0.2)
178
        for t in threads:
179
            t.join()
180
        
181
        res = connect.list_collections()
182
        for item in collection_names:
183
            assert item in res
184
185
186
class TestCreateCollectionInvalid(object):
187
    """
188
    Test creating collections with invalid params
189
    """
190
    @pytest.fixture(
191
        scope="function",
192
        params=gen_invalid_metric_types()
193
    )
194
    def get_metric_type(self, request):
195
        yield request.param
196
197
    @pytest.fixture(
198
        scope="function",
199
        params=gen_invalid_ints()
200
    )
201
    def get_segment_size(self, request):
202
        yield request.param
203
204
    @pytest.fixture(
205
        scope="function",
206
        params=gen_invalid_ints()
207
    )
208
    def get_dim(self, request):
209
        yield request.param
210
211
    @pytest.fixture(
212
        scope="function",
213
        params=gen_invalid_strs()
214
    )
215
    def get_invalid_string(self, request):
216
        yield request.param
217
218
    @pytest.fixture(
219
        scope="function",
220
        params=gen_invalid_field_types()
221
    )
222
    def get_field_type(self, request):
223
        yield request.param
224
225
    @pytest.mark.level(2)
226
    def test_create_collection_with_invalid_segment_size(self, connect, get_segment_size):
227
        collection_name = gen_unique_str()
228
        fields = copy.deepcopy(default_fields)
229
        fields["segment_size"] = get_segment_size
230
        with pytest.raises(Exception) as e:
231
            connect.create_collection(collection_name, fields)
232
233
    @pytest.mark.level(2)
234
    def test_create_collection_with_invalid_metric_type(self, connect, get_metric_type):
235
        collection_name = gen_unique_str()
236
        fields = copy.deepcopy(default_fields)
237
        fields["fields"][-1]["params"]["metric_type"] = get_metric_type
238
        with pytest.raises(Exception) as e:
239
            connect.create_collection(collection_name, fields)
240
241
    @pytest.mark.level(2)
242
    def test_create_collection_with_invalid_dimension(self, connect, get_dim):
243
        dimension = get_dim
244
        collection_name = gen_unique_str()
245
        fields = copy.deepcopy(default_fields)
246
        fields["fields"][-1]["params"]["dimension"] = dimension
247
        with pytest.raises(Exception) as e:
248
             connect.create_collection(collection_name, fields)
249
250
    @pytest.mark.level(2)
251
    def test_create_collection_with_invalid_collectionname(self, connect, get_invalid_string):
252
        collection_name = get_invalid_string
253
        with pytest.raises(Exception) as e:
254
            connect.create_collection(collection_name, default_fields)
255
256
    @pytest.mark.level(2)
257
    def test_create_collection_with_empty_collectionname(self, connect):
258
        collection_name = ''
259
        with pytest.raises(Exception) as e:
260
            connect.create_collection(collection_name, default_fields)
261
262
    @pytest.mark.level(2)
263
    def test_create_collection_with_none_collectionname(self, connect):
264
        collection_name = None
265
        with pytest.raises(Exception) as e:
266
            connect.create_collection(collection_name, default_fields)
267
268
    def test_create_collection_None(self, connect):
269
        '''
270
        target: test create collection but the collection name is None
271
        method: create collection, param collection_name is None
272
        expected: create raise error
273
        '''
274
        with pytest.raises(Exception) as e:
275
            connect.create_collection(None, default_fields)
276
277
    def test_create_collection_no_dimension(self, connect):
278
        '''
279
        target: test create collection with no dimension params
280
        method: create collection with corrent params
281
        expected: create status return ok
282
        '''
283
        collection_name = gen_unique_str(collection_id)
284
        fields = copy.deepcopy(default_fields)
285
        fields["fields"][-1]["params"].pop("dimension")
286
        with pytest.raises(Exception) as e:
287
            connect.create_collection(collection_name, fields)
288
289
    def test_create_collection_no_segment_size(self, connect):
290
        '''
291
        target: test create collection with no segment_size params
292
        method: create collection with corrent params
293
        expected: use default default_segment_size
294
        '''
295
        collection_name = gen_unique_str(collection_id)
296
        fields = copy.deepcopy(default_fields)
297
        fields.pop("segment_size")
298
        connect.create_collection(collection_name, fields)
299
        res = connect.get_collection_info(collection_name)
300
        logging.getLogger().info(res)
301
        assert res["segment_size"] == default_segment_size
302
303
    # TODO:
304
    def _test_create_collection_no_metric_type(self, connect):
305
        '''
306
        target: test create collection with no metric_type params
307
        method: create collection with corrent params
308
        expected: use default L2
309
        '''
310
        collection_name = gen_unique_str(collection_id)
311
        fields = copy.deepcopy(default_fields)
312
        fields["fields"][-1]["params"].pop("metric_type")
313
        connect.create_collection(collection_name, fields)
314
        res = connect.get_collection_info(collection_name)
315
        logging.getLogger().info(res)
316
        assert res["metric_type"] == "L2"
317
318
    # TODO: assert exception
319
    def test_create_collection_limit_fields(self, connect):
320
        collection_name = gen_unique_str(collection_id)
321
        limit_num = 64
322
        fields = copy.deepcopy(default_fields)
323
        for i in range(limit_num):
324
            field_name = gen_unique_str("field_name")
325
            field = {"field": field_name, "type": DataType.INT8}
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DataType does not seem to be defined.
Loading history...
326
            fields["fields"].append(field)
327
        with pytest.raises(Exception) as e:
328
            connect.create_collection(collection_name, fields)
329
330
    # TODO: assert exception
331
    def test_create_collection_invalid_field_name(self, connect, get_invalid_string):
332
        collection_name = gen_unique_str(collection_id)
333
        fields = copy.deepcopy(default_fields)
334
        field_name = get_invalid_string
335
        field = {"field": field_name, "type": DataType.INT8}
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DataType does not seem to be defined.
Loading history...
336
        fields["fields"].append(field)
337
        with pytest.raises(Exception) as e:
338
            connect.create_collection(collection_name, fields)
339
340
    # TODO: assert exception
341
    def test_create_collection_invalid_field_type(self, connect, get_field_type):
342
        collection_name = gen_unique_str(collection_id)
343
        fields = copy.deepcopy(default_fields)
344
        field_type = get_field_type
345
        field = {"field": "test_field", "type": field_type}
346
        fields["fields"].append(field)
347
        with pytest.raises(Exception) as e:
348
            connect.create_collection(collection_name, fields)
349