test_computed_vars.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. """Test computed vars."""
  2. from __future__ import annotations
  3. import time
  4. from typing import Generator
  5. import pytest
  6. from selenium.webdriver.common.by import By
  7. from reflex.testing import DEFAULT_TIMEOUT, AppHarness, WebDriver
  8. def ComputedVars():
  9. """Test app for computed vars."""
  10. import reflex as rx
  11. class State(rx.State):
  12. count: int = 0
  13. # cached var with dep on count
  14. @rx.cached_var(interval=15)
  15. def count1(self) -> int:
  16. return self.count
  17. # same as above, different notation
  18. @rx.var(interval=15, cache=True)
  19. def count2(self) -> int:
  20. return self.count
  21. # explicit disabled auto_deps
  22. @rx.var(interval=15, cache=True, auto_deps=False)
  23. def count3(self) -> int:
  24. # this will not add deps, because auto_deps is False
  25. print(self.count1)
  26. print(self.count2)
  27. return self.count
  28. # explicit dependency on count var
  29. @rx.var(cache=True, deps=["count"], auto_deps=False)
  30. def depends_on_count(self) -> int:
  31. return self.count
  32. # explicit dependency on count1 var
  33. @rx.var(cache=True, deps=[count1], auto_deps=False)
  34. def depends_on_count1(self) -> int:
  35. return self.count
  36. @rx.var(deps=[count3], auto_deps=False, cache=True)
  37. def depends_on_count3(self) -> int:
  38. return self.count
  39. def increment(self):
  40. self.count += 1
  41. def mark_dirty(self):
  42. self._mark_dirty()
  43. def index() -> rx.Component:
  44. return rx.center(
  45. rx.vstack(
  46. rx.input(
  47. id="token",
  48. value=State.router.session.client_token,
  49. is_read_only=True,
  50. ),
  51. rx.button("Increment", on_click=State.increment, id="increment"),
  52. rx.button("Do nothing", on_click=State.mark_dirty, id="mark_dirty"),
  53. rx.text("count:"),
  54. rx.text(State.count, id="count"),
  55. rx.text("count1:"),
  56. rx.text(State.count1, id="count1"),
  57. rx.text("count2:"),
  58. rx.text(State.count2, id="count2"),
  59. rx.text("count3:"),
  60. rx.text(State.count3, id="count3"),
  61. rx.text("depends_on_count:"),
  62. rx.text(
  63. State.depends_on_count,
  64. id="depends_on_count",
  65. ),
  66. rx.text("depends_on_count1:"),
  67. rx.text(
  68. State.depends_on_count1,
  69. id="depends_on_count1",
  70. ),
  71. rx.text("depends_on_count3:"),
  72. rx.text(
  73. State.depends_on_count3,
  74. id="depends_on_count3",
  75. ),
  76. ),
  77. )
  78. # raise Exception(State.count3._deps(objclass=State))
  79. app = rx.App()
  80. app.add_page(index)
  81. @pytest.fixture(scope="module")
  82. def computed_vars(
  83. tmp_path_factory: pytest.TempPathFactory,
  84. ) -> Generator[AppHarness, None, None]:
  85. """Start ComputedVars app at tmp_path via AppHarness.
  86. Args:
  87. tmp_path_factory: pytest tmp_path_factory fixture
  88. Yields:
  89. running AppHarness instance
  90. """
  91. with AppHarness.create(
  92. root=tmp_path_factory.mktemp(f"computed_vars"),
  93. app_source=ComputedVars, # type: ignore
  94. ) as harness:
  95. yield harness
  96. @pytest.fixture
  97. def driver(computed_vars: AppHarness) -> Generator[WebDriver, None, None]:
  98. """Get an instance of the browser open to the computed_vars app.
  99. Args:
  100. computed_vars: harness for ComputedVars app
  101. Yields:
  102. WebDriver instance.
  103. """
  104. assert computed_vars.app_instance is not None, "app is not running"
  105. driver = computed_vars.frontend()
  106. try:
  107. yield driver
  108. finally:
  109. driver.quit()
  110. @pytest.fixture()
  111. def token(computed_vars: AppHarness, driver: WebDriver) -> str:
  112. """Get a function that returns the active token.
  113. Args:
  114. computed_vars: harness for ComputedVars app.
  115. driver: WebDriver instance.
  116. Returns:
  117. The token for the connected client
  118. """
  119. assert computed_vars.app_instance is not None
  120. token_input = driver.find_element(By.ID, "token")
  121. assert token_input
  122. # wait for the backend connection to send the token
  123. token = computed_vars.poll_for_value(token_input, timeout=DEFAULT_TIMEOUT * 2)
  124. assert token is not None
  125. return token
  126. def test_computed_vars(
  127. computed_vars: AppHarness,
  128. driver: WebDriver,
  129. token: str,
  130. ):
  131. """Test that computed vars are working as expected.
  132. Args:
  133. computed_vars: harness for ComputedVars app.
  134. driver: WebDriver instance.
  135. token: The token for the connected client.
  136. """
  137. assert computed_vars.app_instance is not None
  138. count = driver.find_element(By.ID, "count")
  139. assert count
  140. assert count.text == "0"
  141. count1 = driver.find_element(By.ID, "count1")
  142. assert count1
  143. assert count1.text == "0"
  144. count2 = driver.find_element(By.ID, "count2")
  145. assert count2
  146. assert count2.text == "0"
  147. count3 = driver.find_element(By.ID, "count3")
  148. assert count3
  149. assert count3.text == "0"
  150. depends_on_count = driver.find_element(By.ID, "depends_on_count")
  151. assert depends_on_count
  152. assert depends_on_count.text == "0"
  153. depends_on_count1 = driver.find_element(By.ID, "depends_on_count1")
  154. assert depends_on_count1
  155. assert depends_on_count1.text == "0"
  156. depends_on_count3 = driver.find_element(By.ID, "depends_on_count3")
  157. assert depends_on_count3
  158. assert depends_on_count3.text == "0"
  159. increment = driver.find_element(By.ID, "increment")
  160. assert increment.is_enabled()
  161. mark_dirty = driver.find_element(By.ID, "mark_dirty")
  162. assert mark_dirty.is_enabled()
  163. mark_dirty.click()
  164. increment.click()
  165. assert computed_vars.poll_for_content(count, timeout=2, exp_not_equal="0") == "1"
  166. assert computed_vars.poll_for_content(count1, timeout=2, exp_not_equal="0") == "1"
  167. assert computed_vars.poll_for_content(count2, timeout=2, exp_not_equal="0") == "1"
  168. assert (
  169. computed_vars.poll_for_content(depends_on_count, timeout=2, exp_not_equal="0")
  170. == "1"
  171. )
  172. mark_dirty.click()
  173. with pytest.raises(TimeoutError):
  174. _ = computed_vars.poll_for_content(count3, timeout=5, exp_not_equal="0")
  175. time.sleep(10)
  176. assert count3.text == "0"
  177. assert depends_on_count3.text == "0"
  178. mark_dirty.click()
  179. assert computed_vars.poll_for_content(count3, timeout=2, exp_not_equal="0") == "1"
  180. assert depends_on_count3.text == "1"