|
13 | 13 | # limitations under the License. |
14 | 14 | # |
15 | 15 |
|
| 16 | +import enum |
16 | 17 | import logging |
| 18 | +import warnings |
| 19 | + |
17 | 20 | import pydantic |
18 | 21 |
|
19 | 22 | from ... import types |
@@ -275,6 +278,70 @@ def test_union_like_type_conversion_suite2(): |
275 | 278 | assert actual_vertex_ai_schema == expected_schema |
276 | 279 |
|
277 | 280 |
|
| 281 | +def test_integer_enum_conversion(): |
| 282 | + """Test that numeric JSONSchema.enum values become strings in Schema.enum.""" |
| 283 | + json_schema = types.JSONSchema(type='integer', enum=[1, 5, 10]) |
| 284 | + gemini_api_schema = types.Schema.from_json_schema(json_schema=json_schema) |
| 285 | + vertex_ai_schema = types.Schema.from_json_schema( |
| 286 | + json_schema=json_schema, api_option='VERTEX_AI' |
| 287 | + ) |
| 288 | + expected_schema = types.Schema(type='INTEGER', enum=['1', '5', '10']) |
| 289 | + |
| 290 | + assert gemini_api_schema == expected_schema |
| 291 | + assert vertex_ai_schema == expected_schema |
| 292 | + with warnings.catch_warnings(): |
| 293 | + warnings.simplefilter('error') |
| 294 | + gemini_api_schema.model_dump(mode='json') |
| 295 | + |
| 296 | + |
| 297 | +def test_number_enum_conversion(): |
| 298 | + """Test that float and int enum values of a NUMBER schema become strings.""" |
| 299 | + json_schema = types.JSONSchema(type='number', enum=[1.5, 2]) |
| 300 | + schema = types.Schema.from_json_schema(json_schema=json_schema) |
| 301 | + |
| 302 | + assert schema == types.Schema(type='NUMBER', enum=['1.5', '2']) |
| 303 | + |
| 304 | + |
| 305 | +def test_nullable_integer_enum_conversion(): |
| 306 | + """Test that a nullable integer enum keeps nullable and gets string values.""" |
| 307 | + json_schema = types.JSONSchema(type=['integer', 'null'], enum=[1, 2]) |
| 308 | + schema = types.Schema.from_json_schema(json_schema=json_schema) |
| 309 | + |
| 310 | + assert schema == types.Schema(type='INTEGER', nullable=True, enum=['1', '2']) |
| 311 | + |
| 312 | + |
| 313 | +def test_union_like_type_integer_enum_conversion(): |
| 314 | + """Test that the enum copied into an any_of sub-schema becomes strings.""" |
| 315 | + json_schema = types.JSONSchema( |
| 316 | + type=['integer', 'array'], |
| 317 | + enum=[1, 2], |
| 318 | + items=types.JSONSchema(type='integer'), |
| 319 | + ) |
| 320 | + schema = types.Schema.from_json_schema(json_schema=json_schema) |
| 321 | + |
| 322 | + assert schema == types.Schema( |
| 323 | + any_of=[ |
| 324 | + types.Schema(type='INTEGER', enum=['1', '2']), |
| 325 | + types.Schema(type='ARRAY', items=types.Schema(type='INTEGER')), |
| 326 | + ] |
| 327 | + ) |
| 328 | + |
| 329 | + |
| 330 | +def test_int_enum_member_enum_conversion(): |
| 331 | + """Test that IntEnum members convert to their value, not their name.""" |
| 332 | + |
| 333 | + class DaysEnum(enum.IntEnum): |
| 334 | + ONE = 1 |
| 335 | + FIVE = 5 |
| 336 | + |
| 337 | + json_schema = types.JSONSchema( |
| 338 | + type='integer', enum=[DaysEnum.ONE, DaysEnum.FIVE] |
| 339 | + ) |
| 340 | + schema = types.Schema.from_json_schema(json_schema=json_schema) |
| 341 | + |
| 342 | + assert schema == types.Schema(type='INTEGER', enum=['1', '5']) |
| 343 | + |
| 344 | + |
278 | 345 | def test_array_type_conversion(): |
279 | 346 | """Test conversion of JSONSchema.items to Schema.items""" |
280 | 347 | json_schema = types.JSONSchema( |
|
0 commit comments