2017-11-25 21:21:58 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
|
|
|
#
|
2020-11-08 14:39:09 +00:00
|
|
|
# Copyright (C) 2017-2020 Alban 'spl0k' Féron
|
2017-11-25 21:21:58 +00:00
|
|
|
#
|
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
|
|
|
|
|
|
|
import io
|
|
|
|
import mutagen
|
2020-11-08 14:39:09 +00:00
|
|
|
import os
|
2017-11-25 21:21:58 +00:00
|
|
|
import os.path
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from contextlib import contextmanager
|
2018-08-11 14:16:34 +00:00
|
|
|
from pony.orm import db_session, commit
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
from supysonic import db
|
|
|
|
from supysonic.managers.folder import FolderManager
|
|
|
|
from supysonic.scanner import Scanner
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
|
2017-11-25 21:21:58 +00:00
|
|
|
class ScannerTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2019-06-29 15:25:44 +00:00
|
|
|
db.init_database("sqlite:")
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
with db_session:
|
2019-06-29 15:25:44 +00:00
|
|
|
folder = FolderManager.add("folder", os.path.abspath("tests/assets/folder"))
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertIsNotNone(folder)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2020-01-19 15:45:31 +00:00
|
|
|
self.folderid = folder.id
|
|
|
|
self.__scan()
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2017-12-19 22:16:55 +00:00
|
|
|
db.release_database()
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def __temporary_track_copy(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
track = db.Track.select().first()
|
2020-11-08 14:39:09 +00:00
|
|
|
with tempfile.NamedTemporaryFile(
|
|
|
|
dir=os.path.dirname(track.path), delete=False
|
|
|
|
) as tf:
|
2019-06-29 15:25:44 +00:00
|
|
|
with io.open(track.path, "rb") as f:
|
2017-11-25 21:21:58 +00:00
|
|
|
tf.write(f.read())
|
2020-11-08 14:39:09 +00:00
|
|
|
try:
|
|
|
|
yield tf.name
|
|
|
|
finally:
|
|
|
|
os.remove(tf.name)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
def __scan(self, force=False):
|
2019-07-06 15:04:08 +00:00
|
|
|
self.scanner = Scanner(force=force)
|
2019-06-29 15:25:44 +00:00
|
|
|
self.scanner.queue_folder("folder")
|
2019-05-11 15:13:58 +00:00
|
|
|
self.scanner.run()
|
2020-01-19 15:45:31 +00:00
|
|
|
commit()
|
2019-05-11 15:13:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_scan(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-05-11 15:13:58 +00:00
|
|
|
self.assertRaises(TypeError, self.scanner.queue_folder, None)
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(
|
|
|
|
TypeError, self.scanner.queue_folder, db.Folder[self.folderid]
|
|
|
|
)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_rescan(self):
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan()
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_force_rescan(self):
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan(True)
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_scan_file(self):
|
2019-06-29 15:25:44 +00:00
|
|
|
self.scanner.scan_file("/some/inexistent/path")
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_remove_file(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
track = db.Track.select().first()
|
2017-11-25 21:21:58 +00:00
|
|
|
self.assertRaises(TypeError, self.scanner.remove_file, None)
|
|
|
|
self.assertRaises(TypeError, self.scanner.remove_file, track)
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
self.scanner.remove_file("/some/inexistent/path")
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
self.scanner.remove_file(track.path)
|
2019-05-11 15:13:58 +00:00
|
|
|
self.scanner.prune()
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 0)
|
|
|
|
self.assertEqual(db.Album.select().count(), 0)
|
|
|
|
self.assertEqual(db.Artist.select().count(), 0)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_move_file(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
track = db.Track.select().first()
|
2019-06-29 15:25:44 +00:00
|
|
|
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)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
self.scanner.move_file("/some/inexistent/path", track.path)
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
self.scanner.move_file(track.path, track.path)
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(
|
|
|
|
Exception, self.scanner.move_file, track.path, "/some/inexistent/path"
|
|
|
|
)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
with self.__temporary_track_copy() as tf:
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan()
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 2)
|
2020-11-08 14:39:09 +00:00
|
|
|
self.scanner.move_file(tf, track.path)
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
track = db.Track.select().first()
|
2019-06-29 15:25:44 +00:00
|
|
|
new_path = track.path.replace("silence", "silence_moved")
|
2017-11-25 21:21:58 +00:00
|
|
|
self.scanner.move_file(track.path, new_path)
|
2017-12-17 22:25:34 +00:00
|
|
|
commit()
|
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
self.assertEqual(track.path, new_path)
|
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_rescan_corrupt_file(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
track = db.Track.select().first()
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
with self.__temporary_track_copy() as tf:
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan()
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 2)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2020-11-08 14:39:09 +00:00
|
|
|
with open(tf, "wb") as f:
|
|
|
|
f.seek(0, 0)
|
|
|
|
f.write(b"\x00" * 4096)
|
|
|
|
f.truncate()
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan(True)
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_rescan_removed_file(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
track = db.Track.select().first()
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2020-11-08 14:39:09 +00:00
|
|
|
with self.__temporary_track_copy():
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan()
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 2)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan()
|
2017-12-17 22:25:34 +00:00
|
|
|
self.assertEqual(db.Track.select().count(), 1)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2017-12-17 22:25:34 +00:00
|
|
|
@db_session
|
2017-11-25 21:21:58 +00:00
|
|
|
def test_scan_tag_change(self):
|
2017-12-17 22:25:34 +00:00
|
|
|
folder = db.Folder[self.folderid]
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
with self.__temporary_track_copy() as tf:
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan()
|
2020-11-08 14:39:09 +00:00
|
|
|
copy = db.Track.get(path=tf)
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertEqual(copy.artist.name, "Some artist")
|
|
|
|
self.assertEqual(copy.album.name, "Awesome album")
|
2017-11-25 21:21:58 +00:00
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
tags = mutagen.File(copy.path, easy=True)
|
|
|
|
tags["artist"] = "Renamed artist"
|
|
|
|
tags["album"] = "Crappy album"
|
2017-11-25 21:21:58 +00:00
|
|
|
tags.save()
|
|
|
|
|
2019-05-11 15:13:58 +00:00
|
|
|
self.__scan(True)
|
2019-06-29 15:25:44 +00:00
|
|
|
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"))
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
def test_stats(self):
|
2018-01-20 17:26:58 +00:00
|
|
|
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)
|
2017-11-25 21:21:58 +00:00
|
|
|
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|