Coverage for tests/conftest.py: 100%
26 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-30 22:54 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-10-30 22:54 +0000
1"""Shared fixtures and example constructors for the `wonka` tests.
3Contents:
4 Adder: factory that adds an amount to an integer.
5 Doubler: factory that doubles an integer.
6 Thing: simple dataclass used as a created item.
7 restore_options: fixture that restores the global `wonka` options after
8 each test.
10"""
12from __future__ import annotations
14import dataclasses
15from typing import TYPE_CHECKING, Any
17import pytest
19import wonka
20from wonka import options
22if TYPE_CHECKING:
23 from collections.abc import Iterator
25_OPTION_NAMES: tuple[str, ...] = (
26 '_KEY_NAMER',
27 '_METHOD_NAMER',
28 '_OVERWRITE',
29 '_STRICT_COMPATIBILITY',
30 '_VERBOSE')
33@dataclasses.dataclass
34class Thing:
35 """Simple dataclass used as a created item.
37 Args:
38 value: stored value. Defaults to 0.
40 """
42 value: int = 0
45@dataclasses.dataclass
46class Adder(wonka.Factory):
47 """Factory that adds `amount` to an integer."""
49 @classmethod
50 def create(cls, item: int, amount: int = 1, **kwargs: Any) -> int:
51 """Returns `item` plus `amount`.
53 Args:
54 item: integer to add to.
55 amount: amount to add. Defaults to 1.
56 **kwargs: ignored keyword arguments.
58 Returns:
59 The sum of `item` and `amount`.
61 """
62 return item + amount
65@dataclasses.dataclass
66class Doubler(wonka.Factory):
67 """Factory that doubles an integer."""
69 @classmethod
70 def create(cls, item: int, **kwargs: Any) -> int:
71 """Returns `item` doubled.
73 Args:
74 item: integer to double.
75 **kwargs: ignored keyword arguments.
77 Returns:
78 Twice `item`.
80 """
81 return item * 2
84@pytest.fixture(autouse = True)
85def restore_options() -> Iterator[None]:
86 """Restores the global `wonka` options after each test.
88 `pytest-randomly` shuffles the order of the tests, so a test that changes a
89 global option must not affect the tests that run after it.
91 Yields:
92 None: control passes to the test, and then the options are restored.
94 """
95 saved = {name: getattr(options, name) for name in _OPTION_NAMES}
96 yield
97 for name, value in saved.items():
98 setattr(options, name, value)