77
84
connection.commit()
88
class TestTrack(TestMusic):
92
self.track = Track('/path/to/track.mp3', # filename
106
TestMusic.tearDown(self)
108
def testTrackConstructor(self):
109
"""testTrackConstructor - Ensures that a Track object is created"""
110
self.assertTrue(isinstance(self.track, Track))
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']
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])
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',
127
self.assertTrue(isinstance(self.track, Track))
128
self.assertEqual(self.track.get_rating(), i)
130
def testTrackRatingOutOfRange(self):
131
"""testTrackRatingOutOfRange - Ensures that the rating raises an
132
exception for something out of range in construction"""
134
self.assertRaises(TrackRatingOutOfRange, Track, 'a', 'b', 1, 'c',
135
'd', 'e', 'f', 2, i, 3, 'g', 'h')
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')
142
def testTrackGetTitle(self):
143
"""testTrackGetTitle - Ensures that the title is returned"""
144
result = self.track.get_title()
145
self.assertEqual(result, 'title')
147
def testTrackGetTrackNumber(self):
148
"""testTrackGetTrackNumber - Ensures that the tracknumber is
150
result = self.track.get_tracknumber()
151
self.assertEqual(result, 1)
153
def testTrackGetArtist(self):
154
"""testTrackGetArtist - Ensures that the artist is returned"""
155
result = self.track.get_artist()
156
self.assertEqual(result, 'artist0')
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')
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',
171
self.assertRaises(AlbumHasNoTracks, self.badTrack.get_album,
174
def testTrackGetAlbumArtUrlExists(self):
175
"""testTrackGetAlbumArtUrl - Ensures that the album art url is
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")
182
result = self.track.get_album_art_url(self.cursor)
183
self.assertEqual(result, album_art)
184
if os.path.exists(album_art):
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)
193
def testTrackGetGenre(self):
194
"""testTrackGetGenre - Ensures that the genre is returned"""
195
result = self.track.get_genre()
196
self.assertEqual(result, 'genre')
198
def testTrackGetBitrate(self):
199
"""testTrackGetBitrate - Ensures that the bitrate is returned"""
200
result = self.track.get_bitrate()
201
self.assertEqual(result, '128')
203
def testTrackGetYear(self):
204
"""testTrackGetYear - Ensures that the year is returned"""
205
result = self.track.get_year()
206
self.assertEqual(result, 2008)
208
def testTrackGetRating(self):
209
"""testTrackGetRating - Ensures that the rating is returned"""
210
result = self.track.get_rating()
211
self.assertEqual(result, 5)
213
def testTrackGetLength(self):
214
"""testTrackGetLength - Ensures that the length is returned"""
215
result = self.track.get_length()
216
self.assertEqual(result, 240)
218
def testTrackGetComment(self):
219
"""testTrackGetComment - Ensures that the comment is returned"""
220
result = self.track.get_comment()
221
self.assertEqual(result, 'comment')
223
def testTrackGetLyrics(self):
224
"""testTrackGetLyrics - Ensures that the lyrics are returned"""
225
result = self.track.get_lyrics()
226
self.assertEqual(result, 'lyrics')
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')
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(), '')
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)
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')
250
class TestMusicLibrary(TestMusic):
251
'''Tests MusicLibrary'''
254
TestMusic.setUp(self)
256
self.backend_connection = MockBackendConnection()
258
print "\nbackend_connection failed"
259
self.musiclibrary = MusicLibrary(self.backend_connection)
262
self.backend_connection.close_connection()
263
TestMusic.tearDown(self)
265
def testMusicLibraryConstructor(self):
266
"""testMusicLibraryContructor - Ensures instantiation of MusicLibrary
268
self.assertTrue(isinstance(self.musiclibrary, MusicLibrary))
270
def testGetAllArtists(self):
271
"""testGetAllArtists - Ensures that all artists are returned from
273
result = self.musiclibrary.get_all_artists()
274
self.assertEqual(result, ['artist0'])
276
def testGetAllTracks(self):
277
"""testGetAllTracks - Ensures that all tracks are returned"""
278
result = self.musiclibrary.get_all_tracks()
280
self.assertTrue(isinstance(i, Track))
281
self.assertEqual(len(result), 8)
283
def testGetTracksByGenre(self):
284
"""testGetTracksByGenre - Ensures tracks of a certain genre are
286
result = self.musiclibrary.get_tracks_by_genre('genre0')
287
self.assertEqual(len(result), 4)
289
self.assertEqual(i.get_genre(), 'genre0')
291
def testGetTracksByMissingGenre(self):
292
"""testGetTracksByMissingGenre - Ensures proper handling of a missing
295
len(self.musiclibrary.get_tracks_by_genre('foo-bar-baz**')), 0)
297
def testGetTracksByArtist(self):
298
"""testGetTracksByArtist - Ensure tracks by a certain artist are
300
result = self.musiclibrary.get_tracks_by_artist('artist0')
301
self.assertEqual(len(result), 8)
303
self.assertEqual(i.get_artist(), 'artist0')
305
def testGetTracksByUnknownArtist(self):
306
"""testGetTracksByUnknownArtist - Ensure proper handling of an missing
307
artist in the cache"""
309
len(self.musiclibrary.get_tracks_by_artist('foo')), 0)
311
def testGetAllAlbums(self):
312
"""testGetAllAlbums - Ensures all albums are returned"""
313
result = self.musiclibrary.get_all_albums()
315
self.assertTrue(isinstance(i, Album))
316
self.assertEqual(len(result), 2)
318
def testGetAlbumsByArtist(self):
319
"""testGetAlbumsByArtist - Ensures correct albums by an artist is
321
result = self.musiclibrary.get_albums_by_artist('artist0')
322
self.assertEqual(len(result), 2)
324
self.assertEqual(i.get_artist(), 'artist0')
326
def testGetAlbumsByUnknownArtist(self):
327
"""testGetAlbumsByUnknownArtist - Ensures proper handling of an
328
artist not in the cache"""
330
len(self.musiclibrary.get_albums_by_artist('foo')), 0)
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)
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)
343
def testNumberOfTracksByUnknownArtist(self):
344
"""testNumberOfTracksByUnknownArtist - Ensures proper handling when
345
artist called is not in the cache"""
347
self.musiclibrary.number_of_albums_by_artist('foo'), 0)
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)
355
def testNumberOfAlbumsByUnknownArtist(self):
356
"""testNumberOfAlbumsByUnknownArtist - Ensures proper handling when
357
artist called is not in the cache"""
359
self.musiclibrary.number_of_albums_by_artist('foo'), 0)
361
def testSaveLyrics(self):
362
"""testSaveLyrics - Ensures lyrics for a track are saved in the
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, '',
368
self.musiclibrary.save_lyrics(self.track, 'some lyrics here')
369
self.assertEqual(self.track.get_lyrics(), 'some lyrics here')
372
class TestAlbum(TestMusic):
376
"""See unittest.TestCase"""
377
TestMusic.setUp(self)
378
self.album = Album('album1', self.cursor)
381
"""Clean up after the test"""
382
TestMusic.tearDown(self)
384
def testAlbumConstructor(self):
385
"""Test that an Album object is properly constructed"""
386
self.assertTrue(isinstance(self.album, Album))
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)
393
def testAlbumStr(self):
394
"""Test that title is returned in string conversion"""
395
result = str(self.album)
396
self.assertEqual(result, 'album1')
398
def testAlbumGetTitle(self):
399
"""Test that the album title is returned"""
400
result = self.album.get_title()
401
self.assertEqual(result, 'album1')
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")
410
self.assertTrue(self.album.has_album_art())
411
if os.path.exists(album_art):
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())
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)
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)
432
self.assertTrue(isinstance(i, Track))
434
def testAlbumGetNumberOfTracks(self):
435
"""Test correct number of tracks from album is returned"""
436
self.assertEqual(self.album.get_number_of_tracks(), 4)
438
def testAlbumGetYear(self):
439
"""Test that year of album is returned"""
440
self.assertEqual(self.album.get_year(), 0)
442
def testAlbumGetGenre(self):
443
"""Test that genre of album is returned"""
444
self.assertEqual(self.album.get_genre(), 'genre0')
446
def testAlbumGetArtist(self):
447
"""Test that artist of album is returned"""
448
self.assertEqual(self.album.get_artist(), 'artist0')
450
def testAlbumGetTotalLength(self):
451
"""Test that total length of album is returned"""
452
self.assertEqual(self.album.get_total_length(), 8)