File: test_annotation.py

package info (click to toggle)
supysonic 0.7.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,716 kB
  • sloc: python: 9,315; sql: 1,029; sh: 25; makefile: 19; javascript: 6
file content (226 lines) | stat: -rw-r--r-- 8,927 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017 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


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)

            # 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,
            )

            self.folderid = folder.id
            self.artistid = artist.id
            self.albumid = album.id
            self.trackid = track.id
            self.user = User.get(name="alice")

    def test_star(self):
        self._make_request("star", error=10)
        self._make_request("star", {"id": "unknown"}, error=0)
        self._make_request("star", {"albumId": "unknown"}, error=0)
        self._make_request("star", {"artistId": "unknown"}, error=0)
        self._make_request("star", {"id": str(uuid.uuid4())}, error=70)
        self._make_request("star", {"albumId": str(uuid.uuid4())}, error=70)
        self._make_request("star", {"artistId": str(uuid.uuid4())}, error=70)

        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._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._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._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._make_request("star", {"artistId": str(self.artistid)}, error=0)

    def test_unstar(self):
        self._make_request(
            "star",
            {
                "id": [str(self.folderid), str(self.trackid)],
                "artistId": str(self.artistid),
                "albumId": str(self.albumid),
            },
            skip_post=True,
        )

        self._make_request("unstar", error=10)
        self._make_request("unstar", {"id": "unknown"}, error=0)
        self._make_request("unstar", {"albumId": "unknown"}, error=0)
        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._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._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._make_request("unstar", {"artistId": str(self.artistid)}, skip_post=True)
        with db_session:
            self.assertNotIn(
                "starred", Artist[self.artistid].as_subsonic_artist(self.user)
            )

    def test_set_rating(self):
        self._make_request("setRating", error=10)
        self._make_request("setRating", {"id": str(self.trackid)}, error=10)
        self._make_request("setRating", {"rating": 3}, error=10)
        self._make_request("setRating", {"id": "string", "rating": 3}, error=0)
        self._make_request(
            "setRating", {"id": str(uuid.uuid4()), "rating": 3}, error=70
        )
        self._make_request(
            "setRating", {"id": str(self.artistid), "rating": 3}, error=70
        )
        self._make_request(
            "setRating", {"id": str(self.albumid), "rating": 3}, error=70
        )
        self._make_request(
            "setRating", {"id": str(self.trackid), "rating": "string"}, error=0
        )
        self._make_request(
            "setRating", {"id": str(self.trackid), "rating": -1}, error=0
        )
        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)
            )

        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._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", 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._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)
            )

    def test_scrobble(self):
        self._make_request("scrobble", error=10)
        self._make_request("scrobble", {"id": "song"}, error=0)
        self._make_request("scrobble", {"id": str(uuid.uuid4())}, error=70)
        self._make_request("scrobble", {"id": str(self.folderid)}, error=0)

        self._make_request("scrobble", {"id": str(self.trackid)})
        self._make_request("scrobble", {"id": str(self.trackid), "submission": True})
        self._make_request("scrobble", {"id": str(self.trackid), "submission": False})


if __name__ == "__main__":
    unittest.main()