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

238 lines
7.5 KiB
Python
Raw Normal View History

2017-10-28 19:18:34 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
2017-10-28 19:18:34 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
2017-11-27 21:30:13 +00:00
import unittest
2018-01-21 21:02:32 +00:00
import flask.json
2017-10-28 19:18:34 +00:00
from xml.etree import ElementTree
2018-01-08 22:13:50 +00:00
from supysonic.py23 import strtype
2017-11-27 21:30:13 +00:00
from ..testbase import TestBase
2017-10-28 19:18:34 +00:00
2017-11-27 21:30:13 +00:00
class ResponseHelperBaseCase(TestBase):
2017-10-28 19:18:34 +00:00
def setUp(self):
2017-11-27 21:30:13 +00:00
super(ResponseHelperBaseCase, self).setUp()
from supysonic.api.formatters import make_json_response, make_jsonp_response, make_xml_response
self.json = self.__response_unwrapper(make_json_response)
self.jsonp = self.__response_unwrapper(make_jsonp_response)
self.xml = self.__response_unwrapper(make_xml_response)
def __response_unwrapper(self, func):
def execute(*args, **kwargs):
with self.request_context():
rv = func(*args, **kwargs)
return rv.get_data(as_text = True)
return execute
2017-10-28 19:18:34 +00:00
class ResponseHelperJsonTestCase(ResponseHelperBaseCase):
def serialize_and_deserialize(self, d, error = False):
if not isinstance(d, dict):
2018-01-08 22:13:50 +00:00
raise TypeError('Invalid tested value, expecting a dict')
2017-10-28 19:18:34 +00:00
json = self.json(d, error)
2018-01-21 21:02:32 +00:00
return flask.json.loads(json)
2017-10-28 19:18:34 +00:00
def process_and_extract(self, d, error = False):
# Basically returns d with additional version and status
2018-01-08 22:13:50 +00:00
return self.serialize_and_deserialize(d, error)['subsonic-response']
2017-10-28 19:18:34 +00:00
def test_basic(self):
empty = self.serialize_and_deserialize({})
self.assertEqual(len(empty), 1)
2018-01-08 22:13:50 +00:00
self.assertIn('subsonic-response', empty)
self.assertIsInstance(empty['subsonic-response'], dict)
2017-10-28 19:18:34 +00:00
2018-01-08 22:13:50 +00:00
resp = empty['subsonic-response']
2017-10-28 19:18:34 +00:00
self.assertEqual(len(resp), 2)
2018-01-08 22:13:50 +00:00
self.assertIn('status', resp)
self.assertIn('version', resp)
self.assertEqual(resp['status'], 'ok')
2017-10-28 19:18:34 +00:00
resp = self.process_and_extract({}, True)
2018-01-08 22:13:50 +00:00
self.assertEqual(resp['status'], 'failed')
2017-10-28 19:18:34 +00:00
some_dict = {
2018-01-08 22:13:50 +00:00
'intValue': 2,
'someString': 'Hello world!'
2017-10-28 19:18:34 +00:00
}
resp = self.process_and_extract(some_dict)
2018-01-08 22:13:50 +00:00
self.assertIn('intValue', resp)
self.assertIn('someString', resp)
2017-10-28 19:18:34 +00:00
def test_lists(self):
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'someList': [ 2, 4, 8, 16 ],
'emptyList': []
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
self.assertIn('someList', resp)
self.assertNotIn('emptyList', resp)
self.assertListEqual(resp['someList'], [ 2, 4, 8, 16 ])
2017-10-28 19:18:34 +00:00
def test_dicts(self):
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'dict': { 's': 'Blah', 'i': 20 },
'empty': {}
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
self.assertIn('dict', resp)
self.assertIn('empty', resp)
self.assertDictEqual(resp['dict'], { 's': 'Blah', 'i': 20 })
self.assertDictEqual(resp['empty'], {})
2017-10-28 19:18:34 +00:00
def test_nesting(self):
resp = self.process_and_extract({
2018-01-08 22:13:50 +00:00
'dict': {
'value': 'hey look! a string',
'list': [ 1, 2, 3 ],
'emptyList': [],
'subdict': { 'a': 'A' }
2017-10-28 19:18:34 +00:00
},
2018-01-08 22:13:50 +00:00
'list': [
{ 'b': 'B' },
{ 'c': 'C' },
2017-10-28 19:18:34 +00:00
[ 4, 5, 6 ],
2018-01-08 22:13:50 +00:00
'final string'
2017-10-28 19:18:34 +00:00
]
})
self.assertEqual(len(resp), 4) # dict, list, status and version
2018-01-08 22:13:50 +00:00
self.assertIn('dict', resp)
self.assertIn('list', resp)
2017-10-28 19:18:34 +00:00
2018-01-08 22:13:50 +00:00
d = resp['dict']
l = resp['list']
2017-10-28 19:18:34 +00:00
2018-01-08 22:13:50 +00:00
self.assertIn('value', d)
self.assertIn('list', d)
2017-10-28 19:18:34 +00:00
self.assertNotIn('emptyList', d)
2018-01-08 22:13:50 +00:00
self.assertIn('subdict', d)
self.assertIsInstance(d['value'], strtype)
self.assertIsInstance(d['list'], list)
self.assertIsInstance(d['subdict'], dict)
2017-10-28 19:18:34 +00:00
self.assertEqual(l, [
2018-01-08 22:13:50 +00:00
{ 'b': 'B' },
{ 'c': 'C' },
2017-10-28 19:18:34 +00:00
[ 4, 5, 6 ],
2018-01-08 22:13:50 +00:00
'final string'
2017-10-28 19:18:34 +00:00
])
class ResponseHelperJsonpTestCase(ResponseHelperBaseCase):
def test_basic(self):
result = self.jsonp({}, 'callback')
2018-01-08 22:13:50 +00:00
self.assertTrue(result.startswith('callback({'))
self.assertTrue(result.endswith('})'))
2017-10-28 19:18:34 +00:00
2018-01-21 21:02:32 +00:00
json = flask.json.loads(result[9:-1])
2018-01-08 22:13:50 +00:00
self.assertIn('subsonic-response', json)
2017-10-28 19:18:34 +00:00
class ResponseHelperXMLTestCase(ResponseHelperBaseCase):
def serialize_and_deserialize(self, d, error = False):
xml = self.xml(d, error)
2018-01-08 22:13:50 +00:00
xml = xml.replace('xmlns="http://subsonic.org/restapi"', '')
2017-10-28 19:18:34 +00:00
root = ElementTree.fromstring(xml)
return root
def assertAttributesMatchDict(self, elem, d):
d = { k: str(v) for k, v in d.items() }
2017-10-28 19:18:34 +00:00
self.assertDictEqual(elem.attrib, d)
def test_root(self):
xml = self.xml({ 'tag': {}})
2018-01-08 22:13:50 +00:00
self.assertIn('<subsonic-response ', xml)
self.assertIn('xmlns="http://subsonic.org/restapi"', xml)
self.assertTrue(xml.strip().endswith('</subsonic-response>'))
2017-10-28 19:18:34 +00:00
def test_basic(self):
empty = self.serialize_and_deserialize({})
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(empty.find('.[@version]'))
self.assertIsNotNone(empty.find(".[@status='ok']"))
2017-10-28 19:18:34 +00:00
resp = self.serialize_and_deserialize({}, True)
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(resp.find(".[@status='failed']"))
2017-10-28 19:18:34 +00:00
some_dict = {
2018-01-08 22:13:50 +00:00
'intValue': 2,
'someString': 'Hello world!'
2017-10-28 19:18:34 +00:00
}
resp = self.serialize_and_deserialize(some_dict)
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(resp.find('.[@intValue]'))
self.assertIsNotNone(resp.find('.[@someString]'))
2017-10-28 19:18:34 +00:00
def test_lists(self):
resp = self.serialize_and_deserialize({
2018-01-08 22:13:50 +00:00
'someList': [ 2, 4, 8, 16 ],
'emptyList': []
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
elems = resp.findall('./someList')
2017-10-28 19:18:34 +00:00
self.assertEqual(len(elems), 4)
2018-01-08 22:13:50 +00:00
self.assertIsNone(resp.find('./emptyList'))
2017-10-28 19:18:34 +00:00
for e, i in zip(elems, [ 2, 4, 8, 16 ]):
self.assertEqual(int(e.text), i)
def test_dicts(self):
resp = self.serialize_and_deserialize({
2018-01-08 22:13:50 +00:00
'dict': { 's': 'Blah', 'i': 20 },
'empty': {}
2017-10-28 19:18:34 +00:00
})
2018-01-08 22:13:50 +00:00
d = resp.find('./dict')
2017-10-28 19:18:34 +00:00
self.assertIsNotNone(d)
2018-01-08 22:13:50 +00:00
self.assertIsNotNone(resp.find('./empty'))
self.assertAttributesMatchDict(d, { 's': 'Blah', 'i': 20 })
2017-10-28 19:18:34 +00:00
def test_nesting(self):
resp = self.serialize_and_deserialize({
2018-01-08 22:13:50 +00:00
'dict': {
'value': 'hey look! a string',
'list': [ 1, 2, 3 ],
'emptyList': [],
'subdict': { 'a': 'A' }
2017-10-28 19:18:34 +00:00
},
2018-01-08 22:13:50 +00:00
'list': [
{ 'b': 'B' },
{ 'c': 'C' },
'final string'
2017-10-28 19:18:34 +00:00
]
})
self.assertEqual(len(resp), 4) # 'dict' and 3 'list's
2018-01-08 22:13:50 +00:00
d = resp.find('./dict')
lists = resp.findall('./list')
2017-10-28 19:18:34 +00:00
self.assertIsNotNone(d)
2018-01-08 22:13:50 +00:00
self.assertAttributesMatchDict(d, { 'value': 'hey look! a string' })
self.assertEqual(len(d.findall('./list')), 3)
self.assertEqual(len(d.findall('./emptyList')), 0)
self.assertIsNotNone(d.find('./subdict'))
2017-10-28 19:18:34 +00:00
self.assertEqual(len(lists), 3)
2018-01-08 22:13:50 +00:00
self.assertAttributesMatchDict(lists[0], { 'b': 'B' })
self.assertAttributesMatchDict(lists[1], { 'c': 'C' })
self.assertEqual(lists[2].text, 'final string')
2017-10-28 19:18:34 +00:00
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(ResponseHelperJsonTestCase))
suite.addTest(unittest.makeSuite(ResponseHelperJsonpTestCase))
suite.addTest(unittest.makeSuite(ResponseHelperXMLTestCase))
return suite
if __name__ == '__main__':
unittest.main()