aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2015-07-03 19:00:00 -0700
committerMike Crute <mcrute@gmail.com>2015-07-04 14:01:00 -0700
commitab4f1e2d769fae61834f62a9f12dd97bf826049b (patch)
tree34a9091ccf4f2970f5845fa6e2e9a6825381f8e3 /tests
parentda8801e7a3b2b83a9dc30796c03fa10d66205c22 (diff)
downloadpydora-ab4f1e2d769fae61834f62a9f12dd97bf826049b.tar.bz2
pydora-ab4f1e2d769fae61834f62a9f12dd97bf826049b.tar.xz
pydora-ab4f1e2d769fae61834f62a9f12dd97bf826049b.zip
Add config builders and tests
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/pandora/__init__.py0
-rw-r--r--tests/pandora/clientbuilder.py214
-rw-r--r--tests/pandora/models/__init__.py0
-rw-r--r--tests/pandora/pianobar.cfg12
-rw-r--r--tests/pandora/pydora.cfg12
-rw-r--r--tests/pydora/__init__.py0
7 files changed, 238 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/pandora/__init__.py b/tests/pandora/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/pandora/__init__.py
diff --git a/tests/pandora/clientbuilder.py b/tests/pandora/clientbuilder.py
new file mode 100644
index 0000000..b953f9e
--- /dev/null
+++ b/tests/pandora/clientbuilder.py
@@ -0,0 +1,214 @@
1import os
2from unittest import TestCase
3
4try:
5 from unittest.mock import Mock
6except ImportError:
7 from mock import Mock
8
9import pandora.clientbuilder as cb
10from pandora import APIClient, DEFAULT_API_HOST
11
12
13class TestTranslatingDict(TestCase):
14
15 class TestDict(cb.TranslatingDict):
16
17 KEY_TRANSLATIONS = { "FOO": "BAR" }
18 VALUE_TRANSLATIONS = { "BAZ": lambda v: v + 1 }
19 callback_value = None
20
21 def was_translated(self, from_key, to_key):
22 self.callback_value = (from_key, to_key)
23
24 def setUp(self):
25 self.dct = self.TestDict()
26
27 def test_construction_with_dict(self):
28 dct = self.TestDict({ "BIZ": 1, "BUZ": 2 })
29
30 self.assertEqual(1, dct["BIZ"])
31 self.assertEqual(2, dct["BUZ"])
32
33 def test_construction_with_list(self):
34 dct = self.TestDict([("key", "value")])
35
36 self.assertEqual("value", dct["KEY"])
37
38 def test_key_translation(self):
39 self.dct.put(" TEST ", "value")
40 self.dct.put("MoRe", 1)
41 self.dct.put("foo", True)
42
43 self.assertEqual("value", self.dct["TEST"])
44 self.assertEqual(1, self.dct["MORE"])
45 self.assertEqual(True, self.dct["BAR"])
46
47 def test_value_translation(self):
48 dct = self.TestDict({ " Baz": 41 })
49
50 self.assertEqual(42, dct["BAZ"])
51
52 def test_setitem(self):
53 self.dct["Foo"] = "bar"
54
55 self.assertEqual("bar", self.dct["BAR"])
56
57 def test_put(self):
58 self.dct.put("Foo", "bar")
59
60 self.assertEqual("bar", self.dct["BAR"])
61
62 def test_key_translation_hook(self):
63 self.dct.put("Foo", "bar")
64 self.assertEqual(("FOO", "BAR"), self.dct.callback_value)
65
66
67class TestSettingsDictBuilder(TestCase):
68
69 def _build_minimal(self):
70 return cb.SettingsDictBuilder({
71 "DECRYPTION_KEY": "dec",
72 "ENCRYPTION_KEY": "enc",
73 "PARTNER_USER": "user",
74 "PARTNER_PASSWORD": "pass",
75 "DEVICE": "dev",
76 }).build()
77
78 def _build_maximal(self):
79 return cb.SettingsDictBuilder({
80 "DECRYPTION_KEY": "dec",
81 "ENCRYPTION_KEY": "enc",
82 "PARTNER_USER": "user",
83 "PARTNER_PASSWORD": "pass",
84 "DEVICE": "dev",
85 "PROXY": "proxy.example.com",
86 "AUDIO_QUALITY": "high",
87 "API_HOST": "example.com",
88 }).build()
89
90 def test_building(self):
91 client = self._build_minimal()
92
93 self.assertTrue(isinstance(client, APIClient))
94
95 def test_default_values(self):
96 client = self._build_minimal()
97
98 self.assertIsNone(client.transport.proxy)
99 self.assertEqual(DEFAULT_API_HOST, client.transport.api_host)
100 self.assertEqual(APIClient.MED_AUDIO_QUALITY,
101 client.default_audio_quality)
102
103 def test_validate_client(self):
104 client = self._build_maximal()
105
106 self.assertIsNotNone(client.transport.cryptor.bf_in)
107 self.assertIsNotNone(client.transport.cryptor.bf_out)
108
109 self.assertEqual("user", client.partner_user)
110 self.assertEqual("pass", client.partner_password)
111 self.assertEqual("dev", client.device)
112
113 self.assertEqual("proxy.example.com", client.transport.proxy)
114 self.assertEqual("example.com", client.transport.api_host)
115 self.assertEqual("high", client.default_audio_quality)
116
117
118class TestFileBasedBuilder(TestCase):
119
120 class StubBuilder(cb.FileBasedClientBuilder):
121
122 DEFAULT_CONFIG_FILE = "foo"
123
124 def parse_config(self):
125 return { "USER": { "USERNAME": "U", "PASSWORD": "P" }}
126
127 def build_from_settings_dict(self, config):
128 mock = Mock()
129 mock.login = Mock()
130 return mock
131
132 def test_default_config(self):
133 builder = self.StubBuilder()
134
135 self.assertEqual("foo", builder.path)
136
137 def test_setting_valid_path(self):
138 builder = cb.FileBasedClientBuilder(__file__)
139
140 self.assertTrue(builder.file_exists)
141 self.assertEqual(__file__, builder.path)
142
143 def test_setting_invalid_path(self):
144 builder = cb.FileBasedClientBuilder("nowhere")
145
146 with self.assertRaises(IOError):
147 builder.build()
148
149 self.assertFalse(builder.file_exists)
150
151 def test_setting_user_path(self):
152 builder = cb.FileBasedClientBuilder("~/")
153
154 self.assertEqual(os.path.expanduser("~/"), builder.path)
155
156 def test_logging_in(self):
157 client = self.StubBuilder(__file__, True).build()
158 client.login.assert_called_once_with("U", "P")
159
160 def test_not_logging_in(self):
161 client = self.StubBuilder(__file__, False).build()
162 self.assertFalse(client.login.called)
163
164
165class TestPydoraConfigFileBuilder(TestCase):
166
167 def test_cfg_to_dict(self):
168 cfg = Mock()
169 cfg.items = Mock(return_value=[("a", "b"), ("c", "d")])
170
171 dct = cb.PydoraConfigFileBuilder.cfg_to_dict(cfg, "foo")
172
173 self.assertEqual("b", dct["A"])
174 self.assertEqual("d", dct["C"])
175
176 def test_integration(self):
177 path = os.path.join(os.path.dirname(__file__), "pydora.cfg")
178 cfg = cb.PydoraConfigFileBuilder(path).parse_config()
179
180 self.assertDictEqual(cfg, {
181 "AUDIO_QUALITY": "test_quality",
182 "DECRYPTION_KEY": "test_decryption_key",
183 "DEVICE": "test_device",
184 "ENCRYPTION_KEY": "test_encryption_key",
185 "PARTNER_PASSWORD": "test_partner_password",
186 "PARTNER_USER": "test_partner_username",
187 "API_HOST": "test_host",
188 "USER": {
189 "USERNAME": "test_username",
190 "PASSWORD": "test_password",
191 }
192 })
193
194
195class TestPianobarConfigFileBuilder(TestCase):
196
197 def test_integration(self):
198 path = os.path.join(os.path.dirname(__file__), "pianobar.cfg")
199 cfg = cb.PianobarConfigFileBuilder(path).parse_config()
200
201 self.assertDictEqual(cfg, {
202 "AUDIO_QUALITY": "test_qualityQuality",
203 "DECRYPTION_KEY": "test_decryption_key",
204 "DEVICE": "test_device",
205 "ENCRYPTION_KEY": "test_encryption_key",
206 "PARTNER_PASSWORD": "test_partner_password",
207 "PARTNER_USER": "test_partner_username",
208 "API_HOST": "test_host/services/json/",
209 "PROXY": "test_proxy",
210 "USER": {
211 "USERNAME": "test_username",
212 "PASSWORD": "test_password",
213 }
214 })
diff --git a/tests/pandora/models/__init__.py b/tests/pandora/models/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/pandora/models/__init__.py
diff --git a/tests/pandora/pianobar.cfg b/tests/pandora/pianobar.cfg
new file mode 100644
index 0000000..7a915d8
--- /dev/null
+++ b/tests/pandora/pianobar.cfg
@@ -0,0 +1,12 @@
1# high-quality audio (192k mp3, for Pandora One subscribers only!)
2audio_quality = test_quality
3rpc_host = test_host
4partner_user = test_partner_username
5partner_password = test_partner_password
6device = test_device
7encrypt_password = test_encryption_key
8decrypt_password = test_decryption_key
9control_proxy = test_proxy
10
11user = test_username
12password = test_password
diff --git a/tests/pandora/pydora.cfg b/tests/pandora/pydora.cfg
new file mode 100644
index 0000000..854e377
--- /dev/null
+++ b/tests/pandora/pydora.cfg
@@ -0,0 +1,12 @@
1[api]
2api_host = test_host
3encryption_key = test_encryption_key
4decryption_key = test_decryption_key
5username = test_partner_username
6password = test_partner_password
7device = test_device
8default_audio_quality = test_quality
9
10[user]
11username = test_username
12password = test_password
diff --git a/tests/pydora/__init__.py b/tests/pydora/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/pydora/__init__.py