1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56: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.
# 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.
@ -15,7 +15,7 @@ from .exceptions import Forbidden, MissingParameter
@api_routing("/getInternetRadioStations")
def get_radio_stations():
query = RadioStation.select().sort_by(RadioStation.name)
query = RadioStation.select().order_by(RadioStation.name)
return request.formatter(
"internetRadioStations",
{"internetRadioStation": [p.as_subsonic_station() for p in query]},
@ -32,7 +32,7 @@ def create_radio_station():
)
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:
raise MissingParameter("streamUrl or name")
@ -55,6 +55,8 @@ def update_radio_station():
if homepage_url:
res.homepage_url = homepage_url
res.save()
else:
raise MissingParameter("streamUrl or name")
@ -67,6 +69,6 @@ def delete_radio_station():
raise Forbidden()
res = get_entity(RadioStation)
res.delete()
res.delete_instance()
return request.formatter.empty

View File

@ -1,24 +1,18 @@
# This file is part of Supysonic.
# 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.
import uuid
from pony.orm import db_session
from supysonic.db import RadioStation
from .apitestbase import ApiTestBase
class RadioStationTestCase(ApiTestBase):
def setUp(self):
super().setUp()
@db_session
def assertRadioStationCountEqual(self, 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
self.assertRadioStationCountEqual(2)
with db_session:
for rs in RadioStation.select():
self.assertRadioStationEquals(rs, stream_url, name)
RadioStation.select().delete(bulk=True)
RadioStation.delete().execute()
# create w/ all fields
stream_url = "http://example.com/radio/create1"
@ -74,7 +67,6 @@ class RadioStationTestCase(ApiTestBase):
# the correct value is 2 because _make_request uses GET then POST
self.assertRadioStationCountEqual(2)
with db_session:
for rs in RadioStation.select():
self.assertRadioStationEquals(rs, stream_url, name, homepage_url)
@ -98,8 +90,7 @@ class RadioStationTestCase(ApiTestBase):
}
# load a test record
with db_session:
station = RadioStation(
station = RadioStation.create(
stream_url=test["stream_url"],
name=test["name"],
homepage_url=test["homepage_url"],
@ -132,7 +123,6 @@ class RadioStationTestCase(ApiTestBase):
},
)
with db_session:
rs_update = RadioStation[station.id]
self.assertRadioStationEquals(
@ -150,7 +140,6 @@ class RadioStationTestCase(ApiTestBase):
},
)
with db_session:
rs_update = RadioStation[station.id]
self.assertRadioStationEquals(
@ -173,8 +162,7 @@ class RadioStationTestCase(ApiTestBase):
)
# delete
with db_session:
station = RadioStation(
station = RadioStation.create(
stream_url="http://example.com/radio/delete",
name="Radio Delete",
homepage_url="http://example.com/update",
@ -188,9 +176,8 @@ class RadioStationTestCase(ApiTestBase):
def test_get_radio_stations(self):
test_range = 3
with db_session:
for x in range(0, test_range):
RadioStation(
for x in range(test_range):
RadioStation.create(
stream_url="http://example.com/radio-{}".format(x),
name="Radio {}".format(x),
homepage_url="http://example.com/update-{}".format(x),
@ -204,7 +191,7 @@ class RadioStationTestCase(ApiTestBase):
self.assertEqual(len(child), test_range)
# This order is guaranteed to work because the api returns in order by name.
# Test data is sequential by design.
for x in range(0, test_range):
for x in range(test_range):
station = child[x]
self.assertTrue(station.get("streamUrl").endswith("radio-{}".format(x)))
self.assertTrue(station.get("name").endswith("Radio {}".format(x)))