|
7 | 7 | import pytest |
8 | 8 | from pymilvus.client import entity_helper |
9 | 9 | from pymilvus.client.entity_helper import ( |
10 | | - convert_to_str_array, |
11 | | - entity_to_array_arr, |
| 10 | + convert_to_str_array, entity_to_array_arr, |
12 | 11 | entity_to_field_data, |
13 | 12 | entity_to_str_arr, |
14 | 13 | entity_type_to_dtype, |
|
28 | 27 | from pymilvus.grpc_gen import schema_pb2 as schema_types |
29 | 28 | from pymilvus.settings import Config |
30 | 29 | from scipy.sparse import csr_matrix |
| 30 | +from pymilvus.exceptions import DataNotMatchException |
31 | 31 |
|
32 | 32 |
|
33 | 33 | class TestEntityHelperSparse: |
@@ -168,6 +168,83 @@ def test_convert_to_json_dict(self, data: dict): |
168 | 168 | assert isinstance(result, bytes) |
169 | 169 | assert json.loads(result.decode()) == data |
170 | 170 |
|
| 171 | + @pytest.mark.parametrize("json_string,expected", [ |
| 172 | + ('{"key": "value", "number": 42}', {"key": "value", "number": 42}), |
| 173 | + ('{"nested": {"inner": "value"}}', {"nested": {"inner": "value"}}), |
| 174 | + ('[1, 2, 3, "four"]', [1, 2, 3, "four"]), |
| 175 | + ('{"name": "Alice", "age": 30}', {"name": "Alice", "age": 30}), |
| 176 | + ('null', None), |
| 177 | + ('true', True), |
| 178 | + ('false', False), |
| 179 | + ('123', 123), |
| 180 | + ('"simple string"', "simple string"), |
| 181 | + ]) |
| 182 | + def test_convert_to_json_string_valid(self, json_string: str, expected): |
| 183 | + """Test JSON conversion for valid JSON string input""" |
| 184 | + result = entity_helper.convert_to_json(json_string) |
| 185 | + assert isinstance(result, bytes) |
| 186 | + # Verify the result is valid JSON |
| 187 | + parsed = json.loads(result.decode()) |
| 188 | + assert parsed == expected |
| 189 | + |
| 190 | + def test_convert_to_json_from_json_dumps(self): |
| 191 | + """Test JSON conversion from json.dumps() output""" |
| 192 | + original_dict = {"key": "value", "count": 100, "nested": {"inner": "data"}} |
| 193 | + json_string = json.dumps(original_dict) |
| 194 | + |
| 195 | + result = entity_helper.convert_to_json(json_string) |
| 196 | + assert isinstance(result, bytes) |
| 197 | + parsed = json.loads(result.decode()) |
| 198 | + assert parsed == original_dict |
| 199 | + |
| 200 | + @pytest.mark.parametrize("invalid_json_string", [ |
| 201 | + "not a json string", |
| 202 | + '{"invalid": }', |
| 203 | + '{"key": "value"', # missing closing brace |
| 204 | + "{'key': 'value'}", # single quotes not valid in JSON |
| 205 | + "{key: value}", # unquoted keys |
| 206 | + "undefined", |
| 207 | + "{,}", |
| 208 | + ]) |
| 209 | + def test_convert_to_json_string_invalid(self, invalid_json_string: str): |
| 210 | + """Test JSON conversion rejects invalid JSON strings""" |
| 211 | + |
| 212 | + with pytest.raises(DataNotMatchException) as exc_info: |
| 213 | + entity_helper.convert_to_json(invalid_json_string) |
| 214 | + |
| 215 | + # Verify error message contains the invalid JSON string |
| 216 | + error_message = str(exc_info.value) |
| 217 | + assert "Invalid JSON string" in error_message |
| 218 | + # Verify the original input string is in the error message |
| 219 | + assert invalid_json_string in error_message or invalid_json_string[:50] in error_message |
| 220 | + |
| 221 | + def test_convert_to_json_string_with_non_string_keys(self): |
| 222 | + """Test JSON conversion rejects JSON strings with non-string keys in dict""" |
| 223 | + |
| 224 | + # This is actually not possible in standard JSON, as JSON object keys are always strings |
| 225 | + # But we can test that dict validation still works |
| 226 | + invalid_dict = {1: "value", 2: "another"} |
| 227 | + |
| 228 | + with pytest.raises(DataNotMatchException) as exc_info: |
| 229 | + entity_helper.convert_to_json(invalid_dict) |
| 230 | + |
| 231 | + error_message = str(exc_info.value) |
| 232 | + assert "JSON" in error_message |
| 233 | + |
| 234 | + def test_convert_to_json_long_invalid_string_truncated(self): |
| 235 | + """Test that long invalid JSON strings are truncated in error messages""" |
| 236 | + |
| 237 | + # Create a long invalid JSON string |
| 238 | + long_invalid_json = "invalid json " * 50 # > 200 characters |
| 239 | + |
| 240 | + with pytest.raises(DataNotMatchException) as exc_info: |
| 241 | + entity_helper.convert_to_json(long_invalid_json) |
| 242 | + |
| 243 | + error_message = str(exc_info.value) |
| 244 | + assert "Invalid JSON string" in error_message |
| 245 | + # Should contain truncated version with "..." |
| 246 | + assert "..." in error_message |
| 247 | + |
171 | 248 | def test_pack_field_value_to_field_data(self): |
172 | 249 | """Test packing field values into field data protobuf""" |
173 | 250 | # Test with scalar field |
|
0 commit comments