예제 #1
0
def test_browse_directory_uri(config, get_station_list_return_value_mock,
                              station_list_result_mock):
    with mock.patch.object(APIClient,
                           'get_station_list',
                           return_value=get_station_list_return_value_mock):
        backend = conftest.get_backend(config)
        results = backend.library.browse(backend.library.root_directory.uri)

        assert len(results) == 4

        assert results[0].type == models.Ref.DIRECTORY
        assert results[0].uri == PandoraUri('genres').uri

        assert results[1].type == models.Ref.DIRECTORY
        assert results[1].name.startswith('QuickMix')
        assert results[1].uri == StationUri._from_station(
            Station.from_json(backend.api,
                              station_list_result_mock['stations'][2])).uri

        assert results[2].type == models.Ref.DIRECTORY
        assert results[2].uri == StationUri._from_station(
            Station.from_json(backend.api,
                              station_list_result_mock['stations'][1])).uri

        assert results[3].type == models.Ref.DIRECTORY
        assert results[3].uri == StationUri._from_station(
            Station.from_json(backend.api,
                              station_list_result_mock['stations'][0])).uri
예제 #2
0
def test_lookup_of_search_uri(config, get_station_mock_return_value, playlist_item_mock,
                              get_station_list_return_value_mock, station_result_mock):
    with mock.patch.object(MopidyAPIClient, 'get_station', return_value=get_station_mock_return_value):
        with mock.patch.object(APIClient, 'create_station',
                               mock.Mock(return_value=Station.from_json(
                                   mock.MagicMock(MopidyAPIClient),
                                   station_result_mock['result']
                               ))) as create_station_mock:
            with mock.patch.object(APIClient, 'get_station_list', return_value=get_station_list_return_value_mock):
                backend = conftest.get_backend(config)

                get_station_mock_return_value.id = conftest.MOCK_STATION_ID
                backend.library.pandora_station_cache[get_station_mock_return_value.id] = \
                    StationCacheItem(station_result_mock['result'],
                                     iter([playlist_item_mock]))

                track_uri = PlaylistItemUri._from_track(playlist_item_mock)
                backend.library.pandora_track_cache[track_uri.uri] = TrackCacheItem(mock.Mock(spec=models.Ref.track),
                                                                                    playlist_item_mock)

                results = backend.library.lookup('pandora:search:S1234567')
                # Make sure a station is created for the search URI first
                assert create_station_mock.called
                # Check that the first track to be played is returned correctly.
                assert results[0].uri == track_uri.uri
예제 #3
0
def test_lookup_of_search_uri(config, get_station_mock_return_value,
                              playlist_item_mock,
                              get_station_list_return_value_mock,
                              station_result_mock):
    with mock.patch.object(MopidyAPIClient,
                           'get_station',
                           return_value=get_station_mock_return_value):
        with mock.patch.object(
                APIClient, 'create_station',
                mock.Mock(return_value=Station.from_json(
                    mock.MagicMock(MopidyAPIClient),
                    station_result_mock['result']))) as create_station_mock:
            with mock.patch.object(
                    APIClient,
                    'get_station_list',
                    return_value=get_station_list_return_value_mock):
                backend = conftest.get_backend(config)

                get_station_mock_return_value.id = conftest.MOCK_STATION_ID
                backend.library.pandora_station_cache[get_station_mock_return_value.id] = \
                    StationCacheItem(station_result_mock['result'],
                                     iter([playlist_item_mock]))

                track_uri = PlaylistItemUri._from_track(playlist_item_mock)
                backend.library.pandora_track_cache[
                    track_uri.uri] = TrackCacheItem(
                        mock.Mock(spec=models.Ref.track), playlist_item_mock)

                results = backend.library.lookup('pandora:search:S1234567')
                # Make sure a station is created for the search URI first
                assert create_station_mock.called
                # Check that the first track to be played is returned correctly.
                assert results[0].uri == track_uri.uri
예제 #4
0
    def test_reraise_missing_params_exception(self):
        with patch.object(APIClient, 'get_playlist', side_effect=ParameterMissing("ParameterMissing")) as get_playlist_mock:
                with self.assertRaises(ParameterMissing):

                    station = Station.from_json(self.client, {'stationToken': 'token_mock'})
                    track_mock = PlaylistItem.from_json(self.client, {'token': 'token_mock'})
                    get_playlist_mock.return_value=iter([track_mock])

                    station_iter = iterate_forever(station.get_playlist)
                    next(station_iter)
예제 #5
0
    def test_handle_missing_params_exception_due_to_missing_ad_tokens(self):
        with patch.object(APIClient, 'get_playlist') as get_playlist_mock:
            with patch.object(APIClient, 'register_ad', side_effect=ParameterMissing("ParameterMissing")):

                station = Station.from_json(self.client, {'stationToken': 'token_mock'})
                ad_mock = AdItem.from_json(self.client, {'station_id': 'id_mock'})
                get_playlist_mock.return_value=iter([ad_mock])

                station_iter = iterate_forever(station.get_playlist)

                next_track = next(station_iter)
                self.assertEqual(ad_mock, next_track)
