コード例 #1
0
class ScannerTestCase(unittest.TestCase):
    def setUp(self):
        self.store = db.get_store('sqlite:')
        with io.open('schema/sqlite.sql', 'r') as f:
            for statement in f.read().split(';'):
                self.store.execute(statement)

        FolderManager.add(self.store, 'folder',
                          os.path.abspath('tests/assets'))
        self.folder = self.store.find(db.Folder).one()
        self.assertIsNotNone(self.folder)

        self.scanner = Scanner(self.store)
        self.scanner.scan(self.folder)

    def tearDown(self):
        self.scanner.finish()
        self.store.close()

    @contextmanager
    def __temporary_track_copy(self):
        track = self.store.find(db.Track).one()
        with tempfile.NamedTemporaryFile(
                dir=os.path.dirname(track.path)) as tf:
            with io.open(track.path, 'rb') as f:
                tf.write(f.read())
            yield tf

    def test_scan(self):
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.assertRaises(TypeError, self.scanner.scan, None)
        self.assertRaises(TypeError, self.scanner.scan, 'string')

    def test_progress(self):
        def progress(processed, total):
            self.assertIsInstance(processed, int)
            self.assertIsInstance(total, int)
            self.assertLessEqual(processed, total)

        self.scanner.scan(self.folder, progress)

    def test_rescan(self):
        self.scanner.scan(self.folder)
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_force_rescan(self):
        self.scanner = Scanner(self.store, True)
        self.scanner.scan(self.folder)
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_scan_file(self):
        track = self.store.find(db.Track).one()
        self.assertRaises(TypeError, self.scanner.scan_file, None)
        self.assertRaises(TypeError, self.scanner.scan_file, track)

        self.scanner.scan_file('/some/inexistent/path')
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_remove_file(self):
        track = self.store.find(db.Track).one()
        self.assertRaises(TypeError, self.scanner.remove_file, None)
        self.assertRaises(TypeError, self.scanner.remove_file, track)

        self.scanner.remove_file('/some/inexistent/path')
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.scanner.remove_file(track.path)
        self.scanner.finish()
        self.assertEqual(self.store.find(db.Track).count(), 0)
        self.assertEqual(self.store.find(db.Album).count(), 0)
        self.assertEqual(self.store.find(db.Artist).count(), 0)

    def test_move_file(self):
        track = self.store.find(db.Track).one()
        self.assertRaises(TypeError, self.scanner.move_file, None, 'string')
        self.assertRaises(TypeError, self.scanner.move_file, track, 'string')
        self.assertRaises(TypeError, self.scanner.move_file, 'string', None)
        self.assertRaises(TypeError, self.scanner.move_file, 'string', track)

        self.scanner.move_file('/some/inexistent/path', track.path)
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.scanner.move_file(track.path, track.path)
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.assertRaises(Exception, self.scanner.move_file, track.path,
                          '/some/inexistent/path')

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 2)
            self.scanner.move_file(tf.name, track.path)
            self.assertEqual(self.store.find(db.Track).count(), 1)

        track = self.store.find(db.Track).one()
        new_path = os.path.abspath(
            os.path.join(os.path.dirname(track.path), '..', 'silence.mp3'))
        self.scanner.move_file(track.path, new_path)
        self.assertEqual(self.store.find(db.Track).count(), 1)
        self.assertEqual(track.path, new_path)

    def test_rescan_corrupt_file(self):
        track = self.store.find(db.Track).one()
        self.scanner = Scanner(self.store, True)

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 2)

            tf.seek(0, 0)
            tf.write('\x00' * 4096)
            tf.truncate()

            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_rescan_removed_file(self):
        track = self.store.find(db.Track).one()

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 2)

        self.scanner.scan(self.folder)
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_scan_tag_change(self):
        self.scanner = Scanner(self.store, True)

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            copy = self.store.find(db.Track, db.Track.path == tf.name).one()
            self.assertEqual(copy.artist.name, 'Some artist')
            self.assertEqual(copy.album.name, 'Awesome album')

            tags = mutagen.File(copy.path, easy=True)
            tags['artist'] = 'Renamed artist'
            tags['album'] = 'Crappy album'
            tags.save()

            self.scanner.scan(self.folder)
            self.scanner.finish()
            self.assertEqual(copy.artist.name, 'Renamed artist')
            self.assertEqual(copy.album.name, 'Crappy album')
            self.assertIsNotNone(
                self.store.find(db.Artist,
                                db.Artist.name == 'Some artist').one())
            self.assertIsNotNone(
                self.store.find(db.Album,
                                db.Album.name == 'Awesome album').one())

    def test_stats(self):
        self.assertEqual(self.scanner.stats(), ((1, 1, 1), (0, 0, 0)))
