-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trigger a calculated property on mutation
* feat: implement fixture to isolate test of global context
- Loading branch information
1 parent
25abccb
commit a7fb9d4
Showing
2 changed files
with
58 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters