Coverage for tests/test_plugin_data.py: 99%

78 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-30 09:47 +0100

1# -*- encoding: utf-8 -*- 

2"""Unit tests for the Nendo Core class `PluginData`.""" 

3 

4import unittest 

5 

6from nendo import Nendo, NendoConfig, NendoPluginData 

7 

8nd = Nendo( 

9 config=NendoConfig( 

10 log_level="DEBUG", 

11 library_path="tests/library", 

12 library_plugin="default", 

13 copy_to_library=False, 

14 max_threads=1, 

15 plugins=[], 

16 stream_mode=False, 

17 stream_chunk_size=3, 

18 ), 

19) 

20 

21 

22class PluginDataTest(unittest.TestCase): 

23 """Unit test class for testing Nendo plugin data.""" 

24 

25 def test_add_plugin_data(self): 

26 """Test the adding of plugin data to a `NendoTrack`.""" 

27 nd.library.reset(force=True) 

28 track = nd.library.add_track(file_path="tests/assets/test.mp3") 

29 pd = nd.library.add_plugin_data( 

30 track_id=track.id, 

31 plugin_name="test_plugin", 

32 plugin_version="1.0", 

33 key="test", 

34 value="value", 

35 ) 

36 track = nd.library.get_track(track_id=track.id) 

37 self.assertEqual(len(track.plugin_data), 1) 

38 self.assertEqual(track.plugin_data[0], pd) 

39 

40 def test_get_plugin_data(self): 

41 """Test the getting of plugin data from a `NendoTrack`.""" 

42 nd.library.reset(force=True) 

43 track = nd.library.add_track(file_path="tests/assets/test.mp3") 

44 _ = nd.library.add_plugin_data( 

45 track_id=track.id, 

46 plugin_name="test_plugin", 

47 plugin_version="1.0", 

48 key="test", 

49 value="value", 

50 ) 

51 _ = nd.library.add_plugin_data( 

52 track_id=track.id, 

53 plugin_name="test_plugin", 

54 plugin_version="1.0", 

55 key="test2", 

56 value="value2", 

57 ) 

58 track = nd.library.get_track(track_id=track.id) 

59 plugin_data = track.get_plugin_data(plugin_name="test_plugin") 

60 self.assertEqual(type(plugin_data), list) 

61 self.assertEqual(len(plugin_data), 2) 

62 self.assertEqual(type(plugin_data[0]), NendoPluginData) 

63 plugin_data_2 = track.get_plugin_data(plugin_name="test_plugin", key="test2") 

64 self.assertEqual(type(plugin_data_2), list) 

65 self.assertEqual(plugin_data_2[0].value, "value2") 

66 

67 def test_filter_random_track(self): 

68 """Test the retrieval of a random `NendoTrack` from the library.""" 

69 nd.library.reset(force=True) 

70 track = nd.library.add_track(file_path="tests/assets/test.mp3") 

71 pd = nd.library.add_plugin_data( 

72 track_id=track.id, 

73 plugin_name="test_plugin", 

74 plugin_version="1.0", 

75 key="test", 

76 value="value", 

77 ) 

78 example_data = nd.library.filter_tracks( 

79 filters={"test": "value"}, 

80 order_by="random", 

81 plugin_names=["test_plugin"], 

82 )[0].get_plugin_data(plugin_name="test_plugin") 

83 self.assertEqual(type(example_data), list) 

84 self.assertEqual(example_data[0].value, pd.value) 

85 

86 def test_filter_by_plugin_data_and_filename(self): 

87 """Test filtering by plugin data and track file name.""" 

88 nd.library.reset(force=True) 

89 track = nd.library.add_track(file_path="tests/assets/test.mp3") 

90 pd = nd.library.add_plugin_data( 

91 track_id=track.id, 

92 plugin_name="test_plugin", 

93 plugin_version="1.0", 

94 key="test", 

95 value="value", 

96 ) 

97 example_data = nd.library.filter_tracks( 

98 filters={"test": "value"}, 

99 resource_filters={}, 

100 order_by="random", 

101 plugin_names=["test_plugin"], 

102 )[0].get_plugin_data(plugin_name="test_plugin") 

103 self.assertEqual(type(example_data), list) 

104 self.assertEqual(example_data[0].value, pd.value) 

105 

106 def test_filter_tracks_by_plugin_data(self): 

107 """Test the filtering of `NendoTrack`s by plugin data.""" 

108 nd.library.reset(force=True) 

109 track = nd.library.add_track(file_path="tests/assets/test.mp3") 

