Coverage for tests/test_producers.py: 100%
60 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"""Tests the `wonka` producer mixins."""
3from __future__ import annotations
5import dataclasses
6import inspect
7from typing import Any
9from conftest import Thing
11import wonka
14@dataclasses.dataclass
15class Configuration(wonka.Classer, wonka.Delegate):
16 """`Delegate` that always returns a class.
18 Args:
19 contents: stored settings. Defaults to an empty `dict`.
21 """
23 contents: dict[str, Any] = dataclasses.field(default_factory = dict)
25 @classmethod
26 def from_dict(cls, item: dict[str, Any]) -> Configuration:
27 """Returns a `Configuration` instance from a `dict`.
29 Args:
30 item: settings to store.
32 Returns:
33 A `Configuration` instance.
35 """
36 return cls(contents = item)
39@dataclasses.dataclass
40class Settings(wonka.Flexer, wonka.Delegate):
41 """`Delegate` that returns an instance only with parameters.
43 Args:
44 contents: stored settings. Defaults to an empty `dict`.
46 """
48 contents: dict[str, Any] = dataclasses.field(default_factory = dict)
50 @classmethod
51 def from_dict(cls, item: dict[str, Any]) -> Settings:
52 """Returns a `Settings` instance from a `dict`.
54 Args:
55 item: settings to store.
57 Returns:
58 A `Settings` instance.
60 """
61 return cls(contents = item)
63 @classmethod
64 def from_list(cls, item: list[Any]) -> type[Settings]:
65 """Returns the `Settings` class, ignoring `item`.
67 Args:
68 item: ignored `list`.
70 Returns:
71 The `Settings` class.
73 """
74 return cls
77@dataclasses.dataclass
78class Setup(wonka.Instancer, wonka.Delegate):
79 """`Delegate` that always returns an instance.
81 Args:
82 contents: stored settings. Defaults to an empty `dict`.
84 """
86 contents: dict[str, Any] = dataclasses.field(default_factory = dict)
88 @classmethod
89 def from_dict(cls, item: dict[str, Any]) -> Setup:
90 """Returns a `Setup` instance from a `dict`.
92 Args:
93 item: settings to store.
95 Returns:
96 A `Setup` instance.
98 """
99 return cls(contents = item)
101 @classmethod
102 def from_list(cls, item: list[Any]) -> type[Setup]:
103 """Returns the `Setup` class, ignoring `item`.
105 Args:
106 item: ignored `list`.
108 Returns:
109 The `Setup` class.
111 """
112 return cls
115def test_classer() -> None:
116 """Tests that a `Classer` factory returns a class."""
117 config = Configuration.create({'tree': 'house'})
118 assert config is Configuration
121def test_flexer() -> None:
122 """Tests that a `Flexer` factory returns what its method creates."""
123 settings = Settings.create({'tree': 'house'})
124 assert not inspect.isclass(settings)
125 assert Settings.create([]) is Settings
128def test_flexer_with_parameters() -> None:
129 """Tests that a `Flexer` factory returns an instance with parameters."""
130 settings = Settings.create([], parameters = {'contents': {'a': 1}})
131 assert isinstance(settings, Settings)
132 assert settings.contents == {'a': 1}
135def test_instancer() -> None:
136 """Tests that an `Instancer` factory always returns an instance."""
137 assert isinstance(Setup.create({'tree': 'house'}), Setup)
138 assert isinstance(Setup.create([]), Setup)
139 setup = Setup.create([], parameters = {'contents': {'a': 1}})
140 assert setup.contents == {'a': 1}
143def test_producers_directly() -> None:
144 """Tests calling the `produce` methods directly."""
145 thing = Thing()
146 assert wonka.Classer.produce(thing) is Thing
147 assert wonka.Classer.produce(Thing) is Thing
148 assert wonka.Classer.produce(Thing, {'value': 3}) is Thing
149 assert wonka.Instancer.produce(Thing) == Thing()
150 assert wonka.Instancer.produce(Thing, {'value': 3}).value == 3
151 assert wonka.Instancer.produce(thing) is thing
152 assert wonka.Flexer.produce(Thing) is Thing
153 assert wonka.Flexer.produce(Thing, {'value': 5}).value == 5
154 assert wonka.Flexer.produce(thing, {'value': 6}) is thing
155 assert thing.value == 6
156 assert wonka.Instancer.produce(thing, {'value': 7}) is thing
157 assert thing.value == 7
160if __name__ == '__main__':
161 test_classer()
162 test_flexer()
163 test_instancer()