import pandas as pd import pytest from pymilvus import DataType from pymilvus_orm import FieldSchema from base.client_base import TestcaseBase from utils.util_log import test_log as log from common import common_func as cf from common import common_type as ct from common.common_type import CaseLabel, CheckParams prefix = "collection" default_schema = cf.gen_default_collection_schema() default_binary_schema = cf.gen_default_binary_collection_schema() def assert_default_collection(collection, exp_name=None, exp_schema=default_schema, exp_num=0, exp_primary=None): if exp_name: assert collection.name == exp_name assert collection.description == exp_schema.description assert collection.schema == exp_schema if exp_num == 0: assert collection.is_empty assert collection.num_entities == exp_num if exp_primary is None: assert collection.primary_field is None else: assert collection.primary_field == exp_primary class TestCollectionParams(TestcaseBase): """ Test case of collection interface """ ''' def teardown_method(self): if self.collection_wrap is not None and self.collection_wrap.collection is not None: self.collection_wrap.drop() ''' def setup_method(self): pass @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_invalid_type_schema(self, request): if request.param is None: pytest.skip("None schema is valid") yield request.param @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_invalid_type_fields(self, request): if isinstance(request.param, list): pytest.skip("list is valid fields") yield request.param @pytest.fixture(scope="function", params=cf.gen_all_type_fields()) def get_unsupported_primary_field(self, request): if request.param.dtype == DataType.INT64: pytest.skip("int64 type is valid primary key") yield request.param @pytest.fixture(scope="function", params=ct.get_invalid_strs) def get_invalid_dim(self, request): if request.param == 1: request.param = 0 yield request.param @pytest.mark.tags(CaseLabel.L0) def test_collection(self): """ target: test collection with default schema method: create collection with default schema expected: assert collection property """ self._connect() c_name = cf.gen_unique_str(prefix) collection, _ = self.collection_wrap.collection_init(c_name, data=None, schema=default_schema) assert_default_collection(collection, c_name) assert c_name, _ in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L0) def test_collection_empty_name(self): """ target: test collection with empty name method: create collection with a empty name expected: raise exception """ self._connect() c_name = "" ex, check = self.collection_wrap.collection_init(c_name, schema=default_schema, check_res=CheckParams.err_res) assert "value is illegal" in str(ex) @pytest.mark.tags(CaseLabel.L1) def test_collection_invalid_name(self, get_invalid_string): """ target: test collection with invalid name method: create collection with invalid name expected: raise exception """ self._connect() c_name = get_invalid_string ex, check = self.collection_wrap.collection_init(c_name, schema=default_schema, check_res=CheckParams.err_res) assert "invalid" or "illegal" in str(ex) @pytest.mark.tags(CaseLabel.L0) def test_collection_dup_name(self): """ target: test collection with dup name method: create collection with dup name and none schema and data expected: collection properties consistent """ self._connect() collection = self._collection() c_name = collection.name assert_default_collection(collection) dup_collection, _ = self.collection_wrap.collection_init(c_name) assert collection.name == dup_collection.name assert collection.schema == dup_collection.schema assert collection.num_entities == dup_collection.num_entities assert collection.name, _ in self.utility_wrap.list_collections()[0] @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_with_desc(self): """ target: test collection with dup name method: 1. default schema with desc 2. dup name collection expected: desc consistent """ self._connect() schema = cf.gen_default_collection_schema(description=ct.collection_desc) collection = self._collection(schema=schema) assert_default_collection(collection, exp_schema=schema) c_name = collection.name dup_collection, _ = self.collection_wrap.collection_init(c_name) assert_default_collection(dup_collection, c_name, exp_schema=schema) assert collection.description == dup_collection.description @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_new_schema(self): """ target: test collection with dup name and new schema method: 1.create collection with default schema 2. collection with dup name and new schema expected: raise exception """ self._connect() collection = self._collection() c_name = collection.name assert_default_collection(collection) fields = [cf.gen_int64_field()] schema = cf.gen_collection_schema(fields=fields) ex, _ = self.collection_wrap.collection_init(c_name, schema=schema, check_res=CheckParams.err_res) assert "The collection already exist, but the schema isnot the same as the passed in" in str(ex) @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_new_primary(self): """ target: test collection with dup name and new primary_field schema method: 1.collection with default schema 2. collection with same fields and new primary_field schema expected: raise exception """ self._connect() collection = self._collection() c_name = collection.name assert_default_collection(collection) schema = cf.gen_default_collection_schema(primary_field=ct.default_int64_field_name) ex, _ = self.collection_wrap.collection_init(c_name, schema=schema, check_res=CheckParams.err_res) assert "The collection already exist, but the schema isnot the same as the passed in" in str(ex) assert collection.primary_field is None @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_new_dim(self): """ target: test collection with dup name and new dim schema method: 1. default schema 2. schema with new dim expected: raise exception """ self._connect() new_dim = 120 collection = self._collection() c_name = collection.name assert_default_collection(collection) schema = cf.gen_default_collection_schema() new_fields = cf.gen_float_vec_field(dim=new_dim) schema.fields[-1] = new_fields ex, _ = self.collection_wrap.collection_init(c_name, schema=schema, check_res=CheckParams.err_res) assert "The collection already exist, but the schema isnot the same as the passed in" in str(ex) assert collection.primary_field is None @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_invalid_schema_type(self, get_invalid_type_schema): """ target: test collection with dup name and invalid schema method: 1. default schema 2. invalid schema expected: raise exception and """ self._connect() collection = self._collection() c_name = collection.name assert_default_collection(collection) ex, _ = self.collection_wrap.collection_init(c_name, schema=get_invalid_type_schema, check_res=CheckParams.err_res) assert "schema type must be schema.CollectionSchema" in str(ex) assert_default_collection(collection, c_name) @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_same_schema(self): """ target: test collection with dup name and same schema method: dup name and same schema expected: two collection object is available """ self._connect() collection = self._collection(schema=default_schema) c_name = collection.name assert_default_collection(collection) dup_collection, _ = self.collection_wrap.collection_init(c_name, schema=default_schema) assert_default_collection(dup_collection, c_name) @pytest.mark.tags(CaseLabel.L1) @pytest.mark.xfail(reason="issue #5367") def test_collection_dup_name_none_schema_dataframe(self): """ target: test collection with dup name and insert dataframe method: create collection with dup name, none schema, dataframe expected: two collection object is correct """ conn = self._connect() nb = ct.default_nb collection = self._collection() c_name = collection.name assert_default_collection(collection) df = cf.gen_default_dataframe_data(nb) dup_collection, _ = self.collection_wrap.collection_init(c_name, schema=None, data=df) conn.flush([c_name]) assert_default_collection(dup_collection, c_name, exp_num=nb) assert collection.num_entities == nb @pytest.mark.tags(CaseLabel.L1) def test_collection_dup_name_none_schema_data_list(self): """ target: test collection with dup name and insert data (list-like) method: create collection with dup name, none schema, data (list-like) expected: two collection object is correct """ conn = self._connect() nb = ct.default_nb collection = self._collection() c_name = collection.name assert_default_collection(collection) data = cf.gen_default_dataframe_data(nb) dup_collection, _ = self.collection_wrap.collection_init(c_name, schema=None, data=data) conn.flush([c_name]) assert_default_collection(dup_collection, c_name, exp_num=nb) assert collection.num_entities == nb @pytest.mark.tags(CaseLabel.L0) def test_collection_none_schema(self): """ target: test collection with none schema method: create collection with none schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) ex, _ = self.collection_wrap.collection_init(c_name, schema=None, check_res=CheckParams.err_res) assert "Collection missing schema" in str(ex) @pytest.mark.tags(CaseLabel.L0) def test_collection_invalid_type_schema(self, get_invalid_type_schema): """ target: test collection with invalid schema method: create collection with non-CollectionSchema type schema expected: raise exception """ self._connect() c_name = cf.gen_unique_str(prefix) ex, _ = self.collection_wrap.collection_init(c_name, schema=get_invalid_type_schema, check_res=CheckParams.err_res) assert "schema type must be schema.CollectionSchema" in str(ex) @pytest.mark.tags(CaseLabel.L1) def test_collection_invalid_type_fields(self, get_invalid_type_fields): """ target: test collection with invalid fields type, non-list method: create collection schema with non-list invalid fields expected: exception """ self._connect() fields = get_invalid_type_fields with pytest.raises(Exception, match="The fields of schema must be type list"): cf.gen_collection_schema(fields=fields) @pytest.mark.tags(CaseLabel.L1) def test_collection_with_unknown_type(self): """ target: test collection with unknown type method: create with DataType.UNKNOWN expected: raise exception """ self._connect() with pytest.raises(Exception, match="Field type not support