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

1"""Unit tests for the Nendo plugin system.""" 

2import unittest 

3 

4import numpy as np 

5 

6from nendo import ( 

7 Nendo, 

8 NendoAnalysisPlugin, 

9 NendoCollection, 

10 NendoConfig, 

11 NendoEffectPlugin, 

12 NendoGeneratePlugin, 

13 NendoTrack, 

14) 

15 

16nd = Nendo( 

17 config=NendoConfig( 

18 log_level="DEBUG", 

19 library_plugin="default", 

20 library_path="tests/library", 

21 ), 

22) 

23 

24 

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 ) 

34 

35 

36class ExampleAnalysisPlugin(NendoAnalysisPlugin): 

37 """Example plugin for testing the NendoAnalysisPlugin class.""" 

38 

39 @NendoAnalysisPlugin.run_track 

40 def track_function(self, track): 

41 """Example track function.""" 

42 

43 @NendoAnalysisPlugin.run_collection 

44 def collection_function(self, collection): 

45 """Example collection function.""" 

46 

47 

48class ExampleEffectPlugin(NendoEffectPlugin): 

49 """Example plugin for testing the NendoEffectPlugin class.""" 

50 

51 @NendoEffectPlugin.run_track 

52 def track_function(self, track=None): 

53 """Example track function.""" 

54 return track 

55 

56 @NendoEffectPlugin.run_collection 

57 def collection_function(self, collection=None): 

58 """Example collection function.""" 

59 return collection 

60 

61 @NendoEffectPlugin.run_signal 

62 def signal_function(self, signal=None, sr=None): 

63 """Example signal function.""" 

64 return signal, sr 

65 

66 

67class ExampleGeneratePlugin(NendoGeneratePlugin): 

68 """Example plugin for testing the NendoGeneratePlugin class.""" 

69 

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 

76 

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] 

84 

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 

95 

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 

102 

103 

104class NendoAnalysisPluginTest(unittest.TestCase): 

105 """Unit test class for testing the NendoAnalysisPlugin class.""" 

106 

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") 

112 

113 result = plug.track_function(track=track) 

114 self.assertEqual(type(result), NendoTrack) 

115 self.assertEqual(result.id, track.id) 

116 

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]) 

123 

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) 

128 

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") 

134 

135 result = plug.collection_function(track=track) 

136 self.assertEqual(type(result), NendoTrack) 

137 self.assertEqual(result.id, track.id) 

138 

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]) 

145 

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) 

150 

151 

152class NendoGeneratePluginTest(unittest.TestCase): 

153 """Unit test class for testing the NendoGeneratePlugin class.""" 

154 

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") 

160 

161 result = plug.track_function(track=track) 

162 self.assertEqual(type(result), NendoTrack) 

163 self.assertEqual(result.id, track.id) 

164 

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) 

169 

170 result = plug.track_function() 

171 self.assertEqual(type(result), NendoTrack) 

172 

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]) 

179 

180 result = plug.track_function(collection=coll) 

181 self.assertEqual(type(result), NendoCollection) 

182 self.assertEqual(len(result), 1) 

183 

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") 

189 

190 result = plug.track_list_function(track=track) 

191 self.assertEqual(type(result), NendoCollection) 

192 self.assertEqual(len(result), 2) 

193 

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]) 

200 

201 result = plug.track_list_function(collection=coll) 

202 self.assertEqual(type(result), NendoCollection) 

203 self.assertEqual(len(result), 2) 

204 

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) 

209 

210 result = plug.track_list_function() 

211 self.assertEqual(type(result), NendoCollection) 

212 self.assertEqual(len(result), 2) 

213 

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") 

219 

220 result = plug.collection_function(track=track) 

221 self.assertEqual(type(result), NendoCollection) 

222 self.assertEqual(len(result), 1) 

223 

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]) 

230 

231 result = plug.collection_function(collection=coll) 

232 self.assertEqual(type(result), NendoCollection) 

233 self.assertEqual(len(result), 1) 

234 

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) 

239 

240 result = plug.collection_function() 

241 self.assertEqual(type(result), NendoCollection) 

242 self.assertEqual(len(result), 1) 

243 

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") 

249 

250 result = plug.signal_function(track=track) 

251 self.assertEqual(type(result), NendoTrack) 

252 

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]) 

259 

260 result = plug.signal_function(collection=coll) 

261 self.assertEqual(type(result), NendoCollection) 

262 self.assertEqual(len(result), 1) 

263 

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) 

268 

269 result = plug.signal_function() 

270 self.assertEqual(type(result), NendoTrack) 

271 

272 

273class NendoEffectPluginTest(unittest.TestCase): 

274 """Unit test class for testing the NendoEffectPlugin class.""" 

275 

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") 

281 

282 result = plug.track_function(track=track) 

283 self.assertEqual(type(result), NendoTrack) 

284 self.assertEqual(result.id, track.id) 

285 

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]) 

292 

293 result = plug.track_function(collection=coll) 

294 self.assertEqual(type(result), NendoCollection) 

295 self.assertEqual(len(result), 1) 

296 

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") 

302 

303 result = plug.collection_function(track=track) 

304 self.assertEqual(type(result), NendoCollection) 

305 self.assertEqual(len(result), 1) 

306 

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]) 

313 

314 result = plug.collection_function(collection=coll) 

315 self.assertEqual(type(result), NendoCollection) 

316 self.assertEqual(len(result), 1) 

317 

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") 

323 

324 result = plug.signal_function(track=track) 

325 self.assertEqual(type(result), NendoTrack) 

326 

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]) 

333 

334 result = plug.signal_function(collection=coll) 

335 self.assertEqual(type(result), NendoCollection) 

336 self.assertEqual(len(result), 1)