test_state.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. from typing import Dict, List
  2. import pytest
  3. from plotly.graph_objects import Figure
  4. from pynecone.base import Base
  5. from pynecone.constants import RouteVar
  6. from pynecone.event import Event
  7. from pynecone.state import State
  8. from pynecone.utils import format
  9. from pynecone.var import BaseVar, ComputedVar
  10. class Object(Base):
  11. """A test object fixture."""
  12. prop1: int = 42
  13. prop2: str = "hello"
  14. class TestState(State):
  15. """A test state."""
  16. # Set this class as not test one
  17. __test__ = False
  18. num1: int
  19. num2: float = 3.14
  20. key: str
  21. array: List[float] = [1, 2, 3.14]
  22. mapping: Dict[str, List[int]] = {"a": [1, 2, 3], "b": [4, 5, 6]}
  23. obj: Object = Object()
  24. complex: Dict[int, Object] = {1: Object(), 2: Object()}
  25. fig: Figure = Figure()
  26. @ComputedVar
  27. def sum(self) -> float:
  28. """Dynamically sum the numbers.
  29. Returns:
  30. The sum of the numbers.
  31. """
  32. return self.num1 + self.num2
  33. @ComputedVar
  34. def upper(self) -> str:
  35. """Uppercase the key.
  36. Returns:
  37. The uppercased key.
  38. """
  39. return self.key.upper()
  40. def do_something(self):
  41. """Do something."""
  42. pass
  43. class ChildState(TestState):
  44. """A child state fixture."""
  45. value: str
  46. count: int = 23
  47. def change_both(self, value: str, count: int):
  48. """Change both the value and count.
  49. Args:
  50. value: The new value.
  51. count: The new count.
  52. """
  53. self.value = value.upper()
  54. self.count = count * 2
  55. class ChildState2(TestState):
  56. """A child state fixture."""
  57. value: str
  58. class GrandchildState(ChildState):
  59. """A grandchild state fixture."""
  60. value2: str
  61. def do_nothing(self):
  62. """Do something."""
  63. pass
  64. @pytest.fixture
  65. def test_state() -> TestState:
  66. """A state.
  67. Returns:
  68. A test state.
  69. """
  70. return TestState() # type: ignore
  71. @pytest.fixture
  72. def child_state(test_state) -> ChildState:
  73. """A child state.
  74. Args:
  75. test_state: A test state.
  76. Returns:
  77. A test child state.
  78. """
  79. child_state = test_state.get_substate(["child_state"])
  80. assert child_state is not None
  81. return child_state
  82. @pytest.fixture
  83. def child_state2(test_state) -> ChildState2:
  84. """A second child state.
  85. Args:
  86. test_state: A test state.
  87. Returns:
  88. A second test child state.
  89. """
  90. child_state2 = test_state.get_substate(["child_state2"])
  91. assert child_state2 is not None
  92. return child_state2
  93. @pytest.fixture
  94. def grandchild_state(child_state) -> GrandchildState:
  95. """A state.
  96. Args:
  97. child_state: A child state.
  98. Returns:
  99. A test state.
  100. """
  101. grandchild_state = child_state.get_substate(["grandchild_state"])
  102. assert grandchild_state is not None
  103. return grandchild_state
  104. def test_base_class_vars(test_state):
  105. """Test that the class vars are set correctly.
  106. Args:
  107. test_state: A state.
  108. """
  109. fields = test_state.get_fields()
  110. cls = type(test_state)
  111. for field in fields:
  112. if field in (
  113. "parent_state",
  114. "substates",
  115. "dirty_vars",
  116. "dirty_substates",
  117. "router_data",
  118. ):
  119. continue
  120. prop = getattr(cls, field)
  121. assert isinstance(prop, BaseVar)
  122. assert prop.name == field
  123. assert cls.num1.type_ == int
  124. assert cls.num2.type_ == float
  125. assert cls.key.type_ == str
  126. def test_computed_class_var(test_state):
  127. """Test that the class computed vars are set correctly.
  128. Args:
  129. test_state: A state.
  130. """
  131. cls = type(test_state)
  132. vars = [(prop.name, prop.type_) for prop in cls.computed_vars.values()]
  133. assert ("sum", float) in vars
  134. assert ("upper", str) in vars
  135. def test_class_vars(test_state):
  136. """Test that the class vars are set correctly.
  137. Args:
  138. test_state: A state.
  139. """
  140. cls = type(test_state)
  141. assert set(cls.vars.keys()) == {
  142. "num1",
  143. "num2",
  144. "key",
  145. "array",
  146. "mapping",
  147. "obj",
  148. "complex",
  149. "sum",
  150. "upper",
  151. "fig",
  152. }
  153. def test_event_handlers(test_state):
  154. """Test that event handler is set correctly.
  155. Args:
  156. test_state: A state.
  157. """
  158. expected = {
  159. "do_something",
  160. "set_array",
  161. "set_complex",
  162. "set_fig",
  163. "set_key",
  164. "set_mapping",
  165. "set_num1",
  166. "set_num2",
  167. "set_obj",
  168. }
  169. cls = type(test_state)
  170. assert set(cls.event_handlers.keys()).intersection(expected) == expected
  171. def test_default_value(test_state):
  172. """Test that the default value of a var is correct.
  173. Args:
  174. test_state: A state.
  175. """
  176. assert test_state.num1 == 0
  177. assert test_state.num2 == 3.14
  178. assert test_state.key == ""
  179. assert test_state.sum == 3.14
  180. assert test_state.upper == ""
  181. def test_computed_vars(test_state):
  182. """Test that the computed var is computed correctly.
  183. Args:
  184. test_state: A state.
  185. """
  186. test_state.num1 = 1
  187. test_state.num2 = 4
  188. assert test_state.sum == 5
  189. test_state.key = "hello world"
  190. assert test_state.upper == "HELLO WORLD"
  191. def test_dict(test_state):
  192. """Test that the dict representation of a state is correct.
  193. Args:
  194. test_state: A state.
  195. """
  196. substates = {"child_state", "child_state2"}
  197. assert set(test_state.dict().keys()) == set(test_state.vars.keys()) | substates
  198. assert (
  199. set(test_state.dict(include_computed=False).keys())
  200. == set(test_state.base_vars) | substates
  201. )
  202. def test_default_setters(test_state):
  203. """Test that we can set default values.
  204. Args:
  205. test_state: A state.
  206. """
  207. for prop_name in test_state.base_vars:
  208. # Each base var should have a default setter.
  209. assert hasattr(test_state, f"set_{prop_name}")
  210. def test_class_indexing_with_vars():
  211. """Test that we can index into a state var with another var."""
  212. prop = TestState.array[TestState.num1]
  213. assert str(prop) == "{test_state.array.at(test_state.num1)}"
  214. prop = TestState.mapping["a"][TestState.num1]
  215. assert str(prop) == '{test_state.mapping["a"].at(test_state.num1)}'
  216. def test_class_attributes():
  217. """Test that we can get class attributes."""
  218. prop = TestState.obj.prop1
  219. assert str(prop) == "{test_state.obj.prop1}"
  220. prop = TestState.complex[1].prop1
  221. assert str(prop) == "{test_state.complex[1].prop1}"
  222. def test_get_parent_state():
  223. """Test getting the parent state."""
  224. assert TestState.get_parent_state() is None
  225. assert ChildState.get_parent_state() == TestState
  226. assert ChildState2.get_parent_state() == TestState
  227. assert GrandchildState.get_parent_state() == ChildState
  228. def test_get_substates():
  229. """Test getting the substates."""
  230. assert TestState.get_substates() == {ChildState, ChildState2}
  231. assert ChildState.get_substates() == {GrandchildState}
  232. assert ChildState2.get_substates() == set()
  233. assert GrandchildState.get_substates() == set()
  234. def test_get_name():
  235. """Test getting the name of a state."""
  236. assert TestState.get_name() == "test_state"
  237. assert ChildState.get_name() == "child_state"
  238. assert ChildState2.get_name() == "child_state2"
  239. assert GrandchildState.get_name() == "grandchild_state"
  240. def test_get_full_name():
  241. """Test getting the full name."""
  242. assert TestState.get_full_name() == "test_state"
  243. assert ChildState.get_full_name() == "test_state.child_state"
  244. assert ChildState2.get_full_name() == "test_state.child_state2"
  245. assert GrandchildState.get_full_name() == "test_state.child_state.grandchild_state"
  246. def test_get_class_substate():
  247. """Test getting the substate of a class."""
  248. assert TestState.get_class_substate(("child_state",)) == ChildState
  249. assert TestState.get_class_substate(("child_state2",)) == ChildState2
  250. assert ChildState.get_class_substate(("grandchild_state",)) == GrandchildState
  251. assert (
  252. TestState.get_class_substate(("child_state", "grandchild_state"))
  253. == GrandchildState
  254. )
  255. with pytest.raises(ValueError):
  256. TestState.get_class_substate(("invalid_child",))
  257. with pytest.raises(ValueError):
  258. TestState.get_class_substate(
  259. (
  260. "child_state",
  261. "invalid_child",
  262. )
  263. )
  264. def test_get_class_var():
  265. """Test getting the var of a class."""
  266. assert TestState.get_class_var(("num1",)) == TestState.num1
  267. assert TestState.get_class_var(("num2",)) == TestState.num2
  268. assert ChildState.get_class_var(("value",)) == ChildState.value
  269. assert GrandchildState.get_class_var(("value2",)) == GrandchildState.value2
  270. assert TestState.get_class_var(("child_state", "value")) == ChildState.value
  271. assert (
  272. TestState.get_class_var(("child_state", "grandchild_state", "value2"))
  273. == GrandchildState.value2
  274. )
  275. assert (
  276. ChildState.get_class_var(("grandchild_state", "value2"))
  277. == GrandchildState.value2
  278. )
  279. with pytest.raises(ValueError):
  280. TestState.get_class_var(("invalid_var",))
  281. with pytest.raises(ValueError):
  282. TestState.get_class_var(
  283. (
  284. "child_state",
  285. "invalid_var",
  286. )
  287. )
  288. def test_set_class_var():
  289. """Test setting the var of a class."""
  290. with pytest.raises(AttributeError):
  291. TestState.num3 # type: ignore
  292. TestState._set_var(BaseVar(name="num3", type_=int).set_state(TestState))
  293. var = TestState.num3 # type: ignore
  294. assert var.name == "num3"
  295. assert var.type_ == int
  296. assert var.state == TestState.get_full_name()
  297. def test_set_parent_and_substates(test_state, child_state, grandchild_state):
  298. """Test setting the parent and substates.
  299. Args:
  300. test_state: A state.
  301. child_state: A child state.
  302. grandchild_state: A grandchild state.
  303. """
  304. assert len(test_state.substates) == 2
  305. assert set(test_state.substates) == {"child_state", "child_state2"}
  306. assert child_state.parent_state == test_state
  307. assert len(child_state.substates) == 1
  308. assert set(child_state.substates) == {"grandchild_state"}
  309. assert grandchild_state.parent_state == child_state
  310. assert len(grandchild_state.substates) == 0
  311. def test_get_child_attribute(test_state, child_state, child_state2, grandchild_state):
  312. """Test getting the attribute of a state.
  313. Args:
  314. test_state: A state.
  315. child_state: A child state.
  316. child_state2: A child state.
  317. grandchild_state: A grandchild state.
  318. """
  319. assert test_state.num1 == 0
  320. assert child_state.value == ""
  321. assert child_state2.value == ""
  322. assert child_state.count == 23
  323. assert grandchild_state.value2 == ""
  324. with pytest.raises(AttributeError):
  325. test_state.invalid
  326. with pytest.raises(AttributeError):
  327. test_state.child_state.invalid
  328. with pytest.raises(AttributeError):
  329. test_state.child_state.grandchild_state.invalid
  330. def test_set_child_attribute(test_state, child_state, grandchild_state):
  331. """Test setting the attribute of a state.
  332. Args:
  333. test_state: A state.
  334. child_state: A child state.
  335. grandchild_state: A grandchild state.
  336. """
  337. test_state.num1 = 10
  338. assert test_state.num1 == 10
  339. assert child_state.num1 == 10
  340. assert grandchild_state.num1 == 10
  341. grandchild_state.num1 = 5
  342. assert test_state.num1 == 5
  343. assert child_state.num1 == 5
  344. assert grandchild_state.num1 == 5
  345. child_state.value = "test"
  346. assert child_state.value == "test"
  347. assert grandchild_state.value == "test"
  348. grandchild_state.value = "test2"
  349. assert child_state.value == "test2"
  350. assert grandchild_state.value == "test2"
  351. grandchild_state.value2 = "test3"
  352. assert grandchild_state.value2 == "test3"
  353. def test_get_substate(test_state, child_state, child_state2, grandchild_state):
  354. """Test getting the substate of a state.
  355. Args:
  356. test_state: A state.
  357. child_state: A child state.
  358. child_state2: A child state.
  359. grandchild_state: A grandchild state.
  360. """
  361. assert test_state.get_substate(("child_state",)) == child_state
  362. assert test_state.get_substate(("child_state2",)) == child_state2
  363. assert (
  364. test_state.get_substate(("child_state", "grandchild_state")) == grandchild_state
  365. )
  366. assert child_state.get_substate(("grandchild_state",)) == grandchild_state
  367. with pytest.raises(ValueError):
  368. test_state.get_substate(("invalid",))
  369. with pytest.raises(ValueError):
  370. test_state.get_substate(("child_state", "invalid"))
  371. with pytest.raises(ValueError):
  372. test_state.get_substate(("child_state", "grandchild_state", "invalid"))
  373. def test_set_dirty_var(test_state):
  374. """Test changing state vars marks the value as dirty.
  375. Args:
  376. test_state: A state.
  377. """
  378. # Initially there should be no dirty vars.
  379. assert test_state.dirty_vars == set()
  380. # Setting a var should mark it as dirty.
  381. test_state.num1 = 1
  382. assert test_state.dirty_vars == {"num1"}
  383. # Setting another var should mark it as dirty.
  384. test_state.num2 = 2
  385. assert test_state.dirty_vars == {"num1", "num2"}
  386. # Cleaning the state should remove all dirty vars.
  387. test_state.clean()
  388. assert test_state.dirty_vars == set()
  389. def test_set_dirty_substate(test_state, child_state, child_state2, grandchild_state):
  390. """Test changing substate vars marks the value as dirty.
  391. Args:
  392. test_state: A state.
  393. child_state: A child state.
  394. child_state2: A child state.
  395. grandchild_state: A grandchild state.
  396. """
  397. # Initially there should be no dirty vars.
  398. assert test_state.dirty_vars == set()
  399. assert child_state.dirty_vars == set()
  400. assert child_state2.dirty_vars == set()
  401. assert grandchild_state.dirty_vars == set()
  402. # Setting a var should mark it as dirty.
  403. child_state.value = "test"
  404. assert child_state.dirty_vars == {"value"}
  405. assert test_state.dirty_substates == {"child_state"}
  406. assert child_state.dirty_substates == set()
  407. # Cleaning the parent state should remove the dirty substate.
  408. test_state.clean()
  409. assert test_state.dirty_substates == set()
  410. assert child_state.dirty_vars == set()
  411. # Setting a var on the grandchild should bubble up.
  412. grandchild_state.value2 = "test2"
  413. assert child_state.dirty_substates == {"grandchild_state"}
  414. assert test_state.dirty_substates == {"child_state"}
  415. # Cleaning the middle state should keep the parent state dirty.
  416. child_state.clean()
  417. assert test_state.dirty_substates == {"child_state"}
  418. assert child_state.dirty_substates == set()
  419. assert grandchild_state.dirty_vars == set()
  420. def test_reset(test_state, child_state):
  421. """Test resetting the state.
  422. Args:
  423. test_state: A state.
  424. child_state: A child state.
  425. """
  426. # Set some values.
  427. test_state.num1 = 1
  428. test_state.num2 = 2
  429. child_state.value = "test"
  430. # Reset the state.
  431. test_state.reset()
  432. # The values should be reset.
  433. assert test_state.num1 == 0
  434. assert test_state.num2 == 3.14
  435. assert child_state.value == ""
  436. # The dirty vars should be reset.
  437. assert test_state.dirty_vars == set()
  438. assert child_state.dirty_vars == set()
  439. # The dirty substates should be reset.
  440. assert test_state.dirty_substates == set()
  441. @pytest.mark.asyncio
  442. async def test_process_event_simple(test_state):
  443. """Test processing an event.
  444. Args:
  445. test_state: A state.
  446. """
  447. assert test_state.num1 == 0
  448. event = Event(token="t", name="set_num1", payload={"value": 69})
  449. update = await test_state._process(event)
  450. # The event should update the value.
  451. assert test_state.num1 == 69
  452. # The delta should contain the changes, including computed vars.
  453. assert update.delta == {"test_state": {"num1": 69, "sum": 72.14}}
  454. assert update.events == []
  455. @pytest.mark.asyncio
  456. async def test_process_event_substate(test_state, child_state, grandchild_state):
  457. """Test processing an event on a substate.
  458. Args:
  459. test_state: A state.
  460. child_state: A child state.
  461. grandchild_state: A grandchild state.
  462. """
  463. # Events should bubble down to the substate.
  464. assert child_state.value == ""
  465. assert child_state.count == 23
  466. event = Event(
  467. token="t", name="child_state.change_both", payload={"value": "hi", "count": 12}
  468. )
  469. update = await test_state._process(event)
  470. assert child_state.value == "HI"
  471. assert child_state.count == 24
  472. assert update.delta == {
  473. "test_state.child_state": {"value": "HI", "count": 24},
  474. }
  475. test_state.clean()
  476. # Test with the granchild state.
  477. assert grandchild_state.value2 == ""
  478. event = Event(
  479. token="t",
  480. name="child_state.grandchild_state.set_value2",
  481. payload={"value": "new"},
  482. )
  483. update = await test_state._process(event)
  484. assert grandchild_state.value2 == "new"
  485. assert update.delta == {
  486. "test_state.child_state.grandchild_state": {"value2": "new"},
  487. }
  488. def test_format_event_handler():
  489. """Test formatting an event handler."""
  490. assert (
  491. format.format_event_handler(TestState.do_something) == "test_state.do_something" # type: ignore
  492. )
  493. assert (
  494. format.format_event_handler(ChildState.change_both) # type: ignore
  495. == "test_state.child_state.change_both"
  496. )
  497. assert (
  498. format.format_event_handler(GrandchildState.do_nothing) # type: ignore
  499. == "test_state.child_state.grandchild_state.do_nothing"
  500. )
  501. def test_get_token(test_state):
  502. assert test_state.get_token() == ""
  503. token = "b181904c-3953-4a79-dc18-ae9518c22f05"
  504. test_state.router_data = {RouteVar.CLIENT_TOKEN: token}
  505. assert test_state.get_token() == token
  506. def test_get_sid(test_state):
  507. """Test getting session id.
  508. Args:
  509. test_state: A state.
  510. """
  511. assert test_state.get_sid() == ""
  512. sid = "9fpxSzPb9aFMb4wFAAAH"
  513. test_state.router_data = {RouteVar.SESSION_ID: sid}
  514. assert test_state.get_sid() == sid
  515. def test_get_headers(test_state):
  516. """Test getting client headers.
  517. Args:
  518. test_state: A state.
  519. """
  520. assert test_state.get_headers() == {}
  521. headers = {"host": "localhost:8000", "connection": "keep-alive"}
  522. test_state.router_data = {RouteVar.HEADERS: headers}
  523. assert test_state.get_headers() == headers
  524. def test_get_client_ip(test_state):
  525. """Test getting client IP.
  526. Args:
  527. test_state: A state.
  528. """
  529. assert test_state.get_client_ip() == ""
  530. client_ip = "127.0.0.1"
  531. test_state.router_data = {RouteVar.CLIENT_IP: client_ip}
  532. assert test_state.get_client_ip() == client_ip
  533. def test_get_current_page(test_state):
  534. assert test_state.get_current_page() == ""
  535. route = "mypage/subpage"
  536. test_state.router_data = {RouteVar.PATH: route}
  537. assert test_state.get_current_page() == route
  538. def test_get_query_params(test_state):
  539. assert test_state.get_query_params() == {}
  540. params = {"p1": "a", "p2": "b"}
  541. test_state.router_data = {RouteVar.QUERY: params}
  542. assert test_state.get_query_params() == params
  543. def test_add_var(test_state):
  544. test_state.add_var("dynamic_int", int, 42)
  545. assert test_state.dynamic_int == 42
  546. test_state.add_var("dynamic_list", List[int], [5, 10])
  547. assert test_state.dynamic_list == [5, 10]
  548. assert test_state.dynamic_list == [5, 10]
  549. # how to test that one?
  550. # test_state.dynamic_list.append(15)
  551. # assert test_state.dynamic_list == [5, 10, 15]
  552. test_state.add_var("dynamic_dict", Dict[str, int], {"k1": 5, "k2": 10})
  553. assert test_state.dynamic_dict == {"k1": 5, "k2": 10}
  554. assert test_state.dynamic_dict == {"k1": 5, "k2": 10}
  555. class InterdependentState(State):
  556. """A state with 3 vars and 3 computed vars.
  557. x: a variable that no computed var depends on
  558. v1: a varable that one computed var directly depeneds on
  559. _v2: a backend variable that one computed var directly depends on
  560. v1x2: a computed var that depends on v1
  561. v2x2: a computed var that depends on backend var _v2
  562. v1x2x2: a computed var that depends on computed var v1x2
  563. """
  564. x: int = 0
  565. v1: int = 0
  566. _v2: int = 1
  567. @ComputedVar
  568. def v1x2(self) -> int:
  569. """depends on var v1.
  570. Returns:
  571. Var v1 multiplied by 2
  572. """
  573. return self.v1 * 2
  574. @ComputedVar
  575. def v2x2(self) -> int:
  576. """depends on backend var _v2.
  577. Returns:
  578. backend var _v2 multiplied by 2
  579. """
  580. return self._v2 * 2
  581. @ComputedVar
  582. def v1x2x2(self) -> int:
  583. """depends on ComputedVar v1x2.
  584. Returns:
  585. ComputedVar v1x2 multiplied by 2
  586. """
  587. return self.v1x2 * 2
  588. @pytest.fixture
  589. def interdependent_state() -> State:
  590. """A state with varying dependency between vars.
  591. Returns:
  592. instance of InterdependentState
  593. """
  594. s = InterdependentState()
  595. s.dict() # prime initial relationships by accessing all ComputedVars
  596. return s
  597. def test_not_dirty_computed_var_from_var(interdependent_state):
  598. """Set Var that no ComputedVar depends on, expect no recalculation.
  599. Args:
  600. interdependent_state: A state with varying Var dependencies.
  601. """
  602. interdependent_state.x = 5
  603. assert interdependent_state.get_delta() == {
  604. interdependent_state.get_full_name(): {"x": 5},
  605. }
  606. def test_dirty_computed_var_from_var(interdependent_state):
  607. """Set Var that ComputedVar depends on, expect recalculation.
  608. The other ComputedVar depends on the changed ComputedVar and should also be
  609. recalculated. No other ComputedVars should be recalculated.
  610. Args:
  611. interdependent_state: A state with varying Var dependencies.
  612. """
  613. interdependent_state.v1 = 1
  614. assert interdependent_state.get_delta() == {
  615. interdependent_state.get_full_name(): {"v1": 1, "v1x2": 2, "v1x2x2": 4},
  616. }
  617. def test_dirty_computed_var_from_backend_var(interdependent_state):
  618. """Set backend var that ComputedVar depends on, expect recalculation.
  619. Args:
  620. interdependent_state: A state with varying Var dependencies.
  621. """
  622. interdependent_state._v2 = 2
  623. assert interdependent_state.get_delta() == {
  624. interdependent_state.get_full_name(): {"v2x2": 4},
  625. }