test_types.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from typing import Any, List, Literal, Tuple, Union
  2. import pytest
  3. from reflex.utils import types
  4. @pytest.mark.parametrize(
  5. "params, allowed_value_str, value_str",
  6. [
  7. (["size", 1, Literal["1", "2", "3"], "Heading"], "'1','2','3'", "1"),
  8. (["size", "1", Literal[1, 2, 3], "Heading"], "1,2,3", "'1'"),
  9. ],
  10. )
  11. def test_validate_literal_error_msg(params, allowed_value_str, value_str):
  12. with pytest.raises(ValueError) as err:
  13. types.validate_literal(*params)
  14. assert (
  15. err.value.args[0] == f"prop value for {str(params[0])} of the `{params[-1]}` "
  16. f"component should be one of the following: {allowed_value_str}. Got {value_str} instead"
  17. )
  18. @pytest.mark.parametrize(
  19. "cls,cls_check,expected",
  20. [
  21. (int, Any, True),
  22. (Tuple[int], Any, True),
  23. (List[int], Any, True),
  24. (int, int, True),
  25. (int, object, True),
  26. (int, Union[int, str], True),
  27. (int, Union[str, int], True),
  28. (str, Union[str, int], True),
  29. (str, Union[int, str], True),
  30. (int, Union[str, float, int], True),
  31. (int, Union[str, float], False),
  32. (int, Union[float, str], False),
  33. (int, str, False),
  34. (int, List[int], False),
  35. ],
  36. )
  37. def test_issubclass(
  38. cls: types.GenericType, cls_check: types.GenericType, expected: bool
  39. ) -> None:
  40. assert types._issubclass(cls, cls_check) == expected