Coverage for tests/test_plugin.py: 100%
204 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-18 16:00 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-18 16:00 +0100
1"""Unit tests for the Nendo plugin system."""
2import unittest
4import numpy as np
6from nendo import (
7 Nendo,
8 NendoAnalysisPlugin,
9 NendoCollection,
10 NendoConfig,
11 NendoEffectPlugin,
12 NendoGeneratePlugin,
13 NendoTrack,
14)
16nd = Nendo(
17 config=NendoConfig(
18 log_level="DEBUG",
19 library_plugin="default",
20 library_path="tests/library",
21 ),
22)
25def init_plugin(clazz):
26 """Helper function to initialize a plugin for tests."""
27 return clazz(
28 nendo_instance=nd,
29 config=NendoConfig(),
30 logger=nd.logger,
31 plugin_name="test_plugin",
32 plugin_version="0.1",
33 )
36class ExampleAnalysisPlugin(NendoAnalysisPlugin):
37 """Example plugin for testing the NendoAnalysisPlugin class."""
39 @NendoAnalysisPlugin.run_track
40 def track_function(self, track):
41 """Example track function."""
43 @NendoAnalysisPlugin.run_collection
44 def collection_function(self, collection):
45 """Example collection function."""
48class ExampleEffectPlugin(NendoEffectPlugin):
49 """Example plugin for testing the NendoEffectPlugin class."""
51 @NendoEffectPlugin.run_track
52 def track_function(self, track=None):
53 """Example track function."""
54 return track
56 @NendoEffectPlugin.run_collection
57 def collection_function(self, collection=None):
58 """Example collection function."""
59 return collection
61 @NendoEffectPlugin.run_signal
62 def signal_function(self, signal=None, sr=None):
63 """Example signal function."""
64 return signal, sr
67class ExampleGeneratePlugin(NendoGeneratePlugin):
68 """Example plugin for testing the NendoGeneratePlugin class."""
70 @NendoGeneratePlugin.run_track
71 def track_function(self, track=None):
72 """Example track function."""
73 if track is None:
74 track = nd.library.add_track(file_path="tests/assets/test.wav")
75 return track
77 @NendoGeneratePlugin.run_track
78 def track_list_function(self, track=None):
79 """Example track list function."""
80 if track is None:
81 track = nd.library.add_track(file_path="tests/assets/test.wav")
82 track_2 = nd.library.add_track(file_path="tests/assets/test.mp3")
83 return [track, track_2]
85 @NendoGeneratePlugin.run_collection
86 def collection_function(self, collection=None):
87 """Example collection function."""
88 if collection is None:
89 track = nd.library.add_track(file_path="tests/assets/test.wav")
90 collection = nd.library.add_collection(
91 name="test_collection",
92 track_ids=[track.id],
93 )
94 return collection
96 @NendoGeneratePlugin.run_signal
97 def signal_function(self, signal=None, sr=None):
98 """Example signal function."""
99 if signal is None:
100 signal, sr = np.zeros([2, 23000]), 44100
101 return signal, sr
104class NendoAnalysisPluginTest(unittest.TestCase):
105 """Unit test class for testing the NendoAnalysisPlugin class."""
107 def test_run_track_decorator_with_track(self):
108 """Test the `NendoAnalysisPlugin.run_track` decorator with a `NendoTrack`."""
109 nd.library.reset(force=True)
110 plug = init_plugin(ExampleAnalysisPlugin)
111 track = nd.library.add_track(file_path="tests/assets/test.wav")
113 result = plug.track_function(track=track)
114 self.assertEqual(type(result), NendoTrack)
115 self.assertEqual(result.id, track.id)
117 def test_run_track_decorator_with_collection(self):
118 """Test the `NendoAnalysisPlugin.run_track` decorator with a `NendoCollection`."""
119 nd.library.reset(force=True)
120 plug = init_plugin(ExampleAnalysisPlugin)
121 track = nd.library.add_track(file_path="tests/assets/test.wav")
122 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
124 result = plug.track_function(collection=coll)
125 self.assertEqual(type(result), NendoCollection)
126 self.assertEqual(len(result), 1)
127 self.assertEqual(result[0].id, track.id)
129 def test_run_collection_decorator_with_track(self):
130 """Test the `NendoAnalysisPlugin.run_collection` decorator with a `NendoTrack`."""
131 nd.library.reset(force=True)
132 plug = init_plugin(ExampleAnalysisPlugin)
133 track = nd.library.add_track(file_path="tests/assets/test.wav")
135 result = plug.collection_function(track=track)
136 self.assertEqual(type(result), NendoTrack)
137 self.assertEqual(result.id, track.id)
139 def test_run_collection_decorator_with_collection(self):
140 """Test the `NendoAnalysisPlugin.run_collection` decorator with a `NendoCollection`."""
141 nd.library.reset(force=True)
142 plug = init_plugin(ExampleAnalysisPlugin)
143 track = nd.library.add_track(file_path="tests/assets/test.wav")
144 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
146 result = plug.collection_function(collection=coll)
147 self.assertEqual(type(result), NendoCollection)
148 self.assertEqual(len(result), 1)
149 self.assertEqual(result[0].id, track.id)
152class NendoGeneratePluginTest(unittest.TestCase):
153 """Unit test class for testing the NendoGeneratePlugin class."""
155 def test_run_track_decorator_with_track(self):
156 """Test the `NendoGeneratePlugin.run_track` decorator with a `NendoTrack`."""
157 nd.library.reset(force=True)
158 plug = init_plugin(ExampleGeneratePlugin)
159 track = nd.library.add_track(file_path="tests/assets/test.wav")
161 result = plug.track_function(track=track)
162 self.assertEqual(type(result), NendoTrack)
163 self.assertEqual(result.id, track.id)
165 def test_run_track_decorator_with_none(self):
166 """Test the `NendoGeneratePlugin.run_track` decorator with a `None`."""
167 nd.library.reset(force=True)
168 plug = init_plugin(ExampleGeneratePlugin)
170 result = plug.track_function()
171 self.assertEqual(type(result), NendoTrack)
173 def test_run_track_decorator_with_collection(self):
174 """Test the `NendoGeneratePlugin.run_track` decorator with a `NendoCollection`."""
175 nd.library.reset(force=True)
176 plug = init_plugin(ExampleGeneratePlugin)
177 track = nd.library.add_track(file_path="tests/assets/test.wav")
178 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
180 result = plug.track_function(collection=coll)
181 self.assertEqual(type(result), NendoCollection)
182 self.assertEqual(len(result), 1)
184 def test_run_track_list_decorator_with_track(self):
185 """Test the `NendoGeneratePlugin.run_track_list` decorator with a `NendoTrack`."""
186 nd.library.reset(force=True)
187 plug = init_plugin(ExampleGeneratePlugin)
188 track = nd.library.add_track(file_path="tests/assets/test.wav")
190 result = plug.track_list_function(track=track)
191 self.assertEqual(type(result), NendoCollection)
192 self.assertEqual(len(result), 2)
194 def test_run_track_list_decorator_with_collection(self):
195 """Test the `NendoGeneratePlugin.run_track_list` decorator with a `NendoCollection`."""
196 nd.library.reset(force=True)
197 plug = init_plugin(ExampleGeneratePlugin)
198 track = nd.library.add_track(file_path="tests/assets/test.wav")
199 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
201 result = plug.track_list_function(collection=coll)
202 self.assertEqual(type(result), NendoCollection)
203 self.assertEqual(len(result), 2)
205 def test_run_track_list_decorator_with_none(self):
206 """Test the `NendoGeneratePlugin.run_track_list` decorator with a `None`."""
207 nd.library.reset(force=True)
208 plug = init_plugin(ExampleGeneratePlugin)
210 result = plug.track_list_function()
211 self.assertEqual(type(result), NendoCollection)
212 self.assertEqual(len(result), 2)
214 def test_run_collection_decorator_with_track(self):
215 """Test the `NendoGeneratePlugin.run_collection` decorator with a `NendoTrack`."""
216 nd.library.reset(force=True)
217 plug = init_plugin(ExampleGeneratePlugin)
218 track = nd.library.add_track(file_path="tests/assets/test.wav")
220 result = plug.collection_function(track=track)
221 self.assertEqual(type(result), NendoCollection)
222 self.assertEqual(len(result), 1)
224 def test_run_collection_decorator_with_collection(self):
225 """Test the `NendoGeneratePlugin.run_collection` decorator with a `NendoCollection`."""
226 nd.library.reset(force=True)
227 plug = init_plugin(ExampleGeneratePlugin)
228 track = nd.library.add_track(file_path="tests/assets/test.wav")
229 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
231 result = plug.collection_function(collection=coll)
232 self.assertEqual(type(result), NendoCollection)
233 self.assertEqual(len(result), 1)
235 def test_run_collection_decorator_with_none(self):
236 """Test the `NendoGeneratePlugin.run_collection` decorator with a `None`."""
237 nd.library.reset(force=True)
238 plug = init_plugin(ExampleGeneratePlugin)
240 result = plug.collection_function()
241 self.assertEqual(type(result), NendoCollection)
242 self.assertEqual(len(result), 1)
244 def test_run_signal_decorator_with_track(self):
245 """Test the `NendoGeneratePlugin.run_signal` decorator with a `NendoTrack`."""
246 nd.library.reset(force=True)
247 plug = init_plugin(ExampleGeneratePlugin)
248 track = nd.library.add_track(file_path="tests/assets/test.wav")
250 result = plug.signal_function(track=track)
251 self.assertEqual(type(result), NendoTrack)
253 def test_run_signal_decorator_with_collection(self):
254 """Test the `NendoGeneratePlugin.run_signal` decorator with a `NendoCollection`."""
255 nd.library.reset(force=True)
256 plug = init_plugin(ExampleGeneratePlugin)
257 track = nd.library.add_track(file_path="tests/assets/test.wav")
258 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
260 result = plug.signal_function(collection=coll)
261 self.assertEqual(type(result), NendoCollection)
262 self.assertEqual(len(result), 1)
264 def test_run_signal_decorator_with_none(self):
265 """Test the `NendoGeneratePlugin.run_signal` decorator with a `None`."""
266 nd.library.reset(force=True)
267 plug = init_plugin(ExampleGeneratePlugin)
269 result = plug.signal_function()
270 self.assertEqual(type(result), NendoTrack)
273class NendoEffectPluginTest(unittest.TestCase):
274 """Unit test class for testing the NendoEffectPlugin class."""
276 def test_run_track_decorator_with_track(self):
277 """Test the `NendoEffectPlugin.run_track` decorator with a `NendoTrack`."""
278 nd.library.reset(force=True)
279 plug = init_plugin(ExampleEffectPlugin)
280 track = nd.library.add_track(file_path="tests/assets/test.wav")
282 result = plug.track_function(track=track)
283 self.assertEqual(type(result), NendoTrack)
284 self.assertEqual(result.id, track.id)
286 def test_run_track_decorator_with_collection(self):
287 """Test the `NendoEffectPlugin.run_track` decorator with a `NendoCollection`."""
288 nd.library.reset(force=True)
289 plug = init_plugin(ExampleEffectPlugin)
290 track = nd.library.add_track(file_path="tests/assets/test.wav")
291 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
293 result = plug.track_function(collection=coll)
294 self.assertEqual(type(result), NendoCollection)
295 self.assertEqual(len(result), 1)
297 def test_run_collection_decorator_with_track(self):
298 """Test the `NendoEffectPlugin.run_collection` decorator with a `NendoTrack`."""
299 nd.library.reset(force=True)
300 plug = init_plugin(ExampleEffectPlugin)
301 track = nd.library.add_track(file_path="tests/assets/test.wav")
303 result = plug.collection_function(track=track)
304 self.assertEqual(type(result), NendoCollection)
305 self.assertEqual(len(result), 1)
307 def test_run_collection_decorator_with_collection(self):
308 """Test the `NendoEffectPlugin.run_collection` decorator with a `NendoCollection`."""
309 nd.library.reset(force=True)
310 plug = init_plugin(ExampleEffectPlugin)
311 track = nd.library.add_track(file_path="tests/assets/test.wav")
312 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
314 result = plug.collection_function(collection=coll)
315 self.assertEqual(type(result), NendoCollection)
316 self.assertEqual(len(result), 1)
318 def test_run_signal_decorator_with_track(self):
319 """Test the `NendoEffectPlugin.run_signal` decorator with a `NendoTrack`."""
320 nd.library.reset(force=True)
321 plug = init_plugin(ExampleEffectPlugin)
322 track = nd.library.add_track(file_path="tests/assets/test.wav")
324 result = plug.signal_function(track=track)
325 self.assertEqual(type(result), NendoTrack)
327 def test_run_signal_decorator_with_collection(self):
328 """Test the `NendoEffectPlugin.run_signal` decorator with a `NendoCollection`."""
329 nd.library.reset(force=True)
330 plug = init_plugin(ExampleEffectPlugin)
331 track = nd.library.add_track(file_path="tests/assets/test.wav")
332 coll = nd.library.add_collection(name="test_collection", track_ids=[track.id])
334 result = plug.signal_function(collection=coll)
335 self.assertEqual(type(result), NendoCollection)
336 self.assertEqual(len(result), 1)