comparison src/test/test_plugin_misc_radiocol.py @ 1915:fd959c8f64b6

test (plugin radiocol): fixes the test to also handle MP3 files
author souliane <souliane@mailoo.org>
date Fri, 18 Mar 2016 10:25:38 +0100
parents 0681d69cbe0a
children 2daf7b4c6756
comparison
equal deleted inserted replaced
1914:37db78010752 1915:fd959c8f64b6
31 from twisted.internet import reactor 31 from twisted.internet import reactor
32 from twisted.internet import defer 32 from twisted.internet import defer
33 from twisted.python.failure import Failure 33 from twisted.python.failure import Failure
34 from twisted.trial.unittest import SkipTest 34 from twisted.trial.unittest import SkipTest
35 35
36 from mutagen.oggvorbis import OggVorbis 36 try:
37 from mutagen.oggvorbis import OggVorbis
38 from mutagen.mp3 import MP3
39 from mutagen.easyid3 import EasyID3
40 from mutagen.id3 import ID3NoHeaderError
41 except ImportError:
42 raise exceptions.MissingModule(u"Missing module Mutagen, please download/install from https://bitbucket.org/lazka/mutagen")
37 43
38 import uuid 44 import uuid
39 import os 45 import os
40 import copy 46 import copy
41 import shutil 47 import shutil
42 from logging import WARNING 48
43 49
44 ROOM_JID = JID(Const.MUC_STR[0]) 50 ROOM_JID = JID(Const.MUC_STR[0])
45 PROFILE = Const.PROFILE[0] 51 PROFILE = Const.PROFILE[0]
46 REFEREE_FULL = JID(ROOM_JID.userhost() + '/' + Const.JID[0].user) 52 REFEREE_FULL = JID(ROOM_JID.userhost() + '/' + Const.JID[0].user)
47 PLAYERS_INDICES = [0, 1, 3] # referee included 53 PLAYERS_INDICES = [0, 1, 3] # referee included
161 @param d: deferred value or failure got from self.plugin.radiocolSongAdded 167 @param d: deferred value or failure got from self.plugin.radiocolSongAdded
162 @param filepath: full path to the sound file 168 @param filepath: full path to the sound file
163 @param profile_index: the profile index of the uploader 169 @param profile_index: the profile index of the uploader
164 """ 170 """
165 if isinstance(d, Failure): 171 if isinstance(d, Failure):
166 self.fail("OGG song could not be added!") 172 self.fail("OGG or MP3 song could not be added!")
167 173
168 game_data = self.plugin.games[ROOM_JID] 174 game_data = self.plugin.games[ROOM_JID]
169 song = OggVorbis(filepath) 175
176 # this is copied from the plugin
177 if filepath.lower().endswith('.mp3'):
178 actual_song = MP3(filepath)
179 try:
180 song = EasyID3(filepath)
181
182 class Info(object):
183 def __init__(self, length):
184 self.length = length
185 song.info = Info(actual_song.info.length)
186 except ID3NoHeaderError:
187 song = actual_song
188 else:
189 song = OggVorbis(filepath)
190
170 attrs = {'filename': os.path.basename(filepath), 191 attrs = {'filename': os.path.basename(filepath),
171 'title': song.get("title", ["Unknown"])[0], 192 'title': song.get("title", ["Unknown"])[0],
172 'artist': song.get("artist", ["Unknown"])[0], 193 'artist': song.get("artist", ["Unknown"])[0],
173 'album': song.get("album", ["Unknown"])[0], 194 'album': song.get("album", ["Unknown"])[0],
174 'length': str(song.info.length) 195 'length': str(song.info.length)
287 user = room.roster[user_nick] 308 user = room.roster[user_nick]
288 self.plugin_0045.leaveRoom(0, player_index) 309 self.plugin_0045.leaveRoom(0, player_index)
289 self.plugin.userLeftTrigger(room, user, PROFILE) 310 self.plugin.userLeftTrigger(room, user, PROFILE)
290 nicks.remove(user_nick) 311 nicks.remove(user_nick)
291 312
292 def _uploadSong(self, song_index, profile_index, ext='ogg'): 313 def _uploadSong(self, song_index, profile_index):
293 """Upload the song of index song_index (modulo self.songs size) 314 """Upload the song of index song_index (modulo self.songs size) from the profile of index profile_index.
294 from the profile of index profile_index. All the songs in self.songs 315
295 are OGG, but you can set ext to a value different than 'ogg' or 'mp3': 316 @param song_index: index of the song or None to test with non existing file
296 - 'xxx' to test non existent files
297 @param song_index: index of the song
298 @param profile_index: index of the uploader's profile 317 @param profile_index: index of the uploader's profile
299 @param new_ext: change the extension from "ogg" to this value 318 """
300 """ 319 if song_index is None:
301 song_index = song_index % len(self.songs) 320 dst_filepath = unicode(uuid.uuid1())
302 src_filename = self.songs[song_index] 321 expect_io_error = True
303 if ext not in ('ogg', 'mp3'): 322 else:
304 src_filename = os.path.splitext(src_filename)[0] + '.' + ext 323 song_index = song_index % len(self.songs)
305 dst_filepath = '/tmp/%s.%s' % (uuid.uuid1(), ext) 324 src_filename = self.songs[song_index]
306 expect_io_error = ext == 'xxx' 325 dst_filepath = '/tmp/%s%s' % (uuid.uuid1(), os.path.splitext(src_filename)[1])
307 if not expect_io_error:
308 shutil.copy(self.sound_dir + src_filename, dst_filepath) 326 shutil.copy(self.sound_dir + src_filename, dst_filepath)
327 expect_io_error = False
309 328
310 try: 329 try:
311 d = self.plugin.radiocolSongAdded(REFEREE_FULL, dst_filepath, Const.PROFILE[profile_index]) 330 d = self.plugin.radiocolSongAdded(REFEREE_FULL, dst_filepath, Const.PROFILE[profile_index])
312 except IOError: 331 except IOError:
313 self.assertTrue(expect_io_error) 332 self.assertTrue(expect_io_error)
319 def eb(failure): 338 def eb(failure):
320 if not isinstance(failure, Failure): 339 if not isinstance(failure, Failure):
321 self.fail("Adding a song which is not OGG nor MP3 should fail!") 340 self.fail("Adding a song which is not OGG nor MP3 should fail!")
322 self.assertEqual(failure.value.__class__, exceptions.DataError) 341 self.assertEqual(failure.value.__class__, exceptions.DataError)
323 342
324 if self.songs[song_index].endswith('.ogg') or self.songs[song_index].endswith('.mp3'): 343 if src_filename.endswith('.ogg') or src_filename.endswith('.mp3'):
325 d.addCallbacks(cb, cb) 344 d.addCallbacks(cb, cb)
326 else: 345 else:
327 d.addCallbacks(eb, eb) 346 d.addCallbacks(eb, eb)
328 347
329 def test_init(self): 348 def test_init(self):
349 self._joinRoom(room, nicks, 1) # player joins 368 self._joinRoom(room, nicks, 1) # player joins
350 self._joinRoom(room, nicks, 4) # user not playing joins 369 self._joinRoom(room, nicks, 4) # user not playing joins
351 370
352 song_index = 0 371 song_index = 0
353 self._uploadSong(song_index, 0) # ogg or mp3 file should exist in sat_media/test/song 372 self._uploadSong(song_index, 0) # ogg or mp3 file should exist in sat_media/test/song
354 self._uploadSong(song_index, 0, 'xxx') # file should not exist 373 self._uploadSong(None, 0) # non existing file
355 374
356 # another songs are added by Const.JID[1] until the radio starts + 1 to fill the queue 375 # another songs are added by Const.JID[1] until the radio starts + 1 to fill the queue
357 # when the first song starts + 1 to be rejected because the queue is full 376 # when the first song starts + 1 to be rejected because the queue is full
358 for song_index in xrange(1, plugin.QUEUE_TO_START + 1): 377 for song_index in xrange(1, plugin.QUEUE_TO_START + 1):
359 self._uploadSong(song_index, 1) 378 self._uploadSong(song_index, 1)