Skip to content

Commit

Permalink
refactor: drop logging fixture and use standard pytest logger in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Mar 20, 2024
1 parent 4958ceb commit cbfdc9a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.12.6

- refactor: drop logging fixture and use standard pytest logger in tests

## Version 0.12.5

- refactor: add cleanup to `FinishEvent` handler to clean workers, listeners, subscriptions,
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.12.5"
version = "0.12.6"
description = "Redux implementation for Python"
authors = ["Sassan Haradji <[email protected]>"]
license = "Apache-2.0"
Expand Down
11 changes: 2 additions & 9 deletions redux/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pytest

if TYPE_CHECKING:
from logging import Logger
from pathlib import Path

from _pytest.fixtures import SubRequest
Expand All @@ -32,7 +31,6 @@ def __init__(
*,
test_id: str,
path: Path,
logger: Logger,
override: bool,
) -> None:
"""Create a new store snapshot context."""
Expand All @@ -47,7 +45,6 @@ def __init__(
):
file.unlink()
self.results_dir.mkdir(parents=True, exist_ok=True)
self.logger = logger

def set_store(self: StoreSnapshotContext, store: Store) -> None:
"""Set the store to take snapshots of."""
Expand Down Expand Up @@ -94,7 +91,7 @@ def take(self: StoreSnapshotContext, *, title: str | None = None) -> None:
mismatch_path.write_text( # pragma: no cover
f'// MISMATCH: {filename}\n{new_snapshot}\n',
)
assert old_snapshot == new_snapshot, f'Store snapshot mismatch: {title}'
assert new_snapshot == old_snapshot, f'Store snapshot mismatch: {title}'

self.test_counter[title] += 1

Expand All @@ -109,10 +106,7 @@ def close(self: StoreSnapshotContext) -> None:


@pytest.fixture()
def store_snapshot(
request: SubRequest,
logger: Logger,
) -> Generator[StoreSnapshotContext, None, None]:
def store_snapshot(request: SubRequest) -> Generator[StoreSnapshotContext, None, None]:
"""Take a snapshot of the current state of the store."""
override = (
request.config.getoption(
Expand All @@ -127,7 +121,6 @@ def store_snapshot(
context = StoreSnapshotContext(
test_id=request.node.nodeid,
path=request.node.path,
logger=logger,
override=override,
)
yield context
Expand Down
11 changes: 0 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@
import random
import time
import uuid
from typing import TYPE_CHECKING

import pytest

pytest.register_assert_rewrite('redux.test')

from redux.test import pytest_addoption, store_snapshot # noqa: E402

if TYPE_CHECKING:
from logging import Logger

__all__ = ('store_snapshot', 'pytest_addoption')


@pytest.fixture()
def logger() -> Logger:
import logging

return logging.getLogger('test')


@pytest.fixture(autouse=True)
def _(monkeypatch: pytest.MonkeyPatch) -> None:
"""Mock external resources."""
Expand Down
9 changes: 4 additions & 5 deletions tests/test_todo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ruff: noqa: D100, D101, D102, D103, D104, D107
from __future__ import annotations

import logging
import time
import uuid
from dataclasses import replace
Expand All @@ -9,12 +10,10 @@
from immutable import Immutable

if TYPE_CHECKING:
from logging import Logger

from redux.test import StoreSnapshotContext


def test_todo(store_snapshot: StoreSnapshotContext, logger: Logger) -> None:
def test_todo(store_snapshot: StoreSnapshotContext) -> None:
from redux import BaseAction, Store
from redux.basic_types import (
BaseEvent,
Expand Down Expand Up @@ -90,7 +89,7 @@ def reducer(
store_snapshot.set_store(store)

# subscription:
dummy_render = logger.info
dummy_render = logging.getLogger().info
store.subscribe(dummy_render)

# autorun:
Expand All @@ -104,7 +103,7 @@ def reaction(_: str | None) -> None:

# event listener, note that this will run async in a separate thread, so it can
# include async operations like API calls, etc:
dummy_api_call = logger.info
dummy_api_call = logging.getLogger().info
store.subscribe_event(CallApi, lambda event: dummy_api_call(event.parameters))

# dispatch:
Expand Down

0 comments on commit cbfdc9a

Please sign in to comment.