2017-08-07 11:10:06 +00:00
|
|
|
# This file is part of Supysonic.
|
|
|
|
# Supysonic is a Python implementation of the Subsonic server API.
|
2017-07-14 10:14:10 +00:00
|
|
|
#
|
2023-01-14 15:47:57 +00:00
|
|
|
# Copyright (C) 2017-2023 Alban 'spl0k' Féron
|
2017-08-07 11:10:06 +00:00
|
|
|
# 2017 Óscar García Amor
|
|
|
|
#
|
|
|
|
# Distributed under terms of the GNU AGPLv3 license.
|
2017-07-14 10:14:10 +00:00
|
|
|
|
2023-01-14 15:47:57 +00:00
|
|
|
from supysonic.db import (
|
|
|
|
Folder,
|
|
|
|
Album,
|
|
|
|
Artist,
|
|
|
|
RatingFolder,
|
|
|
|
RatingTrack,
|
|
|
|
StarredAlbum,
|
|
|
|
StarredArtist,
|
|
|
|
StarredFolder,
|
|
|
|
StarredTrack,
|
|
|
|
Track,
|
|
|
|
User,
|
|
|
|
init_database,
|
|
|
|
release_database,
|
|
|
|
)
|
2017-07-14 10:14:10 +00:00
|
|
|
from supysonic.managers.folder import FolderManager
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
|
2017-07-14 10:14:10 +00:00
|
|
|
class FolderManagerTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Create an empty sqlite database in memory
|
2022-12-10 15:55:02 +00:00
|
|
|
init_database("sqlite:")
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-14 10:14:10 +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):
|
2022-12-10 15:55:02 +00:00
|
|
|
release_database()
|
2017-12-19 22:16:55 +00:00
|
|
|
shutil.rmtree(self.media_dir)
|
|
|
|
shutil.rmtree(self.music_dir)
|
|
|
|
|
2017-12-16 16:42:53 +00:00
|
|
|
def create_folders(self):
|
2017-07-14 10:14:10 +00:00
|
|
|
# Add test folders
|
2023-01-14 15:47:57 +00:00
|
|
|
media = FolderManager.add("media", self.media_dir)
|
|
|
|
music = FolderManager.add("music", self.music_dir)
|
|
|
|
self.assertIsNotNone(media)
|
|
|
|
self.assertIsNotNone(music)
|
2017-12-16 16:42:53 +00:00
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
Folder.create(
|
2023-01-14 15:47:57 +00:00
|
|
|
root=False,
|
|
|
|
parent=music,
|
|
|
|
name="non-root",
|
|
|
|
path=os.path.join(self.music_dir, "subfolder"),
|
2017-12-16 16:42:53 +00:00
|
|
|
)
|
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
artist = Artist.create(name="Artist")
|
|
|
|
album = Album.create(name="Album", artist=artist)
|
2017-12-16 16:42:53 +00:00
|
|
|
|
2023-01-14 15:47:57 +00:00
|
|
|
Track.create(
|
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"),
|
2023-01-14 15:47:57 +00:00
|
|
|
folder=media,
|
|
|
|
root_folder=media,
|
2019-06-29 15:25:44 +00:00
|
|
|
duration=2,
|
|
|
|
bitrate=320,
|
|
|
|
last_modification=0,
|
2017-12-16 16:42:53 +00:00
|
|
|
)
|
2017-07-14 10:14:10 +00:00
|
|
|
|
2023-01-14 15:47:57 +00:00
|
|
|
def create_annotations(self):
|
|
|
|
track = Track.select().first()
|
|
|
|
user = User.create(name="user", password="secret", salt="ABC+", last_play=track)
|
|
|
|
folder = Folder.get(name="media")
|
|
|
|
|
|
|
|
RatingFolder.create(user=user, rated=folder, rating=3)
|
|
|
|
RatingTrack.create(user=user, rated=track, rating=3)
|
|
|
|
|
|
|
|
StarredFolder.create(user=user, starred=folder)
|
|
|
|
StarredArtist.create(user=user, starred=track.artist_id)
|
|
|
|
StarredAlbum.create(user=user, starred=track.album_id)
|
|
|
|
StarredTrack.create(user=user, starred=track)
|
|
|
|
|
2017-07-14 10:14:10 +00:00
|
|
|
def test_get_folder(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_folders()
|
|
|
|
|
2017-07-14 10:14:10 +00:00
|
|
|
# Get existing folders
|
2019-06-29 15:25:44 +00:00
|
|
|
for name in ["media", "music"]:
|
2022-12-10 15:55:02 +00:00
|
|
|
folder = Folder.get(name=name, root=True)
|
2018-03-04 16:36:35 +00:00
|
|
|
self.assertEqual(FolderManager.get(folder.id), folder)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Get with invalid id
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(ValueError, FolderManager.get, "invalid-uuid")
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-14 10:14:10 +00:00
|
|
|
# Non-existent folder
|
2022-12-10 15:55:02 +00:00
|
|
|
self.assertRaises(Folder.DoesNotExist, FolderManager.get, 1234567890)
|
2017-07-14 10:14:10 +00:00
|
|
|
|
|
|
|
def test_add_folder(self):
|
2017-12-16 16:42:53 +00:00
|
|
|
self.create_folders()
|
2022-12-10 15:55:02 +00:00
|
|
|
self.assertEqual(Folder.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-14 10:14:10 +00:00
|
|
|
# Create duplicate
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(ValueError, FolderManager.add, "media", self.media_dir)
|
2022-12-10 15:55:02 +00:00
|
|
|
self.assertEqual(Folder.select().where(Folder.name == "media").count(), 1)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-14 10:14:10 +00:00
|
|
|
# Duplicate path
|
2019-06-29 15:25:44 +00:00
|
|
|
self.assertRaises(ValueError, FolderManager.add, "new-folder", self.media_dir)
|
|
|
|
self.assertEqual(
|
2022-12-10 15:55:02 +00:00
|
|
|
Folder.select().where(Folder.path == self.media_dir).count(), 1
|
2019-06-29 15:25:44 +00:00
|
|
|
)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-14 10:14:10 +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)
|
2022-12-10 15:55:02 +00:00
|
|
|
self.assertFalse(Folder.select().where(Folder.path == path).exists())
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2017-07-14 10:14:10 +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)
|
2022-12-10 15:55:02 +00:00
|
|
|
self.assertEqual(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)
|
2022-12-10 15:55:02 +00:00
|
|
|
self.assertEqual(Folder.select().count(), 3)
|
2017-07-14 10:14:10 +00:00
|
|
|
|
|
|
|
def test_delete_folder(self):
|
2022-12-10 15:55:02 +00:00
|
|
|
self.create_folders()
|
2017-12-16 16:42:53 +00:00
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Delete invalid Folder ID
|
|
|
|
self.assertRaises(ValueError, FolderManager.delete, "invalid-uuid")
|
|
|
|
self.assertEqual(Folder.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Delete non-existent folder
|
|
|
|
self.assertRaises(Folder.DoesNotExist, FolderManager.delete, 1234567890)
|
|
|
|
self.assertEqual(Folder.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Delete non-root folder
|
|
|
|
folder = Folder.get(name="non-root")
|
|
|
|
self.assertRaises(Folder.DoesNotExist, FolderManager.delete, folder.id)
|
|
|
|
self.assertEqual(Folder.select().count(), 3)
|
2017-10-28 13:54:41 +00:00
|
|
|
|
2023-01-14 15:47:57 +00:00
|
|
|
# Create some annotation to ensure foreign keys are properly handled
|
|
|
|
self.create_annotations()
|
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Delete existing folders
|
|
|
|
for name in ["media", "music"]:
|
|
|
|
folder = Folder.get(name=name, root=True)
|
|
|
|
FolderManager.delete(folder.id)
|
|
|
|
self.assertRaises(Folder.DoesNotExist, Folder.__getitem__, folder.id)
|
2017-07-14 10:14:10 +00:00
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Even if we have only 2 root folders, non-root should never exist and be cleaned anyway
|
|
|
|
self.assertEqual(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):
|
2022-12-10 15:55:02 +00:00
|
|
|
self.create_folders()
|
|
|
|
|
|
|
|
# Delete non-existent folder
|
|
|
|
self.assertRaises(Folder.DoesNotExist, FolderManager.delete_by_name, "null")
|
|
|
|
self.assertEqual(Folder.select().count(), 3)
|
|
|
|
|
2023-01-14 15:47:57 +00:00
|
|
|
# Create some annotation to ensure foreign keys are properly handled
|
|
|
|
self.create_annotations()
|
|
|
|
|
2022-12-10 15:55:02 +00:00
|
|
|
# Delete existing folders
|
|
|
|
for name in ["media", "music"]:
|
|
|
|
FolderManager.delete_by_name(name)
|
|
|
|
self.assertFalse(Folder.select().where(Folder.name == name).exists())
|
2017-10-28 13:54:41 +00:00
|
|
|
|
|
|
|
|
2019-06-29 15:25:44 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|