diff options
author | jcass <john.cass77@gmail.com> | 2015-12-24 21:53:32 +0200 |
---|---|---|
committer | jcass <john.cass77@gmail.com> | 2015-12-24 21:53:32 +0200 |
commit | 79245f34a8abdc296157b78b879994a2738f02e3 (patch) | |
tree | 7699afed6e69609b1c9a1eceef65af94275b4cdb /tests | |
parent | 1ed6b260da26597034c41681cb4f7c1933988d96 (diff) | |
download | pydora-79245f34a8abdc296157b78b879994a2738f02e3.tar.bz2 pydora-79245f34a8abdc296157b78b879994a2738f02e3.tar.xz pydora-79245f34a8abdc296157b78b879994a2738f02e3.zip |
Handle ParameterMissing exceptions due to missing ad tokens.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_pandora/test_client.py | 31 | ||||
-rw-r--r-- | tests/test_pandora/test_models.py | 46 | ||||
-rw-r--r-- | tests/test_pydora/test_utils.py | 32 |
3 files changed, 85 insertions, 24 deletions
diff --git a/tests/test_pandora/test_client.py b/tests/test_pandora/test_client.py index e7ef87f..24d9734 100644 --- a/tests/test_pandora/test_client.py +++ b/tests/test_pandora/test_client.py | |||
@@ -2,7 +2,8 @@ from unittest import TestCase | |||
2 | 2 | ||
3 | from pandora.client import APIClient, BaseAPIClient | 3 | from pandora.client import APIClient, BaseAPIClient |
4 | from pandora.errors import InvalidAuthToken | 4 | from pandora.errors import InvalidAuthToken |
5 | from pandora.py2compat import Mock, MagicMock, call, patch | 5 | from pandora.py2compat import Mock, call, patch |
6 | from tests.test_pandora.test_models import TestAdItem | ||
6 | 7 | ||
7 | 8 | ||
8 | class TestAPIClientLogin(TestCase): | 9 | class TestAPIClientLogin(TestCase): |
@@ -95,26 +96,8 @@ class TestGettingQualities(TestCase): | |||
95 | 96 | ||
96 | class TestGettingAds(TestCase): | 97 | class TestGettingAds(TestCase): |
97 | 98 | ||
98 | mock_ad_metadata_result = { | ||
99 | 'audioUrlMap': { | ||
100 | 'mediumQuality': { | ||
101 | 'audioUrl': 'mock_med_url', 'bitrate': '64', 'protocol': 'http', 'encoding': 'aacplus' | ||
102 | }, | ||
103 | 'highQuality': { | ||
104 | 'audioUrl': 'mock_high_url', 'bitrate': '64', 'protocol': 'http', 'encoding': 'aacplus' | ||
105 | }, | ||
106 | 'lowQuality': { | ||
107 | 'audioUrl': 'mock_low_url', 'bitrate': '32', 'protocol': 'http', 'encoding': 'aacplus'}}, | ||
108 | 'clickThroughUrl': 'mock_click_url', | ||
109 | 'imageUrl': 'mock_img_url', | ||
110 | 'companyName': '', | ||
111 | 'title': '', | ||
112 | 'trackGain': '0.0', | ||
113 | 'adTrackingTokens': ['mock_token_1', 'mock_token_2'] | ||
114 | } | ||
115 | |||
116 | def test_get_ad_item_(self): | 99 | def test_get_ad_item_(self): |
117 | with patch.object(APIClient, '__call__', return_value=self.mock_ad_metadata_result) as ad_metadata_mock: | 100 | with patch.object(APIClient, '__call__', return_value=TestAdItem.JSON_DATA) as ad_metadata_mock: |
118 | transport = Mock(side_effect=[InvalidAuthToken(), None]) | 101 | transport = Mock(side_effect=[InvalidAuthToken(), None]) |
119 | 102 | ||
120 | client = APIClient(transport, None, None, None, None) | 103 | client = APIClient(transport, None, None, None, None) |
@@ -129,9 +112,9 @@ class TestGettingAds(TestCase): | |||
129 | supportAudioAds=True)]) | 112 | supportAudioAds=True)]) |
130 | 113 | ||
131 | def test_get_ad_item_with_no_station_id_specified_raises_exception(self): | 114 | def test_get_ad_item_with_no_station_id_specified_raises_exception(self): |
132 | transport = Mock(side_effect=[InvalidAuthToken(), None]) | 115 | transport = Mock(side_effect=[InvalidAuthToken(), None]) |
133 | 116 | ||
134 | client = APIClient(transport, None, None, None, None) | 117 | client = APIClient(transport, None, None, None, None) |
135 | client.get_ad_metadata = Mock() | 118 | client.get_ad_metadata = Mock() |
136 | 119 | ||
137 | self.assertRaises(ValueError, client.get_ad_item, '', 'mock_token') | 120 | self.assertRaises(ValueError, client.get_ad_item, '', 'mock_token') |
diff --git a/tests/test_pandora/test_models.py b/tests/test_pandora/test_models.py index faee9a7..bb9e00a 100644 --- a/tests/test_pandora/test_models.py +++ b/tests/test_pandora/test_models.py | |||
@@ -1,5 +1,8 @@ | |||
1 | from unittest import TestCase | 1 | from unittest import TestCase |
2 | from datetime import datetime | 2 | from datetime import datetime |
3 | from mock import mock | ||
4 | from pandora import APIClient | ||
5 | from pandora.models.pandora import AdItem, PlaylistModel | ||
3 | 6 | ||
4 | import pandora.models as m | 7 | import pandora.models as m |
5 | 8 | ||
@@ -195,3 +198,46 @@ class TestPandoraDictListModel(TestCase): | |||
195 | "[TestSubModel(fieldS1='Foo', idx='foo'), " | 198 | "[TestSubModel(fieldS1='Foo', idx='foo'), " |
196 | "TestSubModel(fieldS1='Bar', idx='bar')]})") | 199 | "TestSubModel(fieldS1='Bar', idx='bar')]})") |
197 | self.assertEqual(expected, repr(self.result)) | 200 | self.assertEqual(expected, repr(self.result)) |
201 | |||
202 | |||
203 | class TestAdItem(TestCase): | ||
204 | |||
205 | JSON_DATA = { | ||
206 | 'audioUrlMap': { | ||
207 | 'mediumQuality': { | ||
208 | 'audioUrl': 'mock_med_url', 'bitrate': '64', 'protocol': 'http', 'encoding': 'aacplus' | ||
209 | }, | ||
210 | 'highQuality': { | ||
211 | 'audioUrl': 'mock_high_url', 'bitrate': '64', 'protocol': 'http', 'encoding': 'aacplus' | ||
212 | }, | ||
213 | 'lowQuality': { | ||
214 | 'audioUrl': 'mock_low_url', 'bitrate': '32', 'protocol': 'http', 'encoding': 'aacplus'}}, | ||
215 | 'clickThroughUrl': 'mock_click_url', | ||
216 | 'imageUrl': 'mock_img_url', | ||
217 | 'companyName': '', | ||
218 | 'title': '', | ||
219 | 'trackGain': '0.0', | ||
220 | 'adTrackingTokens': ['mock_token_1', 'mock_token_2'] | ||
221 | } | ||
222 | |||
223 | def setUp(self): | ||
224 | api_client_mock = mock.PropertyMock(spec=APIClient) | ||
225 | api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY | ||
226 | self.result = AdItem.from_json(api_client_mock, self.JSON_DATA) | ||
227 | |||
228 | def test_is_ad_is_true(self): | ||
229 | assert self.result.is_ad is True | ||
230 | |||
231 | def test_register_ad(self): | ||
232 | self.result._api_client.register_ad = mock.PropertyMock() | ||
233 | self.result.register_ad('id_dummy') | ||
234 | |||
235 | assert self.result._api_client.register_ad.called | ||
236 | |||
237 | def test_prepare_playback(self): | ||
238 | with mock.patch.object(PlaylistModel, 'prepare_playback') as super_mock: | ||
239 | |||
240 | self.result.register_ad = mock.PropertyMock() | ||
241 | self.result.prepare_playback() | ||
242 | assert self.result.register_ad.called | ||
243 | assert super_mock.called | ||
diff --git a/tests/test_pydora/test_utils.py b/tests/test_pydora/test_utils.py new file mode 100644 index 0000000..8298e28 --- /dev/null +++ b/tests/test_pydora/test_utils.py | |||
@@ -0,0 +1,32 @@ | |||
1 | from unittest import TestCase | ||
2 | from pandora import errors | ||
3 | |||
4 | from pandora.client import APIClient | ||
5 | from pandora.errors import InvalidAuthToken | ||
6 | from pandora.models.pandora import Station, AdItem | ||
7 | from pandora.py2compat import Mock, patch | ||
8 | from pydora.utils import iterate_forever | ||
9 | from tests.test_pandora.test_models import TestAdItem | ||
10 | |||
11 | |||
12 | class TestIterateForever(TestCase): | ||
13 | |||
14 | def setUp(self): | ||
15 | transport = Mock(side_effect=[InvalidAuthToken(), None]) | ||
16 | client = APIClient(transport, None, None, None, None) | ||
17 | client._authenticate = Mock() | ||
18 | self.result = AdItem.from_json(client, TestAdItem.JSON_DATA) | ||
19 | |||
20 | def test_handle_missing_params_exception_due_to_missing_ad_tokens(self): | ||
21 | with patch.object(APIClient, 'register_ad', side_effect=errors.ParameterMissing("ParameterMissing")): | ||
22 | |||
23 | station = Mock(spec=Station) | ||
24 | station.token = 'mock_token' | ||
25 | |||
26 | self.result.tracking_tokens = [] | ||
27 | |||
28 | station.get_playlist.return_value = iter([self.result]) | ||
29 | station_iter = iterate_forever(station.get_playlist) | ||
30 | |||
31 | next_track = station_iter.next() | ||
32 | self.assertEqual(self.result, next_track) | ||