test_props.py 1.5 KB

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