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

1"""Tests the `wonka` producer mixins.""" 

2 

3from __future__ import annotations 

4 

5import dataclasses 

6import inspect 

7from typing import Any 

8 

9from conftest import Thing 

10 

11import wonka 

12 

13 

14@dataclasses.dataclass 

15class Configuration(wonka.Classer, wonka.Delegate): 

16 """`Delegate` that always returns a class. 

17 

18 Args: 

19 contents: stored settings. Defaults to an empty `dict`. 

20 

21 """ 

22 

23 contents: dict[str, Any] = dataclasses.field(default_factory = dict) 

24 

25 @classmethod 

26 def from_dict(cls, item: dict[str, Any]) -> Configuration: 

27 """Returns a `Configuration` instance from a `dict`. 

28 

29 Args: 

30 item: settings to store. 

31 

32 Returns: 

33 A `Configuration` instance. 

34 

35 """ 

36 return cls(contents = item) 

37 

38 

39@dataclasses.dataclass 

40class Settings(wonka.Flexer, wonka.Delegate): 

41 """`Delegate` that returns an instance only with parameters. 

42 

43 Args: 

44 contents: stored settings. Defaults to an empty `dict`. 

45 

46 """ 

47 

48 contents: dict[str, Any] = dataclasses.field(default_factory = dict) 

49 

50 @classmethod 

51 def from_dict(cls, item: dict[str, Any]) -> Settings: 

52 """Returns a `Settings` instance from a `dict`. 

53 

54 Args: 

55 item: settings to store. 

56 

57 Returns: 

58 A `Settings` instance. 

59 

60 """ 

61 return cls(contents = item) 

62 

63 @classmethod 

64 def from_list(cls, item: list[Any]) -> type[Settings]: 

65 """Returns the `Settings` class, ignoring `item`. 

66 

67 Args: 

68 item: ignored `list`. 

69 

70 Returns: 

71 The `Settings` class. 

72 

73 """ 

74 return cls 

75 

76 

77@dataclasses.dataclass 

78class Setup(wonka.Instancer, wonka.Delegate): 

79 """`Delegate` that always returns an instance. 

80 

81 Args: 

82 contents: stored settings. Defaults to an empty `dict`. 

83 

84 """ 

85 

86 contents: dict[str, Any] = dataclasses.field(default_factory = dict) 

87 

88 @classmethod 

89 def from_dict(cls, item: dict[str, Any]) -> Setup: 

90 """Returns a `Setup` instance from a `dict`. 

91 

92 Args: 

93 item: settings to store. 

94 

95 Returns: 

96 A `Setup` instance. 

97 

98 """ 

99 return cls(contents = item) 

100 

101 @classmethod 

102 def from_list(cls, item: list[Any]) -> type[Setup]: 

103 """Returns the `Setup` class, ignoring `item`. 

104 

105 Args: 

106 item: ignored `list`. 

107 

108 Returns: 

109 The `Setup` class. 

110 

111 """ 

112 return cls 

113 

114 

115def test_classer() -> None: 

116 """Tests that a `Classer` factory returns a class.""" 

117 config = Configuration.create({'tree': 'house'}) 

118 assert config is Configuration 

119 

120 

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 

126 

127 

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} 

133 

134 

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} 

141 

142 

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 

158 

159 

160if __name__ == '__main__': 

161 test_classer() 

162 test_flexer() 

163 test_instancer()