110 pd = nd.library.add_plugin_data( 

111 track_id=track.id, 

112 plugin_name="test_plugin", 

113 plugin_version="1.0", 

114 key="foo", 

115 value="bar", 

116 ) 

117 track = nd.library.get_track(track_id=track.id) 

118 example_data = nd.library.filter_tracks( 

119 filters={"foo": "bar"}, 

120 plugin_names=["test_plugin"], 

121 )[0].get_plugin_data(plugin_name="test_plugin") 

122 self.assertEqual(type(example_data), list) 

123 self.assertEqual(example_data[0].value, track.plugin_data[0].value) 

124 example_data = nd.library.filter_tracks( 

125 filters={"foo": ["bar", "baz"]}, 

126 plugin_names=["test_plugin"], 

127 )[0].get_plugin_data(plugin_name="test_plugin") 

128 self.assertEqual(type(example_data), list) 

129 self.assertEqual(example_data[0].value, pd.value) 

130 example_data = nd.library.filter_tracks( 

131 filters={"foo": ["bat", "baz"]}, 

132 plugin_names=["test_plugin"], 

133 ) 

134 self.assertEqual(len(example_data), 0) 

135 pd2 = nd.library.add_plugin_data( 

136 track_id=track.id, 

137 plugin_name="test_plugin", 

138 plugin_version="1.0", 

139 key="number", 

140 value="15.10289371", 

141 ) 

142 track = nd.library.get_track(track_id=track.id) 

143 example_data = nd.library.filter_tracks( 

144 filters={"number": (10.0, 20.0)}, 

145 plugin_names=["test_plugin"], 

146 )[0].get_plugin_data(plugin_name="test_plugin", key="number") 

147 self.assertEqual(type(example_data), list) 

148 self.assertEqual(example_data[0].value, pd2.value) 

149 example_data = nd.library.filter_tracks( 

150 filters={"number": (10, 20)}, 

151 plugin_names=["test_plugin"], 

152 )[0].get_plugin_data(plugin_name="test_plugin", key="number") 

153 self.assertEqual(type(example_data), list) 

154 self.assertEqual(example_data[0].value, pd2.value) 

155 example_data = nd.library.filter_tracks( 

156 filters={"number": (20.0, 30.0)}, 

157 plugin_names=["test_plugin"], 

158 ) 

159 self.assertEqual(len(example_data), 0) 

160 

161 def test_filter_tracks_by_multiple_plugin_keys(self): 

162 """Test the filtering of `NendoTrack`s by multiple plugin keys.""" 

163 nd.library.reset(force=True) 

164 track = nd.library.add_track(file_path="tests/assets/test.mp3") 

165 nd.library.add_plugin_data( 

166 track_id=track.id, 

167 plugin_name="test_plugin", 

168 plugin_version="1.0", 

169 key="foo1", 

170 value="bar1", 

171 ) 

172 nd.library.add_plugin_data( 

173 track_id=track.id, 

174 plugin_name="test_plugin", 

175 plugin_version="1.0", 

176 key="foo2", 

177 value="bar2", 

178 ) 

179 nd.library.add_plugin_data( 

180 track_id=track.id, 

181 plugin_name="test_plugin", 

182 plugin_version="1.0", 

183 key="number", 

184 value="15.10289371", 

185 ) 

186 example_data = nd.library.filter_tracks( 

187 filters={"foo1": ["bar1", "baz1"], "foo2": "bar2", "number": (12.0, 18.0)}, 

188 plugin_names=["test_plugin"], 

189 ) 

190 self.assertEqual(len(example_data), 1) 

191 example_data = nd.library.filter_tracks( 

192 filters={"foo1": ["bar1", "baz1"], "foo2": "bar3", "number": (12.0, 18.0)}, 

193 plugin_names=["test_plugin"], 

194 ) 

195 self.assertEqual(len(example_data), 0) 

196 example_data = nd.library.filter_tracks( 

197 filters={"foo1": ["bar1", "baz1"], "foo2": "bar2", "number": (20.0, 21.0)}, 

198 plugin_names=["test_plugin"], 

199 ) 

200 self.assertEqual(len(example_data), 0) 

201 example_data = nd.library.filter_tracks( 

202 filters={"foo1": ["bat1", "baz1"], "foo2": "bar2", "number": (12.0, 18.0)}, 

203 plugin_names=["test_plugin"], 

204 ) 

205 self.assertEqual(len(example_data), 0) 

206 

207 

208if __name__ == "__main__": 

209 unittest.main()