def test_import_file(self): # put test file in dropbox shutil.copy(os.path.join(TEST_FILES_DIR, 'testfile.ogg'), self.dropbox) filename = os.path.join(self.dropbox, 'testfile.ogg') self.assertFalse(Artist.objects.exists()) self.assertFalse(Album.objects.exists()) self.assertFalse(Song.objects.exists()) self.assertTrue(os.path.exists(filename)) with open(filename, 'rb') as f: original_content = f.read() # import file update.import_file(filename, self.mutagen_opts) song = Song.objects.get(title='The Song') self.assertEqual(song.title, 'The Song') self.assertEqual(song.album.title, 'The Album') self.assertEqual(song.album.artist.name, 'The Artist') self.assertEqual(song.track, '01') self.assertEqual(song.bitrate, 160000) self.assertEqual(song.filetype, 'ogg') self.assertEqual(song.original_path, 'testfile.ogg') self.assertEqual(song.filefield.read(), original_content) self.assertFalse(os.path.exists(filename)) self.assertNoLogError()
def test_importing_unsupported_format_gives_an_error(self): # put file in dropbox and import it shutil.copy(os.path.join(TEST_FILES_DIR, 'testfile.wav'), self.dropbox) filename = os.path.join(self.dropbox, 'testfile.wav') update.import_file(filename, self.mutagen_opts) with open(self.logfile.name, 'r') as f: log_record = f.read() self.assertIn('Problem importing file', log_record) self.assertIn('testfile.wav (Mutagen could not read', log_record) self.assertTrue(os.path.exists(filename))
def test_import_existing_file_with_no_better_bitrate_skips_it(self): # put test files in dropbox shutil.copy(os.path.join(TEST_FILES_DIR, 'testfile.ogg'), self.dropbox) shutil.copy(os.path.join(TEST_FILES_DIR, 'testfile.ogg'), os.path.join(self.dropbox, 'testfile_copy.ogg')) # import files filename = os.path.join(self.dropbox, 'testfile.ogg') update.import_file(filename, self.mutagen_opts) filename = os.path.join(self.dropbox, 'testfile_copy.ogg') update.import_file(filename, self.mutagen_opts) with open(self.logfile.name, 'r') as f: log_record = f.read() self.assertIn('Problem importing file', log_record) self.assertIn('The Song by The Artist already exists', log_record) self.assertTrue(os.path.exists(filename))
def test_import_existing_file_with_better_bitrate_replaces_it(self): # put test files in dropbox shutil.copy(os.path.join(TEST_FILES_DIR, 'testfile-128k.mp3'), self.dropbox) shutil.copy(os.path.join(TEST_FILES_DIR, 'testfile.mp3'), self.dropbox) # import files filename = os.path.join(self.dropbox, 'testfile-128k.mp3') update.import_file(filename, self.mutagen_opts) song = Song.objects.get(title='The Song') self.assertEqual(song.bitrate, 128000) filename = os.path.join(self.dropbox, 'testfile.mp3') update.import_file(filename, self.mutagen_opts) self.assertEqual(Song.objects.count(), 1) song = Song.objects.get(title='The Song') self.assertEqual(song.bitrate, 320000) self.assertNoLogError()