def test_cfg_to_dict(self): cfg = Mock() cfg.items = Mock(return_value=[("a", "b"), ("c", "d")]) dct = cb.PydoraConfigFileBuilder.cfg_to_dict(cfg, "foo") self.assertEqual("b", dct["A"]) self.assertEqual("d", dct["C"])
def test_get_ad_item_with_no_station_id_specified_raises_exception(self): transport = Mock(side_effect=[errors.InvalidAuthToken(), None]) client = APIClient(transport, None, None, None, None) client.get_ad_metadata = Mock() self.assertRaises(errors.ParameterMissing, client.get_ad_item, '', 'token_mock')
def test_call_should_retry_on_token_error(self): transport = Mock(side_effect=[InvalidAuthToken(), None]) client = BaseAPIClient(transport, None, None, None) client._authenticate = Mock() client.login("foo", "bar") client("method") client._authenticate.assert_called_with() transport.assert_has_calls([call("method"), call("method")])
def test_call_should_retry_on_token_error(self): transport = Mock(side_effect=[errors.InvalidAuthToken(), None]) client = BaseAPIClient(transport, None, None, None) client._authenticate = Mock() client.login("foo", "bar") client("method") client._authenticate.assert_called_with() transport.assert_has_calls([call("method"), call("method")])
def test_call_should_retry_max_times_on_sys_call_error(self): with self.assertRaises(SysCallError): client = TestSettingsDictBuilder._build_minimal() time.sleep = Mock() client.transport._make_http_request = Mock( side_effect=SysCallError("error_mock")) client.transport._start_request = Mock() client("method") client.transport._start_request.assert_has_calls([call("method")]) assert client.transport._start_request.call_count == 3
def test_call_should_not_retry_for_pandora_exceptions(self): with self.assertRaises(PandoraException): client = TestSettingsDictBuilder._build_minimal() time.sleep = Mock() client.transport._make_http_request = Mock( side_effect=PandoraException("error_mock")) client.transport._start_request = Mock() client("method") client.transport._start_request.assert_has_calls([call("method")]) assert client.transport._start_request.call_count == 1
def test_call_should_retry_if_auth_token_expired(self): with self.assertRaises(InvalidAuthToken): client = TestSettingsDictBuilder._build_minimal() time.sleep = Mock() client.transport._make_http_request = Mock( side_effect=InvalidAuthToken("error_mock")) client.transport._start_request = Mock() client._authenticate = Mock() client("method") client.transport._start_request.assert_has_calls([call("method")]) assert client.transport._start_request.call_count == 2 assert client._authenticate.call_count == 1
def test_ad_support_enabled_parameters(self): with patch.object(APIClient, '__call__') as playlist_mock: transport = Mock(side_effect=[errors.InvalidAuthToken(), None]) client = APIClient(transport, None, None, None, None) client._authenticate = Mock() client.get_playlist('token_mock') playlist_mock.assert_has_calls([ call("station.getPlaylist", audioAdPodCapable=True, includeTrackLength=True, stationToken='token_mock', xplatformAdCapable=True) ])
def test_prepare_playback(self): with patch.object(PlaylistModel, 'prepare_playback') as super_mock: self.result.register_ad = Mock() self.result.prepare_playback() assert self.result.register_ad.called assert super_mock.called
def test_create_station(self): result = pm.SearchResultItem.from_json(self.api_client_mock, self.ARTIST_JSON_DATA) result._api_client.create_station = Mock() result.create_station() result._api_client.create_station.assert_called_with( artist_token=result.token)
def test_prepare_playback_raises_paramater_missing(self): with patch.object(pm.PlaylistModel, 'prepare_playback') as super_mock: self.result.register_ad = Mock(side_effect=ParameterMissing( 'No ad tracking tokens provided for registration.')) self.assertRaises(ParameterMissing, self.result.prepare_playback) assert self.result.register_ad.called assert not super_mock.called
def test_register_ad_raises_if_no_tracking_tokens_available(self): with self.assertRaises(ParameterMissing): self.result.tracking_tokens = [] self.result._api_client.register_ad = Mock(spec=pm.AdItem) self.result.register_ad('id_mock') assert self.result._api_client.register_ad.called
def test_create_station(self): result = SearchResultItem.from_json(self.api_client_mock, self.GENRE_JSON_DATA) result._api_client.create_station = Mock() result.create_station() result._api_client.create_station.assert_called_with( search_token=result.token)
def test_prepare_playback_handles_paramater_missing_if_no_tokens(self): with patch.object(pm.PlaylistModel, 'prepare_playback') as super_mock: self.result.tracking_tokens = [] self.result.register_ad = Mock(side_effect=ParameterMissing( 'No ad tracking tokens provided for registration.')) self.result.prepare_playback() assert self.result.register_ad.called assert super_mock.called
def test_get_ad_item_(self): with patch.object( APIClient, '__call__', return_value=TestAdItem.JSON_DATA) as ad_metadata_mock: transport = Mock(side_effect=[errors.InvalidAuthToken(), None]) client = APIClient(transport, None, None, None, None) client._authenticate = Mock() ad_item = client.get_ad_item('id_mock', 'token_mock') assert ad_item.station_id == 'id_mock' assert ad_item.ad_token == 'token_mock' ad_metadata_mock.assert_has_calls([ call("ad.getAdMetadata", adToken='token_mock', returnAdTrackingTokens=True, supportAudioAds=True) ])
def test_unplayable_get_is_playable(self): playlist = pm.PlaylistModel(Mock()) playlist.audio_url = "" self.assertFalse(playlist.get_is_playable())
def test_playable_get_is_playable(self): client = Mock() playlist = pm.PlaylistModel(client) playlist.audio_url = "foo" playlist.get_is_playable() client.transport.test_url.assert_called_with("foo")
def build_from_settings_dict(self, config): mock = Mock() mock.login = Mock() return mock
def test_login_user_error(self): with self.assertRaises(errors.InvalidUserLogin): transport = Mock(side_effect=[None, errors.InvalidPartnerLogin]) client = BaseAPIClient(transport, None, None, None) client.login("foobear", "secret")
def test_has_changed(self): api_client = Mock() api_client.get_station_list_checksum.return_value = "foo" stations = pm.StationList.from_json(api_client, self.TEST_DATA) self.assertTrue(stations.has_changed())
def setUp(self): self.api_client_mock = Mock(spec=APIClient) self.api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY
def setUp(self): self.client = Mock()
def setUp(self): api_client_mock = Mock(spec=APIClient) api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY self.result = AdItem.from_json(api_client_mock, self.JSON_DATA) self.result.station_id = 'station_id_mock' self.result.ad_token = 'token_mock'
def setUp(self): api_client_mock = Mock(spec=APIClient) api_client_mock.default_audio_quality = APIClient.HIGH_AUDIO_QUALITY self.result = SearchResult.from_json(api_client_mock, self.JSON_DATA)
def test_empty_quality_map_url_is_map(self): item = pm.PlaylistItem.from_json(Mock(), self.WEIRD_FORMAT) self.assertIsNone(item.bitrate) self.assertIsNone(item.encoding) self.assertIsNone(item.audio_url)
def setUp(self): self.transport = Mock(side_effect=[InvalidAuthToken(), None]) self.client = APIClient(self.transport, None, None, None, None) self.client._authenticate = Mock()
def test_audio_url_without_map(self): item = pm.PlaylistItem.from_json(Mock(), self.AUDIO_URL_NO_MAP) self.assertEqual(item.bitrate, 64) self.assertEqual(item.encoding, "aacplus") self.assertEqual(item.audio_url, "foo")
def test_register_ad(self): self.result._api_client.register_ad = Mock() self.result.register_ad('id_mock') assert self.result._api_client.register_ad.called