Total Complexity | 71 |
Total Lines | 457 |
Duplicated Lines | 10.07 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like test_insert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import logging |
||
2 | import time |
||
3 | import pdb |
||
4 | import copy |
||
5 | import threading |
||
6 | from multiprocessing import Pool, Process |
||
7 | import pytest |
||
8 | from milvus import DataType |
||
9 | from utils import * |
||
10 | from constants import * |
||
11 | |||
12 | ADD_TIMEOUT = 60 |
||
13 | uid = "test_insert" |
||
14 | field_name = default_float_vec_field_name |
||
|
|||
15 | binary_field_name = default_binary_vec_field_name |
||
16 | default_single_query = { |
||
17 | "bool": { |
||
18 | "must": [ |
||
19 | {"vector": {field_name: {"topk": 10, "query": gen_vectors(1, default_dim), "metric_type": "L2", |
||
20 | "params": {"nprobe": 10}}}} |
||
21 | ] |
||
22 | } |
||
23 | } |
||
24 | |||
25 | |||
26 | class TestInsertBase: |
||
27 | """ |
||
28 | ****************************************************************** |
||
29 | The following cases are used to test `insert` function |
||
30 | ****************************************************************** |
||
31 | """ |
||
32 | |||
33 | @pytest.fixture( |
||
34 | scope="function", |
||
35 | params=gen_simple_index() |
||
36 | ) |
||
37 | def get_simple_index(self, request, connect): |
||
38 | if str(connect._cmd("mode")) == "CPU": |
||
39 | if request.param["index_type"] in index_cpu_not_support(): |
||
40 | pytest.skip("CPU not support index_type: ivf_sq8h") |
||
41 | return request.param |
||
42 | |||
43 | @pytest.fixture( |
||
44 | scope="function", |
||
45 | params=gen_single_filter_fields() |
||
46 | ) |
||
47 | def get_filter_field(self, request): |
||
48 | yield request.param |
||
49 | |||
50 | @pytest.fixture( |
||
51 | scope="function", |
||
52 | params=gen_single_vector_fields() |
||
53 | ) |
||
54 | def get_vector_field(self, request): |
||
55 | yield request.param |
||
56 | |||
57 | def test_add_vector_with_empty_vector(self, connect, collection): |
||
58 | ''' |
||
59 | target: test add vectors with empty vectors list |
||
60 | method: set empty vectors list as add method params |
||
61 | expected: raises a Exception |
||
62 | ''' |
||
63 | vector = [] |
||
64 | with pytest.raises(Exception) as e: |
||
65 | status, ids = connect.insert(collection, vector) |
||
66 | |||
67 | def test_add_vector_with_None(self, connect, collection): |
||
68 | ''' |
||
69 | target: test add vectors with None |
||
70 | method: set None as add method params |
||
71 | expected: raises a Exception |
||
72 | ''' |
||
73 | vector = None |
||
74 | with pytest.raises(Exception) as e: |
||
75 | status, ids = connect.insert(collection, vector) |
||
76 | |||
77 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
78 | def test_insert_collection_not_existed(self, connect): |
||
79 | ''' |
||
80 | target: test insert, with collection not existed |
||
81 | method: insert entity into a random named collection |
||
82 | expected: error raised |
||
83 | ''' |
||
84 | collection_name = gen_unique_str(uid) |
||
85 | with pytest.raises(Exception) as e: |
||
86 | connect.insert(collection_name, default_entities_rows) |
||
87 | |||
88 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
89 | def test_insert_drop_collection(self, connect, collection): |
||
90 | ''' |
||
91 | target: test delete collection after insert vector |
||
92 | method: insert vector and delete collection |
||
93 | expected: no error raised |
||
94 | ''' |
||
95 | ids = connect.insert(collection, default_entity_row) |
||
96 | assert len(ids) == 1 |
||
97 | connect.drop_collection(collection) |
||
98 | |||
99 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
100 | def test_insert_sleep_drop_collection(self, connect, collection): |
||
101 | ''' |
||
102 | target: test delete collection after insert vector for a while |
||
103 | method: insert vector, sleep, and delete collection |
||
104 | expected: no error raised |
||
105 | ''' |
||
106 | ids = connect.insert(collection, default_entity_row) |
||
107 | assert len(ids) == 1 |
||
108 | connect.flush([collection]) |
||
109 | connect.drop_collection(collection) |
||
110 | |||
111 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
112 | def test_insert_create_index(self, connect, collection, get_simple_index): |
||
113 | ''' |
||
114 | target: test build index insert after vector |
||
115 | method: insert vector and build index |
||
116 | expected: no error raised |
||
117 | ''' |
||
118 | ids = connect.insert(collection, default_entities_rows) |
||
119 | assert len(ids) == default_nb |
||
120 | connect.flush([collection]) |
||
121 | connect.create_index(collection, field_name, get_simple_index) |
||
122 | info = connect.get_collection_info(collection) |
||
123 | fields = info["fields"] |
||
124 | for field in fields: |
||
125 | if field["name"] == field_name: |
||
126 | assert field["indexes"][0] == get_simple_index |
||
127 | |||
128 | View Code Duplication | @pytest.mark.timeout(ADD_TIMEOUT) |
|
129 | def test_insert_after_create_index(self, connect, collection, get_simple_index): |
||
130 | ''' |
||
131 | target: test build index insert after vector |
||
132 | method: insert vector and build index |
||
133 | expected: no error raised |
||
134 | ''' |
||
135 | connect.create_index(collection, field_name, get_simple_index) |
||
136 | ids = connect.insert(collection, default_entities_rows) |
||
137 | assert len(ids) == default_nb |
||
138 | info = connect.get_collection_info(collection) |
||
139 | fields = info["fields"] |
||
140 | for field in fields: |
||
141 | if field["name"] == field_name: |
||
142 | assert field["indexes"][0] == get_simple_index |
||
143 | |||
144 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
145 | def test_insert_search(self, connect, collection): |
||
146 | ''' |
||
147 | target: test search vector after insert vector after a while |
||
148 | method: insert vector, sleep, and search collection |
||
149 | expected: no error raised |
||
150 | ''' |
||
151 | ids = connect.insert(collection, default_entities_rows) |
||
152 | connect.flush([collection]) |
||
153 | res = connect.search(collection, default_single_query) |
||
154 | logging.getLogger().debug(res) |
||
155 | assert res |
||
156 | |||
157 | def test_insert_segment_row_count(self, connect, collection): |
||
158 | nb = default_segment_row_limit + 1 |
||
159 | res_ids = connect.insert(collection, gen_entities_rows(nb)) |
||
160 | connect.flush([collection]) |
||
161 | assert len(res_ids) == nb |
||
162 | stats = connect.get_collection_stats(collection) |
||
163 | assert len(stats['partitions'][0]['segments']) == 2 |
||
164 | for segment in stats['partitions'][0]['segments']: |
||
165 | assert segment['row_count'] in [default_segment_row_limit, 1] |
||
166 | |||
167 | @pytest.fixture( |
||
168 | scope="function", |
||
169 | params=[ |
||
170 | 1, |
||
171 | 2000 |
||
172 | ], |
||
173 | ) |
||
174 | def insert_count(self, request): |
||
175 | yield request.param |
||
176 | |||
177 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
178 | def test_insert_ids_not_match(self, connect, id_collection, insert_count): |
||
179 | ''' |
||
180 | target: test insert vectors in collection, use customize ids |
||
181 | method: create collection and insert vectors in it, check the ids returned and the collection length after vectors inserted |
||
182 | expected: the length of ids and the collection row count |
||
183 | ''' |
||
184 | nb = insert_count |
||
185 | with pytest.raises(Exception) as e: |
||
186 | res_ids = connect.insert(id_collection, gen_entities_rows(nb)) |
||
187 | |||
188 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
189 | def test_insert_twice_ids_no_ids(self, connect, collection): |
||
190 | ''' |
||
191 | target: check the result of insert, with params ids and no ids |
||
192 | method: test insert vectors twice, use customize ids first, and then use no ids |
||
193 | expected: error raised |
||
194 | ''' |
||
195 | with pytest.raises(Exception) as e: |
||
196 | res_ids = connect.insert(collection, gen_entities_rows(default_nb, _id=False)) |
||
197 | |||
198 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
199 | def test_insert_tag(self, connect, collection): |
||
200 | ''' |
||
201 | target: test insert entities in collection created before |
||
202 | method: create collection and insert entities in it, with the partition_tag param |
||
203 | expected: the collection row count equals to nq |
||
204 | ''' |
||
205 | connect.create_partition(collection, default_tag) |
||
206 | ids = connect.insert(collection, default_entities_rows, partition_tag=default_tag) |
||
207 | assert len(ids) == default_nb |
||
208 | assert connect.has_partition(collection, default_tag) |
||
209 | |||
210 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
211 | def test_insert_tag_with_ids(self, connect, id_collection): |
||
212 | ''' |
||
213 | target: test insert entities in collection created before, insert with ids |
||
214 | method: create collection and insert entities in it, with the partition_tag param |
||
215 | expected: the collection row count equals to nq |
||
216 | ''' |
||
217 | connect.create_partition(id_collection, default_tag) |
||
218 | ids = [i for i in range(default_nb)] |
||
219 | res_ids = connect.insert(id_collection, gen_entities_rows(default_nb, _id=False), partition_tag=default_tag) |
||
220 | assert res_ids == ids |
||
221 | |||
222 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
223 | def test_insert_tag_not_existed(self, connect, collection): |
||
224 | ''' |
||
225 | target: test insert entities in collection created before |
||
226 | method: create collection and insert entities in it, with the not existed partition_tag param |
||
227 | expected: error raised |
||
228 | ''' |
||
229 | tag = gen_unique_str() |
||
230 | with pytest.raises(Exception) as e: |
||
231 | ids = connect.insert(collection, default_entities_rows, partition_tag=tag) |
||
232 | |||
233 | @pytest.mark.timeout(ADD_TIMEOUT) |
||
234 | def test_insert_tag_existed(self, connect, collection): |
||
235 | ''' |
||
236 | target: test insert entities in collection created before |
||
237 | method: create collection and insert entities in it repeatly, with the partition_tag param |
||
238 | expected: the collection row count equals to nq |
||
239 | ''' |
||
240 | connect.create_partition(collection, default_tag) |
||
241 | ids = connect.insert(collection, default_entities_rows, partition_tag=default_tag) |
||
242 | ids = connect.insert(collection, default_entities_rows, partition_tag=default_tag) |
||
243 | connect.flush([collection]) |
||
244 | res_count = connect.count_entities(collection) |
||
245 | assert res_count == 2 * default_nb |
||
246 | |||
247 | @pytest.mark.level(2) |
||
248 | def test_insert_collection_not_existed(self, connect): |
||
249 | ''' |
||
250 | target: test insert entities in collection, which not existed before |
||
251 | method: insert entities collection not existed, check the status |
||
252 | expected: error raised |
||
253 | ''' |
||
254 | with pytest.raises(Exception) as e: |
||
255 | ids = connect.insert(gen_unique_str("not_exist_collection"), default_entities_rows) |
||
256 | |||
257 | def test_insert_dim_not_matched(self, connect, collection): |
||
258 | ''' |
||
259 | target: test insert entities, the vector dimension is not equal to the collection dimension |
||
260 | method: the entities dimension is half of the collection dimension, check the status |
||
261 | expected: error raised |
||
262 | ''' |
||
263 | vectors = gen_vectors(default_nb, int(default_dim) // 2) |
||
264 | insert_entities = copy.deepcopy(default_entities_rows) |
||
265 | insert_entities[-1][default_float_vec_field_name] = vectors |
||
266 | with pytest.raises(Exception) as e: |
||
267 | ids = connect.insert(collection, insert_entities) |
||
268 | |||
269 | |||
270 | class TestInsertBinary: |
||
271 | @pytest.fixture( |
||
272 | scope="function", |
||
273 | params=gen_binary_index() |
||
274 | ) |
||
275 | def get_binary_index(self, request): |
||
276 | request.param["metric_type"] = "JACCARD" |
||
277 | return request.param |
||
278 | |||
279 | def test_insert_binary_entities(self, connect, binary_collection): |
||
280 | ''' |
||
281 | target: test insert entities in binary collection |
||
282 | method: create collection and insert binary entities in it |
||
283 | expected: the collection row count equals to nb |
||
284 | ''' |
||
285 | ids = connect.insert(binary_collection, default_binary_entities_rows) |
||
286 | assert len(ids) == default_nb |
||
287 | connect.flush() |
||
288 | assert connect.count_entities(binary_collection) == default_nb |
||
289 | |||
290 | def test_insert_binary_tag(self, connect, binary_collection): |
||
291 | ''' |
||
292 | target: test insert entities and create partition tag |
||
293 | method: create collection and insert binary entities in it, with the partition_tag param |
||
294 | expected: the collection row count equals to nb |
||
295 | ''' |
||
296 | connect.create_partition(binary_collection, default_tag) |
||
297 | ids = connect.insert(binary_collection, default_binary_entities_rows, partition_tag=default_tag) |
||
298 | assert len(ids) == default_nb |
||
299 | assert connect.has_partition(binary_collection, default_tag) |
||
300 | |||
301 | # TODO |
||
302 | @pytest.mark.level(2) |
||
303 | def test_insert_binary_multi_times(self, connect, binary_collection): |
||
304 | ''' |
||
305 | target: test insert entities multi times and final flush |
||
306 | method: create collection and insert binary entity multi and final flush |
||
307 | expected: the collection row count equals to nb |
||
308 | ''' |
||
309 | for i in range(default_nb): |
||
310 | ids = connect.insert(binary_collection, default_binary_entity_row) |
||
311 | assert len(ids) == 1 |
||
312 | connect.flush([binary_collection]) |
||
313 | assert connect.count_entities(binary_collection) == default_nb |
||
314 | |||
315 | View Code Duplication | def test_insert_binary_after_create_index(self, connect, binary_collection, get_binary_index): |
|
316 | ''' |
||
317 | target: test insert binary entities after build index |
||
318 | method: build index and insert entities |
||
319 | expected: no error raised |
||
320 | ''' |
||
321 | connect.create_index(binary_collection, binary_field_name, get_binary_index) |
||
322 | ids = connect.insert(binary_collection, default_binary_entities_rows) |
||
323 | assert len(ids) == default_nb |
||
324 | connect.flush([binary_collection]) |
||
325 | info = connect.get_collection_info(binary_collection) |
||
326 | fields = info["fields"] |
||
327 | for field in fields: |
||
328 | if field["name"] == binary_field_name: |
||
329 | assert field["indexes"][0] == get_binary_index |
||
330 | |||
331 | View Code Duplication | @pytest.mark.timeout(ADD_TIMEOUT) |
|
332 | def test_insert_binary_create_index(self, connect, binary_collection, get_binary_index): |
||
333 | ''' |
||
334 | target: test build index insert after vector |
||
335 | method: insert vector and build index |
||
336 | expected: no error raised |
||
337 | ''' |
||
338 | ids = connect.insert(binary_collection, default_binary_entities_rows) |
||
339 | assert len(ids) == default_nb |
||
340 | connect.flush([binary_collection]) |
||
341 | connect.create_index(binary_collection, binary_field_name, get_binary_index) |
||
342 | info = connect.get_collection_info(binary_collection) |
||
343 | fields = info["fields"] |
||
344 | for field in fields: |
||
345 | if field["name"] == binary_field_name: |
||
346 | assert field["indexes"][0] == get_binary_index |
||
347 | |||
348 | |||
349 | class TestInsertInvalid(object): |
||
350 | """ |
||
351 | Test inserting vectors with invalid collection names |
||
352 | """ |
||
353 | |||
354 | @pytest.fixture( |
||
355 | scope="function", |
||
356 | params=gen_invalid_strs() |
||
357 | ) |
||
358 | def get_collection_name(self, request): |
||
359 | yield request.param |
||
360 | |||
361 | @pytest.fixture( |
||
362 | scope="function", |
||
363 | params=gen_invalid_strs() |
||
364 | ) |
||
365 | def get_tag_name(self, request): |
||
366 | yield request.param |
||
367 | |||
368 | @pytest.fixture( |
||
369 | scope="function", |
||
370 | params=gen_invalid_strs() |
||
371 | ) |
||
372 | def get_field_name(self, request): |
||
373 | yield request.param |
||
374 | |||
375 | @pytest.fixture( |
||
376 | scope="function", |
||
377 | params=gen_invalid_strs() |
||
378 | ) |
||
379 | def get_field_type(self, request): |
||
380 | yield request.param |
||
381 | |||
382 | @pytest.fixture( |
||
383 | scope="function", |
||
384 | params=gen_invalid_strs() |
||
385 | ) |
||
386 | def get_field_int_value(self, request): |
||
387 | yield request.param |
||
388 | |||
389 | @pytest.fixture( |
||
390 | scope="function", |
||
391 | params=gen_invalid_ints() |
||
392 | ) |
||
393 | def get_entity_id(self, request): |
||
394 | yield request.param |
||
395 | |||
396 | @pytest.fixture( |
||
397 | scope="function", |
||
398 | params=gen_invalid_vectors() |
||
399 | ) |
||
400 | def get_field_vectors_value(self, request): |
||
401 | yield request.param |
||
402 | |||
403 | def test_insert_field_name_not_match(self, connect, collection): |
||
404 | ''' |
||
405 | target: test insert, with field name not matched |
||
406 | method: create collection and insert entities in it |
||
407 | expected: raise an exception |
||
408 | ''' |
||
409 | tmp_entity = copy.deepcopy(default_entity_row) |
||
410 | tmp_entity[0]["string"] = "string" |
||
411 | with pytest.raises(Exception): |
||
412 | connect.insert(collection, tmp_entity) |
||
413 | |||
414 | def test_insert_with_invalid_collection_name(self, connect, get_collection_name): |
||
415 | collection_name = get_collection_name |
||
416 | with pytest.raises(Exception): |
||
417 | connect.insert(collection_name, default_entity_row) |
||
418 | |||
419 | def test_insert_with_invalid_tag_name(self, connect, collection, get_tag_name): |
||
420 | tag_name = get_tag_name |
||
421 | connect.create_partition(collection, default_tag) |
||
422 | if tag_name is not None: |
||
423 | with pytest.raises(Exception): |
||
424 | connect.insert(collection, default_entity_row, partition_tag=tag_name) |
||
425 | else: |
||
426 | connect.insert(collection, default_entity_row, partition_tag=tag_name) |
||
427 | |||
428 | def test_insert_with_less_field(self, connect, collection): |
||
429 | tmp_entity = copy.deepcopy(default_entity_row) |
||
430 | tmp_entity[0].pop(default_float_vec_field_name) |
||
431 | with pytest.raises(Exception): |
||
432 | connect.insert(collection, tmp_entity) |
||
433 | |||
434 | def test_insert_with_less_field_id(self, connect, id_collection): |
||
435 | tmp_entity = copy.deepcopy(gen_entities_rows(default_nb, _id=False)) |
||
436 | tmp_entity[0].pop("_id") |
||
437 | with pytest.raises(Exception): |
||
438 | connect.insert(id_collection, tmp_entity) |
||
439 | |||
440 | def test_insert_with_more_field(self, connect, collection): |
||
441 | tmp_entity = copy.deepcopy(default_entity_row) |
||
442 | tmp_entity[0]["new_field"] = 1 |
||
443 | with pytest.raises(Exception): |
||
444 | connect.insert(collection, tmp_entity) |
||
445 | |||
446 | def test_insert_with_more_field_id(self, connect, collection): |
||
447 | tmp_entity = copy.deepcopy(default_entity_row) |
||
448 | tmp_entity[0]["_id"] = 1 |
||
449 | with pytest.raises(Exception): |
||
450 | connect.insert(collection, tmp_entity) |
||
451 | |||
452 | def test_insert_with_invalid_field_vector_value(self, connect, collection, get_field_vectors_value): |
||
453 | tmp_entity = copy.deepcopy(default_entity_row) |
||
454 | tmp_entity[0][default_float_vec_field_name][1] = get_field_vectors_value |
||
455 | with pytest.raises(Exception): |
||
456 | connect.insert(collection, tmp_entity) |
||
457 |