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
« 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."""
3from __future__ import annotations
5import copy
6import dataclasses
7import threading
8from typing import Any
10import pytest
12import chrisjen
15def add_one(item: Any) -> Any:
16 return item + 1
19""" Technique """
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
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
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)
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
55 assert Cube(name = 'cube').apply(2) == 8
56 assert chrisjen.library['vertex']['cube'] is Cube
59def test_a_technique_is_copied_with_its_tool() -> None:
60 class Guarded:
61 def __init__(self) -> None:
62 self.lock = threading.Lock()
64 def __call__(self, item: Any) -> Any:
65 return item
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)
82""" NullVertex """
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
92""" Step """
95def test_step_applies_its_contents_between_begin_and_end() -> None:
96 calls: list[str] = []
98 @dataclasses.dataclass
99 class Logged(chrisjen.Step):
100 def begin(self, item: Any) -> Any:
101 calls.append('begin')
102 return item + 100
104 def end(self, item: Any) -> Any:
105 calls.append('end')
106 return item * 2
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']
115def test_step_parameter_priority() -> None:
116 def shift(item: Any, amount: int = 1) -> Any:
117 return item + amount
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
128def test_step_returns_the_attributes_of_its_contents() -> None:
129 @dataclasses.dataclass
130 class Model(chrisjen.Technique):
131 kind: str = 'forest'
133 def apply(self, item: Any, **kwargs: Any) -> Any:
134 return item
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'
149def test_step_without_contents() -> None:
150 with pytest.raises(ValueError, match = "step 'e' has no technique"):
151 chrisjen.Step(name = 'e').apply(1)
154""" Worker """
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'
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]]