File: test_response_helper.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 (198 lines) | stat: -rw-r--r-- 6,730 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
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2017-2018 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.

import unittest
import flask.json

from xml.etree import ElementTree

from supysonic.api.formatters import JSONFormatter, JSONPFormatter, XMLFormatter

from ..testbase import TestBase


class UnwrapperMixin:
    def make_response(self, elem, data):
        with self.request_context():
            rv = super().make_response(elem, data)
            return rv.get_data(as_text=True)

    @staticmethod
    def create_from(cls):
        class Unwrapper(UnwrapperMixin, cls):
            pass

        return Unwrapper


class ResponseHelperJsonTestCase(TestBase, UnwrapperMixin.create_from(JSONFormatter)):
    def make_response(self, elem, data):
        rv = super().make_response(elem, data)
        return flask.json.loads(rv)

    def process_and_extract(self, d):
        return self.make_response("tag", d)["subsonic-response"]["tag"]

    def test_basic(self):
        empty = self.empty
        self.assertEqual(len(empty), 1)
        self.assertIn("subsonic-response", empty)
        self.assertIsInstance(empty["subsonic-response"], dict)

        resp = empty["subsonic-response"]
        self.assertEqual(len(resp), 2)
        self.assertIn("status", resp)
        self.assertIn("version", resp)
        self.assertEqual(resp["status"], "ok")

        resp = self.error(0, "message")["subsonic-response"]
        self.assertEqual(resp["status"], "failed")

        some_dict = {"intValue": 2, "someString": "Hello world!"}
        resp = self.process_and_extract(some_dict)
        self.assertIn("intValue", resp)
        self.assertIn("someString", resp)

    def test_lists(self):
        resp = self.process_and_extract({"someList": [2, 4, 8, 16], "emptyList": []})
        self.assertIn("someList", resp)
        self.assertNotIn("emptyList", resp)
        self.assertListEqual(resp["someList"], [2, 4, 8, 16])

    def test_dicts(self):
        resp = self.process_and_extract({"dict": {"s": "Blah", "i": 20}, "empty": {}})
        self.assertIn("dict", resp)
        self.assertIn("empty", resp)
        self.assertDictEqual(resp["dict"], {"s": "Blah", "i": 20})
        self.assertDictEqual(resp["empty"], {})

    def test_nesting(self):
        resp = self.process_and_extract(
            {
                "dict": {
                    "value": "hey look! a string",
                    "list": [1, 2, 3],
                    "emptyList": [],
                    "subdict": {"a": "A"},
                },
                "list": [{"b": "B"}, {"c": "C"}, [4, 5, 6], "final string"],
            }
        )

        self.assertEqual(len(resp), 2)
        self.assertIn("dict", resp)
        self.assertIn("list", resp)

        dct = resp["dict"]
        lst = resp["list"]

        self.assertIn("value", dct)
        self.assertIn("list", dct)
        self.assertNotIn("emptyList", dct)
        self.assertIn("subdict", dct)
        self.assertIsInstance(dct["value"], str)
        self.assertIsInstance(dct["list"], list)
        self.assertIsInstance(dct["subdict"], dict)

        self.assertEqual(lst, [{"b": "B"}, {"c": "C"}, [4, 5, 6], "final string"])


class ResponseHelperJsonpTestCase(TestBase, UnwrapperMixin.create_from(JSONPFormatter)):
    def test_basic(self):
        self._JSONPFormatter__callback = "callback"  # hacky
        result = self.empty
        self.assertTrue(result.startswith("callback({"))
        self.assertTrue(result.endswith("})"))

        json = flask.json.loads(result[9:-1])
        self.assertIn("subsonic-response", json)


class ResponseHelperXMLTestCase(TestBase, UnwrapperMixin.create_from(XMLFormatter)):
    def make_response(self, elem, data):
        xml = super().make_response(elem, data)
        xml = xml.replace('xmlns="http://subsonic.org/restapi"', "")
        root = ElementTree.fromstring(xml)
        return root

    def process_and_extract(self, d):
        rv = self.make_response("tag", d)
        return rv.find("tag")

    def assertAttributesMatchDict(self, elem, d):
        d = {k: str(v) for k, v in d.items()}
        self.assertDictEqual(elem.attrib, d)

    def test_root(self):
        xml = super().make_response("tag", {})
        self.assertIn("<subsonic-response ", xml)
        self.assertIn('xmlns="http://subsonic.org/restapi"', xml)
        self.assertTrue(xml.strip().endswith("</subsonic-response>"))

    def test_basic(self):
        empty = self.empty
        self.assertIsNotNone(empty.find(".[@version]"))
        self.assertIsNotNone(empty.find(".[@status='ok']"))

        resp = self.error(0, "message")
        self.assertIsNotNone(resp.find(".[@status='failed']"))

        some_dict = {"intValue": 2, "someString": "Hello world!"}
        resp = self.process_and_extract(some_dict)
        self.assertIsNotNone(resp.find(".[@intValue]"))
        self.assertIsNotNone(resp.find(".[@someString]"))

    def test_lists(self):
        resp = self.process_and_extract({"someList": [2, 4, 8, 16], "emptyList": []})

        elems = resp.findall("./someList")
        self.assertEqual(len(elems), 4)
        self.assertIsNone(resp.find("./emptyList"))

        for e, i in zip(elems, [2, 4, 8, 16]):
            self.assertEqual(int(e.text), i)

    def test_dicts(self):
        resp = self.process_and_extract({"dict": {"s": "Blah", "i": 20}, "empty": {}})

        d = resp.find("./dict")
        self.assertIsNotNone(d)
        self.assertIsNotNone(resp.find("./empty"))
        self.assertAttributesMatchDict(d, {"s": "Blah", "i": 20})

    def test_nesting(self):
        resp = self.process_and_extract(
            {
                "dict": {
                    "somevalue": "hey look! a string",
                    "list": [1, 2, 3],
                    "emptyList": [],
                    "subdict": {"a": "A"},
                },
                "list": [{"b": "B"}, {"c": "C"}, "final string"],
            }
        )

        self.assertEqual(len(resp), 4)  # 'dict' and 3 'list's

        d = resp.find("./dict")
        lists = resp.findall("./list")

        self.assertIsNotNone(d)
        self.assertAttributesMatchDict(d, {"somevalue": "hey look! a string"})
        self.assertEqual(len(d.findall("./list")), 3)
        self.assertEqual(len(d.findall("./emptyList")), 0)
        self.assertIsNotNone(d.find("./subdict"))

        self.assertEqual(len(lists), 3)
        self.assertAttributesMatchDict(lists[0], {"b": "B"})
        self.assertAttributesMatchDict(lists[1], {"c": "C"})
        self.assertEqual(lists[2].text, "final string")


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