test_app.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import os.path
  2. from typing import List, Tuple, Type
  3. import pytest
  4. from pynecone.app import App, DefaultState
  5. from pynecone.components import Box
  6. from pynecone.event import Event
  7. from pynecone.middleware import HydrateMiddleware
  8. from pynecone.state import State
  9. from pynecone.style import Style
  10. @pytest.fixture
  11. def app() -> App:
  12. """A base app.
  13. Returns:
  14. The app.
  15. """
  16. return App()
  17. @pytest.fixture
  18. def index_page():
  19. """An index page.
  20. Returns:
  21. The index page.
  22. """
  23. def index():
  24. return Box.create("Index")
  25. return index
  26. @pytest.fixture
  27. def about_page():
  28. """An about page.
  29. Returns:
  30. The about page.
  31. """
  32. def about():
  33. return Box.create("About")
  34. return about
  35. @pytest.fixture()
  36. def TestState() -> Type[State]:
  37. """A default state.
  38. Returns:
  39. A default state.
  40. """
  41. class TestState(State):
  42. var: int
  43. return TestState
  44. def test_default_app(app: App):
  45. """Test creating an app with no args.
  46. Args:
  47. app: The app to test.
  48. """
  49. assert app.state() == DefaultState()
  50. assert app.middleware == [HydrateMiddleware()]
  51. assert app.style == Style()
  52. def test_add_page_default_route(app: App, index_page, about_page):
  53. """Test adding a page to an app.
  54. Args:
  55. app: The app to test.
  56. index_page: The index page.
  57. about_page: The about page.
  58. """
  59. assert app.pages == {}
  60. app.add_page(index_page)
  61. assert set(app.pages.keys()) == {"index"}
  62. app.add_page(about_page)
  63. assert set(app.pages.keys()) == {"index", "about"}
  64. def test_add_page_set_route(app: App, index_page, windows_platform: bool):
  65. """Test adding a page to an app.
  66. Args:
  67. app: The app to test.
  68. index_page: The index page.
  69. windows_platform: Whether the system is windows.
  70. """
  71. route = "test" if windows_platform else "/test"
  72. assert app.pages == {}
  73. app.add_page(index_page, route=route)
  74. assert set(app.pages.keys()) == {"test"}
  75. def test_add_page_set_route_nested(app: App, index_page, windows_platform: bool):
  76. """Test adding a page to an app.
  77. Args:
  78. app: The app to test.
  79. index_page: The index page.
  80. windows_platform: Whether the system is windows.
  81. """
  82. route = "test\\nested" if windows_platform else "/test/nested"
  83. assert app.pages == {}
  84. app.add_page(index_page, route=route)
  85. assert set(app.pages.keys()) == {route.strip(os.path.sep)}
  86. def test_initialize_with_state(TestState: Type[State]):
  87. """Test setting the state of an app.
  88. Args:
  89. TestState: The default state.
  90. """
  91. app = App(state=TestState)
  92. assert app.state == TestState
  93. # Get a state for a given token.
  94. token = "token"
  95. state = app.state_manager.get_state(token)
  96. assert isinstance(state, TestState)
  97. assert state.var == 0 # type: ignore
  98. def test_set_and_get_state(TestState: Type[State]):
  99. """Test setting and getting the state of an app with different tokens.
  100. Args:
  101. TestState: The default state.
  102. """
  103. app = App(state=TestState)
  104. # Create two tokens.
  105. token1 = "token1"
  106. token2 = "token2"
  107. # Get the default state for each token.
  108. state1 = app.state_manager.get_state(token1)
  109. state2 = app.state_manager.get_state(token2)
  110. assert state1.var == 0 # type: ignore
  111. assert state2.var == 0 # type: ignore
  112. # Set the vars to different values.
  113. state1.var = 1
  114. state2.var = 2
  115. app.state_manager.set_state(token1, state1)
  116. app.state_manager.set_state(token2, state2)
  117. # Get the states again and check the values.
  118. state1 = app.state_manager.get_state(token1)
  119. state2 = app.state_manager.get_state(token2)
  120. assert state1.var == 1 # type: ignore
  121. assert state2.var == 2 # type: ignore
  122. @pytest.mark.asyncio
  123. @pytest.mark.parametrize(
  124. "event_tuples",
  125. [
  126. pytest.param(
  127. [
  128. (
  129. "test_state.make_friend",
  130. {"test_state": {"plain_friends": ["Tommy", "another-fd"]}},
  131. ),
  132. (
  133. "test_state.change_first_friend",
  134. {"test_state": {"plain_friends": ["Jenny", "another-fd"]}},
  135. ),
  136. ],
  137. id="append then __setitem__",
  138. ),
  139. pytest.param(
  140. [
  141. (
  142. "test_state.unfriend_first_friend",
  143. {"test_state": {"plain_friends": []}},
  144. ),
  145. (
  146. "test_state.make_friend",
  147. {"test_state": {"plain_friends": ["another-fd"]}},
  148. ),
  149. ],
  150. id="delitem then append",
  151. ),
  152. pytest.param(
  153. [
  154. (
  155. "test_state.make_friends_with_colleagues",
  156. {"test_state": {"plain_friends": ["Tommy", "Peter", "Jimmy"]}},
  157. ),
  158. (
  159. "test_state.remove_tommy",
  160. {"test_state": {"plain_friends": ["Peter", "Jimmy"]}},
  161. ),
  162. (
  163. "test_state.remove_last_friend",
  164. {"test_state": {"plain_friends": ["Peter"]}},
  165. ),
  166. (
  167. "test_state.unfriend_all_friends",
  168. {"test_state": {"plain_friends": []}},
  169. ),
  170. ],
  171. id="extend, remove, pop, clear",
  172. ),
  173. pytest.param(
  174. [
  175. (
  176. "test_state.add_jimmy_to_second_group",
  177. {
  178. "test_state": {
  179. "friends_in_nested_list": [["Tommy"], ["Jenny", "Jimmy"]]
  180. }
  181. },
  182. ),
  183. (
  184. "test_state.remove_first_person_from_first_group",
  185. {
  186. "test_state": {
  187. "friends_in_nested_list": [[], ["Jenny", "Jimmy"]]
  188. }
  189. },
  190. ),
  191. (
  192. "test_state.remove_first_group",
  193. {"test_state": {"friends_in_nested_list": [["Jenny", "Jimmy"]]}},
  194. ),
  195. ],
  196. id="nested list",
  197. ),
  198. pytest.param(
  199. [
  200. (
  201. "test_state.add_jimmy_to_tommy_friends",
  202. {"test_state": {"friends_in_dict": {"Tommy": ["Jenny", "Jimmy"]}}},
  203. ),
  204. (
  205. "test_state.remove_jenny_from_tommy",
  206. {"test_state": {"friends_in_dict": {"Tommy": ["Jimmy"]}}},
  207. ),
  208. (
  209. "test_state.tommy_has_no_fds",
  210. {"test_state": {"friends_in_dict": {"Tommy": []}}},
  211. ),
  212. ],
  213. id="list in dict",
  214. ),
  215. ],
  216. )
  217. async def test_list_mutation_detection__plain_list(
  218. event_tuples: List[Tuple[str, List[str]]], list_mutation_state: State
  219. ):
  220. """Test list mutation detection
  221. when reassignment is not explicitly included in the logic.
  222. Args:
  223. event_tuples: From parametrization.
  224. list_mutation_state: A state with list mutation features.
  225. """
  226. for event_name, expected_delta in event_tuples:
  227. result = await list_mutation_state.process(
  228. Event(
  229. token="fake-token",
  230. name=event_name,
  231. router_data={"pathname": "/", "query": {}},
  232. payload={},
  233. )
  234. )
  235. assert result.delta == expected_delta
  236. @pytest.mark.asyncio
  237. @pytest.mark.parametrize(
  238. "event_tuples",
  239. [
  240. pytest.param(
  241. [
  242. (
  243. "test_state.add_age",
  244. {"test_state": {"details": {"name": "Tommy", "age": 20}}},
  245. ),
  246. (
  247. "test_state.change_name",
  248. {"test_state": {"details": {"name": "Jenny", "age": 20}}},
  249. ),
  250. (
  251. "test_state.remove_last_detail",
  252. {"test_state": {"details": {"name": "Jenny"}}},
  253. ),
  254. ],
  255. id="update then __setitem__",
  256. ),
  257. pytest.param(
  258. [
  259. (
  260. "test_state.clear_details",
  261. {"test_state": {"details": {}}},
  262. ),
  263. (
  264. "test_state.add_age",
  265. {"test_state": {"details": {"age": 20}}},
  266. ),
  267. ],
  268. id="delitem then update",
  269. ),
  270. pytest.param(
  271. [
  272. (
  273. "test_state.add_age",
  274. {"test_state": {"details": {"name": "Tommy", "age": 20}}},
  275. ),
  276. (
  277. "test_state.remove_name",
  278. {"test_state": {"details": {"age": 20}}},
  279. ),
  280. (
  281. "test_state.pop_out_age",
  282. {"test_state": {"details": {}}},
  283. ),
  284. ],
  285. id="add, remove, pop",
  286. ),
  287. pytest.param(
  288. [
  289. (
  290. "test_state.remove_home_address",
  291. {"test_state": {"address": [{}, {"work": "work address"}]}},
  292. ),
  293. (
  294. "test_state.add_street_to_home_address",
  295. {
  296. "test_state": {
  297. "address": [
  298. {"street": "street address"},
  299. {"work": "work address"},
  300. ]
  301. }
  302. },
  303. ),
  304. ],
  305. id="dict in list",
  306. ),
  307. pytest.param(
  308. [
  309. (
  310. "test_state.change_friend_name",
  311. {
  312. "test_state": {
  313. "friend_in_nested_dict": {
  314. "name": "Nikhil",
  315. "friend": {"name": "Tommy"},
  316. }
  317. }
  318. },
  319. ),
  320. (
  321. "test_state.add_friend_age",
  322. {
  323. "test_state": {
  324. "friend_in_nested_dict": {
  325. "name": "Nikhil",
  326. "friend": {"name": "Tommy", "age": 30},
  327. }
  328. }
  329. },
  330. ),
  331. (
  332. "test_state.remove_friend",
  333. {"test_state": {"friend_in_nested_dict": {"name": "Nikhil"}}},
  334. ),
  335. ],
  336. id="nested dict",
  337. ),
  338. ],
  339. )
  340. async def test_dict_mutation_detection__plain_list(
  341. event_tuples: List[Tuple[str, List[str]]], dict_mutation_state: State
  342. ):
  343. """Test dict mutation detection
  344. when reassignment is not explicitly included in the logic.
  345. Args:
  346. event_tuples: From parametrization.
  347. dict_mutation_state: A state with dict mutation features.
  348. """
  349. for event_name, expected_delta in event_tuples:
  350. result = await dict_mutation_state.process(
  351. Event(
  352. token="fake-token",
  353. name=event_name,
  354. router_data={"pathname": "/", "query": {}},
  355. payload={},
  356. )
  357. )
  358. assert result.delta == expected_delta