예제 #6
0
def test_browse_directory_uri(config, get_station_list_return_value_mock, station_list_result_mock):
    with mock.patch.object(APIClient, 'get_station_list', return_value=get_station_list_return_value_mock):
        backend = conftest.get_backend(config)
        results = backend.library.browse(backend.library.root_directory.uri)

        assert len(results) == 4

        assert results[0].type == models.Ref.DIRECTORY
        assert results[0].uri == PandoraUri('genres').uri

        assert results[1].type == models.Ref.DIRECTORY
        assert results[1].name.startswith('QuickMix')
        assert results[1].uri == StationUri._from_station(
            Station.from_json(backend.api, station_list_result_mock['stations'][2])).uri

        assert results[2].type == models.Ref.DIRECTORY
        assert results[2].uri == StationUri._from_station(
            Station.from_json(backend.api, station_list_result_mock['stations'][1])).uri

        assert results[3].type == models.Ref.DIRECTORY
        assert results[3].uri == StationUri._from_station(
            Station.from_json(backend.api, station_list_result_mock['stations'][0])).uri
예제 #7
0
    def test_reraise_missing_params_exception(self):
        plmock = patch.object(APIClient,
                              'get_playlist',
                              side_effect=ParameterMissing("ParameterMissing"))

        with plmock as get_playlist_mock:
            with self.assertRaises(ParameterMissing):
                station = Station.from_json(self.client,
                                            {'stationToken': 'token_mock'})
                track_mock = PlaylistItem.from_json(self.client,
                                                    {'token': 'token_mock'})
                get_playlist_mock.return_value = iter([track_mock])

                station_iter = iterate_forever(station.get_playlist)
                next(station_iter)
예제 #8
0
    def test_handle_missing_params_exception_due_to_missing_ad_tokens(self):
        with patch.object(APIClient, 'get_playlist') as get_playlist_mock:
            with patch.object(
                    APIClient,
                    'register_ad',
                    side_effect=ParameterMissing("ParameterMissing")):

                station = Station.from_json(self.client,
                                            {'stationToken': 'token_mock'})
                ad_mock = AdItem.from_json(self.client,
                                           {'station_id': 'id_mock'})
                get_playlist_mock.return_value = iter([ad_mock])

                station_iter = iterate_forever(station.get_playlist)

                next_track = next(station_iter)
                self.assertEqual(ad_mock, next_track)
예제 #9
0
def test_create_genre_station_invalidates_cache(config, get_station_list_return_value_mock,
                                                get_genre_stations_return_value_mock, station_result_mock):
    with mock.patch.object(APIClient, 'get_station_list', return_value=get_station_list_return_value_mock):
        with mock.patch.object(MopidyAPIClient, 'get_genre_stations',
                               return_value=get_genre_stations_return_value_mock):
            backend = conftest.get_backend(config)

            backend.api.create_station = mock.PropertyMock(return_value=Station.from_json(
                mock.MagicMock(MopidyAPIClient),
                station_result_mock['result'])
            )
            t = time.time()
            backend.api.station_list_cache[t] = mock.Mock(spec=StationList)
            assert t in list(backend.api.station_list_cache)

            backend.library._create_station_for_token('test_token')
            assert t not in list(backend.api.station_list_cache)
            assert backend.api.station_list_cache.currsize == 1
예제 #10
0
def test_create_genre_station_invalidates_cache(
        config, get_station_list_return_value_mock,
        get_genre_stations_return_value_mock, station_result_mock):
    with mock.patch.object(APIClient,
                           'get_station_list',
                           return_value=get_station_list_return_value_mock):
        with mock.patch.object(
                MopidyAPIClient,
                'get_genre_stations',
                return_value=get_genre_stations_return_value_mock):
            backend = conftest.get_backend(config)

            backend.api.create_station = mock.PropertyMock(
                return_value=Station.from_json(mock.MagicMock(MopidyAPIClient),
                                               station_result_mock['result']))
            t = time.time()
            backend.api.station_list_cache[t] = mock.Mock(spec=StationList)
            assert t in list(backend.api.station_list_cache)

            backend.library._create_station_for_token('test_token')
            assert t not in list(backend.api.station_list_cache)
            assert backend.api.station_list_cache.currsize == 1
예제 #11
0
def get_station_mock_return_value(config,
                                  station_result_mock,
                                  simulate_request_exceptions=False):
    return Station.from_json(
        get_backend(config, simulate_request_exceptions).api,
        station_result_mock['result'])
예제 #12
0
    def _create_station_for_token(self, token):
        json_result = self.backend.api.create_station(search_token=token)
        new_station = Station.from_json(self.backend.api, json_result)

        self.refresh()
        return PandoraUri.factory(new_station)
예제 #13
0
def get_station_mock_return_value(config, station_result_mock, simulate_request_exceptions=False):
    return Station.from_json(get_backend(config, simulate_request_exceptions).api,
                             station_result_mock['result'])
예제 #14
0
def station_mock(simulate_request_exceptions=False):
    return Station.from_json(
        get_backend(config(), simulate_request_exceptions).api,
        station_result_mock()['result'])