Coverage for tests/conftest.py: 100%

26 statements  

« 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. 

2 

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. 

9 

10""" 

11 

12from __future__ import annotations 

13 

14import dataclasses 

15from typing import TYPE_CHECKING, Any 

16 

17import pytest 

18 

19import wonka 

20from wonka import options 

21 

22if TYPE_CHECKING: 

23 from collections.abc import Iterator 

24 

25_OPTION_NAMES: tuple[str, ...] = ( 

26 '_KEY_NAMER', 

27 '_METHOD_NAMER', 

28 '_OVERWRITE', 

29 '_STRICT_COMPATIBILITY', 

30 '_VERBOSE') 

31 

32 

33@dataclasses.dataclass 

34class Thing: 

35 """Simple dataclass used as a created item. 

36 

37 Args: 

38 value: stored value. Defaults to 0. 

39 

40 """ 

41 

42 value: int = 0 

43 

44 

45@dataclasses.dataclass 

46class Adder(wonka.Factory): 

47 """Factory that adds `amount` to an integer.""" 

48 

49 @classmethod 

50 def create(cls, item: int, amount: int = 1, **kwargs: Any) -> int: 

51 """Returns `item` plus `amount`. 

52 

53 Args: 

54 item: integer to add to. 

55 amount: amount to add. Defaults to 1. 

56 **kwargs: ignored keyword arguments. 

57 

58 Returns: 

59 The sum of `item` and `amount`. 

60 

61 """ 

62 return item + amount 

63 

64 

65@dataclasses.dataclass 

66class Doubler(wonka.Factory): 

67 """Factory that doubles an integer.""" 

68 

69 @classmethod 

70 def create(cls, item: int, **kwargs: Any) -> int: 

71 """Returns `item` doubled. 

72 

73 Args: 

74 item: integer to double. 

75 **kwargs: ignored keyword arguments. 

76 

77 Returns: 

78 Twice `item`. 

79 

80 """ 

81 return item * 2 

82 

83 

84@pytest.fixture(autouse = True) 

85def restore_options() -> Iterator[None]: 

86 """Restores the global `wonka` options after each test. 

87 

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. 

90 

91 Yields: 

92 None: control passes to the test, and then the options are restored. 

93 

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)