コード例 #2
0
class ScannerTestCase(unittest.TestCase):
    def setUp(self):
        db.init_database("sqlite:")

        with db_session:
            folder = FolderManager.add("folder",
                                       os.path.abspath("tests/assets/folder"))
            self.assertIsNotNone(folder)

        self.folderid = folder.id
        self.__scan()

    def tearDown(self):
        db.release_database()

    @contextmanager
    def __temporary_track_copy(self):
        track = db.Track.select().first()
        with tempfile.NamedTemporaryFile(
                dir=os.path.dirname(track.path)) as tf:
            with io.open(track.path, "rb") as f:
                tf.write(f.read())
            yield tf

    def __scan(self, force=False):
        self.scanner = Scanner(force=force)
        self.scanner.queue_folder("folder")
        self.scanner.run()
        commit()

    @db_session
    def test_scan(self):
        self.assertEqual(db.Track.select().count(), 1)

        self.assertRaises(TypeError, self.scanner.queue_folder, None)
        self.assertRaises(TypeError, self.scanner.queue_folder,
                          db.Folder[self.folderid])

    @db_session
    def test_rescan(self):
        self.__scan()
        self.assertEqual(db.Track.select().count(), 1)

    @db_session
    def test_force_rescan(self):
        self.__scan(True)
        self.assertEqual(db.Track.select().count(), 1)

    @db_session
    def test_scan_file(self):
        self.scanner.scan_file("/some/inexistent/path")
        commit()
        self.assertEqual(db.Track.select().count(), 1)

    @db_session
    def test_remove_file(self):
        track = db.Track.select().first()
        self.assertRaises(TypeError, self.scanner.remove_file, None)
        self.assertRaises(TypeError, self.scanner.remove_file, track)

        self.scanner.remove_file("/some/inexistent/path")
        commit()
        self.assertEqual(db.Track.select().count(), 1)

        self.scanner.remove_file(track.path)
        self.scanner.prune()
        commit()
        self.assertEqual(db.Track.select().count(), 0)
        self.assertEqual(db.Album.select().count(), 0)
        self.assertEqual(db.Artist.select().count(), 0)

    @db_session
    def test_move_file(self):
        track = db.Track.select().first()
        self.assertRaises(TypeError, self.scanner.move_file, None, "string")
        self.assertRaises(TypeError, self.scanner.move_file, track, "string")
        self.assertRaises(TypeError, self.scanner.move_file, "string", None)
        self.assertRaises(TypeError, self.scanner.move_file, "string", track)

        self.scanner.move_file("/some/inexistent/path", track.path)
        commit()
        self.assertEqual(db.Track.select().count(), 1)

        self.scanner.move_file(track.path, track.path)
        commit()
        self.assertEqual(db.Track.select().count(), 1)

        self.assertRaises(Exception, self.scanner.move_file, track.path,
                          "/some/inexistent/path")

        with self.__temporary_track_copy() as tf:
            self.__scan()
            self.assertEqual(db.Track.select().count(), 2)
            self.scanner.move_file(tf.name, track.path)
            commit()
            self.assertEqual(db.Track.select().count(), 1)

        track = db.Track.select().first()
        new_path = track.path.replace("silence", "silence_moved")
        self.scanner.move_file(track.path, new_path)
        commit()
        self.assertEqual(db.Track.select().count(), 1)
        self.assertEqual(track.path, new_path)

    @db_session
    def test_rescan_corrupt_file(self):
        track = db.Track.select().first()

        with self.__temporary_track_copy() as tf:
            self.__scan()
            self.assertEqual(db.Track.select().count(), 2)

            tf.seek(0, 0)
            tf.write(b"\x00" * 4096)
            tf.truncate()

            self.__scan(True)
            self.assertEqual(db.Track.select().count(), 1)

    @db_session
    def test_rescan_removed_file(self):
        track = db.Track.select().first()

        with self.__temporary_track_copy() as tf:
            self.__scan()
            self.assertEqual(db.Track.select().count(), 2)

        self.__scan()
        self.assertEqual(db.Track.select().count(), 1)

    @db_session
    def test_scan_tag_change(self):
        folder = db.Folder[self.folderid]

        with self.__temporary_track_copy() as tf:
            self.__scan()
            copy = db.Track.get(path=tf.name)
            self.assertEqual(copy.artist.name, "Some artist")
            self.assertEqual(copy.album.name, "Awesome album")

            tags = mutagen.File(copy.path, easy=True)
            tags["artist"] = "Renamed artist"
            tags["album"] = "Crappy album"
            tags.save()

            self.__scan(True)
            self.assertEqual(copy.artist.name, "Renamed artist")
            self.assertEqual(copy.album.name, "Crappy album")
            self.assertIsNotNone(db.Artist.get(name="Some artist"))
            self.assertIsNotNone(db.Album.get(name="Awesome album"))

    def test_stats(self):
        stats = self.scanner.stats()
        self.assertEqual(stats.added.artists, 1)
        self.assertEqual(stats.added.albums, 1)
        self.assertEqual(stats.added.tracks, 1)
        self.assertEqual(stats.deleted.artists, 0)
        self.assertEqual(stats.deleted.albums, 0)
        self.assertEqual(stats.deleted.tracks, 0)
