test_computed_vars.py 6.2 KB

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