test_var.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import pytest
  2. from pynecone.base import Base
  3. from pynecone.var import BaseVar, Var
  4. test_vars = [
  5. BaseVar(name="prop1", type_=int),
  6. BaseVar(name="key", type_=str),
  7. BaseVar(name="value", type_=str, state="state"),
  8. BaseVar(name="local", type_=str, state="state", is_local=True),
  9. BaseVar(name="local2", type_=str, is_local=True),
  10. ]
  11. @pytest.fixture
  12. def TestObj():
  13. class TestObj(Base):
  14. foo: int
  15. bar: str
  16. return TestObj
  17. @pytest.mark.parametrize(
  18. "prop,expected",
  19. zip(
  20. test_vars,
  21. [
  22. "prop1",
  23. "key",
  24. "state.value",
  25. "state.local",
  26. "local2",
  27. ],
  28. ),
  29. )
  30. def test_full_name(prop, expected):
  31. """Test that the full name of a var is correct.
  32. Args:
  33. prop: The var to test.
  34. expected: The expected full name.
  35. """
  36. assert prop.full_name == expected
  37. @pytest.mark.parametrize(
  38. "prop,expected",
  39. zip(
  40. test_vars,
  41. ["{prop1}", "{key}", "{state.value}", "state.local", "local2"],
  42. ),
  43. )
  44. def test_str(prop, expected):
  45. """Test that the string representation of a var is correct.
  46. Args:
  47. prop: The var to test.
  48. expected: The expected string representation.
  49. """
  50. assert str(prop) == expected
  51. @pytest.mark.parametrize(
  52. "prop,expected",
  53. [
  54. (BaseVar(name="p", type_=int), 0),
  55. (BaseVar(name="p", type_=float), 0.0),
  56. (BaseVar(name="p", type_=str), ""),
  57. (BaseVar(name="p", type_=bool), False),
  58. (BaseVar(name="p", type_=list), []),
  59. (BaseVar(name="p", type_=dict), {}),
  60. (BaseVar(name="p", type_=tuple), ()),
  61. (BaseVar(name="p", type_=set), set()),
  62. ],
  63. )
  64. def test_default_value(prop, expected):
  65. """Test that the default value of a var is correct.
  66. Args:
  67. prop: The var to test.
  68. expected: The expected default value.
  69. """
  70. assert prop.get_default_value() == expected
  71. @pytest.mark.parametrize(
  72. "prop,expected",
  73. zip(
  74. test_vars,
  75. [
  76. "set_prop1",
  77. "set_key",
  78. "state.set_value",
  79. "state.set_local",
  80. "set_local2",
  81. ],
  82. ),
  83. )
  84. def test_get_setter(prop, expected):
  85. """Test that the name of the setter function of a var is correct.
  86. Args:
  87. prop: The var to test.
  88. expected: The expected name of the setter function.
  89. """
  90. assert prop.get_setter_name() == expected
  91. @pytest.mark.parametrize(
  92. "value,expected",
  93. [
  94. (None, None),
  95. (1, BaseVar(name="1", type_=int, is_local=True)),
  96. ("key", BaseVar(name="key", type_=str, is_local=True)),
  97. (3.14, BaseVar(name="3.14", type_=float, is_local=True)),
  98. ([1, 2, 3], BaseVar(name="[1, 2, 3]", type_=list, is_local=True)),
  99. (
  100. {"a": 1, "b": 2},
  101. BaseVar(name='{"a": 1, "b": 2}', type_=dict, is_local=True),
  102. ),
  103. ],
  104. )
  105. def test_create(value, expected):
  106. """Test the var create function.
  107. Args:
  108. value: The value to create a var from.
  109. expected: The expected name of the setter function.
  110. """
  111. prop = Var.create(value)
  112. if value is None:
  113. assert prop == expected
  114. else:
  115. assert prop.equals(expected) # type: ignore
  116. def test_basic_operations(TestObj):
  117. """Test the var operations.
  118. Args:
  119. TestObj: The test object.
  120. """
  121. def v(value) -> Var:
  122. val = Var.create(value)
  123. assert val is not None
  124. return val
  125. assert str(v(1) == v(2)) == "{(1 == 2)}"
  126. assert str(v(1) != v(2)) == "{(1 != 2)}"
  127. assert str(v(1) < v(2)) == "{(1 < 2)}"
  128. assert str(v(1) <= v(2)) == "{(1 <= 2)}"
  129. assert str(v(1) > v(2)) == "{(1 > 2)}"
  130. assert str(v(1) >= v(2)) == "{(1 >= 2)}"
  131. assert str(v(1) + v(2)) == "{(1 + 2)}"
  132. assert str(v(1) - v(2)) == "{(1 - 2)}"
  133. assert str(v(1) * v(2)) == "{(1 * 2)}"
  134. assert str(v(1) / v(2)) == "{(1 / 2)}"
  135. assert str(v(1) // v(2)) == "{Math.floor(1 / 2)}"
  136. assert str(v(1) % v(2)) == "{(1 % 2)}"
  137. assert str(v(1) ** v(2)) == "{Math.pow(1 , 2)}"
  138. assert str(v(1) & v(2)) == "{(1 && 2)}"
  139. assert str(v(1) | v(2)) == "{(1 || 2)}"
  140. assert str(v([1, 2, 3])[v(0)]) == "{[1, 2, 3][0]}"
  141. assert str(v({"a": 1, "b": 2})["a"]) == '{{"a": 1, "b": 2}["a"]}'
  142. assert (
  143. str(BaseVar(name="foo", state="state", type_=TestObj).bar) == "{state.foo.bar}"
  144. )