test_app.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. from __future__ import annotations
  2. import functools
  3. import io
  4. import json
  5. import os.path
  6. import re
  7. import unittest.mock
  8. import uuid
  9. from collections.abc import Generator
  10. from contextlib import nullcontext as does_not_raise
  11. from pathlib import Path
  12. from unittest.mock import AsyncMock
  13. import pytest
  14. import sqlmodel
  15. from fastapi.responses import StreamingResponse
  16. from pytest_mock import MockerFixture
  17. from starlette.applications import Starlette
  18. from starlette.datastructures import UploadFile
  19. from starlette_admin.auth import AuthProvider
  20. from starlette_admin.contrib.sqla.admin import Admin
  21. from starlette_admin.contrib.sqla.view import ModelView
  22. import reflex as rx
  23. from reflex import AdminDash, constants
  24. from reflex.app import (
  25. App,
  26. ComponentCallable,
  27. OverlayFragment,
  28. default_overlay_component,
  29. process,
  30. upload,
  31. )
  32. from reflex.components import Component
  33. from reflex.components.base.bare import Bare
  34. from reflex.components.base.fragment import Fragment
  35. from reflex.components.core.cond import Cond
  36. from reflex.components.radix.themes.typography.text import Text
  37. from reflex.event import Event
  38. from reflex.middleware import HydrateMiddleware
  39. from reflex.model import Model
  40. from reflex.state import (
  41. BaseState,
  42. OnLoadInternalState,
  43. RouterData,
  44. State,
  45. StateManagerDisk,
  46. StateManagerMemory,
  47. StateManagerRedis,
  48. StateUpdate,
  49. _substate_key,
  50. )
  51. from reflex.style import Style
  52. from reflex.utils import console, exceptions, format
  53. from reflex.vars.base import computed_var
  54. from .conftest import chdir
  55. from .states import (
  56. ChildFileUploadState,
  57. FileStateBase1,
  58. FileUploadState,
  59. GenState,
  60. GrandChildFileUploadState,
  61. )
  62. class EmptyState(BaseState):
  63. """An empty state."""
  64. pass
  65. @pytest.fixture
  66. def index_page() -> ComponentCallable:
  67. """An index page.
  68. Returns:
  69. The index page.
  70. """
  71. def index():
  72. return rx.box("Index")
  73. return index
  74. @pytest.fixture
  75. def about_page() -> ComponentCallable:
  76. """An about page.
  77. Returns:
  78. The about page.
  79. """
  80. def about():
  81. return rx.box("About")
  82. return about
  83. class ATestState(BaseState):
  84. """A simple state for testing."""
  85. var: int
  86. @pytest.fixture()
  87. def test_state() -> type[BaseState]:
  88. """A default state.
  89. Returns:
  90. A default state.
  91. """
  92. return ATestState
  93. @pytest.fixture()
  94. def redundant_test_state() -> type[BaseState]:
  95. """A default state.
  96. Returns:
  97. A default state.
  98. """
  99. class RedundantTestState(BaseState):
  100. var: int
  101. return RedundantTestState
  102. @pytest.fixture(scope="session")
  103. def test_model() -> type[Model]:
  104. """A default model.
  105. Returns:
  106. A default model.
  107. """
  108. class TestModel(Model, table=True):
  109. pass
  110. return TestModel
  111. @pytest.fixture(scope="session")
  112. def test_model_auth() -> type[Model]:
  113. """A default model.
  114. Returns:
  115. A default model.
  116. """
  117. class TestModelAuth(Model, table=True):
  118. """A test model with auth."""
  119. pass
  120. return TestModelAuth
  121. @pytest.fixture()
  122. def test_get_engine():
  123. """A default database engine.
  124. Returns:
  125. A default database engine.
  126. """
  127. enable_admin = True
  128. url = "sqlite:///test.db"
  129. return sqlmodel.create_engine(
  130. url,
  131. echo=False,
  132. connect_args={"check_same_thread": False} if enable_admin else {},
  133. )
  134. @pytest.fixture()
  135. def test_custom_auth_admin() -> type[AuthProvider]:
  136. """A default auth provider.
  137. Returns:
  138. A default default auth provider.
  139. """
  140. class TestAuthProvider(AuthProvider):
  141. """A test auth provider."""
  142. login_path: str = "/login"
  143. logout_path: str = "/logout"
  144. def login(self): # pyright: ignore [reportIncompatibleMethodOverride]
  145. """Login."""
  146. pass
  147. def is_authenticated(self): # pyright: ignore [reportIncompatibleMethodOverride]
  148. """Is authenticated."""
  149. pass
  150. def get_admin_user(self): # pyright: ignore [reportIncompatibleMethodOverride]
  151. """Get admin user."""
  152. pass
  153. def logout(self): # pyright: ignore [reportIncompatibleMethodOverride]
  154. """Logout."""
  155. pass
  156. return TestAuthProvider
  157. def test_default_app(app: App):
  158. """Test creating an app with no args.
  159. Args:
  160. app: The app to test.
  161. """
  162. assert app._middlewares == [HydrateMiddleware()]
  163. assert app.style == Style()
  164. assert app.admin_dash is None
  165. def test_multiple_states_error(monkeypatch, test_state, redundant_test_state):
  166. """Test that an error is thrown when multiple classes subclass rx.BaseState.
  167. Args:
  168. monkeypatch: Pytest monkeypatch object.
  169. test_state: A test state subclassing rx.BaseState.
  170. redundant_test_state: Another test state subclassing rx.BaseState.
  171. """
  172. monkeypatch.delenv(constants.PYTEST_CURRENT_TEST)
  173. with pytest.raises(ValueError):
  174. App()
  175. def test_add_page_default_route(app: App, index_page, about_page):
  176. """Test adding a page to an app.
  177. Args:
  178. app: The app to test.
  179. index_page: The index page.
  180. about_page: The about page.
  181. """
  182. assert app._pages == {}
  183. assert app._unevaluated_pages == {}
  184. app.add_page(index_page)
  185. app._compile_page("index")
  186. assert app._pages.keys() == {"index"}
  187. app.add_page(about_page)
  188. app._compile_page("about")
  189. assert app._pages.keys() == {"index", "about"}
  190. def test_add_page_set_route(app: App, index_page, windows_platform: bool):
  191. """Test adding a page to an app.
  192. Args:
  193. app: The app to test.
  194. index_page: The index page.
  195. windows_platform: Whether the system is windows.
  196. """
  197. route = "test" if windows_platform else "/test"
  198. assert app._unevaluated_pages == {}
  199. app.add_page(index_page, route=route)
  200. app._compile_page("test")
  201. assert app._pages.keys() == {"test"}
  202. def test_add_page_set_route_dynamic(index_page, windows_platform: bool):
  203. """Test adding a page with dynamic route variable to an app.
  204. Args:
  205. index_page: The index page.
  206. windows_platform: Whether the system is windows.
  207. """
  208. app = App(_state=EmptyState)
  209. assert app._state is not None
  210. route = "/test/[dynamic]"
  211. assert app._unevaluated_pages == {}
  212. app.add_page(index_page, route=route)
  213. app._compile_page("test/[dynamic]")
  214. assert app._pages.keys() == {"test/[dynamic]"}
  215. assert "dynamic" in app._state.computed_vars
  216. assert app._state.computed_vars["dynamic"]._deps(objclass=EmptyState) == {
  217. EmptyState.get_full_name(): {constants.ROUTER},
  218. }
  219. assert constants.ROUTER in app._state()._var_dependencies
  220. def test_add_page_set_route_nested(app: App, index_page, windows_platform: bool):
  221. """Test adding a page to an app.
  222. Args:
  223. app: The app to test.
  224. index_page: The index page.
  225. windows_platform: Whether the system is windows.
  226. """
  227. route = "test\\nested" if windows_platform else "/test/nested"
  228. assert app._unevaluated_pages == {}
  229. app.add_page(index_page, route=route)
  230. assert app._unevaluated_pages.keys() == {route.strip(os.path.sep)}
  231. def test_add_page_invalid_api_route(app: App, index_page):
  232. """Test adding a page with an invalid route to an app.
  233. Args:
  234. app: The app to test.
  235. index_page: The index page.
  236. """
  237. with pytest.raises(ValueError):
  238. app.add_page(index_page, route="api")
  239. with pytest.raises(ValueError):
  240. app.add_page(index_page, route="/api")
  241. with pytest.raises(ValueError):
  242. app.add_page(index_page, route="/api/")
  243. with pytest.raises(ValueError):
  244. app.add_page(index_page, route="api/foo")
  245. with pytest.raises(ValueError):
  246. app.add_page(index_page, route="/api/foo")
  247. # These should be fine
  248. app.add_page(index_page, route="api2")
  249. app.add_page(index_page, route="/foo/api")
  250. def page1():
  251. return rx.fragment()
  252. def page2():
  253. return rx.fragment()
  254. def index():
  255. return rx.fragment()
  256. @pytest.mark.parametrize(
  257. ("first_page", "second_page", "route"),
  258. [
  259. (index, index, None),
  260. (page1, page1, None),
  261. ],
  262. )
  263. def test_add_the_same_page(
  264. mocker: MockerFixture, app: App, first_page, second_page, route
  265. ):
  266. app.add_page(first_page, route=route)
  267. mock_object = mocker.Mock()
  268. mocker.patch.object(
  269. console,
  270. "warn",
  271. mock_object,
  272. )
  273. app.add_page(second_page, route="/" + route.strip("/") if route else None)
  274. assert mock_object.call_count == 1
  275. @pytest.mark.parametrize(
  276. ("first_page", "second_page", "route"),
  277. [
  278. (lambda: rx.fragment(), lambda: rx.fragment(rx.text("second")), "/"),
  279. (rx.fragment(rx.text("first")), rx.fragment(rx.text("second")), "/page1"),
  280. (
  281. lambda: rx.fragment(rx.text("first")),
  282. rx.fragment(rx.text("second")),
  283. "page3",
  284. ),
  285. (page1, page2, "page1"),
  286. ],
  287. )
  288. def test_add_duplicate_page_route_error(app: App, first_page, second_page, route):
  289. app.add_page(first_page, route=route)
  290. with pytest.raises(ValueError):
  291. app.add_page(second_page, route="/" + route.strip("/") if route else None)
  292. def test_initialize_with_admin_dashboard(test_model):
  293. """Test setting the admin dashboard of an app.
  294. Args:
  295. test_model: The default model.
  296. """
  297. app = App(admin_dash=AdminDash(models=[test_model]))
  298. assert app.admin_dash is not None
  299. assert len(app.admin_dash.models) > 0
  300. assert app.admin_dash.models[0] == test_model
  301. def test_initialize_with_custom_admin_dashboard(
  302. test_get_engine,
  303. test_custom_auth_admin,
  304. test_model_auth,
  305. ):
  306. """Test setting the custom admin dashboard of an app.
  307. Args:
  308. test_get_engine: The default database engine.
  309. test_model_auth: The default model for an auth admin dashboard.
  310. test_custom_auth_admin: The custom auth provider.
  311. """
  312. custom_auth_provider = test_custom_auth_admin()
  313. custom_admin = Admin(engine=test_get_engine, auth_provider=custom_auth_provider)
  314. app = App(admin_dash=AdminDash(models=[test_model_auth], admin=custom_admin))
  315. assert app.admin_dash is not None
  316. assert app.admin_dash.admin is not None
  317. assert len(app.admin_dash.models) > 0
  318. assert app.admin_dash.models[0] == test_model_auth
  319. assert app.admin_dash.admin.auth_provider == custom_auth_provider
  320. def test_initialize_admin_dashboard_with_view_overrides(test_model):
  321. """Test setting the admin dashboard of an app with view class overridden.
  322. Args:
  323. test_model: The default model.
  324. """
  325. class TestModelView(ModelView):
  326. pass
  327. app = App(
  328. admin_dash=AdminDash(
  329. models=[test_model], view_overrides={test_model: TestModelView}
  330. )
  331. )
  332. assert app.admin_dash is not None
  333. assert app.admin_dash.models == [test_model]
  334. assert app.admin_dash.view_overrides[test_model] == TestModelView
  335. @pytest.mark.asyncio
  336. async def test_initialize_with_state(test_state: type[ATestState], token: str):
  337. """Test setting the state of an app.
  338. Args:
  339. test_state: The default state.
  340. token: a Token.
  341. """
  342. app = App(_state=test_state)
  343. assert app._state == test_state
  344. # Get a state for a given token.
  345. state = await app.state_manager.get_state(_substate_key(token, test_state))
  346. assert isinstance(state, test_state)
  347. assert state.var == 0
  348. if isinstance(app.state_manager, StateManagerRedis):
  349. await app.state_manager.close()
  350. @pytest.mark.asyncio
  351. async def test_set_and_get_state(test_state):
  352. """Test setting and getting the state of an app with different tokens.
  353. Args:
  354. test_state: The default state.
  355. """
  356. app = App(_state=test_state)
  357. # Create two tokens.
  358. token1 = str(uuid.uuid4()) + f"_{test_state.get_full_name()}"
  359. token2 = str(uuid.uuid4()) + f"_{test_state.get_full_name()}"
  360. # Get the default state for each token.
  361. state1 = await app.state_manager.get_state(token1)
  362. state2 = await app.state_manager.get_state(token2)
  363. assert state1.var == 0
  364. assert state2.var == 0
  365. # Set the vars to different values.
  366. state1.var = 1
  367. state2.var = 2
  368. await app.state_manager.set_state(token1, state1)
  369. await app.state_manager.set_state(token2, state2)
  370. # Get the states again and check the values.
  371. state1 = await app.state_manager.get_state(token1)
  372. state2 = await app.state_manager.get_state(token2)
  373. assert state1.var == 1
  374. assert state2.var == 2
  375. if isinstance(app.state_manager, StateManagerRedis):
  376. await app.state_manager.close()
  377. @pytest.mark.asyncio
  378. async def test_dynamic_var_event(test_state: type[ATestState], token: str):
  379. """Test that the default handler of a dynamic generated var
  380. works as expected.
  381. Args:
  382. test_state: State Fixture.
  383. token: a Token.
  384. """
  385. state = test_state() # pyright: ignore [reportCallIssue]
  386. state.add_var("int_val", int, 0)
  387. async for result in state._process(
  388. Event(
  389. token=token,
  390. name=f"{test_state.get_name()}.set_int_val",
  391. router_data={"pathname": "/", "query": {}},
  392. payload={"value": 50},
  393. )
  394. ):
  395. assert result.delta == {test_state.get_name(): {"int_val": 50}}
  396. @pytest.mark.asyncio
  397. @pytest.mark.parametrize(
  398. "event_tuples",
  399. [
  400. pytest.param(
  401. [
  402. (
  403. "make_friend",
  404. {"plain_friends": ["Tommy", "another-fd"]},
  405. ),
  406. (
  407. "change_first_friend",
  408. {"plain_friends": ["Jenny", "another-fd"]},
  409. ),
  410. ],
  411. id="append then __setitem__",
  412. ),
  413. pytest.param(
  414. [
  415. (
  416. "unfriend_first_friend",
  417. {"plain_friends": []},
  418. ),
  419. (
  420. "make_friend",
  421. {"plain_friends": ["another-fd"]},
  422. ),
  423. ],
  424. id="delitem then append",
  425. ),
  426. pytest.param(
  427. [
  428. (
  429. "make_friends_with_colleagues",
  430. {"plain_friends": ["Tommy", "Peter", "Jimmy"]},
  431. ),
  432. (
  433. "remove_tommy",
  434. {"plain_friends": ["Peter", "Jimmy"]},
  435. ),
  436. (
  437. "remove_last_friend",
  438. {"plain_friends": ["Peter"]},
  439. ),
  440. (
  441. "unfriend_all_friends",
  442. {"plain_friends": []},
  443. ),
  444. ],
  445. id="extend, remove, pop, clear",
  446. ),
  447. pytest.param(
  448. [
  449. (
  450. "add_jimmy_to_second_group",
  451. {"friends_in_nested_list": [["Tommy"], ["Jenny", "Jimmy"]]},
  452. ),
  453. (
  454. "remove_first_person_from_first_group",
  455. {"friends_in_nested_list": [[], ["Jenny", "Jimmy"]]},
  456. ),
  457. (
  458. "remove_first_group",
  459. {"friends_in_nested_list": [["Jenny", "Jimmy"]]},
  460. ),
  461. ],
  462. id="nested list",
  463. ),
  464. pytest.param(
  465. [
  466. (
  467. "add_jimmy_to_tommy_friends",
  468. {"friends_in_dict": {"Tommy": ["Jenny", "Jimmy"]}},
  469. ),
  470. (
  471. "remove_jenny_from_tommy",
  472. {"friends_in_dict": {"Tommy": ["Jimmy"]}},
  473. ),
  474. (
  475. "tommy_has_no_fds",
  476. {"friends_in_dict": {"Tommy": []}},
  477. ),
  478. ],
  479. id="list in dict",
  480. ),
  481. ],
  482. )
  483. async def test_list_mutation_detection__plain_list(
  484. event_tuples: list[tuple[str, list[str]]],
  485. list_mutation_state: State,
  486. token: str,
  487. ):
  488. """Test list mutation detection
  489. when reassignment is not explicitly included in the logic.
  490. Args:
  491. event_tuples: From parametrization.
  492. list_mutation_state: A state with list mutation features.
  493. token: a Token.
  494. """
  495. for event_name, expected_delta in event_tuples:
  496. async for result in list_mutation_state._process(
  497. Event(
  498. token=token,
  499. name=f"{list_mutation_state.get_name()}.{event_name}",
  500. router_data={"pathname": "/", "query": {}},
  501. payload={},
  502. )
  503. ):
  504. # prefix keys in expected_delta with the state name
  505. expected_delta = {list_mutation_state.get_name(): expected_delta}
  506. assert result.delta == expected_delta
  507. @pytest.mark.asyncio
  508. @pytest.mark.parametrize(
  509. "event_tuples",
  510. [
  511. pytest.param(
  512. [
  513. (
  514. "add_age",
  515. {"details": {"name": "Tommy", "age": 20}},
  516. ),
  517. (
  518. "change_name",
  519. {"details": {"name": "Jenny", "age": 20}},
  520. ),
  521. (
  522. "remove_last_detail",
  523. {"details": {"name": "Jenny"}},
  524. ),
  525. ],
  526. id="update then __setitem__",
  527. ),
  528. pytest.param(
  529. [
  530. (
  531. "clear_details",
  532. {"details": {}},
  533. ),
  534. (
  535. "add_age",
  536. {"details": {"age": 20}},
  537. ),
  538. ],
  539. id="delitem then update",
  540. ),
  541. pytest.param(
  542. [
  543. (
  544. "add_age",
  545. {"details": {"name": "Tommy", "age": 20}},
  546. ),
  547. (
  548. "remove_name",
  549. {"details": {"age": 20}},
  550. ),
  551. (
  552. "pop_out_age",
  553. {"details": {}},
  554. ),
  555. ],
  556. id="add, remove, pop",
  557. ),
  558. pytest.param(
  559. [
  560. (
  561. "remove_home_address",
  562. {"address": [{}, {"work": "work address"}]},
  563. ),
  564. (
  565. "add_street_to_home_address",
  566. {
  567. "address": [
  568. {"street": "street address"},
  569. {"work": "work address"},
  570. ]
  571. },
  572. ),
  573. ],
  574. id="dict in list",
  575. ),
  576. pytest.param(
  577. [
  578. (
  579. "change_friend_name",
  580. {
  581. "friend_in_nested_dict": {
  582. "name": "Nikhil",
  583. "friend": {"name": "Tommy"},
  584. }
  585. },
  586. ),
  587. (
  588. "add_friend_age",
  589. {
  590. "friend_in_nested_dict": {
  591. "name": "Nikhil",
  592. "friend": {"name": "Tommy", "age": 30},
  593. }
  594. },
  595. ),
  596. (
  597. "remove_friend",
  598. {"friend_in_nested_dict": {"name": "Nikhil"}},
  599. ),
  600. ],
  601. id="nested dict",
  602. ),
  603. ],
  604. )
  605. async def test_dict_mutation_detection__plain_list(
  606. event_tuples: list[tuple[str, list[str]]],
  607. dict_mutation_state: State,
  608. token: str,
  609. ):
  610. """Test dict mutation detection
  611. when reassignment is not explicitly included in the logic.
  612. Args:
  613. event_tuples: From parametrization.
  614. dict_mutation_state: A state with dict mutation features.
  615. token: a Token.
  616. """
  617. for event_name, expected_delta in event_tuples:
  618. async for result in dict_mutation_state._process(
  619. Event(
  620. token=token,
  621. name=f"{dict_mutation_state.get_name()}.{event_name}",
  622. router_data={"pathname": "/", "query": {}},
  623. payload={},
  624. )
  625. ):
  626. # prefix keys in expected_delta with the state name
  627. expected_delta = {dict_mutation_state.get_name(): expected_delta}
  628. assert result.delta == expected_delta
  629. @pytest.mark.asyncio
  630. @pytest.mark.parametrize(
  631. ("state", "delta"),
  632. [
  633. (
  634. FileUploadState,
  635. {
  636. FileUploadState.get_full_name(): {
  637. "img_list": ["image1.jpg", "image2.jpg"]
  638. }
  639. },
  640. ),
  641. (
  642. ChildFileUploadState,
  643. {
  644. ChildFileUploadState.get_full_name(): {
  645. "img_list": ["image1.jpg", "image2.jpg"]
  646. }
  647. },
  648. ),
  649. (
  650. GrandChildFileUploadState,
  651. {
  652. GrandChildFileUploadState.get_full_name(): {
  653. "img_list": ["image1.jpg", "image2.jpg"]
  654. }
  655. },
  656. ),
  657. ],
  658. )
  659. async def test_upload_file(tmp_path, state, delta, token: str, mocker):
  660. """Test that file upload works correctly.
  661. Args:
  662. tmp_path: Temporary path.
  663. state: The state class.
  664. delta: Expected delta
  665. token: a Token.
  666. mocker: pytest mocker object.
  667. """
  668. mocker.patch(
  669. "reflex.state.State.class_subclasses",
  670. {state if state is FileUploadState else FileStateBase1},
  671. )
  672. state._tmp_path = tmp_path
  673. # The App state must be the "root" of the state tree
  674. app = App()
  675. app._enable_state()
  676. app.event_namespace.emit = AsyncMock() # pyright: ignore [reportOptionalMemberAccess]
  677. current_state = await app.state_manager.get_state(_substate_key(token, state))
  678. data = b"This is binary data"
  679. # Create a binary IO object and write data to it
  680. bio = io.BytesIO()
  681. bio.write(data)
  682. request_mock = unittest.mock.Mock()
  683. request_mock.headers = {
  684. "reflex-client-token": token,
  685. "reflex-event-handler": f"{state.get_full_name()}.multi_handle_upload",
  686. }
  687. file1 = UploadFile(
  688. filename="image1.jpg",
  689. file=bio,
  690. )
  691. file2 = UploadFile(
  692. filename="image2.jpg",
  693. file=bio,
  694. )
  695. async def form():
  696. files_mock = unittest.mock.Mock()
  697. def getlist(key: str):
  698. assert key == "files"
  699. return [file1, file2]
  700. files_mock.getlist = getlist
  701. return files_mock
  702. request_mock.form = form
  703. upload_fn = upload(app)
  704. streaming_response = await upload_fn(request_mock)
  705. assert isinstance(streaming_response, StreamingResponse)
  706. async for state_update in streaming_response.body_iterator:
  707. assert (
  708. state_update
  709. == StateUpdate(delta=delta, events=[], final=True).json() + "\n"
  710. )
  711. current_state = await app.state_manager.get_state(_substate_key(token, state))
  712. state_dict = current_state.dict()[state.get_full_name()]
  713. assert state_dict["img_list"] == [
  714. "image1.jpg",
  715. "image2.jpg",
  716. ]
  717. if isinstance(app.state_manager, StateManagerRedis):
  718. await app.state_manager.close()
  719. @pytest.mark.asyncio
  720. @pytest.mark.parametrize(
  721. "state",
  722. [FileUploadState, ChildFileUploadState, GrandChildFileUploadState],
  723. )
  724. async def test_upload_file_without_annotation(state, tmp_path, token):
  725. """Test that an error is thrown when there's no param annotated with rx.UploadFile or list[UploadFile].
  726. Args:
  727. state: The state class.
  728. tmp_path: Temporary path.
  729. token: a Token.
  730. """
  731. state._tmp_path = tmp_path
  732. app = App(_state=State)
  733. request_mock = unittest.mock.Mock()
  734. request_mock.headers = {
  735. "reflex-client-token": token,
  736. "reflex-event-handler": f"{state.get_full_name()}.handle_upload2",
  737. }
  738. async def form():
  739. files_mock = unittest.mock.Mock()
  740. def getlist(key: str):
  741. assert key == "files"
  742. return [unittest.mock.Mock(filename="image1.jpg")]
  743. files_mock.getlist = getlist
  744. return files_mock
  745. request_mock.form = form
  746. fn = upload(app)
  747. with pytest.raises(ValueError) as err:
  748. await fn(request_mock)
  749. assert (
  750. err.value.args[0]
  751. == f"`{state.get_full_name()}.handle_upload2` handler should have a parameter annotated as list[rx.UploadFile]"
  752. )
  753. if isinstance(app.state_manager, StateManagerRedis):
  754. await app.state_manager.close()
  755. @pytest.mark.asyncio
  756. @pytest.mark.parametrize(
  757. "state",
  758. [FileUploadState, ChildFileUploadState, GrandChildFileUploadState],
  759. )
  760. async def test_upload_file_background(state, tmp_path, token):
  761. """Test that an error is thrown handler is a background task.
  762. Args:
  763. state: The state class.
  764. tmp_path: Temporary path.
  765. token: a Token.
  766. """
  767. state._tmp_path = tmp_path
  768. app = App(_state=State)
  769. request_mock = unittest.mock.Mock()
  770. request_mock.headers = {
  771. "reflex-client-token": token,
  772. "reflex-event-handler": f"{state.get_full_name()}.bg_upload",
  773. }
  774. async def form():
  775. files_mock = unittest.mock.Mock()
  776. def getlist(key: str):
  777. assert key == "files"
  778. return [unittest.mock.Mock(filename="image1.jpg")]
  779. files_mock.getlist = getlist
  780. return files_mock
  781. request_mock.form = form
  782. fn = upload(app)
  783. with pytest.raises(TypeError) as err:
  784. await fn(request_mock)
  785. assert (
  786. err.value.args[0]
  787. == f"@rx.event(background=True) is not supported for upload handler `{state.get_full_name()}.bg_upload`."
  788. )
  789. if isinstance(app.state_manager, StateManagerRedis):
  790. await app.state_manager.close()
  791. class DynamicState(BaseState):
  792. """State class for testing dynamic route var.
  793. This is defined at module level because event handlers cannot be addressed
  794. correctly when the class is defined as a local.
  795. There are several counters:
  796. * loaded: counts how many times `on_load` was triggered by the hydrate middleware
  797. * counter: counts how many times `on_counter` was triggered by a non-navigational event
  798. -> these events should NOT trigger reload or recalculation of router_data dependent vars
  799. * side_effect_counter: counts how many times a computed var was
  800. recalculated when the dynamic route var was dirty
  801. """
  802. is_hydrated: bool = False
  803. loaded: int = 0
  804. counter: int = 0
  805. @rx.event
  806. def on_load(self):
  807. """Event handler for page on_load, should trigger for all navigation events."""
  808. self.loaded = self.loaded + 1
  809. @rx.event
  810. def on_counter(self):
  811. """Increment the counter var."""
  812. self.counter = self.counter + 1
  813. @computed_var
  814. def comp_dynamic(self) -> str:
  815. """A computed var that depends on the dynamic var.
  816. Returns:
  817. same as self.dynamic
  818. """
  819. return self.dynamic
  820. on_load_internal = OnLoadInternalState.on_load_internal.fn # pyright: ignore [reportFunctionMemberAccess]
  821. def test_dynamic_arg_shadow(
  822. index_page: ComponentCallable,
  823. windows_platform: bool,
  824. token: str,
  825. app_module_mock: unittest.mock.Mock,
  826. mocker,
  827. ):
  828. """Create app with dynamic route var and try to add a page with a dynamic arg that shadows a state var.
  829. Args:
  830. index_page: The index page.
  831. windows_platform: Whether the system is windows.
  832. token: a Token.
  833. app_module_mock: Mocked app module.
  834. mocker: pytest mocker object.
  835. """
  836. arg_name = "counter"
  837. route = f"/test/[{arg_name}]"
  838. app = app_module_mock.app = App(_state=DynamicState)
  839. assert app._state is not None
  840. with pytest.raises(NameError):
  841. app.add_page(index_page, route=route, on_load=DynamicState.on_load)
  842. def test_multiple_dynamic_args(
  843. index_page: ComponentCallable,
  844. windows_platform: bool,
  845. token: str,
  846. app_module_mock: unittest.mock.Mock,
  847. mocker,
  848. ):
  849. """Create app with multiple dynamic route vars with the same name.
  850. Args:
  851. index_page: The index page.
  852. windows_platform: Whether the system is windows.
  853. token: a Token.
  854. app_module_mock: Mocked app module.
  855. mocker: pytest mocker object.
  856. """
  857. arg_name = "my_arg"
  858. route = f"/test/[{arg_name}]"
  859. route2 = f"/test2/[{arg_name}]"
  860. app = app_module_mock.app = App(_state=EmptyState)
  861. app.add_page(index_page, route=route)
  862. app.add_page(index_page, route=route2)
  863. @pytest.mark.asyncio
  864. async def test_dynamic_route_var_route_change_completed_on_load(
  865. index_page: ComponentCallable,
  866. windows_platform: bool,
  867. token: str,
  868. app_module_mock: unittest.mock.Mock,
  869. mocker,
  870. ):
  871. """Create app with dynamic route var, and simulate navigation.
  872. on_load should fire, allowing any additional vars to be updated before the
  873. initial page hydrate.
  874. Args:
  875. index_page: The index page.
  876. windows_platform: Whether the system is windows.
  877. token: a Token.
  878. app_module_mock: Mocked app module.
  879. mocker: pytest mocker object.
  880. """
  881. arg_name = "dynamic"
  882. route = f"/test/[{arg_name}]"
  883. app = app_module_mock.app = App(_state=DynamicState)
  884. assert app._state is not None
  885. assert arg_name not in app._state.vars
  886. app.add_page(index_page, route=route, on_load=DynamicState.on_load)
  887. assert arg_name in app._state.vars
  888. assert arg_name in app._state.computed_vars
  889. assert app._state.computed_vars[arg_name]._deps(objclass=DynamicState) == {
  890. DynamicState.get_full_name(): {constants.ROUTER},
  891. }
  892. assert constants.ROUTER in app._state()._var_dependencies
  893. substate_token = _substate_key(token, DynamicState)
  894. sid = "mock_sid"
  895. client_ip = "127.0.0.1"
  896. async with app.state_manager.modify_state(substate_token) as state:
  897. state.router_data = {"simulate": "hydrated"}
  898. assert state.dynamic == ""
  899. exp_vals = ["foo", "foobar", "baz"]
  900. def _event(name, val, **kwargs):
  901. return Event(
  902. token=kwargs.pop("token", token),
  903. name=name,
  904. router_data=kwargs.pop(
  905. "router_data", {"pathname": route, "query": {arg_name: val}}
  906. ),
  907. payload=kwargs.pop("payload", {}),
  908. **kwargs,
  909. )
  910. def _dynamic_state_event(name, val, **kwargs):
  911. return _event(
  912. name=format.format_event_handler(getattr(DynamicState, name)),
  913. val=val,
  914. **kwargs,
  915. )
  916. prev_exp_val = ""
  917. for exp_index, exp_val in enumerate(exp_vals):
  918. on_load_internal = _event(
  919. name=f"{state.get_full_name()}.{constants.CompileVars.ON_LOAD_INTERNAL.rpartition('.')[2]}",
  920. val=exp_val,
  921. )
  922. exp_router_data = {
  923. "headers": {},
  924. "ip": client_ip,
  925. "sid": sid,
  926. "token": token,
  927. **on_load_internal.router_data,
  928. }
  929. exp_router = RouterData(exp_router_data)
  930. process_coro = process(
  931. app,
  932. event=on_load_internal,
  933. sid=sid,
  934. headers={},
  935. client_ip=client_ip,
  936. )
  937. update = await process_coro.__anext__()
  938. # route change (on_load_internal) triggers: [call on_load events, call set_is_hydrated(True)]
  939. assert update == StateUpdate(
  940. delta={
  941. state.get_name(): {
  942. arg_name: exp_val,
  943. f"comp_{arg_name}": exp_val,
  944. constants.CompileVars.IS_HYDRATED: False,
  945. "router": exp_router,
  946. }
  947. },
  948. events=[
  949. _dynamic_state_event(
  950. name="on_load",
  951. val=exp_val,
  952. ),
  953. _event(
  954. name=f"{State.get_name()}.set_is_hydrated",
  955. payload={"value": True},
  956. val=exp_val,
  957. router_data={},
  958. ),
  959. ],
  960. )
  961. if isinstance(app.state_manager, StateManagerRedis):
  962. # When redis is used, the state is not updated until the processing is complete
  963. state = await app.state_manager.get_state(substate_token)
  964. assert state.dynamic == prev_exp_val
  965. # complete the processing
  966. with pytest.raises(StopAsyncIteration):
  967. await process_coro.__anext__()
  968. # check that router data was written to the state_manager store
  969. state = await app.state_manager.get_state(substate_token)
  970. assert state.dynamic == exp_val
  971. process_coro = process(
  972. app,
  973. event=_dynamic_state_event(name="on_load", val=exp_val),
  974. sid=sid,
  975. headers={},
  976. client_ip=client_ip,
  977. )
  978. on_load_update = await process_coro.__anext__()
  979. assert on_load_update == StateUpdate(
  980. delta={
  981. state.get_name(): {
  982. "loaded": exp_index + 1,
  983. },
  984. },
  985. events=[],
  986. )
  987. # complete the processing
  988. with pytest.raises(StopAsyncIteration):
  989. await process_coro.__anext__()
  990. process_coro = process(
  991. app,
  992. event=_dynamic_state_event(
  993. name="set_is_hydrated", payload={"value": True}, val=exp_val
  994. ),
  995. sid=sid,
  996. headers={},
  997. client_ip=client_ip,
  998. )
  999. on_set_is_hydrated_update = await process_coro.__anext__()
  1000. assert on_set_is_hydrated_update == StateUpdate(
  1001. delta={
  1002. state.get_name(): {
  1003. "is_hydrated": True,
  1004. },
  1005. },
  1006. events=[],
  1007. )
  1008. # complete the processing
  1009. with pytest.raises(StopAsyncIteration):
  1010. await process_coro.__anext__()
  1011. # a simple state update event should NOT trigger on_load or route var side effects
  1012. process_coro = process(
  1013. app,
  1014. event=_dynamic_state_event(name="on_counter", val=exp_val),
  1015. sid=sid,
  1016. headers={},
  1017. client_ip=client_ip,
  1018. )
  1019. update = await process_coro.__anext__()
  1020. assert update == StateUpdate(
  1021. delta={
  1022. state.get_name(): {
  1023. "counter": exp_index + 1,
  1024. }
  1025. },
  1026. events=[],
  1027. )
  1028. # complete the processing
  1029. with pytest.raises(StopAsyncIteration):
  1030. await process_coro.__anext__()
  1031. prev_exp_val = exp_val
  1032. state = await app.state_manager.get_state(substate_token)
  1033. assert state.loaded == len(exp_vals)
  1034. assert state.counter == len(exp_vals)
  1035. if isinstance(app.state_manager, StateManagerRedis):
  1036. await app.state_manager.close()
  1037. @pytest.mark.asyncio
  1038. async def test_process_events(mocker, token: str):
  1039. """Test that an event is processed properly and that it is postprocessed
  1040. n+1 times. Also check that the processing flag of the last stateupdate is set to
  1041. False.
  1042. Args:
  1043. mocker: mocker object.
  1044. token: a Token.
  1045. """
  1046. router_data = {
  1047. "pathname": "/",
  1048. "query": {},
  1049. "token": token,
  1050. "sid": "mock_sid",
  1051. "headers": {},
  1052. "ip": "127.0.0.1",
  1053. }
  1054. app = App(_state=GenState)
  1055. mocker.patch.object(app, "_postprocess", AsyncMock())
  1056. event = Event(
  1057. token=token,
  1058. name=f"{GenState.get_name()}.go",
  1059. payload={"c": 5},
  1060. router_data=router_data,
  1061. )
  1062. async with app.state_manager.modify_state(event.substate_token) as state:
  1063. state.router_data = {"simulate": "hydrated"}
  1064. async for _update in process(app, event, "mock_sid", {}, "127.0.0.1"):
  1065. pass
  1066. assert (await app.state_manager.get_state(event.substate_token)).value == 5
  1067. assert app._postprocess.call_count == 6 # pyright: ignore [reportFunctionMemberAccess]
  1068. if isinstance(app.state_manager, StateManagerRedis):
  1069. await app.state_manager.close()
  1070. @pytest.mark.parametrize(
  1071. ("state", "overlay_component", "exp_page_child"),
  1072. [
  1073. (None, default_overlay_component, None),
  1074. (None, None, None),
  1075. (None, Text.create("foo"), Text),
  1076. (State, default_overlay_component, Fragment),
  1077. (State, None, None),
  1078. (State, Text.create("foo"), Text),
  1079. (State, lambda: Text.create("foo"), Text),
  1080. ],
  1081. )
  1082. def test_overlay_component(
  1083. state: type[State] | None,
  1084. overlay_component: Component | ComponentCallable | None,
  1085. exp_page_child: type[Component] | None,
  1086. ):
  1087. """Test that the overlay component is set correctly.
  1088. Args:
  1089. state: The state class to pass to App.
  1090. overlay_component: The overlay_component to pass to App.
  1091. exp_page_child: The type of the expected child in the page fragment.
  1092. """
  1093. app = App(_state=state, overlay_component=overlay_component)
  1094. app._setup_overlay_component()
  1095. if exp_page_child is None:
  1096. assert app.overlay_component is None
  1097. elif isinstance(exp_page_child, OverlayFragment):
  1098. assert app.overlay_component is not None
  1099. generated_component = app._generate_component(app.overlay_component)
  1100. assert isinstance(generated_component, OverlayFragment)
  1101. assert isinstance(
  1102. generated_component.children[0],
  1103. Cond, # ConnectionModal is a Cond under the hood
  1104. )
  1105. else:
  1106. assert app.overlay_component is not None
  1107. assert isinstance(
  1108. app._generate_component(app.overlay_component),
  1109. exp_page_child,
  1110. )
  1111. app.add_page(rx.box("Index"), route="/test")
  1112. # overlay components are wrapped during compile only
  1113. app._compile_page("test")
  1114. app._setup_overlay_component()
  1115. page = app._pages["test"]
  1116. if exp_page_child is not None:
  1117. assert len(page.children) == 3
  1118. children_types = (type(child) for child in page.children)
  1119. assert exp_page_child in children_types # pyright: ignore [reportOperatorIssue]
  1120. else:
  1121. assert len(page.children) == 2
  1122. @pytest.fixture
  1123. def compilable_app(tmp_path) -> Generator[tuple[App, Path], None, None]:
  1124. """Fixture for an app that can be compiled.
  1125. Args:
  1126. tmp_path: Temporary path.
  1127. Yields:
  1128. Tuple containing (app instance, Path to ".web" directory)
  1129. The working directory is set to the app dir (parent of .web),
  1130. allowing app.compile() to be called.
  1131. """
  1132. app_path = tmp_path / "app"
  1133. web_dir = app_path / ".web"
  1134. web_dir.mkdir(parents=True)
  1135. (web_dir / constants.PackageJson.PATH).touch()
  1136. app = App(theme=None)
  1137. app._get_frontend_packages = unittest.mock.Mock()
  1138. with chdir(app_path):
  1139. yield app, web_dir
  1140. @pytest.mark.parametrize(
  1141. "react_strict_mode",
  1142. [True, False],
  1143. )
  1144. def test_app_wrap_compile_theme(
  1145. react_strict_mode: bool, compilable_app: tuple[App, Path], mocker
  1146. ):
  1147. """Test that the radix theme component wraps the app.
  1148. Args:
  1149. react_strict_mode: Whether to use React Strict Mode.
  1150. compilable_app: compilable_app fixture.
  1151. mocker: pytest mocker object.
  1152. """
  1153. conf = rx.Config(app_name="testing", react_strict_mode=react_strict_mode)
  1154. mocker.patch("reflex.config._get_config", return_value=conf)
  1155. app, web_dir = compilable_app
  1156. app.theme = rx.theme(accent_color="plum")
  1157. app._compile()
  1158. app_js_contents = (web_dir / "pages" / "_app.js").read_text()
  1159. app_js_lines = [
  1160. line.strip() for line in app_js_contents.splitlines() if line.strip()
  1161. ]
  1162. lines = "".join(app_js_lines)
  1163. expected = (
  1164. "function AppWrap({children}) {"
  1165. "return ("
  1166. + ("jsx(StrictMode,{}," if react_strict_mode else "")
  1167. + "jsx(RadixThemesColorModeProvider,{},"
  1168. "jsx(RadixThemesTheme,{accentColor:\"plum\",css:{...theme.styles.global[':root'], ...theme.styles.global.body}},"
  1169. "jsx(Fragment,{},"
  1170. "jsx(MemoizedToastProvider,{},),"
  1171. "jsx(Fragment,{},"
  1172. "children,"
  1173. "),"
  1174. "),"
  1175. "),"
  1176. ")" + (",)" if react_strict_mode else "") + ")"
  1177. "}"
  1178. )
  1179. assert expected in lines
  1180. @pytest.mark.parametrize(
  1181. "react_strict_mode",
  1182. [True, False],
  1183. )
  1184. def test_app_wrap_priority(
  1185. react_strict_mode: bool, compilable_app: tuple[App, Path], mocker
  1186. ):
  1187. """Test that the app wrap components are wrapped in the correct order.
  1188. Args:
  1189. react_strict_mode: Whether to use React Strict Mode.
  1190. compilable_app: compilable_app fixture.
  1191. mocker: pytest mocker object.
  1192. """
  1193. conf = rx.Config(app_name="testing", react_strict_mode=react_strict_mode)
  1194. mocker.patch("reflex.config._get_config", return_value=conf)
  1195. app, web_dir = compilable_app
  1196. class Fragment1(Component):
  1197. tag = "Fragment1"
  1198. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]: # pyright: ignore [reportIncompatibleMethodOverride]
  1199. return {(99, "Box"): rx.box()}
  1200. class Fragment2(Component):
  1201. tag = "Fragment2"
  1202. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]: # pyright: ignore [reportIncompatibleMethodOverride]
  1203. return {(50, "Text"): rx.text()}
  1204. class Fragment3(Component):
  1205. tag = "Fragment3"
  1206. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]: # pyright: ignore [reportIncompatibleMethodOverride]
  1207. return {(10, "Fragment2"): Fragment2.create()}
  1208. def page():
  1209. return Fragment1.create(Fragment3.create())
  1210. app.add_page(page)
  1211. app._compile()
  1212. app_js_contents = (web_dir / "pages" / "_app.js").read_text()
  1213. app_js_lines = [
  1214. line.strip() for line in app_js_contents.splitlines() if line.strip()
  1215. ]
  1216. lines = "".join(app_js_lines)
  1217. expected = (
  1218. "function AppWrap({children}) {"
  1219. "return ("
  1220. + ("jsx(StrictMode,{}," if react_strict_mode else "")
  1221. + "jsx(RadixThemesBox,{},"
  1222. 'jsx(RadixThemesText,{as:"p"},'
  1223. "jsx(RadixThemesColorModeProvider,{},"
  1224. "jsx(Fragment2,{},"
  1225. "jsx(Fragment,{},"
  1226. "jsx(MemoizedToastProvider,{},),"
  1227. "jsx(Fragment,{},"
  1228. "children"
  1229. ",),),),),)" + (",)" if react_strict_mode else "")
  1230. )
  1231. assert expected in lines
  1232. def test_app_state_determination():
  1233. """Test that the stateless status of an app is determined correctly."""
  1234. a1 = App()
  1235. assert a1._state is None
  1236. # No state, no router, no event handlers.
  1237. a1.add_page(rx.box("Index"), route="/")
  1238. assert a1._state is None
  1239. # Add a page with `on_load` enables state.
  1240. a1.add_page(rx.box("About"), route="/about", on_load=rx.console_log(""))
  1241. a1._compile_page("about")
  1242. assert a1._state is not None
  1243. a2 = App()
  1244. assert a2._state is None
  1245. # Referencing a state Var enables state.
  1246. a2.add_page(rx.box(rx.text(GenState.value)), route="/")
  1247. a2._compile_page("index")
  1248. assert a2._state is not None
  1249. a3 = App()
  1250. assert a3._state is None
  1251. # Referencing router enables state.
  1252. a3.add_page(rx.box(rx.text(State.router.page.full_path)), route="/")
  1253. a3._compile_page("index")
  1254. assert a3._state is not None
  1255. a4 = App()
  1256. assert a4._state is None
  1257. a4.add_page(rx.box(rx.button("Click", on_click=rx.console_log(""))), route="/")
  1258. assert a4._state is None
  1259. a4.add_page(
  1260. rx.box(rx.button("Click", on_click=DynamicState.on_counter)), route="/page2"
  1261. )
  1262. a4._compile_page("page2")
  1263. assert a4._state is not None
  1264. def test_raise_on_state():
  1265. """Test that the state is set."""
  1266. # state kwargs is deprecated, we just make sure the app is created anyway.
  1267. _app = App(_state=State)
  1268. assert _app._state is not None
  1269. assert issubclass(_app._state, State)
  1270. def test_call_app():
  1271. """Test that the app can be called."""
  1272. app = App()
  1273. app._compile = unittest.mock.Mock()
  1274. api = app()
  1275. assert isinstance(api, Starlette)
  1276. def test_app_with_optional_endpoints():
  1277. from reflex.components.core.upload import Upload
  1278. app = App()
  1279. Upload.is_used = True
  1280. app._add_optional_endpoints()
  1281. # TODO: verify the availability of the endpoints in app.api
  1282. def test_app_state_manager():
  1283. app = App()
  1284. with pytest.raises(ValueError):
  1285. app.state_manager
  1286. app._enable_state()
  1287. assert app.state_manager is not None
  1288. assert isinstance(
  1289. app.state_manager, (StateManagerMemory, StateManagerDisk, StateManagerRedis)
  1290. )
  1291. def test_generate_component():
  1292. def index():
  1293. return rx.box("Index")
  1294. def index_mismatch():
  1295. return rx.match(
  1296. 1,
  1297. (1, rx.box("Index")),
  1298. (2, "About"),
  1299. "Bar",
  1300. )
  1301. comp = App._generate_component(index)
  1302. assert isinstance(comp, Component)
  1303. with pytest.raises(exceptions.MatchTypeError):
  1304. App._generate_component(index_mismatch)
  1305. def test_add_page_component_returning_tuple():
  1306. """Test that a component or render method returning a
  1307. tuple is unpacked in a Fragment.
  1308. """
  1309. app = App()
  1310. def index():
  1311. return rx.text("first"), rx.text("second")
  1312. def page2():
  1313. return (rx.text("third"),)
  1314. app.add_page(index)
  1315. app.add_page(page2)
  1316. app._compile_page("index")
  1317. app._compile_page("page2")
  1318. fragment_wrapper = app._pages["index"].children[0]
  1319. assert isinstance(fragment_wrapper, Fragment)
  1320. first_text = fragment_wrapper.children[0]
  1321. assert isinstance(first_text, Text)
  1322. assert isinstance(first_text.children[0], Bare)
  1323. assert str(first_text.children[0].contents) == '"first"'
  1324. second_text = fragment_wrapper.children[1]
  1325. assert isinstance(second_text, Text)
  1326. assert isinstance(second_text.children[0], Bare)
  1327. assert str(second_text.children[0].contents) == '"second"'
  1328. # Test page with trailing comma.
  1329. page2_fragment_wrapper = app._pages["page2"].children[0]
  1330. assert isinstance(page2_fragment_wrapper, Fragment)
  1331. third_text = page2_fragment_wrapper.children[0]
  1332. assert isinstance(third_text, Text)
  1333. assert isinstance(third_text.children[0], Bare)
  1334. assert str(third_text.children[0].contents) == '"third"'
  1335. @pytest.mark.parametrize("export", (True, False))
  1336. def test_app_with_transpile_packages(compilable_app: tuple[App, Path], export: bool):
  1337. class C1(rx.Component):
  1338. library = "foo@1.2.3"
  1339. tag = "Foo"
  1340. transpile_packages: list[str] = ["foo"]
  1341. class C2(rx.Component):
  1342. library = "bar@4.5.6"
  1343. tag = "Bar"
  1344. transpile_packages: list[str] = ["bar@4.5.6"]
  1345. class C3(rx.NoSSRComponent):
  1346. library = "baz@7.8.10"
  1347. tag = "Baz"
  1348. transpile_packages: list[str] = ["baz@7.8.9"]
  1349. class C4(rx.NoSSRComponent):
  1350. library = "quuc@2.3.4"
  1351. tag = "Quuc"
  1352. transpile_packages: list[str] = ["quuc"]
  1353. class C5(rx.Component):
  1354. library = "quuc"
  1355. tag = "Quuc"
  1356. app, web_dir = compilable_app
  1357. page = Fragment.create(
  1358. C1.create(), C2.create(), C3.create(), C4.create(), C5.create()
  1359. )
  1360. app.add_page(page, route="/")
  1361. app._compile(export=export)
  1362. next_config = (web_dir / "next.config.js").read_text()
  1363. transpile_packages_match = re.search(r"transpilePackages: (\[.*?\])", next_config)
  1364. transpile_packages_json = transpile_packages_match.group(1) # pyright: ignore [reportOptionalMemberAccess]
  1365. transpile_packages = sorted(json.loads(transpile_packages_json))
  1366. assert transpile_packages == [
  1367. "bar",
  1368. "foo",
  1369. "quuc",
  1370. ]
  1371. if export:
  1372. assert 'output: "export"' in next_config
  1373. assert f'distDir: "{constants.Dirs.STATIC}"' in next_config
  1374. else:
  1375. assert 'output: "export"' not in next_config
  1376. assert f'distDir: "{constants.Dirs.STATIC}"' not in next_config
  1377. def test_app_with_valid_var_dependencies(compilable_app: tuple[App, Path]):
  1378. app, _ = compilable_app
  1379. class ValidDepState(BaseState):
  1380. base: int = 0
  1381. _backend: int = 0
  1382. @computed_var()
  1383. def foo(self) -> str:
  1384. return "foo"
  1385. @computed_var(deps=["_backend", "base", foo])
  1386. def bar(self) -> str:
  1387. return "bar"
  1388. class Child1(ValidDepState):
  1389. @computed_var(deps=["base", ValidDepState.bar])
  1390. def other(self) -> str:
  1391. return "other"
  1392. class Child2(ValidDepState):
  1393. @computed_var(deps=["base", Child1.other])
  1394. def other(self) -> str:
  1395. return "other"
  1396. app._state = ValidDepState
  1397. app._compile()
  1398. def test_app_with_invalid_var_dependencies(compilable_app: tuple[App, Path]):
  1399. app, _ = compilable_app
  1400. class InvalidDepState(BaseState):
  1401. @computed_var(deps=["foolksjdf"])
  1402. def bar(self) -> str:
  1403. return "bar"
  1404. app._state = InvalidDepState
  1405. with pytest.raises(exceptions.VarDependencyError):
  1406. app._compile()
  1407. # Test custom exception handlers
  1408. def valid_custom_handler(exception: Exception, logger: str = "test"):
  1409. print("Custom Backend Exception")
  1410. print(exception)
  1411. def custom_exception_handler_with_wrong_arg_order(
  1412. logger: str,
  1413. exception: Exception, # Should be first
  1414. ):
  1415. print("Custom Backend Exception")
  1416. print(exception)
  1417. def custom_exception_handler_with_wrong_argspec(
  1418. exception: str, # Should be Exception
  1419. ):
  1420. print("Custom Backend Exception")
  1421. print(exception)
  1422. class DummyExceptionHandler:
  1423. """Dummy exception handler class."""
  1424. def handle(self, exception: Exception):
  1425. """Handle the exception.
  1426. Args:
  1427. exception: The exception.
  1428. """
  1429. print("Custom Backend Exception")
  1430. print(exception)
  1431. custom_exception_handlers = {
  1432. "lambda": lambda exception: print("Custom Exception Handler", exception),
  1433. "wrong_argspec": custom_exception_handler_with_wrong_argspec,
  1434. "wrong_arg_order": custom_exception_handler_with_wrong_arg_order,
  1435. "valid": valid_custom_handler,
  1436. "partial": functools.partial(valid_custom_handler, logger="test"),
  1437. "method": DummyExceptionHandler().handle,
  1438. }
  1439. @pytest.mark.parametrize(
  1440. "handler_fn, expected",
  1441. [
  1442. pytest.param(
  1443. custom_exception_handlers["partial"],
  1444. pytest.raises(ValueError),
  1445. id="partial",
  1446. ),
  1447. pytest.param(
  1448. custom_exception_handlers["lambda"],
  1449. pytest.raises(ValueError),
  1450. id="lambda",
  1451. ),
  1452. pytest.param(
  1453. custom_exception_handlers["wrong_argspec"],
  1454. pytest.raises(ValueError),
  1455. id="wrong_argspec",
  1456. ),
  1457. pytest.param(
  1458. custom_exception_handlers["wrong_arg_order"],
  1459. pytest.raises(ValueError),
  1460. id="wrong_arg_order",
  1461. ),
  1462. pytest.param(
  1463. custom_exception_handlers["valid"],
  1464. does_not_raise(),
  1465. id="valid_handler",
  1466. ),
  1467. pytest.param(
  1468. custom_exception_handlers["method"],
  1469. does_not_raise(),
  1470. id="valid_class_method",
  1471. ),
  1472. ],
  1473. )
  1474. def test_frontend_exception_handler_validation(handler_fn, expected):
  1475. """Test that the custom frontend exception handler is properly validated.
  1476. Args:
  1477. handler_fn: The handler function.
  1478. expected: The expected result.
  1479. """
  1480. with expected:
  1481. rx.App(frontend_exception_handler=handler_fn)._validate_exception_handlers()
  1482. def backend_exception_handler_with_wrong_return_type(exception: Exception) -> int:
  1483. """Custom backend exception handler with wrong return type.
  1484. Args:
  1485. exception: The exception.
  1486. Returns:
  1487. int: The wrong return type.
  1488. """
  1489. print("Custom Backend Exception")
  1490. print(exception)
  1491. return 5
  1492. @pytest.mark.parametrize(
  1493. "handler_fn, expected",
  1494. [
  1495. pytest.param(
  1496. backend_exception_handler_with_wrong_return_type,
  1497. pytest.raises(ValueError),
  1498. id="wrong_return_type",
  1499. ),
  1500. pytest.param(
  1501. custom_exception_handlers["partial"],
  1502. pytest.raises(ValueError),
  1503. id="partial",
  1504. ),
  1505. pytest.param(
  1506. custom_exception_handlers["lambda"],
  1507. pytest.raises(ValueError),
  1508. id="lambda",
  1509. ),
  1510. pytest.param(
  1511. custom_exception_handlers["wrong_argspec"],
  1512. pytest.raises(ValueError),
  1513. id="wrong_argspec",
  1514. ),
  1515. pytest.param(
  1516. custom_exception_handlers["wrong_arg_order"],
  1517. pytest.raises(ValueError),
  1518. id="wrong_arg_order",
  1519. ),
  1520. pytest.param(
  1521. custom_exception_handlers["valid"],
  1522. does_not_raise(),
  1523. id="valid_handler",
  1524. ),
  1525. pytest.param(
  1526. custom_exception_handlers["method"],
  1527. does_not_raise(),
  1528. id="valid_class_method",
  1529. ),
  1530. ],
  1531. )
  1532. def test_backend_exception_handler_validation(handler_fn, expected):
  1533. """Test that the custom backend exception handler is properly validated.
  1534. Args:
  1535. handler_fn: The handler function.
  1536. expected: The expected result.
  1537. """
  1538. with expected:
  1539. rx.App(backend_exception_handler=handler_fn)._validate_exception_handlers()