|
8 | 8 | from pymilvus import FieldSchema, Function, FunctionType, StructFieldSchema |
9 | 9 | from pymilvus.client.cache import GlobalCache |
10 | 10 | from pymilvus.client.types import DataType |
11 | | -from pymilvus.exceptions import DescribeCollectionException |
| 11 | +from pymilvus.exceptions import DescribeCollectionException, MilvusException |
12 | 12 | from pymilvus.grpc_gen import common_pb2, schema_pb2 |
13 | 13 |
|
14 | 14 | from .conftest import ( |
@@ -243,6 +243,50 @@ def test_add_collection_field_falls_back_for_older_server(self, handler): |
243 | 243 | assert GlobalCache.schema.get(handler.server_address, "", "coll") is None |
244 | 244 | GlobalCache._reset_for_testing() |
245 | 245 |
|
| 246 | + def test_add_collection_field_falls_back_for_external_collection(self, handler): |
| 247 | + response = MagicMock() |
| 248 | + response.alter_status = common_pb2.Status( |
| 249 | + code=1100, |
| 250 | + error_code=common_pb2.IllegalArgument, |
| 251 | + reason=( |
| 252 | + "alter collection schema operation is not supported for external collection coll: " |
| 253 | + "invalid parameter" |
| 254 | + ), |
| 255 | + ) |
| 256 | + handler._stub.AlterCollectionSchema.return_value = response |
| 257 | + handler._stub.AddCollectionField.return_value = make_status() |
| 258 | + field = FieldSchema( |
| 259 | + name="new_field", |
| 260 | + dtype=DataType.VARCHAR, |
| 261 | + max_length=256, |
| 262 | + external_field="source_field", |
| 263 | + ) |
| 264 | + |
| 265 | + handler.add_collection_field("coll", field) |
| 266 | + |
| 267 | + handler._stub.AlterCollectionSchema.assert_called_once() |
| 268 | + handler._stub.AddCollectionField.assert_called_once() |
| 269 | + request = handler._stub.AddCollectionField.call_args.args[0] |
| 270 | + field_schema = schema_pb2.FieldSchema() |
| 271 | + field_schema.ParseFromString(request.schema) |
| 272 | + assert field_schema.name == "new_field" |
| 273 | + assert field_schema.external_field == "source_field" |
| 274 | + |
| 275 | + def test_add_collection_field_does_not_fallback_for_other_business_errors(self, handler): |
| 276 | + response = MagicMock() |
| 277 | + response.alter_status = common_pb2.Status( |
| 278 | + code=1100, |
| 279 | + error_code=common_pb2.IllegalArgument, |
| 280 | + reason="invalid add field request", |
| 281 | + ) |
| 282 | + handler._stub.AlterCollectionSchema.return_value = response |
| 283 | + field = FieldSchema(name="new_field", dtype=DataType.VARCHAR, max_length=256) |
| 284 | + |
| 285 | + with pytest.raises(MilvusException, match="invalid add field request"): |
| 286 | + handler.add_collection_field("coll", field) |
| 287 | + |
| 288 | + handler._stub.AddCollectionField.assert_not_called() |
| 289 | + |
246 | 290 | def test_add_collection_struct_field(self, handler): |
247 | 291 | handler._stub.AddCollectionStructField.return_value = make_status() |
248 | 292 | struct_field = StructFieldSchema(nullable=True) |
|
0 commit comments