|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +from copy import deepcopy |
| 3 | +from urllib.parse import parse_qs, urlsplit |
| 4 | + |
| 5 | +import pytest |
| 6 | +import trakt |
2 | 7 | from trakt.movies import Movie |
3 | 8 | from trakt.people import Person |
4 | 9 | from trakt.tv import TVEpisode, TVSeason, TVShow |
@@ -132,6 +137,67 @@ def test_get_watched_movies(): |
132 | 137 | assert all([isinstance(m, Movie) for m in watched_movies]) |
133 | 138 |
|
134 | 139 |
|
| 140 | +def test_watched_movies_pagination(): |
| 141 | + sean = User('sean') |
| 142 | + client = trakt.core.api() |
| 143 | + request_calls = [] |
| 144 | + original_request = client.request |
| 145 | + |
| 146 | + def request(method, uri, data=None): |
| 147 | + request_calls.append((method, uri, data)) |
| 148 | + if uri.startswith('users/sean/watched/movies?'): |
| 149 | + query = parse_qs(urlsplit(f'https://api.trakt.tv/{uri}').query) |
| 150 | + if query.get('limit') == ['1']: |
| 151 | + response = original_request('GET', 'users/sean/watched/movies') |
| 152 | + return deepcopy(response[:1]) |
| 153 | + if uri == 'users/sean/watched/movies': |
| 154 | + response = original_request('GET', 'users/sean/watched/movies') |
| 155 | + return deepcopy(response) |
| 156 | + return original_request(method, uri, data) |
| 157 | + |
| 158 | + client.request = request |
| 159 | + try: |
| 160 | + expected_uri = 'users/sean/watched/movies?page=2&limit=1' |
| 161 | + watched_movies = sean.get_watched_movies(page=2, limit=1) |
| 162 | + assert request_calls[-1] == ('get', expected_uri, None) |
| 163 | + assert len(watched_movies) == 1 |
| 164 | + assert all([isinstance(movie, Movie) for movie in watched_movies]) |
| 165 | + |
| 166 | + watched_movies = sean.get_watched_movies(limit=1) |
| 167 | + expected_uri = 'users/sean/watched/movies?limit=1' |
| 168 | + assert request_calls[-1] == ('get', expected_uri, None) |
| 169 | + assert len(watched_movies) == 1 |
| 170 | + assert all([isinstance(movie, Movie) for movie in watched_movies]) |
| 171 | + |
| 172 | + watched_movies = sean.watched_movies |
| 173 | + assert request_calls[-1] == ('get', 'users/sean/watched/movies', None) |
| 174 | + assert all([isinstance(movie, Movie) for movie in watched_movies]) |
| 175 | + finally: |
| 176 | + client.request = original_request |
| 177 | + |
| 178 | + |
| 179 | +def test_watched_movies_pagination_validation(): |
| 180 | + sean = User('sean') |
| 181 | + |
| 182 | + with pytest.raises(ValueError, match='page must be a positive integer'): |
| 183 | + sean.get_watched_movies(page=0) |
| 184 | + |
| 185 | + with pytest.raises(ValueError, match='page must be a positive integer'): |
| 186 | + sean.get_watched_movies(page=-1) |
| 187 | + |
| 188 | + with pytest.raises(ValueError, match='page must be a valid integer'): |
| 189 | + sean.get_watched_movies(page='invalid') |
| 190 | + |
| 191 | + with pytest.raises(ValueError, match='limit must be a positive integer'): |
| 192 | + sean.get_watched_movies(limit=0) |
| 193 | + |
| 194 | + with pytest.raises(ValueError, match='limit must be a positive integer'): |
| 195 | + sean.get_watched_movies(limit=-1) |
| 196 | + |
| 197 | + with pytest.raises(ValueError, match='limit must be a valid integer'): |
| 198 | + sean.get_watched_movies(limit='invalid') |
| 199 | + |
| 200 | + |
135 | 201 | def test_stats(): |
136 | 202 | sean = User('sean') |
137 | 203 | assert isinstance(sean.get_stats(), dict) |
|
0 commit comments