<s> import pretend <EOL> import pytest <EOL> from webob . multidict import MultiDict <EOL> from warehouse . utils import paginate <EOL> class FakeSuggestion : <EOL> def __init__ ( self , options ) : <EOL> self . options = options <EOL> class FakeSuggest : <EOL> def __init__ ( self , name_suggestion ) : <EOL> self . name_suggestion = name_suggestion <EOL> class FakeResult : <EOL> def __init__ ( self , data , total ) : <EOL> self . data = data <EOL> self . total = total <EOL> @ property <EOL> def hits ( self ) : <EOL> return pretend . stub ( total = self . total ) <EOL> def __iter__ ( self ) : <EOL> for i in self . data : <EOL> yield i <EOL> class FakeSuggestResult ( FakeResult ) : <EOL> def __init__ ( self , data , total , options ) : <EOL> super ( ) . __init__ ( data , total ) <EOL> self . options = options <EOL> @ property <EOL> def suggest ( self ) : <EOL> suggestion = FakeSuggestion ( options = self . options ) <EOL> return FakeSuggest ( name_suggestion = [ suggestion ] ) <EOL> class FakeQuery : <EOL> def __init__ ( self , fake ) : <EOL> self . fake = fake <EOL> self . range = slice ( None ) <EOL> def __getitem__ ( self , range ) : <EOL> self . range = range <EOL> return self <EOL> @ property <EOL> def results ( self ) : <EOL> return pretend . stub ( hits = pretend . stub ( total = len ( self . fake ) ) ) <EOL> def execute ( self ) : <EOL> return FakeResult ( self . fake [ self . range ] , len ( self . fake ) ) <EOL> class FakeSuggestQuery ( FakeQuery ) : <EOL> def __init__ ( self , fake , options ) : <EOL> super ( ) . __init__ ( fake ) <EOL> self . options = options <EOL> def execute ( self ) : <EOL> data = self . fake [ self . range ] <EOL> total = len ( self . fake ) <EOL> return FakeSuggestResult ( data , total , self . options ) <EOL> class TestElasticsearchWrapper : <EOL> def test_slices_and_length ( self ) : <EOL> wrapper = paginate . _ElasticsearchWrapper ( FakeQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> assert wrapper [ <NUM_LIT:1> : <NUM_LIT:3> ] == [ <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> assert len ( wrapper ) == <NUM_LIT:6> <EOL> def test_slice_start_clamps_to_max ( self ) : <EOL> wrapper = paginate . _ElasticsearchWrapper ( FakeQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> wrapper . max_results = <NUM_LIT:5> <EOL> assert wrapper [ <NUM_LIT:6> : <NUM_LIT:10> ] == [ ] <EOL> assert len ( wrapper ) == <NUM_LIT:5> <EOL> def test_slice_end_clamps_to_max ( self ) : <EOL> wrapper = paginate . _ElasticsearchWrapper ( FakeQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> wrapper . max_results = <NUM_LIT:5> <EOL> assert wrapper [ <NUM_LIT:1> : <NUM_LIT:10> ] == [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] <EOL> assert len ( wrapper ) == <NUM_LIT:5> <EOL> def test_second_slice_fails ( self ) : <EOL> wrapper = paginate . _ElasticsearchWrapper ( FakeQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> wrapper [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> with pytest . raises ( RuntimeError ) : <EOL> wrapper [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> def test_len_before_slice_fails ( self ) : <EOL> wrapper = paginate . _ElasticsearchWrapper ( FakeQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ) ) <EOL> with pytest . raises ( RuntimeError ) : <EOL> len ( wrapper ) <EOL> def test_best_guess_suggestion ( self ) : <EOL> fake_option = pretend . stub ( ) <EOL> query = FakeSuggestQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , options = [ fake_option ] ) <EOL> wrapper = paginate . _ElasticsearchWrapper ( query ) <EOL> wrapper [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> assert wrapper . best_guess == fake_option <EOL> def test_best_guess_suggestion_no_options ( self ) : <EOL> query = FakeSuggestQuery ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , options = [ ] ) <EOL> wrapper = paginate . _ElasticsearchWrapper ( query ) <EOL> wrapper [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> assert wrapper . best_guess is None <EOL> def test_elasticsearch_page_has_wrapper ( monkeypatch ) : <EOL> page_obj = pretend . stub ( ) <EOL> page_cls = pretend . call_recorder ( lambda * a , ** kw : page_obj ) <EOL> monkeypatch . setattr ( paginate , "<STR_LIT>" , page_cls ) <EOL> assert paginate . ElasticsearchPage ( "<STR_LIT>" , second = "<STR_LIT:foo>" ) is page_obj <EOL> assert page_cls . calls == [ <EOL> pretend . call ( <EOL> "<STR_LIT>" , <EOL> second = "<STR_LIT:foo>" , <EOL> wrapper_class = paginate . _ElasticsearchWrapper , <EOL> ) , <EOL> ] <EOL> def test_paginate_url ( pyramid_request ) : <EOL> pyramid_request . GET = MultiDict ( pyramid_request . GET ) <EOL> pyramid_request . GET [ "<STR_LIT:foo>" ] = "<STR_LIT:bar>" <EOL> url = pretend . stub ( ) <EOL> pyramid_request . current_route_path = pretend . call_recorder ( lambda _query : url ) <EOL> url_maker = paginate . paginate_url_factory ( pyramid_request ) <EOL> assert url_maker ( <NUM_LIT:5> ) is url <EOL> assert pyramid_request . current_route_path . calls == [ <EOL> pretend . call ( _query = [ ( "<STR_LIT:foo>" , "<STR_LIT:bar>" ) , ( "<STR_LIT>" , <NUM_LIT:5> ) ] ) , <EOL> ] </s>
