<s> __all__ = [ '<STR_LIT>' ] <EOL> import unittest <EOL> from iktomi . utils . paginator import Paginator , ModelPaginator , FancyPageRange , ChunkedPageRange <EOL> from iktomi . web . url import URL <EOL> from webob import Request <EOL> from webob . exc import HTTPNotFound <EOL> class MockQuery ( list ) : <EOL> def count ( self ) : <EOL> return len ( self ) <EOL> NONE = ( None , None ) <EOL> class PaginatorTests ( unittest . TestCase ) : <EOL> def test_page_object ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:0> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( bool ( paginator . next ) ) <EOL> self . assertEqual ( paginator . last . page , <NUM_LIT:1> ) <EOL> self . assertEqual ( paginator . last . url , '<STR_LIT>' ) <EOL> self . assertIsInstance ( paginator . last . url , URL ) <EOL> def test_empty ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:0> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . count , <NUM_LIT:0> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:1> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( paginator . prev , NONE ) <EOL> self . assertEqual ( paginator . next , NONE ) <EOL> self . assertEqual ( paginator . first , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . last , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . pages , [ ( <NUM_LIT:1> , '<STR_LIT>' ) ] ) <EOL> def test_no_limit ( self ) : <EOL> def pages ( paginator ) : <EOL> return [ x . page for x in paginator . pages ] <EOL> paginator = Paginator ( count = <NUM_LIT:10> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT:8> ) ) <EOL> self . assertEqual ( paginator . count , <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:1> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( paginator . prev , NONE ) <EOL> self . assertEqual ( paginator . next , NONE ) <EOL> self . assertEqual ( paginator . first , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . last , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( pages ( paginator ) , [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( paginator . items , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> def test_not_existing_page ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:8> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT:8> ) ) <EOL> self . assertEqual ( paginator . count , <NUM_LIT:8> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:3> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( paginator . prev , ( <NUM_LIT:2> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . next , NONE ) <EOL> self . assertEqual ( paginator . first , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . last , ( <NUM_LIT:2> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . pages , [ ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( paginator . items , [ ] ) <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:8> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . prev , ( <NUM_LIT:2> , '<STR_LIT>' ) ) <EOL> def test_last_page ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:5> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT:5> ) ) <EOL> self . assertEqual ( paginator . count , <NUM_LIT:5> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:2> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( paginator . prev , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . next , NONE ) <EOL> self . assertEqual ( paginator . first , ( <NUM_LIT:1> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . last , ( <NUM_LIT:2> , '<STR_LIT>' ) ) <EOL> self . assertEqual ( paginator . pages , [ ( <NUM_LIT:1> , '<STR_LIT>' ) , <EOL> ( <NUM_LIT:2> , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( paginator . items , [ <NUM_LIT:4> ] ) <EOL> def test_page_param ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:9> , <EOL> request = Request . blank ( '<STR_LIT>' ) , <EOL> page_param = '<STR_LIT:p>' ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT:5> ) ) <EOL> self . assertEqual ( paginator . next . url , '<STR_LIT>' ) <EOL> def test_enumerate ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , <EOL> count = <NUM_LIT:6> , <EOL> request = Request . blank ( '<STR_LIT>' ) , <EOL> page_param = '<STR_LIT:p>' ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT:6> ) ) <EOL> self . assertEqual ( list ( paginator . enumerate ( ) ) , [ <EOL> ( <NUM_LIT:5> , <NUM_LIT:4> ) , ( <NUM_LIT:6> , <NUM_LIT:5> ) , <EOL> ] ) <EOL> def test_model_paginator ( self ) : <EOL> query = MockQuery ( range ( <NUM_LIT:20> ) ) <EOL> paginator = ModelPaginator ( Request . blank ( '<STR_LIT>' ) , <EOL> query , <EOL> limit = <NUM_LIT:4> ) <EOL> self . assertEqual ( paginator . count , <NUM_LIT:20> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:2> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:5> ) <EOL> self . assertEqual ( paginator . items , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> self . assertEqual ( list ( paginator ) , [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ] ) <EOL> self . assertEqual ( len ( paginator ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( paginator [ <NUM_LIT:0> ] , <NUM_LIT:4> ) <EOL> def test_fancy ( self ) : <EOL> def pages ( paginator ) : <EOL> return [ x . page for x in paginator . pages ] <EOL> impl = FancyPageRange ( edge = <NUM_LIT:2> , surround = <NUM_LIT:3> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <EOL> <NUM_LIT:1> , <NUM_LIT:2> , None , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , None , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <EOL> <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , None , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <EOL> <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , None , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <EOL> <NUM_LIT:1> , <NUM_LIT:2> , None , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> impl = FancyPageRange ( edge = <NUM_LIT:2> , surround = <NUM_LIT:5> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <EOL> <NUM_LIT:1> , <NUM_LIT:2> , None , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> impl = FancyPageRange ( edge = <NUM_LIT:8> , surround = <NUM_LIT:2> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <EOL> <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> , None , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:100> ] ) <EOL> impl = FancyPageRange ( edge = <NUM_LIT:2> , surround = <NUM_LIT:5> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:2> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> def test_chunked ( self ) : <EOL> def pages ( paginator ) : <EOL> return [ x . page for x in paginator . pages ] <EOL> impl = ChunkedPageRange ( size = <NUM_LIT:3> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( paginator . chunk_size , <NUM_LIT:3> ) <EOL> self . assertEqual ( paginator . prev_chunk [ <NUM_LIT:0> ] , None ) <EOL> self . assertEqual ( paginator . next_chunk [ <NUM_LIT:0> ] , <NUM_LIT:4> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <NUM_LIT:100> ] ) <EOL> self . assertEqual ( paginator . chunk_size , <NUM_LIT:3> ) <EOL> self . assertEqual ( paginator . prev_chunk [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> self . assertEqual ( paginator . next_chunk [ <NUM_LIT:0> ] , None ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:100> , impl = impl ) <EOL> self . assertEqual ( pages ( paginator ) , [ <NUM_LIT:10> , <NUM_LIT:11> , <NUM_LIT:12> ] ) <EOL> self . assertEqual ( paginator . chunk_size , <NUM_LIT:3> ) <EOL> self . assertEqual ( paginator . prev_chunk [ <NUM_LIT:0> ] , <NUM_LIT:9> ) <EOL> self . assertEqual ( paginator . next_chunk [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_invalid_404 ( self ) : <EOL> def invalid_page ( ) : <EOL> raise HTTPNotFound ( ) <EOL> def make_paginator ( pg ) : <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' + pg ) , <EOL> limit = <NUM_LIT:1> , count = <NUM_LIT:10> , <EOL> invalid_page = invalid_page ) <EOL> paginator . page <EOL> self . assertRaises ( HTTPNotFound , make_paginator , '<STR_LIT>' ) <EOL> make_paginator ( '<STR_LIT:100>' ) <EOL> def test_invalid ( self ) : <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , limit = <NUM_LIT:1> , count = <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:1> ) <EOL> paginator = Paginator ( Request . blank ( '<STR_LIT>' ) , limit = <NUM_LIT:1> , count = <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:1> ) <EOL> def test_nonzero ( self ) : <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , count = <NUM_LIT:1> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( bool ( paginator ) ) <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , count = <NUM_LIT:1> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( bool ( paginator ) ) <EOL> paginator = Paginator ( limit = <NUM_LIT:4> , count = <NUM_LIT:10> , <EOL> request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( bool ( paginator ) ) <EOL> paginator = Paginator ( count = <NUM_LIT:1> , request = Request . blank ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( bool ( paginator ) ) <EOL> def test_orphans ( self ) : <EOL> request = Request . blank ( '<STR_LIT>' ) <EOL> paginator = Paginator ( request = request , <EOL> count = <NUM_LIT> , <EOL> limit = <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:11> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . slice ( range ( <NUM_LIT> ) ) , range ( <NUM_LIT> , <NUM_LIT:100> ) ) <EOL> paginator = Paginator ( request = request , <EOL> count = <NUM_LIT> , <EOL> orphans = <NUM_LIT:5> , <EOL> limit = <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . slice ( range ( <NUM_LIT> ) ) , range ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT> ) ) <EOL> self . assertEqual ( list ( paginator . enumerate ( ) ) , <EOL> zip ( range ( <NUM_LIT> , <NUM_LIT> ) , range ( <NUM_LIT> , <NUM_LIT> ) ) ) <EOL> paginator = Paginator ( request = request , <EOL> count = <NUM_LIT> , <EOL> orphans = <NUM_LIT:5> , <EOL> limit = <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . pages_count , <NUM_LIT:11> ) <EOL> self . assertEqual ( paginator . page , <NUM_LIT:10> ) <EOL> self . assertEqual ( paginator . slice ( range ( <NUM_LIT> ) ) , range ( <NUM_LIT> , <NUM_LIT:100> ) ) <EOL> paginator . items = paginator . slice ( range ( <NUM_LIT> ) ) <EOL> self . assertEqual ( list ( paginator . enumerate ( ) ) , <EOL> zip ( range ( <NUM_LIT> , <NUM_LIT> ) , range ( <NUM_LIT> , <NUM_LIT:100> ) ) ) </s>
