Skip to content

Commit 90e700e

Browse files
committed
add patch to fixture
1 parent a220477 commit 90e700e

File tree

1 file changed

+72
-116
lines changed

1 file changed

+72
-116
lines changed

tests/tendencies/periodic/test_periodic_base.py

Lines changed: 72 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
from waveform_editor.tendencies.periodic.periodic_base import PeriodicBaseTendency
99

1010

11+
@pytest.fixture(autouse=True)
12+
def patch_periodic_base_tendency():
13+
arr = np.array([0])
14+
with (
15+
patch.object(PeriodicBaseTendency, "get_value", return_value=(arr, arr)),
16+
patch.object(PeriodicBaseTendency, "get_derivative", return_value=arr),
17+
):
18+
yield
19+
20+
1121
@pytest.mark.parametrize(
1222
"base, amplitude, min, max, expected_base, expected_amplitude, has_error",
1323
[
@@ -46,28 +56,18 @@ def test_bounds(
4656
"""
4757
Test the base, amplitude, minimum and maximum values of the periodic base tendency
4858
"""
49-
with (
50-
patch.object(
51-
PeriodicBaseTendency,
52-
"get_value",
53-
return_value=(np.array([0]), np.array([0])),
54-
),
55-
patch.object(
56-
PeriodicBaseTendency, "get_derivative", return_value=np.array([0])
57-
),
58-
):
59-
tendency = PeriodicBaseTendency(
60-
user_duration=1,
61-
user_base=base,
62-
user_amplitude=amplitude,
63-
user_min=min,
64-
user_max=max,
65-
)
66-
if has_error:
67-
assert tendency.value_error is not None
68-
else:
69-
assert tendency.base == approx(expected_base)
70-
assert tendency.amplitude == approx(expected_amplitude)
59+
tendency = PeriodicBaseTendency(
60+
user_duration=1,
61+
user_base=base,
62+
user_amplitude=amplitude,
63+
user_min=min,
64+
user_max=max,
65+
)
66+
if has_error:
67+
assert tendency.value_error is not None
68+
else:
69+
assert tendency.base == approx(expected_base)
70+
assert tendency.amplitude == approx(expected_amplitude)
7171

7272

7373
@pytest.mark.parametrize(
@@ -103,36 +103,26 @@ def test_bounds_prev(
103103
when the tendency has a previous tendency.
104104
"""
105105
prev_tendency = ConstantTendency(user_start=0, user_duration=1, user_value=8)
106-
with (
107-
patch.object(
108-
PeriodicBaseTendency,
109-
"get_value",
110-
return_value=(np.array([0]), np.array([0])),
111-
),
112-
patch.object(
113-
PeriodicBaseTendency, "get_derivative", return_value=np.array([0])
114-
),
115-
):
116-
if has_error:
117-
with pytest.raises(ValueError):
118-
PeriodicBaseTendency(
119-
user_duration=1,
120-
user_base=base,
121-
user_amplitude=amplitude,
122-
user_min=min,
123-
user_max=max,
124-
)
125-
else:
126-
tendency = PeriodicBaseTendency(
106+
if has_error:
107+
with pytest.raises(ValueError):
108+
PeriodicBaseTendency(
127109
user_duration=1,
128110
user_base=base,
129111
user_amplitude=amplitude,
130112
user_min=min,
131113
user_max=max,
132114
)
133-
tendency.set_previous_tendency(prev_tendency)
134-
assert tendency.base == approx(expected_base)
135-
assert tendency.amplitude == approx(expected_amplitude)
115+
else:
116+
tendency = PeriodicBaseTendency(
117+
user_duration=1,
118+
user_base=base,
119+
user_amplitude=amplitude,
120+
user_min=min,
121+
user_max=max,
122+
)
123+
tendency.set_previous_tendency(prev_tendency)
124+
assert tendency.base == approx(expected_base)
125+
assert tendency.amplitude == approx(expected_amplitude)
136126

137127

138128
@pytest.mark.parametrize(
@@ -168,97 +158,63 @@ def test_bounds_next(
168158
when the tendency has a next tendency.
169159
"""
170160
next_tendency = ConstantTendency(user_duration=1, user_value=8)
171-
with (
172-
patch.object(
173-
PeriodicBaseTendency,
174-
"get_value",
175-
return_value=(np.array([0]), np.array([0])),
176-
),
177-
patch.object(
178-
PeriodicBaseTendency, "get_derivative", return_value=np.array([0])
179-
),
180-
):
181-
if has_error:
182-
with pytest.raises(ValueError):
183-
PeriodicBaseTendency(
184-
user_duration=1,
185-
user_base=base,
186-
user_amplitude=amplitude,
187-
user_min=min,
188-
user_max=max,
189-
)
190-
else:
191-
tendency = PeriodicBaseTendency(
161+
if has_error:
162+
with pytest.raises(ValueError):
163+
PeriodicBaseTendency(
192164
user_duration=1,
193165
user_base=base,
194166
user_amplitude=amplitude,
195167
user_min=min,
196168
user_max=max,
197169
)
198-
tendency.set_next_tendency(next_tendency)
199-
assert tendency.base == approx(expected_base)
170+
else:
171+
tendency = PeriodicBaseTendency(
172+
user_duration=1,
173+
user_base=base,
174+
user_amplitude=amplitude,
175+
user_min=min,
176+
user_max=max,
177+
)
178+
tendency.set_next_tendency(next_tendency)
179+
assert tendency.base == approx(expected_base)
200180

201181

202182
def test_frequency_and_period():
203183
"""Test if the frequency and period of the tendency are being set correctly."""
204184

205-
with (
206-
patch.object(
207-
PeriodicBaseTendency,
208-
"get_value",
209-
return_value=(np.array([0]), np.array([0])),
210-
),
211-
patch.object(
212-
PeriodicBaseTendency, "get_derivative", return_value=np.array([0])
213-
),
214-
):
215-
tendency = PeriodicBaseTendency(user_duration=1, user_frequency=5)
216-
assert tendency.frequency == 5
217-
assert tendency.period == approx(0.2)
185+
tendency = PeriodicBaseTendency(user_duration=1, user_frequency=5)
186+
assert tendency.frequency == 5
187+
assert tendency.period == approx(0.2)
218188

219-
tendency = PeriodicBaseTendency(user_duration=1, user_period=4)
220-
assert tendency.period == 4
221-
assert tendency.frequency == approx(0.25)
189+
tendency = PeriodicBaseTendency(user_duration=1, user_period=4)
190+
assert tendency.period == 4
191+
assert tendency.frequency == approx(0.25)
222192

223-
tendency = PeriodicBaseTendency(
224-
user_duration=1, user_period=2, user_frequency=0.5
225-
)
226-
assert tendency.period == 2
227-
assert tendency.frequency == 0.5
193+
tendency = PeriodicBaseTendency(user_duration=1, user_period=2, user_frequency=0.5)
194+
assert tendency.period == 2
195+
assert tendency.frequency == 0.5
228196

229-
tendency = PeriodicBaseTendency(
230-
user_duration=1, user_period=2, user_frequency=2
231-
)
232-
assert tendency.value_error is not None
197+
tendency = PeriodicBaseTendency(user_duration=1, user_period=2, user_frequency=2)
198+
assert tendency.value_error is not None
233199

234-
with pytest.raises(ValueError):
235-
tendency = PeriodicBaseTendency(user_duration=1, user_period=0)
200+
with pytest.raises(ValueError):
201+
tendency = PeriodicBaseTendency(user_duration=1, user_period=0)
236202

237-
with pytest.raises(ValueError):
238-
tendency = PeriodicBaseTendency(user_duration=1, user_frequency=0)
203+
with pytest.raises(ValueError):
204+
tendency = PeriodicBaseTendency(user_duration=1, user_frequency=0)
239205

240206

241207
def test_phase():
242208
"""Test if the phase shift of the tendency is being set correctly."""
243209

244-
with (
245-
patch.object(
246-
PeriodicBaseTendency,
247-
"get_value",
248-
return_value=(np.array([0]), np.array([0])),
249-
),
250-
patch.object(
251-
PeriodicBaseTendency, "get_derivative", return_value=np.array([0])
252-
),
253-
):
254-
tendency = PeriodicBaseTendency(user_duration=1, user_phase=np.pi / 2)
255-
assert tendency.phase == approx(np.pi / 2)
210+
tendency = PeriodicBaseTendency(user_duration=1, user_phase=np.pi / 2)
211+
assert tendency.phase == approx(np.pi / 2)
256212

257-
tendency = PeriodicBaseTendency(user_duration=1, user_phase=np.pi)
258-
assert tendency.phase == approx(np.pi)
213+
tendency = PeriodicBaseTendency(user_duration=1, user_phase=np.pi)
214+
assert tendency.phase == approx(np.pi)
259215

260-
tendency = PeriodicBaseTendency(user_duration=1, user_phase=2 * np.pi)
261-
assert tendency.phase == approx(0)
216+
tendency = PeriodicBaseTendency(user_duration=1, user_phase=2 * np.pi)
217+
assert tendency.phase == approx(0)
262218

263-
tendency = PeriodicBaseTendency(user_duration=1, user_phase=3 * np.pi)
264-
assert tendency.phase == approx(np.pi)
219+
tendency = PeriodicBaseTendency(user_duration=1, user_phase=3 * np.pi)
220+
assert tendency.phase == approx(np.pi)

0 commit comments

Comments
 (0)