1+ from trakt .pagination import paginate
2+
3+
4+ class FakeClient :
5+ def __init__ (self , pages ):
6+ self .pages = list (pages )
7+ self .urls = []
8+
9+ def get (self , url , include_headers = False ):
10+ self .urls .append ((url , include_headers ))
11+ return self .pages .pop (0 )
12+
13+
14+ def test_paginate_fetches_all_list_pages ():
15+ client = FakeClient ([
16+ ([{'title' : 'One' }], {'X-Pagination-Page-Count' : '3' }),
17+ ([{'title' : 'Two' }], {'X-Pagination-Page-Count' : '3' }),
18+ ([{'title' : 'Three' }], {'X-Pagination-Page-Count' : '3' }),
19+ ])
20+
21+ result = paginate (
22+ 'users/{user}/watched/movies' ,
23+ api = client ,
24+ user = 'sean' ,
25+ limit = 2 ,
26+ )
27+
28+ assert result == [
29+ {'title' : 'One' },
30+ {'title' : 'Two' },
31+ {'title' : 'Three' },
32+ ]
33+ assert client .urls == [
34+ ('users/sean/watched/movies?limit=2' , True ),
35+ ('users/sean/watched/movies?page=2&limit=2' , True ),
36+ ('users/sean/watched/movies?page=3&limit=2' , True ),
37+ ]
38+
39+
40+ def test_paginate_stops_after_one_page_without_valid_page_count ():
41+ client = FakeClient ([
42+ ([{'title' : 'One' }], {'X-Pagination-Page-Count' : 'invalid' }),
43+ ([{'title' : 'Two' }], {'X-Pagination-Page-Count' : '2' }),
44+ ])
45+
46+ result = paginate ('movies/popular' , api = client )
47+
48+ assert result == [{'title' : 'One' }]
49+ assert client .urls == [('movies/popular' , True )]
50+
51+
52+ def test_paginate_stops_after_one_page_without_page_count ():
53+ client = FakeClient ([
54+ ([{'title' : 'One' }], {}),
55+ ([{'title' : 'Two' }], {'X-Pagination-Page-Count' : '2' }),
56+ ])
57+
58+ result = paginate ('movies/popular' , api = client )
59+
60+ assert result == [{'title' : 'One' }]
61+ assert client .urls == [('movies/popular' , True )]
62+
63+
64+ def test_paginate_skips_none_extends_lists_and_appends_objects ():
65+ client = FakeClient ([
66+ (None , {'X-Pagination-Page-Count' : '3' }),
67+ ([{'title' : 'Two' }], {'X-Pagination-Page-Count' : '3' }),
68+ ({'title' : 'Three' }, {'X-Pagination-Page-Count' : '3' }),
69+ ])
70+
71+ result = paginate ('movies/popular' , api = client )
72+
73+ assert result == [{'title' : 'Two' }, {'title' : 'Three' }]
0 commit comments