from typing import Any import pytest from pdp.fastapi.schemas.identity import Role @pytest.mark.parametrize( "payload, expect_exception, expected", [ pytest.param({}, True, None, id="empty dict is not allowed"), pytest.param("This is not a dict", True, None, id="must be a dict"), pytest.param( {"shorthand": "hello"}, True, {"cursor": None, "shorthand": "hello"}, ), pytest.param( {"role": "one-and-only"}, False, {"role": "one-and-only"}, id="role is required field", ), pytest.param( {" role ": " one-and-only "}, False, {"role": "one-and-only"}, id="whitespace is removed", ), pytest.param( {" role ": " one-and-only ", "other": "field"}, False, {"role": "one-and-only", "other": "field"}, id="additional fields are supported", ), pytest.param( {" role ": "content_viewer", " content_type ": ["field "]}, False, {"role": "content_viewer", "content_type": ["field "]}, id="complicated dict values are not transformed (whitespace left alone)", ), ], ) def test_role_schema( payload: Any, expect_exception: bool, expected: dict[str, Any] | None, ) -> None: """Test Role schema / model validate.""" if expect_exception: with pytest.raises(Exception): Role.model_validate(payload) else: actual = Role.model_validate(payload) assert expected, "Expected should not be None" assert actual.model_dump() == expected