<s> from __future__ import unicode_literals <EOL> from django . utils . six . moves . urllib . parse import parse_qs , urlsplit <EOL> from reviewboard . hostingsvcs . utils . paginator import ( APIPaginator , <EOL> InvalidPageError , <EOL> ProxyPaginator ) <EOL> from reviewboard . testing import TestCase <EOL> class DummyAPIPaginator ( APIPaginator ) : <EOL> start_query_param = '<STR_LIT:start>' <EOL> per_page_query_param = '<STR_LIT>' <EOL> def fetch_url ( self , url ) : <EOL> return { <EOL> '<STR_LIT:data>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : { } , <EOL> } <EOL> class APIPaginatorTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_construct_initial_load ( self ) : <EOL> """<STR_LIT>""" <EOL> paginator = DummyAPIPaginator ( None , '<STR_LIT>' , start = <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . page_data , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_construct_with_start ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' <EOL> paginator = DummyAPIPaginator ( None , url , start = <NUM_LIT:10> ) <EOL> parts = urlsplit ( paginator . url ) <EOL> query_params = parse_qs ( parts [ <NUM_LIT:3> ] ) <EOL> self . assertEqual ( query_params [ '<STR_LIT:foo>' ] , [ '<STR_LIT:1>' ] ) <EOL> self . assertEqual ( query_params [ '<STR_LIT:start>' ] , [ '<STR_LIT>' ] ) <EOL> def test_construct_with_per_page ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' <EOL> paginator = DummyAPIPaginator ( None , url , per_page = <NUM_LIT:10> ) <EOL> parts = urlsplit ( paginator . url ) <EOL> query_params = parse_qs ( parts [ <NUM_LIT:3> ] ) <EOL> self . assertEqual ( query_params [ '<STR_LIT:foo>' ] , [ '<STR_LIT:1>' ] ) <EOL> self . assertEqual ( query_params [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def test_extract_page_info ( self ) : <EOL> """<STR_LIT>""" <EOL> class PageInfoAPIPaginator ( APIPaginator ) : <EOL> def fetch_url ( self , url ) : <EOL> return { <EOL> '<STR_LIT:data>' : [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:100> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> paginator = PageInfoAPIPaginator ( None , '<STR_LIT>' ) <EOL> self . assertEqual ( paginator . page_data , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertEqual ( paginator . page_headers [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( paginator . per_page , <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . total_count , <NUM_LIT:100> ) <EOL> self . assertEqual ( paginator . prev_url , '<STR_LIT>' ) <EOL> self . assertEqual ( paginator . next_url , '<STR_LIT>' ) <EOL> def test_prev ( self ) : <EOL> """<STR_LIT>""" <EOL> prev_url = '<STR_LIT>' <EOL> paginator = DummyAPIPaginator ( None , '<STR_LIT>' ) <EOL> paginator . prev_url = prev_url <EOL> self . assertTrue ( paginator . has_prev ) <EOL> self . assertFalse ( paginator . has_next ) <EOL> data = paginator . prev ( ) <EOL> self . assertEqual ( data , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( paginator . url , prev_url ) <EOL> def test_prev_without_prev_page ( self ) : <EOL> """<STR_LIT>""" <EOL> paginator = DummyAPIPaginator ( None , '<STR_LIT>' ) <EOL> url = paginator . url <EOL> self . assertFalse ( paginator . has_prev ) <EOL> self . assertRaises ( InvalidPageError , paginator . prev ) <EOL> self . assertEqual ( paginator . url , url ) <EOL> def test_next ( self ) : <EOL> """<STR_LIT>""" <EOL> next_url = '<STR_LIT>' <EOL> paginator = DummyAPIPaginator ( None , '<STR_LIT>' ) <EOL> paginator . next_url = next_url <EOL> self . assertFalse ( paginator . has_prev ) <EOL> self . assertTrue ( paginator . has_next ) <EOL> data = paginator . next ( ) <EOL> self . assertEqual ( data , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( paginator . url , next_url ) <EOL> def test_next_without_next_page ( self ) : <EOL> """<STR_LIT>""" <EOL> paginator = DummyAPIPaginator ( None , '<STR_LIT>' ) <EOL> url = paginator . url <EOL> self . assertFalse ( paginator . has_next ) <EOL> self . assertRaises ( InvalidPageError , paginator . next ) <EOL> self . assertEqual ( paginator . url , url ) <EOL> class ProxyPaginatorTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . paginator = DummyAPIPaginator ( None , '<STR_LIT>' ) <EOL> self . proxy = ProxyPaginator ( self . paginator ) <EOL> def test_has_prev ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertFalse ( self . proxy . has_prev ) <EOL> self . paginator . prev_url = '<STR_LIT>' <EOL> self . assertTrue ( self . proxy . has_prev ) <EOL> def test_has_next ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertFalse ( self . proxy . has_next ) <EOL> self . paginator . next_url = '<STR_LIT>' <EOL> self . assertTrue ( self . proxy . has_next ) <EOL> def test_per_page ( self ) : <EOL> """<STR_LIT>""" <EOL> self . paginator . per_page = <NUM_LIT:10> <EOL> self . assertEqual ( self . proxy . per_page , <NUM_LIT:10> ) <EOL> def test_total_count ( self ) : <EOL> """<STR_LIT>""" <EOL> self . paginator . total_count = <NUM_LIT:100> <EOL> self . assertEqual ( self . proxy . total_count , <NUM_LIT:100> ) <EOL> def test_prev ( self ) : <EOL> """<STR_LIT>""" <EOL> prev_url = '<STR_LIT>' <EOL> self . paginator . prev_url = prev_url <EOL> self . assertTrue ( self . proxy . has_prev ) <EOL> self . assertFalse ( self . proxy . has_next ) <EOL> data = self . proxy . prev ( ) <EOL> self . assertEqual ( data , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( self . paginator . url , prev_url ) <EOL> def test_next ( self ) : <EOL> """<STR_LIT>""" <EOL> next_url = '<STR_LIT>' <EOL> self . paginator . next_url = next_url <EOL> self . assertFalse ( self . proxy . has_prev ) <EOL> self . assertTrue ( self . proxy . has_next ) <EOL> data = self . proxy . next ( ) <EOL> self . assertEqual ( data , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( self . paginator . url , next_url ) <EOL> def test_normalize_page_data ( self ) : <EOL> """<STR_LIT>""" <EOL> proxy = ProxyPaginator ( <EOL> self . paginator , <EOL> normalize_page_data_func = lambda data : list ( reversed ( data ) ) ) <EOL> self . assertEqual ( proxy . page_data , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> def test_normalize_page_data_on_prev ( self ) : <EOL> """<STR_LIT>""" <EOL> proxy = ProxyPaginator ( <EOL> self . paginator , <EOL> normalize_page_data_func = lambda data : list ( reversed ( data ) ) ) <EOL> self . paginator . prev_url = '<STR_LIT>' <EOL> data = proxy . prev ( ) <EOL> self . assertEqual ( data , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ) <EOL> def test_normalize_page_data_on_next ( self ) : <EOL> """<STR_LIT>""" <EOL> proxy = ProxyPaginator ( <EOL> self . paginator , <EOL> normalize_page_data_func = lambda data : list ( reversed ( data ) ) ) <EOL> self . paginator . next_url = '<STR_LIT>' <EOL> data = proxy . next ( ) <EOL> self . assertEqual ( data , [ <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ] ) </s>
