Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Tests for the pydantic utility module.""" |
| 2 | |
| 3 | import pydantic |
| 4 | import pytest |
| 5 | from composio_client import omit |
| 6 | |
| 7 | from composio.utils.pydantic import none_to_omit, parse_pydantic_error |
| 8 | |
| 9 | |
| 10 | class _Model(pydantic.BaseModel): |
| 11 | name: str |
| 12 | age: int |
| 13 | |
| 14 | |
| 15 | def test_none_to_omit_converts_none(): |
| 16 | assert none_to_omit(None) is omit |
| 17 | |
| 18 | |
| 19 | def test_none_to_omit_passes_values_through(): |
| 20 | assert none_to_omit("hello") == "hello" |
| 21 | assert none_to_omit(42) == 42 |
| 22 | # Falsy-but-not-None values are preserved (not converted to omit). |
| 23 | assert none_to_omit(0) == 0 |
| 24 | assert none_to_omit("") == "" |
| 25 | |
| 26 | |
| 27 | def _validation_error(**kwargs) -> pydantic.ValidationError: |
| 28 | with pytest.raises(pydantic.ValidationError) as exc_info: |
| 29 | _Model(**kwargs) |
| 30 | return exc_info.value |
| 31 | |
| 32 | |
| 33 | def test_parse_pydantic_error_lists_missing_fields(): |
| 34 | message = parse_pydantic_error(_validation_error(age=5)) |
| 35 | assert "Invalid request data provided" in message |
| 36 | assert "missing" in message |
| 37 | assert "name" in message |
| 38 | |
| 39 | |
| 40 | def test_parse_pydantic_error_reports_type_errors_with_param(): |
| 41 | message = parse_pydantic_error(_validation_error(name="a", age="not-an-int")) |
| 42 | # A non-missing error names the offending parameter. |
| 43 | assert "age" in message |
| 44 | assert "missing" not in message |
| 45 | |
| 46 | |
| 47 | def test_parse_pydantic_error_handles_missing_and_other_together(): |
| 48 | message = parse_pydantic_error(_validation_error(age="not-an-int")) |
| 49 | assert "name" in message # missing |
| 50 | assert "age" in message # type error |