From a7fb9d4a13b7d838e226e7d637085614a7f0c918 Mon Sep 17 00:00:00 2001 From: Fabien Arcellier Date: Fri, 26 Jul 2024 08:38:28 +0200 Subject: [PATCH] feat: trigger a calculated property on mutation * feat: implement fixture to isolate test of global context --- tests/backend/fixtures/writer_fixtures.py | 40 +++++++++++++++++++++++ tests/backend/test_core.py | 35 ++++++++++---------- 2 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 tests/backend/fixtures/writer_fixtures.py diff --git a/tests/backend/fixtures/writer_fixtures.py b/tests/backend/fixtures/writer_fixtures.py new file mode 100644 index 000000000..e6e9341ff --- /dev/null +++ b/tests/backend/fixtures/writer_fixtures.py @@ -0,0 +1,40 @@ +import contextlib +import copy + +from writer import WriterState, core, core_ui +from writer.core import Config, SessionManager + + +@contextlib.contextmanager +def new_app_context(): + """ + Creates a new application context for testing, independent of the global state. + + This fixture avoids conflicts between tests that use the same global state. + At the end of the context, the global state is restored to its original state. + + >>> with writer_fixtures.new_app_context(): + >>> initial_state = wf.init_state({ + >>> "counter": 0, + >>> "total": 0 + >>> }, schema=MyState) + """ + saved_context_vars = {} + core_context_vars = ['initial_state', 'base_component_tree', 'session_manager'] + core_config_vars = copy.deepcopy(core.Config) + + for var in core_context_vars: + saved_context_vars[var] = getattr(core, var) + + core.initial_state = WriterState() + core.base_component_tree = core_ui.build_base_component_tree() + core.session_manager = SessionManager() + Config.mode = "run" + Config.logger = None + + yield + + for var in core_context_vars: + setattr(core, var, saved_context_vars[var]) + + core.Config = core_config_vars diff --git a/tests/backend/test_core.py b/tests/backend/test_core.py index 950e4a36a..26bce1dec 100644 --- a/tests/backend/test_core.py +++ b/tests/backend/test_core.py @@ -26,7 +26,7 @@ from writer.core_ui import Component from writer.ss_types import WriterEvent -from backend.fixtures import core_ui_fixtures +from backend.fixtures import core_ui_fixtures, writer_fixtures from tests.backend import test_app_dir raw_state_dict = { @@ -439,27 +439,28 @@ def test_subscribe_mutation_with_typed_state_should_manage_mutation(self): """ Tests that a mutation handler is triggered on a typed state and can use attributes directly. """ - # Assign - class MyState(wf.WriterState): - counter: int - total: int + with writer_fixtures.new_app_context(): + # Assign + class MyState(wf.WriterState): + counter: int + total: int - def cumulative_sum(state: MyState): - state.total += state.counter + def cumulative_sum(state: MyState): + state.total += state.counter - initial_state = wf.init_state({ - "counter": 0, - "total": 0 - }, schema=MyState) + initial_state = wf.init_state({ + "counter": 0, + "total": 0 + }, schema=MyState) - initial_state.subscribe_mutation('counter', cumulative_sum) + initial_state.subscribe_mutation('counter', cumulative_sum) - # Acts - initial_state['counter'] = 1 - initial_state['counter'] = 3 + # Acts + initial_state['counter'] = 1 + initial_state['counter'] = 3 - # Assert - assert initial_state['total'] == 4 + # Assert + assert initial_state['total'] == 4 class TestWriterState: