~charclo-michael/entertainer/small-fixes

« back to all changes in this revision

Viewing changes to entertainerlib/tests/test_music.py

  • Committer: Paul Hummer
  • Date: 2009-02-04 04:26:49 UTC
  • mfrom: (338.1.7 trial-integration)
  • Revision ID: paul@eventuallyanyway.com-20090204042649-ohjvb2yge5dltwwy
Entertainer now uses trial to run its tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
__copyright__ = "2008, Matt Layman"
5
5
__author__ = "Matt Layman <laymansterms.dev@gmail.com>"
6
6
 
 
7
import os
 
8
 
7
9
from pysqlite2 import dbapi2 as sqlite
8
10
 
 
11
from entertainerlib.frontend.medialibrary.music import (
 
12
    Album, AlbumHasNoTracks, MusicLibrary, Track, TrackRatingOutOfRange,
 
13
    TrackTypeError)
 
14
from entertainerlib.tests.mock import MockBackendConnection
 
15
from entertainerlib.frontend.medialibrary.playable import Playable
9
16
from entertainerlib.tests import EntertainerTest
10
17
 
11
18
class TestMusic(EntertainerTest):
77
84
        connection.commit()
78
85
        connection.close()
79
86
 
 
87
 
 
88
class TestTrack(TestMusic):
 
89
 
 
90
    def setUp(self):
 
91
        TestMusic.setUp(self)
 
92
        self.track = Track('/path/to/track.mp3', # filename
 
93
                           'title',
 
94
                           1, # tracknumber
 
95
                           'artist0',
 
96
                           'album0',
 
97
                           'genre',
 
98
                           '128', # bitrate
 
99
                           2008, # year
 
100
                           5, # rating
 
101
                           240, # length
 
102
                           'comment',
 
103
                           'lyrics')
 
104
 
 
105
    def tearDown(self):
 
106
        TestMusic.tearDown(self)
 
107
 
 
108
    def testTrackConstructor(self):
 
109
        """testTrackConstructor - Ensures that a Track object is created"""
 
110
        self.assertTrue(isinstance(self.track, Track))
 
111
 
 
112
    def testTrackBadConstructor(self):
 
113
        """testTrackBadConstructor - Ensures that bad track construction
 
114
        raises an exception for the integer fields"""
 
115
        for i in [2, 7, 8, 9]:
 
116
            t = ['a', 'b', 1, 'c', 'd', 'e', 'f', 2, 3, 4, 'g', 'h']
 
117
            t[i] = str(t[i])
 
118
            self.assertRaises(TrackTypeError, Track, t[0], t[1], t[2], t[3],
 
119
                              t[4], t[5], t[6], t[7], t[8], t[9], t[10], t[11])
 
120
 
 
121
    def testTrackRatingInRange(self):
 
122
        """testTrackRatingInRange - Ensures that the ratings in range create
 
123
        valid Track objects"""
 
124
        for i in range(1, 6):
 
125
            self.track = Track('a', 'b', 1, 'c', 'd', 'e', 'f', 2, i, 3, 'g',
 
126
                               'h')
 
127
            self.assertTrue(isinstance(self.track, Track))
 
128
            self.assertEqual(self.track.get_rating(), i)
 
129
 
 
130
    def testTrackRatingOutOfRange(self):
 
131
        """testTrackRatingOutOfRange - Ensures that the rating raises an
 
132
        exception for something out of range in construction"""
 
133
        for i in [0, 6]:
 
134
            self.assertRaises(TrackRatingOutOfRange, Track, 'a', 'b', 1, 'c',
 
135
                              'd', 'e', 'f', 2, i, 3, 'g', 'h')
 
136
 
 
137
    def testTrackGetFilename(self):
 
138
        """testTrackGetFilename - Ensures that the filename is returned"""
 
139
        result = self.track.get_filename()
 
140
        self.assertEqual(result, '/path/to/track.mp3')
 
141
 
 
142
    def testTrackGetTitle(self):
 
143
        """testTrackGetTitle - Ensures that the title is returned"""
 
144
        result = self.track.get_title()
 
145
        self.assertEqual(result, 'title')
 
146
 
 
147
    def testTrackGetTrackNumber(self):
 
148
        """testTrackGetTrackNumber - Ensures that the tracknumber is
 
149
        returned"""
 
150
        result = self.track.get_tracknumber()
 
151
        self.assertEqual(result, 1)
 
152
 
 
153
    def testTrackGetArtist(self):
 
154
        """testTrackGetArtist - Ensures that the artist is returned"""
 
155
        result = self.track.get_artist()
 
156
        self.assertEqual(result, 'artist0')
 
157
 
 
158
    def testTrackGetAlbum(self):
 
159
        """testTrackGetAlbum - Ensures that an album object is returned"""
 
160
        result = self.track.get_album(self.cursor)
 
161
        self.assertTrue(isinstance(result, Album))
 
162
        self.assertEqual(result.get_title(), 'album0')
 
163
 
 
164
    def testTrackGetAlbumNot(self):
 
165
        """testTrackGetAlbumNot - Ensures that a bad album in the track
 
166
        returns AlbumHasNoTracks"""
 
167
        self.badTrack = Track('path', 'title', 1, 'artist',
 
168
                              'foo-bar-baz**', # Here is the bad input
 
169
                              'genre', '128', 2008, 5, 240, 'comment',
 
170
                              'lyrics')
 
171
        self.assertRaises(AlbumHasNoTracks, self.badTrack.get_album,
 
172
                          self.cursor)
 
173
 
 
174
    def testTrackGetAlbumArtUrlExists(self):
 
175
        """testTrackGetAlbumArtUrl - Ensures that the album art url is
 
176
        returned"""
 
177
        album_artist = "artist0 - album0"
 
178
        album_artist = album_artist.encode("base64")
 
179
        album_art = os.path.join(self.art_path, album_artist + ".jpg")
 
180
        file = open(album_art, "wb")
 
181
        file.close()
 
182
        result = self.track.get_album_art_url(self.cursor)
 
183
        self.assertEqual(result, album_art)
 
184
        if os.path.exists(album_art):
 
185
            os.remove(album_art)
 
186
 
 
187
    def testTrackGetAlbumArtUrlNotExists(self):
 
188
        """testTrackGetAlbumArtUrlNotExists - Ensures that when art does
 
189
        not exist, None is returned"""
 
190
        result = self.track.get_album_art_url(self.cursor)
 
191
        self.assertEqual(result, None)
 
192
 
 
193
    def testTrackGetGenre(self):
 
194
        """testTrackGetGenre - Ensures that the genre is returned"""
 
195
        result = self.track.get_genre()
 
196
        self.assertEqual(result, 'genre')
 
197
 
 
198
    def testTrackGetBitrate(self):
 
199
        """testTrackGetBitrate - Ensures that the bitrate is returned"""
 
200
        result = self.track.get_bitrate()
 
201
        self.assertEqual(result, '128')
 
202
 
 
203
    def testTrackGetYear(self):
 
204
        """testTrackGetYear - Ensures that the year is returned"""
 
205
        result = self.track.get_year()
 
206
        self.assertEqual(result, 2008)
 
207
 
 
208
    def testTrackGetRating(self):
 
209
        """testTrackGetRating - Ensures that the rating is returned"""
 
210
        result = self.track.get_rating()
 
211
        self.assertEqual(result, 5)
 
212
 
 
213
    def testTrackGetLength(self):
 
214
        """testTrackGetLength - Ensures that the length is returned"""
 
215
        result = self.track.get_length()
 
216
        self.assertEqual(result, 240)
 
217
 
 
218
    def testTrackGetComment(self):
 
219
        """testTrackGetComment - Ensures that the comment is returned"""
 
220
        result = self.track.get_comment()
 
221
        self.assertEqual(result, 'comment')
 
222
 
 
223
    def testTrackGetLyrics(self):
 
224
        """testTrackGetLyrics - Ensures that the lyrics are returned"""
 
225
        result = self.track.get_lyrics()
 
226
        self.assertEqual(result, 'lyrics')
 
227
 
 
228
    def testTrackSetLyrics(self):
 
229
        """testTrackSetLyrics - Ensures that lyrics are properly set"""
 
230
        self.track.set_lyrics('some new lyrics')
 
231
        self.assertEqual(self.track.get_lyrics(), 'some new lyrics')
 
232
 
 
233
    def testTrackSetNoneLyrics(self):
 
234
        """testTrackSetNoneLyrics - Ensures that lyrics are set to empty
 
235
        string when None is passed as lyrics input"""
 
236
        self.track.set_lyrics(None)
 
237
        self.assertEqual(self.track.get_lyrics(), '')
 
238
 
 
239
    def testTrackGetType(self):
 
240
        """testTrackGetType - Ensures that the type is returned"""
 
241
        result = self.track.get_type()
 
242
        self.assertEqual(result, Playable.AUDIO_STREAM)
 
243
 
 
244
    def testTrackGetUri(self):
 
245
        """testTrackGetUri - Ensures that the uri is returned"""
 
246
        result = self.track.get_uri()
 
247
        self.assertEqual(result, 'file:///path/to/track.mp3')
 
248
 
 
249
 
 
250
class TestMusicLibrary(TestMusic):
 
251
    '''Tests MusicLibrary'''
 
252
 
 
253
    def setUp(self):
 
254
        TestMusic.setUp(self)
 
255
        try:
 
256
            self.backend_connection = MockBackendConnection()
 
257
        except:
 
258
            print "\nbackend_connection failed"
 
259
        self.musiclibrary = MusicLibrary(self.backend_connection)
 
260
 
 
261
    def tearDown(self):
 
262
        self.backend_connection.close_connection()
 
263
        TestMusic.tearDown(self)
 
264
 
 
265
    def testMusicLibraryConstructor(self):
 
266
        """testMusicLibraryContructor - Ensures instantiation of MusicLibrary
 
267
        class"""
 
268
        self.assertTrue(isinstance(self.musiclibrary, MusicLibrary))
 
269
 
 
270
    def testGetAllArtists(self):
 
271
        """testGetAllArtists - Ensures that all artists are returned from
 
272
        the music library"""
 
273
        result = self.musiclibrary.get_all_artists()
 
274
        self.assertEqual(result, ['artist0'])
 
275
 
 
276
    def testGetAllTracks(self):
 
277
        """testGetAllTracks - Ensures that all tracks are returned"""
 
278
        result = self.musiclibrary.get_all_tracks()
 
279
        for i in result:
 
280
            self.assertTrue(isinstance(i, Track))
 
281
        self.assertEqual(len(result), 8)
 
282
 
 
283
    def testGetTracksByGenre(self):
 
284
        """testGetTracksByGenre - Ensures tracks of a certain genre are
 
285
        returned"""
 
286
        result = self.musiclibrary.get_tracks_by_genre('genre0')
 
287
        self.assertEqual(len(result), 4)
 
288
        for i in result:
 
289
            self.assertEqual(i.get_genre(), 'genre0')
 
290
 
 
291
    def testGetTracksByMissingGenre(self):
 
292
        """testGetTracksByMissingGenre - Ensures proper handling of a missing
 
293
        genre"""
 
294
        self.assertEquals(
 
295
            len(self.musiclibrary.get_tracks_by_genre('foo-bar-baz**')), 0)
 
296
 
 
297
    def testGetTracksByArtist(self):
 
298
        """testGetTracksByArtist - Ensure tracks by a certain artist are
 
299
        returned"""
 
300
        result = self.musiclibrary.get_tracks_by_artist('artist0')
 
301
        self.assertEqual(len(result), 8)
 
302
        for i in result:
 
303
            self.assertEqual(i.get_artist(), 'artist0')
 
304
 
 
305
    def testGetTracksByUnknownArtist(self):
 
306
        """testGetTracksByUnknownArtist - Ensure proper handling of an missing
 
307
        artist in the cache"""
 
308
        self.assertEquals(
 
309
            len(self.musiclibrary.get_tracks_by_artist('foo')), 0)
 
310
 
 
311
    def testGetAllAlbums(self):
 
312
        """testGetAllAlbums - Ensures all albums are returned"""
 
313
        result = self.musiclibrary.get_all_albums()
 
314
        for i in result:
 
315
            self.assertTrue(isinstance(i, Album))
 
316
        self.assertEqual(len(result), 2)
 
317
 
 
318
    def testGetAlbumsByArtist(self):
 
319
        """testGetAlbumsByArtist - Ensures correct albums by an artist is
 
320
        returned"""
 
321
        result = self.musiclibrary.get_albums_by_artist('artist0')
 
322
        self.assertEqual(len(result), 2)
 
323
        for i in result:
 
324
            self.assertEqual(i.get_artist(), 'artist0')
 
325
 
 
326
    def testGetAlbumsByUnknownArtist(self):
 
327
        """testGetAlbumsByUnknownArtist - Ensures proper handling of an
 
328
        artist not in the cache"""
 
329
        self.assertEquals(
 
330
            len(self.musiclibrary.get_albums_by_artist('foo')), 0)
 
331
 
 
332
    def testNumberOfTracks(self):
 
333
        """testNumberOfTracks - Ensures number of all tracks is returned"""
 
334
        result = self.musiclibrary.number_of_tracks()
 
335
        self.assertEqual(result, 8)
 
336
 
 
337
    def testNumberOfTracksByArtist(self):
 
338
        """testNumberOfTracksByArtist - Ensures number of all tracks by one
 
339
        artist is returned"""
 
340
        result = self.musiclibrary.number_of_tracks_by_artist('artist0')
 
341
        self.assertEqual(result, 8)
 
342
 
 
343
    def testNumberOfTracksByUnknownArtist(self):
 
344
        """testNumberOfTracksByUnknownArtist - Ensures proper handling when
 
345
        artist called is not in the cache"""
 
346
        self.assertEqual(
 
347
            self.musiclibrary.number_of_albums_by_artist('foo'), 0)
 
348
 
 
349
    def testNumberOfAlbumsByArtist(self):
 
350
        """testNumberOfAlbumsByArtist - Ensures number of all albums by one
 
351
        artist is returned"""
 
352
        result = self.musiclibrary.number_of_albums_by_artist('artist0')
 
353
        self.assertEqual(result, 2)
 
354
 
 
355
    def testNumberOfAlbumsByUnknownArtist(self):
 
356
        """testNumberOfAlbumsByUnknownArtist - Ensures proper handling when
 
357
        artist called is not in the cache"""
 
358
        self.assertEqual(
 
359
            self.musiclibrary.number_of_albums_by_artist('foo'), 0)
 
360
 
 
361
    def testSaveLyrics(self):
 
362
        """testSaveLyrics - Ensures lyrics for a track are saved in the
 
363
        database"""
 
364
        # Only need a filename that matches something in the database, the rest
 
365
        # of the track is for construction purposes only
 
366
        self.track = Track('/filename/000', '', 0, '', '', '', '', 0, 1, 0, '',
 
367
                           '')
 
368
        self.musiclibrary.save_lyrics(self.track, 'some lyrics here')
 
369
        self.assertEqual(self.track.get_lyrics(), 'some lyrics here')
 
370
 
 
371
 
 
372
class TestAlbum(TestMusic):
 
373
    '''Tests Album'''
 
374
 
 
375
    def setUp(self):
 
376
        """See unittest.TestCase"""
 
377
        TestMusic.setUp(self)
 
378
        self.album = Album('album1', self.cursor)
 
379
 
 
380
    def tearDown(self):
 
381
        """Clean up after the test"""
 
382
        TestMusic.tearDown(self)
 
383
 
 
384
    def testAlbumConstructor(self):
 
385
        """Test that an Album object is properly constructed"""
 
386
        self.assertTrue(isinstance(self.album, Album))
 
387
 
 
388
    def testAlbumConstructorNot(self):
 
389
        """Test that an AlbumHasNoTracks exception is raised when the created
 
390
        album doesn't exist in the cache"""
 
391
        self.assertRaises(AlbumHasNoTracks, Album, 'foo', self.cursor)
 
392
 
 
393
    def testAlbumStr(self):
 
394
        """Test that title is returned in string conversion"""
 
395
        result = str(self.album)
 
396
        self.assertEqual(result, 'album1')
 
397
 
 
398
    def testAlbumGetTitle(self):
 
399
        """Test that the album title is returned"""
 
400
        result = self.album.get_title()
 
401
        self.assertEqual(result, 'album1')
 
402
 
 
403
    def testAlbumHasAlbumArt(self):
 
404
        """Test that album art exists for the file"""
 
405
        album_artist = "artist0 - album1"
 
406
        album_artist = album_artist.encode("base64")
 
407
        album_art = os.path.join(self.art_path, album_artist + ".jpg")
 
408
        file = open(album_art, "wb")
 
409
        file.close()
 
410
        self.assertTrue(self.album.has_album_art())
 
411
        if os.path.exists(album_art):
 
412
            os.remove(album_art)
 
413
 
 
414
    def testAlbumHasAlbumArtNot(self):
 
415
        """Test that missing album art is reported back"""
 
416
        otherAlbum = Album('album1', self.cursor)
 
417
        self.assertFalse(otherAlbum.has_album_art())
 
418
 
 
419
    def testAlbumGetAlbumArtUrl(self):
 
420
        """Test that the path to the album's art is returned"""
 
421
        result = self.album.get_album_art_url()
 
422
        album_artist = "artist0 - album1"
 
423
        album_artist = album_artist.encode("base64")
 
424
        album_art = os.path.join(self.art_path, album_artist + ".jpg")
 
425
        self.assertEqual(result, album_art)
 
426
 
 
427
    def testAlbumGetTracks(self):
 
428
        """Test that all tracks for an album are returned"""
 
429
        result = self.album.get_tracks()
 
430
        self.assertEqual(len(result), 4)
 
431
        for i in result:
 
432
            self.assertTrue(isinstance(i, Track))
 
433
 
 
434
    def testAlbumGetNumberOfTracks(self):
 
435
        """Test correct number of tracks from album is returned"""
 
436
        self.assertEqual(self.album.get_number_of_tracks(), 4)
 
437
 
 
438
    def testAlbumGetYear(self):
 
439
        """Test that year of album is returned"""
 
440
        self.assertEqual(self.album.get_year(), 0)
 
441
 
 
442
    def testAlbumGetGenre(self):
 
443
        """Test that genre of album is returned"""
 
444
        self.assertEqual(self.album.get_genre(), 'genre0')
 
445
 
 
446
    def testAlbumGetArtist(self):
 
447
        """Test that artist of album is returned"""
 
448
        self.assertEqual(self.album.get_artist(), 'artist0')
 
449
 
 
450
    def testAlbumGetTotalLength(self):
 
451
        """Test that total length of album is returned"""
 
452
        self.assertEqual(self.album.get_total_length(), 8)
 
453