from unittest import TestCase

from pandora.client import APIClient
from pandora.errors import InvalidAuthToken, ParameterMissing
from pandora.models.pandora import Station, AdItem, PlaylistItem
from pandora.py2compat import Mock, patch
from pydora.utils import iterate_forever


class TestIterateForever(TestCase):

    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_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)

    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)

[(14, 'pandora.client.APIClient', 'APIClient', False, 'from pandora.client import APIClient\n'), (15, 'pandora.py2compat.Mock', 'Mock', False, 'from pandora.py2compat import Mock, patch\n'), (18, 'pandora.py2compat.patch.object', 'patch.object', False, 'from pandora.py2compat import Mock, patch\n'), (21, 'pandora.models.pandora.Station.from_json', 'Station.from_json', False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), (22, 'pandora.models.pandora.AdItem.from_json', 'AdItem.from_json', False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), (25, 'pydora.utils.iterate_forever', 'iterate_forever', False, 'from pydora.utils import iterate_forever\n'), (34, 'pandora.models.pandora.Station.from_json', 'Station.from_json', False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), (35, 'pandora.models.pandora.PlaylistItem.from_json', 'PlaylistItem.from_json', False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), (38, 'pydora.utils.iterate_forever', 'iterate_forever', False, 'from pydora.utils import iterate_forever\n'), (13, 'pandora.errors.InvalidAuthToken', 'InvalidAuthToken', False, 'from pandora.errors import InvalidAuthToken, ParameterMissing\n'), (31, 'pandora.errors.ParameterMissing', 'ParameterMissing', False, 'from pandora.errors import InvalidAuthToken, ParameterMissing\n'), (19, 'pandora.errors.ParameterMissing', 'ParameterMissing', False, 'from pandora.errors import InvalidAuthToken, ParameterMissing\n')]