Skip to content

Commit

Permalink
feat(test-snapshot): the selector function can signal the monitor
Browse files Browse the repository at this point in the history
… it should ignore a particular snapshot of the state by returning `None`
  • Loading branch information
sassanh committed Jun 18, 2024
1 parent e1eaaef commit 16e68cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 0.15.8

- feat(test-snapshot): the `selector` function can signal the `monitor` it should
ignore a particular snapshot of the state by returning `None`

## Version 0.15.7

- refactor(test-snapshot): make it aligned with `pyfakefs` by using `try`/`except`
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-redux"
version = "0.15.7"
version = "0.15.8"
description = "Redux implementation for Python"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand Down
12 changes: 8 additions & 4 deletions redux_pytest/fixtures/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import os
from collections import defaultdict
from distutils.util import strtobool
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Generic, cast

Expand Down Expand Up @@ -97,9 +98,9 @@ def take(
if self.override:
json_path.write_text(f'// {filename}\n{new_snapshot}\n') # pragma: no cover
else:
try:
if json_path.exists():
old_snapshot = json_path.read_text().split('\n', 1)[1][:-1]
except Exception: # noqa: BLE001
else:
old_snapshot = None
if old_snapshot != new_snapshot:
self._is_failed = True
Expand All @@ -114,7 +115,9 @@ def monitor(self: StoreSnapshot[State], selector: Callable[[State], Any]) -> Non
"""Monitor the state of the store and take snapshots."""

@self.store.autorun(selector=selector)
def _(state: State) -> None:
def _(state: object | None) -> None:
if state is None:
return
self.take(selector=lambda _: state)

def close(self: StoreSnapshot[State]) -> None:
Expand All @@ -137,7 +140,8 @@ def store_snapshot(request: SubRequest, store: Store) -> StoreSnapshot:
'--override-store-snapshots',
default=cast(
Any,
os.environ.get('REDUX_TEST_OVERRIDE_SNAPSHOTS', '0') == '1',
strtobool(os.environ.get('REDUX_TEST_OVERRIDE_SNAPSHOTS', 'false'))
== 1,
),
)
is True
Expand Down
25 changes: 13 additions & 12 deletions redux_pytest/fixtures/wait_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,26 @@ def __call__( # noqa: PLR0913

args['wait'] = wait or wait_exponential(multiplier=0.5)

if run_async:

def async_decorator(check: Callable[[], None]) -> AsyncWaiter:
async def async_wrapper() -> None:
async for attempt in AsyncRetrying(**args):
with attempt:
check()

return async_wrapper

return async_decorator(check) if check else async_decorator

def decorator(check: Callable[[], None]) -> Waiter:
@retry(**args)
def wrapper() -> None:
check()

return wrapper

def async_decorator(check: Callable[[], None]) -> AsyncWaiter:
async def async_wrapper() -> None:
async for attempt in AsyncRetrying(**args):
with attempt:
check()

return async_wrapper

if check:
return async_decorator(check) if run_async else decorator(check)

return async_decorator if run_async else decorator
return decorator(check) if check else decorator


@pytest.fixture()
Expand Down

0 comments on commit 16e68cc

Please sign in to comment.