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, 22, 14, 71), 'pandora.client.APIClient', 'APIClient', ({(14, 32, 14, 46): 'self.transport', (14, 48, 14, 52): 'None', (14, 54, 14, 58): 'None', (14, 60, 14, 64): 'None', (14, 66, 14, 70): 'None'}, {}), '(self.transport, None, None, None, None)', False, 'from pandora.client import APIClient\n'), ((15, 36, 15, 42), 'pandora.py2compat.Mock', 'Mock', ({}, {}), '()', False, 'from pandora.py2compat import Mock, patch\n'), ((18, 13, 18, 52), 'pandora.py2compat.patch.object', 'patch.object', ({(18, 26, 18, 35): 'APIClient', (18, 37, 18, 51): '"""get_playlist"""'}, {}), "(APIClient, 'get_playlist')", False, 'from pandora.py2compat import Mock, patch\n'), ((21, 26, 21, 88), 'pandora.models.pandora.Station.from_json', 'Station.from_json', ({(21, 44, 21, 55): 'self.client', (21, 57, 21, 87): "{'stationToken': 'token_mock'}"}, {}), "(self.client, {'stationToken': 'token_mock'})", False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), ((22, 26, 22, 82), 'pandora.models.pandora.AdItem.from_json', 'AdItem.from_json', ({(22, 43, 22, 54): 'self.client', (22, 56, 22, 81): "{'station_id': 'id_mock'}"}, {}), "(self.client, {'station_id': 'id_mock'})", False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), ((25, 31, 25, 68), 'pydora.utils.iterate_forever', 'iterate_forever', ({(25, 47, 25, 67): 'station.get_playlist'}, {}), '(station.get_playlist)', False, 'from pydora.utils import iterate_forever\n'), ((34, 30, 34, 92), 'pandora.models.pandora.Station.from_json', 'Station.from_json', ({(34, 48, 34, 59): 'self.client', (34, 61, 34, 91): "{'stationToken': 'token_mock'}"}, {}), "(self.client, {'stationToken': 'token_mock'})", False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), ((35, 33, 35, 93), 'pandora.models.pandora.PlaylistItem.from_json', 'PlaylistItem.from_json', ({(35, 56, 35, 67): 'self.client', (35, 69, 35, 92): "{'token': 'token_mock'}"}, {}), "(self.client, {'token': 'token_mock'})", False, 'from pandora.models.pandora import Station, AdItem, PlaylistItem\n'), ((38, 35, 38, 72), 'pydora.utils.iterate_forever', 'iterate_forever', ({(38, 51, 38, 71): 'station.get_playlist'}, {}), '(station.get_playlist)', False, 'from pydora.utils import iterate_forever\n'), ((13, 43, 13, 61), 'pandora.errors.InvalidAuthToken', 'InvalidAuthToken', ({}, {}), '()', False, 'from pandora.errors import InvalidAuthToken, ParameterMissing\n'), ((31, 65, 31, 101), 'pandora.errors.ParameterMissing', 'ParameterMissing', ({(31, 82, 31, 100): '"""ParameterMissing"""'}, {}), "('ParameterMissing')", False, 'from pandora.errors import InvalidAuthToken, ParameterMissing\n'), ((19, 68, 19, 104), 'pandora.errors.ParameterMissing', 'ParameterMissing', ({(19, 85, 19, 103): '"""ParameterMissing"""'}, {}), "('ParameterMissing')", False, 'from pandora.errors import InvalidAuthToken, ParameterMissing\n')]