test_props.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pytest
  2. from pydantic.v1 import ValidationError
  3. from reflex.components.props import NoExtrasAllowedProps
  4. from reflex.utils.exceptions import InvalidPropValueError
  5. class PropA(NoExtrasAllowedProps):
  6. """Base prop class."""
  7. foo: str
  8. bar: str
  9. class PropB(NoExtrasAllowedProps):
  10. """Prop class with nested props."""
  11. foobar: str
  12. foobaz: PropA
  13. @pytest.mark.parametrize(
  14. "props_class, kwargs, should_raise",
  15. [
  16. (PropA, {"foo": "value", "bar": "another_value"}, False),
  17. (PropA, {"fooz": "value", "bar": "another_value"}, True),
  18. (
  19. PropB,
  20. {
  21. "foobaz": {"foo": "value", "bar": "another_value"},
  22. "foobar": "foo_bar_value",
  23. },
  24. False,
  25. ),
  26. (
  27. PropB,
  28. {
  29. "fooba": {"foo": "value", "bar": "another_value"},
  30. "foobar": "foo_bar_value",
  31. },
  32. True,
  33. ),
  34. (
  35. PropB,
  36. {
  37. "foobaz": {"foobar": "value", "bar": "another_value"},
  38. "foobar": "foo_bar_value",
  39. },
  40. True,
  41. ),
  42. ],
  43. )
  44. def test_no_extras_allowed_props(props_class, kwargs, should_raise):
  45. if should_raise:
  46. with pytest.raises((ValidationError, InvalidPropValueError)):
  47. props_class(**kwargs)
  48. else:
  49. props_instance = props_class(**kwargs)
  50. assert isinstance(props_instance, props_class)