From a6530a7e7806ded7ac37857f43985d11e6b2c9d5 Mon Sep 17 00:00:00 2001 From: Elijah Date: Fri, 25 Oct 2024 16:35:21 +0000 Subject: [PATCH] Fix exception message plus unit tests --- reflex/utils/exceptions.py | 2 +- tests/units/components/test_props.py | 57 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/units/components/test_props.py diff --git a/reflex/utils/exceptions.py b/reflex/utils/exceptions.py index a9788aa9a..6d4a0bbc7 100644 --- a/reflex/utils/exceptions.py +++ b/reflex/utils/exceptions.py @@ -145,5 +145,5 @@ class DynamicComponentInvalidSignature(ReflexError, TypeError): """Raised when a dynamic component has an invalid signature.""" -class InvalidPropValueError(ReflexError, ValueError): +class InvalidPropValueError(ReflexError): """Raised when a prop value is invalid.""" diff --git a/tests/units/components/test_props.py b/tests/units/components/test_props.py new file mode 100644 index 000000000..15ae5bcbe --- /dev/null +++ b/tests/units/components/test_props.py @@ -0,0 +1,57 @@ +import pytest +from reflex.utils.exceptions import InvalidPropValueError +from reflex.components.props import NoExtrasAllowedProps + + +class PropA(NoExtrasAllowedProps): + """Base prop class.""" + + foo: str + bar: str + + +class PropB(NoExtrasAllowedProps): + """Prop class with nested props.""" + + foobar: str + foobaz: PropA + + +@pytest.mark.parametrize( + "props_class, kwargs, should_raise", + [ + (PropA, {"foo": "value", "bar": "another_value"}, False), + (PropA, {"fooz": "value", "bar": "another_value"}, True), + ( + PropB, + { + "foobaz": {"foo": "value", "bar": "another_value"}, + "foobar": "foo_bar_value", + }, + False, + ), + ( + PropB, + { + "fooba": {"foo": "value", "bar": "another_value"}, + "foobar": "foo_bar_value", + }, + True, + ), + ( + PropB, + { + "foobaz": {"foobar": "value", "bar": "another_value"}, + "foobar": "foo_bar_value", + }, + True, + ), + ], +) +def test_no_extras_allowed_props(props_class, kwargs, should_raise): + if should_raise: + with pytest.raises(InvalidPropValueError): + props_class(**kwargs) + else: + props_instance = props_class(**kwargs) + assert isinstance(props_instance, props_class)