Skip to content

Commit

Permalink
feat: trigger a calculated property on mutation
Browse files Browse the repository at this point in the history
* feat: implement fixture to isolate test of global context
  • Loading branch information
FabienArcellier committed Jul 26, 2024
1 parent 25abccb commit a7fb9d4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
40 changes: 40 additions & 0 deletions tests/backend/fixtures/writer_fixtures.py
Original file line number Diff line number Diff line change
@@ -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
35 changes: 18 additions & 17 deletions tests/backend/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:

Expand Down

0 comments on commit a7fb9d4

Please sign in to comment.