1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Porting supysonic.api.radio

This commit is contained in:
Alban Féron 2022-12-30 15:08:13 +01:00
parent 6179daf4ce
commit 4fa744efcd
No known key found for this signature in database
GPG Key ID: 8CE0313646D16165
2 changed files with 31 additions and 42 deletions

View File

@ -1,7 +1,7 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2020 Alban 'spl0k' Féron # Copyright (C) 2020-2022 Alban 'spl0k' Féron
# #
# Distributed under terms of the GNU AGPLv3 license. # Distributed under terms of the GNU AGPLv3 license.
@ -15,7 +15,7 @@ from .exceptions import Forbidden, MissingParameter
@api_routing("/getInternetRadioStations") @api_routing("/getInternetRadioStations")
def get_radio_stations(): def get_radio_stations():
query = RadioStation.select().sort_by(RadioStation.name) query = RadioStation.select().order_by(RadioStation.name)
return request.formatter( return request.formatter(
"internetRadioStations", "internetRadioStations",
{"internetRadioStation": [p.as_subsonic_station() for p in query]}, {"internetRadioStation": [p.as_subsonic_station() for p in query]},
@ -32,7 +32,7 @@ def create_radio_station():
) )
if stream_url and name: if stream_url and name:
RadioStation(stream_url=stream_url, name=name, homepage_url=homepage_url) RadioStation.create(stream_url=stream_url, name=name, homepage_url=homepage_url)
else: else:
raise MissingParameter("streamUrl or name") raise MissingParameter("streamUrl or name")
@ -55,6 +55,8 @@ def update_radio_station():
if homepage_url: if homepage_url:
res.homepage_url = homepage_url res.homepage_url = homepage_url
res.save()
else: else:
raise MissingParameter("streamUrl or name") raise MissingParameter("streamUrl or name")
@ -67,6 +69,6 @@ def delete_radio_station():
raise Forbidden() raise Forbidden()
res = get_entity(RadioStation) res = get_entity(RadioStation)
res.delete() res.delete_instance()
return request.formatter.empty return request.formatter.empty

View File

@ -1,24 +1,18 @@
# This file is part of Supysonic. # This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API. # Supysonic is a Python implementation of the Subsonic server API.
# #
# Copyright (C) 2020 Alban 'spl0k' Féron # Copyright (C) 2020-2022 Alban 'spl0k' Féron
# #
# Distributed under terms of the GNU AGPLv3 license. # Distributed under terms of the GNU AGPLv3 license.
import uuid import uuid
from pony.orm import db_session
from supysonic.db import RadioStation from supysonic.db import RadioStation
from .apitestbase import ApiTestBase from .apitestbase import ApiTestBase
class RadioStationTestCase(ApiTestBase): class RadioStationTestCase(ApiTestBase):
def setUp(self):
super().setUp()
@db_session
def assertRadioStationCountEqual(self, count): def assertRadioStationCountEqual(self, count):
self.assertEqual(RadioStation.select().count(), count) self.assertEqual(RadioStation.select().count(), count)
@ -55,11 +49,10 @@ class RadioStationTestCase(ApiTestBase):
# the correct value is 2 because _make_request uses GET then POST # the correct value is 2 because _make_request uses GET then POST
self.assertRadioStationCountEqual(2) self.assertRadioStationCountEqual(2)
with db_session: for rs in RadioStation.select():
for rs in RadioStation.select(): self.assertRadioStationEquals(rs, stream_url, name)
self.assertRadioStationEquals(rs, stream_url, name)
RadioStation.select().delete(bulk=True) RadioStation.delete().execute()
# create w/ all fields # create w/ all fields
stream_url = "http://example.com/radio/create1" stream_url = "http://example.com/radio/create1"
@ -74,9 +67,8 @@ class RadioStationTestCase(ApiTestBase):
# the correct value is 2 because _make_request uses GET then POST # the correct value is 2 because _make_request uses GET then POST
self.assertRadioStationCountEqual(2) self.assertRadioStationCountEqual(2)
with db_session: for rs in RadioStation.select():
for rs in RadioStation.select(): self.assertRadioStationEquals(rs, stream_url, name, homepage_url)
self.assertRadioStationEquals(rs, stream_url, name, homepage_url)
def test_update_radio_station(self): def test_update_radio_station(self):
self._make_request( self._make_request(
@ -98,12 +90,11 @@ class RadioStationTestCase(ApiTestBase):
} }
# load a test record # load a test record
with db_session: station = RadioStation.create(
station = RadioStation( stream_url=test["stream_url"],
stream_url=test["stream_url"], name=test["name"],
name=test["name"], homepage_url=test["homepage_url"],
homepage_url=test["homepage_url"], )
)
# check params # check params
self._make_request( self._make_request(
@ -132,8 +123,7 @@ class RadioStationTestCase(ApiTestBase):
}, },
) )
with db_session: rs_update = RadioStation[station.id]
rs_update = RadioStation[station.id]
self.assertRadioStationEquals( self.assertRadioStationEquals(
rs_update, update["stream_url"], update["name"], test["homepage_url"] rs_update, update["stream_url"], update["name"], test["homepage_url"]
@ -150,8 +140,7 @@ class RadioStationTestCase(ApiTestBase):
}, },
) )
with db_session: rs_update = RadioStation[station.id]
rs_update = RadioStation[station.id]
self.assertRadioStationEquals( self.assertRadioStationEquals(
rs_update, update["stream_url"], update["name"], update["homepage_url"] rs_update, update["stream_url"], update["name"], update["homepage_url"]
@ -173,12 +162,11 @@ class RadioStationTestCase(ApiTestBase):
) )
# delete # delete
with db_session: station = RadioStation.create(
station = RadioStation( stream_url="http://example.com/radio/delete",
stream_url="http://example.com/radio/delete", name="Radio Delete",
name="Radio Delete", homepage_url="http://example.com/update",
homepage_url="http://example.com/update", )
)
self._make_request( self._make_request(
"deleteInternetRadioStation", {"id": station.id}, skip_post=True "deleteInternetRadioStation", {"id": station.id}, skip_post=True
@ -188,13 +176,12 @@ class RadioStationTestCase(ApiTestBase):
def test_get_radio_stations(self): def test_get_radio_stations(self):
test_range = 3 test_range = 3
with db_session: for x in range(test_range):
for x in range(0, test_range): RadioStation.create(
RadioStation( stream_url="http://example.com/radio-{}".format(x),
stream_url="http://example.com/radio-{}".format(x), name="Radio {}".format(x),
name="Radio {}".format(x), homepage_url="http://example.com/update-{}".format(x),
homepage_url="http://example.com/update-{}".format(x), )
)
# verify happy path is clean # verify happy path is clean
self.assertRadioStationCountEqual(test_range) self.assertRadioStationCountEqual(test_range)
@ -204,7 +191,7 @@ class RadioStationTestCase(ApiTestBase):
self.assertEqual(len(child), test_range) self.assertEqual(len(child), test_range)
# This order is guaranteed to work because the api returns in order by name. # This order is guaranteed to work because the api returns in order by name.
# Test data is sequential by design. # Test data is sequential by design.
for x in range(0, test_range): for x in range(test_range):
station = child[x] station = child[x]
self.assertTrue(station.get("streamUrl").endswith("radio-{}".format(x))) self.assertTrue(station.get("streamUrl").endswith("radio-{}".format(x)))
self.assertTrue(station.get("name").endswith("Radio {}".format(x))) self.assertTrue(station.get("name").endswith("Radio {}".format(x)))