Coverage for tests/test_nodes.py: 98%

113 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-14 09:29 +0000

1"""Tests the nodes module: techniques, null vertexes, steps, and workers.""" 

2 

3from __future__ import annotations 

4 

5import copy 

6import dataclasses 

7import threading 

8from typing import Any 

9 

10import pytest 

11 

12import chrisjen 

13 

14 

15def add_one(item: Any) -> Any: 

16 return item + 1 

17 

18 

19""" Technique """ 

20 

21 

22def test_technique_wraps_a_callable_or_an_import_path() -> None: 

23 assert chrisjen.Technique(name = 't', contents = add_one).apply(1) == 2 

24 mean = chrisjen.Technique(name = 'm', contents = 'statistics.fmean') 

25 assert mean.apply([1, 2, 3, 6]) == 3.0 

26 colon = chrisjen.Technique(name = 'm', contents = 'statistics:fmean') 

27 assert colon.apply([2, 4]) == 3.0 

28 

29 

30def test_technique_parameters_are_filtered_for_the_tool() -> None: 

31 rounded = chrisjen.Technique( 

32 name = 'round', 

33 contents = round, 

34 parameters = {'ndigits': 1, 'unused': True}, 

35 ) 

36 assert rounded.apply(3.14159) == 3.1 

37 assert rounded.apply(3.14159, ndigits = 3) == 3.142 

38 

39 

40def test_technique_errors() -> None: 

41 with pytest.raises(NotImplementedError, match = 'no tool'): 

42 chrisjen.Technique(name = 'empty').apply(1) 

43 with pytest.raises(TypeError, match = 'not callable'): 

44 chrisjen.Technique(name = 'constant', contents = 5).apply(1) 

45 with pytest.raises(ImportError, match = 'no_such_package'): 

46 chrisjen.Technique(name = 'x', contents = 'no_such_package.x').apply(1) 

47 

48 

49def test_technique_subclasses_override_apply() -> None: 

50 @dataclasses.dataclass 

51 class Cube(chrisjen.Technique): 

52 def apply(self, item: Any, **kwargs: Any) -> Any: 

53 return item**3 

54 

55 assert Cube(name = 'cube').apply(2) == 8 

56 assert chrisjen.library['vertex']['cube'] is Cube 

57 

58 

59def test_a_technique_is_copied_with_its_tool() -> None: 

60 class Guarded: 

61 def __init__(self) -> None: 

62 self.lock = threading.Lock() 

63 

64 def __call__(self, item: Any) -> Any: 

65 return item 

66 

67 counter = [0] 

68 technique = chrisjen.Technique( 

69 name = 'c', contents = counter.append, parameters = {'a': [1]}) 

70 clone = copy.deepcopy(technique) 

71 assert clone.parameters['a'] is not technique.parameters['a'] 

72 tool = Guarded() 

73 shared = copy.deepcopy(chrisjen.Technique(name = 'g', contents = tool)) 

74 assert shared.contents is tool 

75 # Only the tool in contents may be shared. Other fields must be copied. 

76 locked = chrisjen.Technique( 

77 name = 'l', parameters = {'lock': threading.Lock()}) 

78 with pytest.raises(TypeError): 

79 copy.deepcopy(locked) 

80 

81 

82""" NullVertex """ 

83 

84 

85def test_null_vertex() -> None: 

86 sentinel = object() 

87 assert chrisjen.NullVertex().name == 'none' 

88 assert chrisjen.NullVertex().apply(sentinel, anything = 1) is sentinel 

89 assert chrisjen.library.all['none'] is chrisjen.NullVertex 

90 

91 

92""" Step """ 

93 

94 

95def test_step_applies_its_contents_between_begin_and_end() -> None: 

96 calls: list[str] = [] 

97 

98 @dataclasses.dataclass 

99 class Logged(chrisjen.Step): 

100 def begin(self, item: Any) -> Any: 

101 calls.append('begin') 

102 return item + 100 

103 

104 def end(self, item: Any) -> Any: 

105 calls.append('end') 

106 return item * 2 

107 

108 step = Logged( 

109 name = 'l', 

110 contents = chrisjen.Technique(name = 'a', contents = add_one)) 

111 assert step.apply(1) == (1 + 100 + 1) * 2 

112 assert calls == ['begin', 'end'] 

113 

114 

115def test_step_parameter_priority() -> None: 

116 def shift(item: Any, amount: int = 1) -> Any: 

117 return item + amount 

118 

119 technique = chrisjen.Technique( 

120 name = 't', contents = shift, parameters = {'amount': 2}) 

121 assert chrisjen.Step(name = 's', contents = technique).apply(0) == 2 

122 step = chrisjen.Step( 

123 name = 's', contents = technique, parameters = {'amount': 5}) 

124 assert step.apply(0) == 5 

125 assert step.apply(0, amount = 7) == 7 

126 

127 

128def test_step_returns_the_attributes_of_its_contents() -> None: 

129 @dataclasses.dataclass 

130 class Model(chrisjen.Technique): 

131 kind: str = 'forest' 

132 

133 def apply(self, item: Any, **kwargs: Any) -> Any: 

134 return item 

135 

136 step = chrisjen.Step(name = 's', contents = Model(name = 'm')) 

137 assert step.kind == 'forest' 

138 assert step.name == 's' 

139 with pytest.raises(AttributeError): 

140 step.missing # noqa: B018 

141 with pytest.raises(AttributeError): 

142 chrisjen.Step(name = 'e').kind # noqa: B018 

143 clone = copy.deepcopy(step) 

144 assert type(clone) is chrisjen.Step 

145 assert clone.contents is not step.contents 

146 assert clone.kind == 'forest' 

147 

148 

149def test_step_without_contents() -> None: 

150 with pytest.raises(ValueError, match = "step 'e' has no technique"): 

151 chrisjen.Step(name = 'e').apply(1) 

152 

153 

154""" Worker """ 

155 

156 

157def test_worker_is_abstract_and_hashed_by_name() -> None: 

158 with pytest.raises(TypeError): 

159 chrisjen.Worker(name = 'w') 

160 for design in ( 

161 chrisjen.Flow, chrisjen.Benchmark, chrisjen.Contest, chrisjen.Survey): 

162 worker = design(name = 'w') 

163 assert hash(worker) == hash('w') 

164 assert worker == 'w' 

165 

166 

167def test_populate_connects_nodes_in_sequence() -> None: 

168 nodes = [ 

169 chrisjen.Technique(name = name, contents = add_one) 

170 for name in ('a', 'b', 'c')] 

171 flow = chrisjen.Flow(name = 'f') 

172 flow.populate(nodes) 

173 assert flow.walk() == [nodes] 

174 assert flow.root == [nodes[0]] 

175 assert flow.endpoint == [nodes[2]]