From 7401b4dec9014b6df2b5a0cfe0a8d67ca10db53b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alban=20F=C3=A9ron?= Date: Sun, 18 Dec 2022 17:24:05 +0100 Subject: [PATCH] Porting supysonic.api.annotation --- supysonic/api/annotation.py | 31 ++++--- tests/api/test_annotation.py | 160 ++++++++++++++--------------------- 2 files changed, 77 insertions(+), 114 deletions(-) diff --git a/supysonic/api/annotation.py b/supysonic/api/annotation.py index a8c8bb0..ae1fa4f 100644 --- a/supysonic/api/annotation.py +++ b/supysonic/api/annotation.py @@ -28,16 +28,16 @@ def star_single(cls, starcls, eid): try: e = cls[eid] - except ObjectNotFound: + except cls.DoesNotExist: raise NotFound("{} {}".format(cls.__name__, eid)) try: starcls[request.user, eid] raise GenericError("{} {} already starred".format(cls.__name__, eid)) - except ObjectNotFound: + except starcls.DoesNotExist: pass - starcls(user=request.user, starred=e) + starcls.create(user=request.user, starred=e) def unstar_single(cls, starcls, eid): @@ -48,7 +48,9 @@ def unstar_single(cls, starcls, eid): :param eid: id of the entity to unstar """ - delete(s for s in starcls if s.user.id == request.user.id and s.starred.id == eid) + starcls.delete().where( + starcls.user == request.user, starcls.starred == eid + ).execute() def handle_star_request(func): @@ -139,17 +141,13 @@ def rate(): if rating == 0: if tid is not None: - delete( - r - for r in RatingTrack - if r.user.id == request.user.id and r.rated.id == tid - ) + RatingTrack.delete().where( + RatingTrack.user == request.user, RatingTrack.rated == tid + ).execute() else: - delete( - r - for r in RatingFolder - if r.user.id == request.user.id and r.rated.id == fid - ) + RatingFolder.delete().where( + RatingFolder.user == request.user, RatingFolder.rated == fid + ).execute() else: if tid is not None: rated = Track[tid] @@ -163,8 +161,9 @@ def rate(): try: rating_info = rating_cls[request.user, uid] rating_info.rating = rating - except ObjectNotFound: - rating_cls(user=request.user, rated=rated, rating=rating) + rating_info.save() + except rating_cls.DoesNotExist: + rating_cls.create(user=request.user, rated=rated, rating=rating) return request.formatter.empty diff --git a/tests/api/test_annotation.py b/tests/api/test_annotation.py index 07b96e7..845f242 100644 --- a/tests/api/test_annotation.py +++ b/tests/api/test_annotation.py @@ -1,15 +1,13 @@ # This file is part of Supysonic. # Supysonic is a Python implementation of the Subsonic server API. # -# Copyright (C) 2017 Alban 'spl0k' Féron +# Copyright (C) 2017-2022 Alban 'spl0k' Féron # # Distributed under terms of the GNU AGPLv3 license. import unittest import uuid -from pony.orm import db_session - from supysonic.db import Folder, Artist, Album, Track, User, ClientPrefs from .apitestbase import ApiTestBase @@ -19,35 +17,37 @@ class AnnotationTestCase(ApiTestBase): def setUp(self): super().setUp() - with db_session: - root = Folder(name="Root", root=True, path="tests") - folder = Folder(name="Folder", path="tests/assets", parent=root) - artist = Artist(name="Artist") - album = Album(name="Album", artist=artist) + root = Folder.create(name="Root", root=True, path="tests") + folder = Folder.create( + name="Folder", root=False, path="tests/assets", parent=root + ) + artist = Artist.create(name="Artist") + album = Album.create(name="Album", artist=artist) - # Populate folder ids - root = Folder.get(name="Root") - folder = Folder.get(name="Folder") + # Populate folder ids + root = Folder.get(name="Root") + folder = Folder.get(name="Folder") - track = Track( - title="Track", - album=album, - artist=artist, - disc=1, - number=1, - path="tests/assets/empty", - folder=folder, - root_folder=root, - duration=2, - bitrate=320, - last_modification=0, - ) + track = Track.create( + title="Track", + album=album, + artist=artist, + disc=1, + number=1, + path="tests/assets/empty", + folder=folder, + root_folder=root, + duration=2, + bitrate=320, + last_modification=0, + ) - self.folderid = folder.id - self.artistid = artist.id - self.albumid = album.id - self.trackid = track.id - self.user = User.get(name="alice") + self.folderid = folder.id + self.artistid = artist.id + self.albumid = album.id + self.trackid = track.id + self.user = User.get(name="alice") + self.prefs = ClientPrefs.create(user=self.user, client_name="tests") def test_star(self): self._make_request("star", error=10) @@ -61,36 +61,27 @@ class AnnotationTestCase(ApiTestBase): self._make_request("star", {"id": str(self.artistid)}, error=70) self._make_request("star", {"id": str(self.albumid)}, error=70) self._make_request("star", {"id": str(self.trackid)}, skip_post=True) - with db_session: - prefs = ClientPrefs.get( - lambda p: p.user.name == "alice" and p.client_name == "tests" - ) - self.assertIn( - "starred", Track[self.trackid].as_subsonic_child(self.user, prefs) - ) + self.assertIn( + "starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs) + ) self._make_request("star", {"id": str(self.trackid)}, error=0) self._make_request("star", {"id": str(self.folderid)}, skip_post=True) - with db_session: - self.assertIn("starred", Folder[self.folderid].as_subsonic_child(self.user)) + self.assertIn("starred", Folder[self.folderid].as_subsonic_child(self.user)) self._make_request("star", {"id": str(self.folderid)}, error=0) self._make_request("star", {"albumId": str(self.folderid)}, error=0) self._make_request("star", {"albumId": str(self.artistid)}, error=70) self._make_request("star", {"albumId": str(self.trackid)}, error=70) self._make_request("star", {"albumId": str(self.albumid)}, skip_post=True) - with db_session: - self.assertIn("starred", Album[self.albumid].as_subsonic_album(self.user)) + self.assertIn("starred", Album[self.albumid].as_subsonic_album(self.user)) self._make_request("star", {"albumId": str(self.albumid)}, error=0) self._make_request("star", {"artistId": str(self.folderid)}, error=0) self._make_request("star", {"artistId": str(self.albumid)}, error=70) self._make_request("star", {"artistId": str(self.trackid)}, error=70) self._make_request("star", {"artistId": str(self.artistid)}, skip_post=True) - with db_session: - self.assertIn( - "starred", Artist[self.artistid].as_subsonic_artist(self.user) - ) + self.assertIn("starred", Artist[self.artistid].as_subsonic_artist(self.user)) self._make_request("star", {"artistId": str(self.artistid)}, error=0) def test_unstar(self): @@ -110,31 +101,18 @@ class AnnotationTestCase(ApiTestBase): self._make_request("unstar", {"artistId": "unknown"}, error=0) self._make_request("unstar", {"id": str(self.trackid)}, skip_post=True) - with db_session: - prefs = ClientPrefs.get( - lambda p: p.user.name == "alice" and p.client_name == "tests" - ) - self.assertNotIn( - "starred", Track[self.trackid].as_subsonic_child(self.user, prefs) - ) + self.assertNotIn( + "starred", Track[self.trackid].as_subsonic_child(self.user, self.prefs) + ) self._make_request("unstar", {"id": str(self.folderid)}, skip_post=True) - with db_session: - self.assertNotIn( - "starred", Folder[self.folderid].as_subsonic_child(self.user) - ) + self.assertNotIn("starred", Folder[self.folderid].as_subsonic_child(self.user)) self._make_request("unstar", {"albumId": str(self.albumid)}, skip_post=True) - with db_session: - self.assertNotIn( - "starred", Album[self.albumid].as_subsonic_album(self.user) - ) + self.assertNotIn("starred", Album[self.albumid].as_subsonic_album(self.user)) self._make_request("unstar", {"artistId": str(self.artistid)}, skip_post=True) - with db_session: - self.assertNotIn( - "starred", Artist[self.artistid].as_subsonic_artist(self.user) - ) + self.assertNotIn("starred", Artist[self.artistid].as_subsonic_artist(self.user)) def test_set_rating(self): self._make_request("setRating", error=10) @@ -158,58 +136,44 @@ class AnnotationTestCase(ApiTestBase): ) self._make_request("setRating", {"id": str(self.trackid), "rating": 6}, error=0) - with db_session: - prefs = ClientPrefs.get( - lambda p: p.user.name == "alice" and p.client_name == "tests" - ) - self.assertNotIn( - "userRating", Track[self.trackid].as_subsonic_child(self.user, prefs) - ) + self.assertNotIn( + "userRating", Track[self.trackid].as_subsonic_child(self.user, self.prefs) + ) for i in range(1, 6): self._make_request( "setRating", {"id": str(self.trackid), "rating": i}, skip_post=True ) - with db_session: - prefs = ClientPrefs.get( - lambda p: p.user.name == "alice" and p.client_name == "tests" - ) - self.assertEqual( - Track[self.trackid].as_subsonic_child(self.user, prefs)[ - "userRating" - ], - i, - ) + self.assertEqual( + Track[self.trackid].as_subsonic_child(self.user, self.prefs)[ + "userRating" + ], + i, + ) self._make_request( "setRating", {"id": str(self.trackid), "rating": 0}, skip_post=True ) - with db_session: - prefs = ClientPrefs.get( - lambda p: p.user.name == "alice" and p.client_name == "tests" - ) - self.assertNotIn( - "userRating", Track[self.trackid].as_subsonic_child(self.user, prefs) - ) + self.assertNotIn( + "userRating", Track[self.trackid].as_subsonic_child(self.user, self.prefs) + ) - self.assertNotIn( - "userRating", Folder[self.folderid].as_subsonic_child(self.user) - ) + self.assertNotIn( + "userRating", Folder[self.folderid].as_subsonic_child(self.user) + ) for i in range(1, 6): self._make_request( "setRating", {"id": str(self.folderid), "rating": i}, skip_post=True ) - with db_session: - self.assertEqual( - Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i - ) + self.assertEqual( + Folder[self.folderid].as_subsonic_child(self.user)["userRating"], i + ) self._make_request( "setRating", {"id": str(self.folderid), "rating": 0}, skip_post=True ) - with db_session: - self.assertNotIn( - "userRating", Folder[self.folderid].as_subsonic_child(self.user) - ) + self.assertNotIn( + "userRating", Folder[self.folderid].as_subsonic_child(self.user) + ) def test_scrobble(self): self._make_request("scrobble", error=10)