MCPcopy
hub / github.com/reflex-dev/reflex / test_app_modify_state_clean

Function test_app_modify_state_clean

tests/units/test_app.py:3343–3398  ·  view source on GitHub ↗

Test that modify_state does not leave dirty_vars or dirty_substates. Args: token: A client token. substate: Whether to modify a substate. frontend: Whether to modify a frontend or backend var.

(token: str, substate: bool, frontend: bool)

Source from the content-addressed store, hash-verified

3341)
3342@pytest.mark.asyncio
3343async def test_app_modify_state_clean(token: str, substate: bool, frontend: bool):
3344 """Test that modify_state does not leave dirty_vars or dirty_substates.
3345
3346 Args:
3347 token: A client token.
3348 substate: Whether to modify a substate.
3349 frontend: Whether to modify a frontend or backend var.
3350 """
3351
3352 class Base(BaseState):
3353 count: int = 0
3354 _backend: int = 0
3355
3356 class Sub(Base):
3357 sub_count: int = 0
3358 _sub_backend: int = 0
3359
3360 app = App(_state=Base)
3361 app._event_namespace = AsyncMock()
3362
3363 async with app.modify_state(
3364 token=BaseStateToken(ident=token, cls=Sub)
3365 ) as root_state:
3366 sub = root_state.substates[Sub.get_name()]
3367 if substate:
3368 if frontend:
3369 sub.sub_count = 1
3370 else:
3371 sub._sub_backend = 1
3372 else:
3373 if frontend:
3374 root_state.count = 1
3375 else:
3376 root_state._backend = 1
3377
3378 assert not root_state.dirty_vars
3379 assert not root_state.dirty_substates
3380 if substate:
3381 assert sub._was_touched
3382 assert not root_state._was_touched
3383 else:
3384 assert root_state._was_touched
3385 assert not sub._was_touched
3386
3387 if frontend:
3388 assert app._event_namespace.emit_update.call_count == 1
3389 if substate:
3390 exp_delta = {Sub.get_full_name(): {"sub_count_rx_state_": 1}}
3391 else:
3392 exp_delta = {Base.get_full_name(): {"count_rx_state_": 1}}
3393 assert (
3394 app._event_namespace.emit_update.call_args.kwargs["update"].delta
3395 == exp_delta
3396 )
3397 else:
3398 assert app._event_namespace.emit_update.call_count == 0
3399
3400

Callers

nothing calls this directly

Calls 5

modify_stateMethod · 0.95
AppClass · 0.90
BaseStateTokenClass · 0.90
get_nameMethod · 0.80
get_full_nameMethod · 0.80

Tested by

no test coverage detected