diff --git a/CHANGELOG.md b/CHANGELOG.md index eddd5ce8..81a7cef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Version 0.14.3 - feat(tests): add `pyfakefs` to mock filesystem in tests +- feat(tests): add `set_persistent_storage_value` to app fixture ## Version 0.14.2 diff --git a/tests/conftest.py b/tests/conftest.py index c5d91e71..c6e79a34 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,7 +19,7 @@ LoadServices, Stability, WindowSnapshot, - app_context, + app_context as original_app_context, load_services, stability, store, @@ -36,7 +36,6 @@ wait_for, ) - fixtures = ( AppContext, LoadServices, @@ -45,7 +44,7 @@ WaitFor, WindowSnapshot, StoreMonitor, - app_context, + original_app_context, load_services, needs_finish, stability, @@ -63,6 +62,16 @@ def pytest_addoption(parser: pytest.Parser) -> None: parser.addoption('--make-screenshots', action='store_true') +@pytest.fixture(autouse=True) +def app_context(original_app_context: AppContext) -> AppContext: + """Set defaults for app-context for tests.""" + original_app_context.set_persistent_storage_value( + 'wifi_has_visited_onboarding', + value=True, + ) + return original_app_context + + @pytest.fixture(autouse=True) def _logger() -> None: import logging diff --git a/tests/fixtures/app.py b/tests/fixtures/app.py index 20861efa..0f1f11ba 100644 --- a/tests/fixtures/app.py +++ b/tests/fixtures/app.py @@ -4,6 +4,7 @@ import asyncio import gc +import json import logging import sys import weakref @@ -13,6 +14,7 @@ import platformdirs import pytest +from ubo_app.constants import PERSISTENT_STORE_PATH from ubo_app.setup import setup if TYPE_CHECKING: @@ -32,10 +34,29 @@ class AppContext: def __init__(self: AppContext, request: SubRequest, *, fs: FakeFilesystem) -> None: """Initialize the context.""" self.request = request + self.persistent_store_data = {} self.fs = fs + def set_persistent_storage_value( + self: AppContext, + key: str, + *, + value: object, + ) -> None: + """Set initial value in persistent store.""" + assert not hasattr( + self, + 'app', + ), "Can't set persistent storage values after app has been set" + self.persistent_store_data[key] = value + def set_app(self: AppContext, app: MenuApp) -> None: """Set the application.""" + self.fs.create_file( + PERSISTENT_STORE_PATH.as_posix(), + contents=json.dumps(self.persistent_store_data), + ) + from ubo_app.utils.loop import setup_event_loop setup_event_loop() diff --git a/tests/monkeypatch.py b/tests/monkeypatch.py index 51e7d6d9..733979ae 100644 --- a/tests/monkeypatch.py +++ b/tests/monkeypatch.py @@ -245,25 +245,6 @@ def _monkeypatch(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr('ubo_app.utils.serializer.add_type_field', lambda _, y: y) monkeypatch.setattr('pyzbar.pyzbar.decode', Fake()) - def fake_read_from_persistent_store( - key: str, - *, - object_type: type[object] | None = None, - default: object | None = None, - ) -> object: - if key == 'wifi_has_visited_onboarding': - return True - if default is not None: - return default - if object_type is not None: - return object_type() - return None - - sys.modules['ubo_app.utils.persistent_store'] = Fake( - _Fake__props={ - 'read_from_persistent_store': fake_read_from_persistent_store, - }, - ) sys.modules['ubo_app.utils.secrets'] = Fake( _Fake__props={'read_secret': lambda _: None}, ) diff --git a/ubo_app/utils/persistent_store.py b/ubo_app/utils/persistent_store.py index 1a4e2ed3..79962509 100644 --- a/ubo_app/utils/persistent_store.py +++ b/ubo_app/utils/persistent_store.py @@ -81,7 +81,11 @@ def read_from_persistent_store( file_content = Path(PERSISTENT_STORE_PATH).read_text() current_state = json.loads(file_content) except FileNotFoundError: - return default or (None if object_type is None else object_type()) + return ( + (None if object_type is None else object_type()) + if default is None + else default + ) except json.JSONDecodeError: continue else: @@ -91,7 +95,11 @@ def read_from_persistent_store( raise RuntimeError(msg) value = current_state.get(key) if value is None: - return default or (None if object_type is None else object_type()) + return ( + (None if object_type is None else object_type()) + if default is None + else default + ) return store.load_object( value, object_type=cast(type[T], object_type),