|
8 | 8 | from waveform_editor.tendencies.periodic.periodic_base import PeriodicBaseTendency |
9 | 9 |
|
10 | 10 |
|
| 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 | + |
11 | 21 | @pytest.mark.parametrize( |
12 | 22 | "base, amplitude, min, max, expected_base, expected_amplitude, has_error", |
13 | 23 | [ |
@@ -46,28 +56,18 @@ def test_bounds( |
46 | 56 | """ |
47 | 57 | Test the base, amplitude, minimum and maximum values of the periodic base tendency |
48 | 58 | """ |
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) |
71 | 71 |
|
72 | 72 |
|
73 | 73 | @pytest.mark.parametrize( |
@@ -103,36 +103,26 @@ def test_bounds_prev( |
103 | 103 | when the tendency has a previous tendency. |
104 | 104 | """ |
105 | 105 | 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( |
127 | 109 | user_duration=1, |
128 | 110 | user_base=base, |
129 | 111 | user_amplitude=amplitude, |
130 | 112 | user_min=min, |
131 | 113 | user_max=max, |
132 | 114 | ) |
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) |
136 | 126 |
|
137 | 127 |
|
138 | 128 | @pytest.mark.parametrize( |
@@ -168,97 +158,63 @@ def test_bounds_next( |
168 | 158 | when the tendency has a next tendency. |
169 | 159 | """ |
170 | 160 | 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( |
192 | 164 | user_duration=1, |
193 | 165 | user_base=base, |
194 | 166 | user_amplitude=amplitude, |
195 | 167 | user_min=min, |
196 | 168 | user_max=max, |
197 | 169 | ) |
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) |
200 | 180 |
|
201 | 181 |
|
202 | 182 | def test_frequency_and_period(): |
203 | 183 | """Test if the frequency and period of the tendency are being set correctly.""" |
204 | 184 |
|
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) |
218 | 188 |
|
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) |
222 | 192 |
|
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 |
228 | 196 |
|
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 |
233 | 199 |
|
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) |
236 | 202 |
|
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) |
239 | 205 |
|
240 | 206 |
|
241 | 207 | def test_phase(): |
242 | 208 | """Test if the phase shift of the tendency is being set correctly.""" |
243 | 209 |
|
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) |
256 | 212 |
|
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) |
259 | 215 |
|
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) |
262 | 218 |
|
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