1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-09 19:52:16 +00:00
supysonic/tests/managers/test_manager_folder.py

154 lines
5.1 KiB
Python
Raw Normal View History

# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
2018-03-04 20:49:56 +00:00
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
# 2017 Óscar García Amor
#
# Distributed under terms of the GNU AGPLv3 license.
from supysonic import db
from supysonic.managers.folder import FolderManager
import os
import shutil
import tempfile
import unittest
from pony.orm import db_session, ObjectNotFound
2017-12-16 16:42:53 +00:00
2019-06-29 15:25:44 +00:00
class FolderManagerTestCase(unittest.TestCase):
def setUp(self):
# Create an empty sqlite database in memory
2019-06-29 15:25:44 +00:00
db.init_database("sqlite:")
2017-10-28 13:54:41 +00:00
# Create some temporary directories
self.media_dir = tempfile.mkdtemp()
self.music_dir = tempfile.mkdtemp()
2017-12-16 16:42:53 +00:00
2017-12-19 22:16:55 +00:00
def tearDown(self):
db.release_database()
shutil.rmtree(self.media_dir)
shutil.rmtree(self.music_dir)
2017-12-16 16:42:53 +00:00
def create_folders(self):
# Add test folders
2019-06-29 15:25:44 +00:00
self.assertIsNotNone(FolderManager.add("media", self.media_dir))
self.assertIsNotNone(FolderManager.add("music", self.music_dir))
2017-12-16 16:42:53 +00:00
db.Folder(
2019-06-29 15:25:44 +00:00
root=False, name="non-root", path=os.path.join(self.music_dir, "subfolder")
2017-12-16 16:42:53 +00:00
)
2019-06-29 15:25:44 +00:00
artist = db.Artist(name="Artist")
album = db.Album(name="Album", artist=artist)
2017-12-16 16:42:53 +00:00
2019-06-29 15:25:44 +00:00
root = db.Folder.get(name="media")
db.Track(
2019-06-29 15:25:44 +00:00
title="Track",
artist=artist,
album=album,
disc=1,
number=1,
path=os.path.join(self.media_dir, "somefile"),
folder=root,
root_folder=root,
duration=2,
bitrate=320,
last_modification=0,
2017-12-16 16:42:53 +00:00
)
2017-12-16 16:42:53 +00:00
@db_session
def test_get_folder(self):
2017-12-16 16:42:53 +00:00
self.create_folders()
# Get existing folders
2019-06-29 15:25:44 +00:00
for name in ["media", "music"]:
folder = db.Folder.get(name=name, root=True)
self.assertEqual(FolderManager.get(folder.id), folder)
2017-10-28 13:54:41 +00:00
# Get with invalid UUID
2019-06-29 15:25:44 +00:00
self.assertRaises(ValueError, FolderManager.get, "invalid-uuid")
self.assertRaises(ValueError, FolderManager.get, 0xDEADBEEF)
2017-10-28 13:54:41 +00:00
# Non-existent folder
2019-10-01 14:55:23 +00:00
self.assertRaises(ObjectNotFound, FolderManager.get, 1234567890)
2017-12-16 16:42:53 +00:00
@db_session
def test_add_folder(self):
2017-12-16 16:42:53 +00:00
self.create_folders()
self.assertEqual(db.Folder.select().count(), 3)
2017-10-28 13:54:41 +00:00
# Create duplicate
2019-06-29 15:25:44 +00:00
self.assertRaises(ValueError, FolderManager.add, "media", self.media_dir)
self.assertEqual(db.Folder.select(lambda f: f.name == "media").count(), 1)
2017-10-28 13:54:41 +00:00
# Duplicate path
2019-06-29 15:25:44 +00:00
self.assertRaises(ValueError, FolderManager.add, "new-folder", self.media_dir)
self.assertEqual(
db.Folder.select(lambda f: f.path == self.media_dir).count(), 1
)
2017-10-28 13:54:41 +00:00
# Invalid path
2019-06-29 15:25:44 +00:00
path = os.path.abspath("/this/not/is/valid")
self.assertRaises(ValueError, FolderManager.add, "invalid-path", path)
self.assertFalse(db.Folder.exists(path=path))
2017-10-28 13:54:41 +00:00
# Subfolder of already added path
2019-06-29 15:25:44 +00:00
path = os.path.join(self.media_dir, "subfolder")
2017-10-28 13:54:41 +00:00
os.mkdir(path)
2019-06-29 15:25:44 +00:00
self.assertRaises(ValueError, FolderManager.add, "subfolder", path)
2017-12-16 16:42:53 +00:00
self.assertEqual(db.Folder.select().count(), 3)
2017-10-28 13:54:41 +00:00
# Parent folder of an already added path
2019-06-29 15:25:44 +00:00
path = os.path.join(self.media_dir, "..")
self.assertRaises(ValueError, FolderManager.add, "parent", path)
2017-12-16 16:42:53 +00:00
self.assertEqual(db.Folder.select().count(), 3)
def test_delete_folder(self):
2018-03-15 19:50:01 +00:00
with db_session:
self.create_folders()
2017-12-16 16:42:53 +00:00
2018-03-15 19:50:01 +00:00
with db_session:
2019-10-01 14:55:23 +00:00
# Delete invalid Folder ID
2019-06-29 15:25:44 +00:00
self.assertRaises(ValueError, FolderManager.delete, "invalid-uuid")
2018-03-15 19:50:01 +00:00
self.assertEqual(db.Folder.select().count(), 3)
2017-10-28 13:54:41 +00:00
2018-03-15 19:50:01 +00:00
# Delete non-existent folder
2019-10-01 14:55:23 +00:00
self.assertRaises(ObjectNotFound, FolderManager.delete, 1234567890)
2018-03-15 19:50:01 +00:00
self.assertEqual(db.Folder.select().count(), 3)
2017-10-28 13:54:41 +00:00
2018-03-15 19:50:01 +00:00
# Delete non-root folder
2019-06-29 15:25:44 +00:00
folder = db.Folder.get(name="non-root")
2018-03-15 19:50:01 +00:00
self.assertRaises(ObjectNotFound, FolderManager.delete, folder.id)
self.assertEqual(db.Folder.select().count(), 3)
2017-10-28 13:54:41 +00:00
2018-03-15 19:50:01 +00:00
with db_session:
# Delete existing folders
2019-06-29 15:25:44 +00:00
for name in ["media", "music"]:
folder = db.Folder.get(name=name, root=True)
2018-03-15 19:50:01 +00:00
FolderManager.delete(folder.id)
self.assertRaises(ObjectNotFound, db.Folder.__getitem__, folder.id)
2018-03-15 19:50:01 +00:00
# Even if we have only 2 root folders, non-root should never exist and be cleaned anyway
self.assertEqual(db.Folder.select().count(), 0)
2017-12-16 16:42:53 +00:00
2018-03-15 19:50:01 +00:00
def test_delete_by_name(self):
with db_session:
self.create_folders()
with db_session:
# Delete non-existent folder
2019-06-29 15:25:44 +00:00
self.assertRaises(ObjectNotFound, FolderManager.delete_by_name, "null")
2018-03-15 19:50:01 +00:00
self.assertEqual(db.Folder.select().count(), 3)
with db_session:
# Delete existing folders
2019-06-29 15:25:44 +00:00
for name in ["media", "music"]:
2018-03-15 19:50:01 +00:00
FolderManager.delete_by_name(name)
2019-06-29 15:25:44 +00:00
self.assertFalse(db.Folder.exists(name=name))
2017-10-28 13:54:41 +00:00
2019-06-29 15:25:44 +00:00
if __name__ == "__main__":
unittest.main()