<s> import pytest <EOL> from mock import Mock , MagicMock , patch , call , PropertyMock <EOL> from nefertari . view import ( <EOL> BaseView , error_view , key_error_view , value_error_view ) <EOL> from nefertari . utils import dictset <EOL> from nefertari . json_httpexceptions import ( <EOL> JHTTPBadRequest , JHTTPNotFound , JHTTPMethodNotAllowed ) <EOL> from nefertari . wrappers import wrap_me , ValidationError , ResourceNotFound <EOL> from nefertari . renderers import _JSONEncoder <EOL> class DummyBaseView ( BaseView ) : <EOL> _json_encoder = _JSONEncoder <EOL> class TestViewMapper ( object ) : <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_trigger_events_called ( self , mock_manager ) : <EOL> from nefertari . view import ViewMapper <EOL> class MyView ( object ) : <EOL> Model = Mock <EOL> def __init__ ( self , ctx , req ) : <EOL> self . _before_calls = { } <EOL> self . _after_calls = { } <EOL> self . _json_params = { } <EOL> self . context = '<STR_LIT:foo>' <EOL> def index ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> request = MagicMock ( <EOL> json = { '<STR_LIT:username>' : '<STR_LIT>' } , <EOL> body = '<STR_LIT>' ) <EOL> resource = MagicMock ( actions = [ '<STR_LIT:index>' ] ) <EOL> wrapper = ViewMapper ( ** { '<STR_LIT>' : '<STR_LIT:index>' } ) ( MyView ) <EOL> wrapper ( resource , request ) <EOL> assert mock_manager . called <EOL> def test_viewmapper ( self ) : <EOL> from nefertari . view import ViewMapper <EOL> bc1 = Mock ( ) <EOL> bc3 = Mock ( ) <EOL> bc2 = Mock ( ) <EOL> class MyView ( object ) : <EOL> Model = Mock <EOL> _response = None <EOL> def __init__ ( self , ctx , req ) : <EOL> self . _before_calls = { '<STR_LIT:index>' : [ bc1 ] , '<STR_LIT>' : [ bc3 ] } <EOL> self . _after_calls = { '<STR_LIT>' : [ bc2 ] } <EOL> self . _json_params = { } <EOL> self . context = '<STR_LIT:foo>' <EOL> self . request = Mock ( action = '<STR_LIT:index>' ) <EOL> @ wrap_me ( before = bc2 ) <EOL> def index ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> request = MagicMock ( ) <EOL> resource = MagicMock ( actions = [ '<STR_LIT:index>' ] ) <EOL> wrapper = ViewMapper ( ** { '<STR_LIT>' : '<STR_LIT:index>' } ) ( MyView ) <EOL> result = wrapper ( resource , request ) <EOL> assert request . filters == { '<STR_LIT>' : [ bc2 ] } <EOL> assert request . action == '<STR_LIT:index>' <EOL> assert result == [ '<STR_LIT>' ] <EOL> bc1 . assert_called_with ( request = request ) <EOL> assert not bc2 . called <EOL> assert not bc3 . called <EOL> def test_viewmapper_bad_request ( self ) : <EOL> from nefertari . view import ViewMapper <EOL> bc1 = Mock ( side_effect = ValidationError ) <EOL> class MyView ( object ) : <EOL> Model = Mock <EOL> def __init__ ( self , ctx , req ) : <EOL> self . _before_calls = { '<STR_LIT:index>' : [ bc1 ] } <EOL> self . _after_calls = { } <EOL> def index ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> request = Mock ( ) <EOL> resource = Mock ( actions = [ '<STR_LIT:index>' ] ) <EOL> wrapper = ViewMapper ( ** { '<STR_LIT>' : '<STR_LIT:index>' } ) ( MyView ) <EOL> with pytest . raises ( JHTTPBadRequest ) : <EOL> wrapper ( resource , request ) <EOL> def test_viewmapper_not_found ( self ) : <EOL> from nefertari . view import ViewMapper <EOL> bc1 = Mock ( side_effect = ResourceNotFound ) <EOL> class MyView ( object ) : <EOL> Model = '<STR_LIT:foo>' <EOL> def __init__ ( self , ctx , req ) : <EOL> self . _before_calls = { '<STR_LIT:index>' : [ bc1 ] } <EOL> self . _after_calls = { } <EOL> self . _json_params = { } <EOL> self . context = '<STR_LIT:foo>' <EOL> self . request = Mock ( action = '<STR_LIT:index>' ) <EOL> def index ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> request = Mock ( ) <EOL> resource = Mock ( actions = [ '<STR_LIT:index>' ] ) <EOL> wrapper = ViewMapper ( ** { '<STR_LIT>' : '<STR_LIT:index>' } ) ( MyView ) <EOL> with pytest . raises ( JHTTPNotFound ) : <EOL> wrapper ( resource , request ) <EOL> class TestBaseView ( object ) : <EOL> def get_common_mock_request ( self ) : <EOL> return Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] , user = None ) <EOL> def test_baseview ( self , * a ) : <EOL> class UsersView ( BaseView ) : <EOL> _json_encoder = _JSONEncoder <EOL> def __init__ ( self , context , request ) : <EOL> BaseView . __init__ ( self , context , request ) <EOL> self . _json_params = { } <EOL> self . context = '<STR_LIT:foo>' <EOL> self . request = Mock ( action = '<STR_LIT:index>' ) <EOL> def show ( self , id ) : <EOL> return '<STR_LIT>' <EOL> def convert_ids2objects ( self , * args , ** kwargs ) : <EOL> pass <EOL> request = MagicMock ( content_type = '<STR_LIT>' ) <EOL> request . matched_route . pattern = '<STR_LIT>' <EOL> view = UsersView ( request . context , request ) <EOL> assert '<STR_LIT>' == view . show ( <NUM_LIT:1> ) <EOL> with pytest . raises ( JHTTPMethodNotAllowed ) : <EOL> view . index ( ) <EOL> with pytest . raises ( AttributeError ) : <EOL> view . frobnicate ( ) <EOL> with pytest . raises ( JHTTPMethodNotAllowed ) : <EOL> view . delete ( ) <EOL> def test_convert_dotted ( self ) : <EOL> converted = BaseView . convert_dotted ( { <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT>' : '<STR_LIT:value>' , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> } ) <EOL> assert sorted ( converted . keys ( ) ) == sorted ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert converted [ '<STR_LIT>' ] == { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> assert converted [ '<STR_LIT>' ] == '<STR_LIT:value>' <EOL> assert converted [ '<STR_LIT>' ] == { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:4> } , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> } , <EOL> } <EOL> assert '<STR_LIT>' not in converted <EOL> def test_convert_dotted_no_dotted ( self ) : <EOL> converted = BaseView . convert_dotted ( { <EOL> '<STR_LIT>' : '<STR_LIT:value>' <EOL> } ) <EOL> assert converted == { '<STR_LIT>' : '<STR_LIT:value>' } <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_init ( self , run ) : <EOL> request = Mock ( <EOL> content_type = '<STR_LIT:application/json>' , <EOL> json = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> method = '<STR_LIT:POST>' , <EOL> accept = [ '<STR_LIT>' ] , <EOL> ) <EOL> request . params . mixed . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = DummyBaseView ( context = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , request = request ) <EOL> run . assert_called_once_with ( ) <EOL> assert request . override_renderer == '<STR_LIT>' <EOL> assert list ( sorted ( view . _params . keys ( ) ) ) == [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert view . _params [ '<STR_LIT>' ] == { '<STR_LIT:foo>' : '<STR_LIT>' } <EOL> assert view . _params [ '<STR_LIT>' ] == { '<STR_LIT:foo>' : '<STR_LIT>' } <EOL> assert view . _params [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> assert view . request == request <EOL> assert view . context == { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> assert view . _before_calls == { } <EOL> assert view . _after_calls == { } <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_init_json_accept_header ( self , run ) : <EOL> request = Mock ( <EOL> content_type = '<STR_LIT:application/json>' , <EOL> json = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> method = '<STR_LIT:POST>' , <EOL> accept = [ '<STR_LIT:application/json>' ] , <EOL> ) <EOL> request . params . mixed . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> DummyBaseView ( context = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , request = request ) <EOL> assert request . override_renderer == '<STR_LIT>' <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_init_text_ct_and_accept ( self , run ) : <EOL> request = Mock ( <EOL> content_type = '<STR_LIT>' , <EOL> json = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> method = '<STR_LIT:POST>' , <EOL> accept = [ '<STR_LIT>' ] , <EOL> ) <EOL> request . params . mixed . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = DummyBaseView ( context = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , request = request ) <EOL> assert request . override_renderer == '<STR_LIT:string>' <EOL> assert list ( view . _params . keys ( ) ) == [ '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_init_json_error ( self , run ) : <EOL> import simplejson <EOL> request = Mock ( <EOL> content_type = '<STR_LIT:application/json>' , <EOL> method = '<STR_LIT:POST>' , <EOL> accept = [ '<STR_LIT:application/json>' ] , <EOL> ) <EOL> type ( request ) . json = PropertyMock ( <EOL> side_effect = simplejson . JSONDecodeError ( <EOL> '<STR_LIT:foo>' , '<STR_LIT>' , pos = <NUM_LIT:1> ) ) <EOL> request . params . mixed . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> view = DummyBaseView ( context = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , request = request ) <EOL> assert request . override_renderer == '<STR_LIT>' <EOL> assert list ( view . _params . keys ( ) ) == [ '<STR_LIT>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_run_init_actions ( self , limit , conv , setpub ) : <EOL> request = Mock ( <EOL> content_type = '<STR_LIT>' , <EOL> json = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> method = '<STR_LIT:POST>' , <EOL> accept = [ '<STR_LIT>' ] , <EOL> ) <EOL> request . params . mixed . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> DummyBaseView ( context = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , request = request ) <EOL> limit . assert_called_once_with ( ) <EOL> conv . assert_called_once_with ( ) <EOL> setpub . assert_called_once_with ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_aggregation_es_disabled ( self , aggregator , mock_es ) : <EOL> mock_es . settings = dictset ( enable_aggregations = False ) <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> view = DummyBaseView ( context = { } , request = request , <EOL> _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . index = <NUM_LIT:1> <EOL> view . _setup_aggregation ( ) <EOL> assert view . index == <NUM_LIT:1> <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_aggregation_index_not_defined ( self , aggregator , mock_es ) : <EOL> mock_es . settings = dictset ( enable_aggregations = True ) <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> view = DummyBaseView ( context = { } , request = request , <EOL> _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> assert view . index == view . not_allowed_action <EOL> view . _setup_aggregation ( ) <EOL> with pytest . raises ( JHTTPMethodNotAllowed ) : <EOL> view . index ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_aggregation ( self , aggregator , mock_es ) : <EOL> mock_es . settings = dictset ( enable_aggregations = True ) <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> view = DummyBaseView ( context = { } , request = request , <EOL> _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> type ( view ) . index = <NUM_LIT:1> <EOL> view . _setup_aggregation ( ) <EOL> aggregator . assert_called_once_with ( view ) <EOL> aggregator ( ) . wrap . assert_called_once_with ( <NUM_LIT:1> ) <EOL> assert view . index == aggregator ( ) . wrap ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_collection_es ( self , mock_es ) : <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , <EOL> _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . Model = Mock ( __name__ = '<STR_LIT>' ) <EOL> view . _query_params [ '<STR_LIT:q>' ] = '<STR_LIT>' <EOL> result = view . get_collection_es ( ) <EOL> mock_es . assert_called_once_with ( '<STR_LIT>' ) <EOL> mock_es ( ) . get_collection . assert_called_once_with ( <EOL> foo = '<STR_LIT:bar>' , q = '<STR_LIT>' ) <EOL> assert result == mock_es ( ) . get_collection ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_fill_null_values ( self , run ) : <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , <EOL> _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . Model = Mock ( ) <EOL> view . Model . get_null_values . return_value = { <EOL> '<STR_LIT:name>' : None , '<STR_LIT:email>' : <NUM_LIT:1> , '<STR_LIT:foo>' : None } <EOL> view . _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> view . fill_null_values ( ) <EOL> assert view . _json_params == { <EOL> '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT:name>' : None , '<STR_LIT:email>' : <NUM_LIT:1> <EOL> } <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_init_no_root ( self , run ) : <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> kwargs = dict ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view = DummyBaseView ( ** kwargs ) <EOL> view . root_resource = None <EOL> view . __init__ ( ** kwargs ) <EOL> assert not view . _auth_enabled <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_set_public_limits_no_auth ( self , run , wrap ) : <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] ) <EOL> kwargs = dict ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view = DummyBaseView ( ** kwargs ) <EOL> view . _auth_enabled = False <EOL> view . set_public_limits ( ) <EOL> assert not wrap . set_public_limits . called <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_set_public_limits_user_authenticated ( self , run , wrap ) : <EOL> request = Mock ( content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] , user = '<STR_LIT:foo>' ) <EOL> kwargs = dict ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view = DummyBaseView ( ** kwargs ) <EOL> view . _auth_enabled = True <EOL> view . set_public_limits ( ) <EOL> assert not wrap . set_public_limits . called <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_set_public_limits_applied ( self , run , wrap ) : <EOL> request = self . get_common_mock_request ( ) <EOL> kwargs = dict ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view = DummyBaseView ( ** kwargs ) <EOL> view . _auth_enabled = True <EOL> view . set_public_limits ( ) <EOL> wrap . set_public_limits . assert_called_once_with ( view ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_convert_ids2objects_non_relational ( self , run , id2obj , eng ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT>' : '<STR_LIT:bar>' } , <EOL> _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . Model = '<STR_LIT>' <EOL> eng . is_relationship_field . return_value = False <EOL> view . convert_ids2objects ( ) <EOL> eng . is_relationship_field . assert_called_once_with ( '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> assert not id2obj . called <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_convert_ids2objects_relational ( self , run , id2obj , eng ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT>' : '<STR_LIT:bar>' } , <EOL> _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . Model = '<STR_LIT>' <EOL> eng . is_relationship_field . return_value = True <EOL> view . convert_ids2objects ( ) <EOL> eng . get_relationship_cls . assert_called_once_with ( '<STR_LIT:foo>' , '<STR_LIT>' ) <EOL> id2obj . assert_called_once_with ( '<STR_LIT:foo>' , eng . get_relationship_cls ( ) ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_default_wrappers_with_auth ( self , run , wrap ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . _auth_enabled = True <EOL> view . setup_default_wrappers ( ) <EOL> assert len ( view . _after_calls [ '<STR_LIT:index>' ] ) == <NUM_LIT:5> <EOL> assert len ( view . _after_calls [ '<STR_LIT>' ] ) == <NUM_LIT:4> <EOL> assert len ( view . _after_calls [ '<STR_LIT>' ] ) == <NUM_LIT:4> <EOL> assert len ( view . _after_calls [ '<STR_LIT>' ] ) == <NUM_LIT:4> <EOL> assert len ( view . _after_calls [ '<STR_LIT:replace>' ] ) == <NUM_LIT:4> <EOL> assert wrap . apply_privacy . call_count == <NUM_LIT:5> <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_setup_default_wrappers_no_auth ( self , run , wrap ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . _auth_enabled = False <EOL> view . setup_default_wrappers ( ) <EOL> assert len ( view . _after_calls [ '<STR_LIT:index>' ] ) == <NUM_LIT:4> <EOL> assert len ( view . _after_calls [ '<STR_LIT>' ] ) == <NUM_LIT:3> <EOL> assert not wrap . apply_privacy . called <EOL> def test_defalt_wrappers_and_wrap_me ( self ) : <EOL> from nefertari import wrappers <EOL> self . maxDiff = None <EOL> def before_call ( * a ) : <EOL> return a [ <NUM_LIT:2> ] <EOL> def after_call ( * a ) : <EOL> return a [ <NUM_LIT:2> ] <EOL> class MyView ( BaseView ) : <EOL> _json_encoder = _JSONEncoder <EOL> @ wrappers . wrap_me ( before = before_call , after = after_call ) <EOL> def index ( self ) : <EOL> return [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> def convert_ids2objects ( self , * args , ** kwargs ) : <EOL> pass <EOL> request = MagicMock ( content_type = '<STR_LIT>' ) <EOL> resource = MagicMock ( actions = [ '<STR_LIT:index>' ] ) <EOL> view = MyView ( resource , request ) <EOL> assert len ( view . _after_calls [ '<STR_LIT:index>' ] ) == <NUM_LIT:4> <EOL> assert len ( view . _after_calls [ '<STR_LIT>' ] ) == <NUM_LIT:3> <EOL> assert view . index . _before_calls == [ before_call ] <EOL> assert view . index . _after_calls == [ after_call ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_not_allowed_action ( self , run ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> with pytest . raises ( JHTTPMethodNotAllowed ) : <EOL> view . not_allowed_action ( ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_add_before_or_after_before ( self , run ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> callable_ = lambda x : x <EOL> view . add_before_or_after_call ( <EOL> action = '<STR_LIT:foo>' , _callable = callable_ , pos = None , before = True ) <EOL> assert callable_ in view . _before_calls [ '<STR_LIT:foo>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_add_before_or_after_after ( self , run ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> callable_ = lambda x : x <EOL> view . add_before_or_after_call ( <EOL> action = '<STR_LIT:foo>' , _callable = callable_ , pos = None , before = False ) <EOL> assert callable_ in view . _after_calls [ '<STR_LIT:foo>' ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_add_before_or_after_position ( self , run ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> callable1 = lambda x : x <EOL> callable2 = lambda x : x + x <EOL> view . add_before_or_after_call ( <EOL> action = '<STR_LIT:foo>' , _callable = callable1 , pos = None , <EOL> before = False ) <EOL> assert callable1 is view . _after_calls [ '<STR_LIT:foo>' ] [ <NUM_LIT:0> ] <EOL> view . add_before_or_after_call ( <EOL> action = '<STR_LIT:foo>' , _callable = callable2 , pos = <NUM_LIT:0> , <EOL> before = False ) <EOL> assert callable2 is view . _after_calls [ '<STR_LIT:foo>' ] [ <NUM_LIT:0> ] <EOL> assert callable1 is view . _after_calls [ '<STR_LIT:foo>' ] [ <NUM_LIT:1> ] <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_add_before_or_after_not_callable ( self , run ) : <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> with pytest . raises ( ValueError ) as ex : <EOL> view . add_before_or_after_call ( <EOL> action = '<STR_LIT:foo>' , _callable = '<STR_LIT>' , pos = None , <EOL> before = False ) <EOL> assert str ( ex . value ) == '<STR_LIT>' <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_subrequest_get ( self , run , req , ulib ) : <EOL> request = Mock ( <EOL> content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] , user = None , <EOL> cookies = [ '<STR_LIT:1>' ] ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . subrequest ( url = '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT>' } , method = '<STR_LIT:GET>' ) <EOL> req . blank . assert_called_once_with ( <EOL> '<STR_LIT>' , cookies = [ '<STR_LIT:1>' ] , content_type = '<STR_LIT:application/json>' , <EOL> method = '<STR_LIT:GET>' ) <EOL> view . request . invoke_subrequest . assert_called_once_with ( req . blank ( ) ) <EOL> ulib . parse . urlencode . assert_called_once_with ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_subrequest_post ( self , run , req , json ) : <EOL> request = Mock ( <EOL> content_type = '<STR_LIT>' , method = '<STR_LIT>' , accept = [ '<STR_LIT>' ] , user = None , <EOL> cookies = [ '<STR_LIT:1>' ] ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _query_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> view . subrequest ( url = '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT>' } , method = '<STR_LIT:POST>' ) <EOL> req . blank . assert_called_once_with ( <EOL> '<STR_LIT>' , cookies = [ '<STR_LIT:1>' ] , content_type = '<STR_LIT:application/json>' , <EOL> method = '<STR_LIT:POST>' ) <EOL> view . request . invoke_subrequest . assert_called_once_with ( req . blank ( ) ) <EOL> json . dumps . assert_called_once_with ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj ( self , run ) : <EOL> model = Mock ( ) <EOL> model . pk_field . return_value = '<STR_LIT>' <EOL> model . get_item . return_value = '<STR_LIT:foo>' <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . _json_params [ '<STR_LIT:user>' ] = '<STR_LIT:1>' <EOL> view . id2obj ( name = '<STR_LIT:user>' , model = model ) <EOL> assert view . _json_params [ '<STR_LIT:user>' ] == '<STR_LIT:foo>' <EOL> model . pk_field . assert_called_once_with ( ) <EOL> model . get_item . assert_called_once_with ( <EOL> idname = '<STR_LIT:1>' , _raise_on_empty = False ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj_list ( self , run ) : <EOL> model = Mock ( ) <EOL> model . pk_field . return_value = '<STR_LIT>' <EOL> model . get_item . return_value = '<STR_LIT:foo>' <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . _json_params [ '<STR_LIT:user>' ] = [ '<STR_LIT:1>' ] <EOL> view . id2obj ( name = '<STR_LIT:user>' , model = model ) <EOL> assert view . _json_params [ '<STR_LIT:user>' ] == [ '<STR_LIT:foo>' ] <EOL> model . pk_field . assert_called_once_with ( ) <EOL> model . get_item . assert_called_once_with ( <EOL> idname = '<STR_LIT:1>' , _raise_on_empty = False ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj_not_in_params ( self , run ) : <EOL> model = Mock ( ) <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . id2obj ( name = '<STR_LIT>' , model = model ) <EOL> assert not model . pk_field . called <EOL> assert not model . get_item . called <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj_setdefault ( self , run ) : <EOL> model = Mock ( ) <EOL> model . pk_field . return_value = '<STR_LIT>' <EOL> model . get_item . return_value = None <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . _json_params [ '<STR_LIT:user>' ] = '<STR_LIT:1>' <EOL> view . id2obj ( name = '<STR_LIT:user>' , model = model , setdefault = <NUM_LIT> ) <EOL> assert view . _json_params [ '<STR_LIT:user>' ] == <NUM_LIT> <EOL> model . pk_field . assert_called_once_with ( ) <EOL> model . get_item . assert_called_once_with ( <EOL> idname = '<STR_LIT:1>' , _raise_on_empty = False ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj_value_none ( self , run ) : <EOL> model = Mock ( ) <EOL> model . pk_field . return_value = '<STR_LIT>' <EOL> model . get_item . return_value = '<STR_LIT:foo>' <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . _json_params [ '<STR_LIT>' ] = [ None , '<STR_LIT:1>' ] <EOL> view . _json_params [ '<STR_LIT>' ] = None <EOL> view . id2obj ( name = '<STR_LIT>' , model = model ) <EOL> view . id2obj ( name = '<STR_LIT>' , model = model ) <EOL> assert view . _json_params [ '<STR_LIT>' ] == [ None , '<STR_LIT:foo>' ] <EOL> assert view . _json_params [ '<STR_LIT>' ] is None <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj_already_object ( self , run ) : <EOL> id_ = Mock ( ) <EOL> model = Mock ( ) <EOL> model . pk_field . return_value = '<STR_LIT>' <EOL> model . get_item . return_value = None <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . _json_params [ '<STR_LIT:user>' ] = id_ <EOL> view . id2obj ( name = '<STR_LIT:user>' , model = model , setdefault = <NUM_LIT> ) <EOL> assert view . _json_params [ '<STR_LIT:user>' ] == id_ <EOL> model . pk_field . assert_called_once_with ( ) <EOL> assert not model . get_item . called <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_id2obj_not_found ( self , run ) : <EOL> model = Mock ( ) <EOL> model . pk_field . return_value = '<STR_LIT>' <EOL> model . get_item . return_value = None <EOL> request = self . get_common_mock_request ( ) <EOL> view = DummyBaseView ( <EOL> context = { } , request = request , _json_params = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> _query_params = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> view . _json_params [ '<STR_LIT:user>' ] = '<STR_LIT:1>' <EOL> with pytest . raises ( JHTTPBadRequest ) as ex : <EOL> view . id2obj ( name = '<STR_LIT:user>' , model = model ) <EOL> assert str ( ex . value ) == '<STR_LIT>' <EOL> class TestViewHelpers ( object ) : <EOL> def test_key_error_view ( self ) : <EOL> resp = key_error_view ( Mock ( args = ( '<STR_LIT:foo>' , ) ) , None ) <EOL> assert str ( resp . message ) == "<STR_LIT>" <EOL> def test_value_error_view ( self ) : <EOL> resp = value_error_view ( Mock ( args = ( '<STR_LIT:foo>' , ) ) , None ) <EOL> assert str ( resp . message ) == "<STR_LIT>" <EOL> def test_error_view ( self ) : <EOL> resp = error_view ( Mock ( args = ( '<STR_LIT:foo>' , ) ) , None ) <EOL> assert str ( resp . message ) == "<STR_LIT:foo>" <EOL> def test_includeme ( self ) : <EOL> from nefertari . view import includeme <EOL> config = Mock ( ) <EOL> includeme ( config ) <EOL> calls = [ <EOL> call ( key_error_view , context = KeyError ) , <EOL> call ( value_error_view , context = ValueError ) , <EOL> call ( error_view , context = Exception ) <EOL> ] <EOL> config . add_view . assert_has_calls ( calls , any_order = True ) </s>
