changeset 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 37db78010752
children 72837638f076
files src/test/test_plugin_misc_radiocol.py
diffstat 1 files changed, 39 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/test_plugin_misc_radiocol.py	Fri Mar 18 09:14:00 2016 +0100
+++ b/src/test/test_plugin_misc_radiocol.py	Fri Mar 18 10:25:38 2016 +0100
@@ -33,13 +33,19 @@
 from twisted.python.failure import Failure
 from twisted.trial.unittest import SkipTest
 
-from mutagen.oggvorbis import OggVorbis
+try:
+    from mutagen.oggvorbis import OggVorbis
+    from mutagen.mp3 import MP3
+    from mutagen.easyid3 import EasyID3
+    from mutagen.id3 import ID3NoHeaderError
+except ImportError:
+    raise exceptions.MissingModule(u"Missing module Mutagen, please download/install from https://bitbucket.org/lazka/mutagen")
 
 import uuid
 import os
 import copy
 import shutil
-from logging import WARNING
+
 
 ROOM_JID = JID(Const.MUC_STR[0])
 PROFILE = Const.PROFILE[0]
@@ -163,10 +169,25 @@
         @param profile_index: the profile index of the uploader
         """
         if isinstance(d, Failure):
-            self.fail("OGG song could not be added!")
+            self.fail("OGG or MP3 song could not be added!")
 
         game_data = self.plugin.games[ROOM_JID]
-        song = OggVorbis(filepath)
+
+        # this is copied from the plugin
+        if filepath.lower().endswith('.mp3'):
+            actual_song = MP3(filepath)
+            try:
+                song = EasyID3(filepath)
+
+                class Info(object):
+                    def __init__(self, length):
+                        self.length = length
+                song.info = Info(actual_song.info.length)
+            except ID3NoHeaderError:
+                song = actual_song
+        else:
+            song = OggVorbis(filepath)
+
         attrs = {'filename': os.path.basename(filepath),
                  'title': song.get("title", ["Unknown"])[0],
                  'artist': song.get("artist", ["Unknown"])[0],
@@ -289,23 +310,21 @@
         self.plugin.userLeftTrigger(room, user, PROFILE)
         nicks.remove(user_nick)
 
-    def _uploadSong(self, song_index, profile_index, ext='ogg'):
-        """Upload the song of index song_index (modulo self.songs size)
-        from the profile of index profile_index. All the songs in self.songs
-        are OGG, but you can set ext to a value different than 'ogg' or 'mp3':
-        - 'xxx' to test non existent files
-        @param song_index: index of the song
+    def _uploadSong(self, song_index, profile_index):
+        """Upload the song of index song_index (modulo self.songs size) from the profile of index profile_index.
+
+        @param song_index: index of the song or None to test with non existing file
         @param profile_index: index of the uploader's profile
-        @param new_ext: change the extension from "ogg" to this value
         """
-        song_index = song_index % len(self.songs)
-        src_filename = self.songs[song_index]
-        if ext not in ('ogg', 'mp3'):
-            src_filename = os.path.splitext(src_filename)[0] + '.' + ext
-        dst_filepath = '/tmp/%s.%s' % (uuid.uuid1(), ext)
-        expect_io_error = ext == 'xxx'
-        if not expect_io_error:
+        if song_index is None:
+            dst_filepath = unicode(uuid.uuid1())
+            expect_io_error = True
+        else:
+            song_index = song_index % len(self.songs)
+            src_filename = self.songs[song_index]
+            dst_filepath = '/tmp/%s%s' % (uuid.uuid1(), os.path.splitext(src_filename)[1])
             shutil.copy(self.sound_dir + src_filename, dst_filepath)
+            expect_io_error = False
 
         try:
             d = self.plugin.radiocolSongAdded(REFEREE_FULL, dst_filepath, Const.PROFILE[profile_index])
@@ -321,7 +340,7 @@
                 self.fail("Adding a song which is not OGG nor MP3 should fail!")
             self.assertEqual(failure.value.__class__, exceptions.DataError)
 
-        if self.songs[song_index].endswith('.ogg') or self.songs[song_index].endswith('.mp3'):
+        if src_filename.endswith('.ogg') or src_filename.endswith('.mp3'):
             d.addCallbacks(cb, cb)
         else:
             d.addCallbacks(eb, eb)
@@ -351,7 +370,7 @@
 
         song_index = 0
         self._uploadSong(song_index, 0)  # ogg or mp3 file should exist in sat_media/test/song
-        self._uploadSong(song_index, 0, 'xxx')  # file should not exist
+        self._uploadSong(None, 0)  # non existing file
 
         # another songs are added by Const.JID[1] until the radio starts + 1 to fill the queue
         # when the first song starts + 1 to be rejected because the queue is full