Skip to content

Commit

Permalink
feat(tests): add set_persistent_storage_value to app fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed May 16, 2024
1 parent dec8e22 commit 80e5f15
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
LoadServices,
Stability,
WindowSnapshot,
app_context,
app_context as original_app_context,
load_services,
stability,
store,
Expand All @@ -36,7 +36,6 @@
wait_for,
)


fixtures = (
AppContext,
LoadServices,
Expand All @@ -45,7 +44,7 @@
WaitFor,
WindowSnapshot,
StoreMonitor,
app_context,
original_app_context,
load_services,
needs_finish,
stability,
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
import gc
import json
import logging
import sys
import weakref
Expand All @@ -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:
Expand All @@ -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()
Expand Down
19 changes: 0 additions & 19 deletions tests/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)
Expand Down
12 changes: 10 additions & 2 deletions ubo_app/utils/persistent_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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),
Expand Down

0 comments on commit 80e5f15

Please sign in to comment.