コード例 #3
0
ファイル: test_scanner.py プロジェクト: andrenam/supysonic
class ScannerTestCase(unittest.TestCase):
    def setUp(self):
        self.store = db.get_store('sqlite:')
        with io.open('schema/sqlite.sql', 'r') as f:
            for statement in f.read().split(';'):
                self.store.execute(statement)

        FolderManager.add(self.store, 'folder', os.path.abspath('tests/assets'))
        self.folder = self.store.find(db.Folder).one()
        self.assertIsNotNone(self.folder)

        self.scanner = Scanner(self.store)
        self.scanner.scan(self.folder)

    def tearDown(self):
        self.scanner.finish()
        self.store.close()

    @contextmanager
    def __temporary_track_copy(self):
        track = self.store.find(db.Track).one()
        with tempfile.NamedTemporaryFile(dir = os.path.dirname(track.path)) as tf:
            with io.open(track.path, 'rb') as f:
                tf.write(f.read())
            yield tf

    def test_scan(self):
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.assertRaises(TypeError, self.scanner.scan, None)
        self.assertRaises(TypeError, self.scanner.scan, 'string')

    def test_progress(self):
        def progress(processed, total):
            self.assertIsInstance(processed, int)
            self.assertIsInstance(total, int)
            self.assertLessEqual(processed, total)

        self.scanner.scan(self.folder, progress)

    def test_rescan(self):
        self.scanner.scan(self.folder)
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_force_rescan(self):
        self.scanner = Scanner(self.store, True)
        self.scanner.scan(self.folder)
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_scan_file(self):
        track = self.store.find(db.Track).one()
        self.assertRaises(TypeError, self.scanner.scan_file, None)
        self.assertRaises(TypeError, self.scanner.scan_file, track)

        self.scanner.scan_file('/some/inexistent/path')
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_remove_file(self):
        track = self.store.find(db.Track).one()
        self.assertRaises(TypeError, self.scanner.remove_file, None)
        self.assertRaises(TypeError, self.scanner.remove_file, track)

        self.scanner.remove_file('/some/inexistent/path')
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.scanner.remove_file(track.path)
        self.scanner.finish()
        self.assertEqual(self.store.find(db.Track).count(), 0)
        self.assertEqual(self.store.find(db.Album).count(), 0)
        self.assertEqual(self.store.find(db.Artist).count(), 0)

    def test_move_file(self):
        track = self.store.find(db.Track).one()
        self.assertRaises(TypeError, self.scanner.move_file, None, 'string')
        self.assertRaises(TypeError, self.scanner.move_file, track, 'string')
        self.assertRaises(TypeError, self.scanner.move_file, 'string', None)
        self.assertRaises(TypeError, self.scanner.move_file, 'string', track)

        self.scanner.move_file('/some/inexistent/path', track.path)
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.scanner.move_file(track.path, track.path)
        self.assertEqual(self.store.find(db.Track).count(), 1)

        self.assertRaises(Exception, self.scanner.move_file, track.path, '/some/inexistent/path')

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 2)
            self.scanner.move_file(tf.name, track.path)
            self.assertEqual(self.store.find(db.Track).count(), 1)

        track = self.store.find(db.Track).one()
        new_path = os.path.abspath(os.path.join(os.path.dirname(track.path), '..', 'silence.mp3'))
        self.scanner.move_file(track.path, new_path)
        self.assertEqual(self.store.find(db.Track).count(), 1)
        self.assertEqual(track.path, new_path)

    def test_rescan_corrupt_file(self):
        track = self.store.find(db.Track).one()
        self.scanner = Scanner(self.store, True)

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 2)

            tf.seek(0, 0)
            tf.write('\x00' * 4096)
            tf.truncate()

            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_rescan_removed_file(self):
        track = self.store.find(db.Track).one()

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            self.assertEqual(self.store.find(db.Track).count(), 2)

        self.scanner.scan(self.folder)
        self.assertEqual(self.store.find(db.Track).count(), 1)

    def test_scan_tag_change(self):
        self.scanner = Scanner(self.store, True)

        with self.__temporary_track_copy() as tf:
            self.scanner.scan(self.folder)
            copy = self.store.find(db.Track, db.Track.path == tf.name).one()
            self.assertEqual(copy.artist.name, 'Some artist')
            self.assertEqual(copy.album.name, 'Awesome album')

            tags = mutagen.File(copy.path, easy = True)
            tags['artist'] = 'Renamed artist'
            tags['album'] = 'Crappy album'
            tags.save()

            self.scanner.scan(self.folder)
            self.scanner.finish()
            self.assertEqual(copy.artist.name, 'Renamed artist')
            self.assertEqual(copy.album.name, 'Crappy album')
            self.assertIsNotNone(self.store.find(db.Artist, db.Artist.name == 'Some artist').one())
            self.assertIsNotNone(self.store.find(db.Album, db.Album.name == 'Awesome album').one())

    def test_stats(self):
        self.assertEqual(self.scanner.stats(), ((1,1,1),(0,0,0)))