<s> from nose . tools import eq_ , ok_ <EOL> import wtforms <EOL> from flask import Flask <EOL> from flask_superadmin import Admin <EOL> from flask_superadmin . model import base <EOL> from flask . ext import wtf <EOL> class Model ( object ) : <EOL> def __init__ ( self , id = None , c1 = <NUM_LIT:1> , c2 = <NUM_LIT:2> , c3 = <NUM_LIT:3> ) : <EOL> self . id = id <EOL> self . col1 = c1 <EOL> self . col2 = c2 <EOL> self . col3 = c3 <EOL> DoesNotExist = '<STR_LIT>' <EOL> class Form ( wtf . Form ) : <EOL> col1 = wtforms . TextField ( ) <EOL> col2 = wtforms . TextField ( ) <EOL> col3 = wtforms . TextField ( ) <EOL> class MockModelView ( base . BaseModelAdmin ) : <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , model , name = None , category = None , endpoint = None , <EOL> url = None , ** kwargs ) : <EOL> for k , v in kwargs . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> super ( MockModelView , self ) . __init__ ( model , name , category , endpoint , url ) <EOL> self . created_models = [ ] <EOL> self . updated_models = [ ] <EOL> self . deleted_models = [ ] <EOL> self . search_arguments = [ ] <EOL> self . all_models = { <NUM_LIT:1> : Model ( <NUM_LIT:1> ) , <EOL> <NUM_LIT:2> : Model ( <NUM_LIT:2> ) } <EOL> self . last_id = <NUM_LIT:3> <EOL> def get_pk ( self , instance ) : <EOL> return instance . id <EOL> def get_object ( self , pk ) : <EOL> return self . all_models . get ( int ( pk ) ) <EOL> def get_objects ( self , * pks ) : <EOL> ret = [ ] <EOL> for pk in pks : <EOL> ret . append ( self . all_models . get ( int ( pk ) ) ) <EOL> return ret <EOL> def get_model_form ( self ) : <EOL> def fake_model_form ( * args , ** kwargs ) : <EOL> return Form <EOL> return fake_model_form <EOL> def get_converter ( self ) : <EOL> pass <EOL> def scaffold_list_columns ( self ) : <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if self . excluded_list_columns : <EOL> return filter ( lambda x : x not in self . excluded_list_columns , columns ) <EOL> return columns <EOL> def init_search ( self ) : <EOL> return bool ( self . searchable_columns ) <EOL> def scaffold_sortable_columns ( self ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def scaffold_form ( self ) : <EOL> return Form <EOL> def get_list ( self , page , sort , sort_desc , search_query ) : <EOL> self . search_arguments . append ( ( page , sort , sort_desc , search_query ) ) <EOL> return len ( self . all_models ) , self . all_models . itervalues ( ) <EOL> def save_model ( self , instance , form , adding = False ) : <EOL> if adding : <EOL> model = Model ( self . last_id ) <EOL> self . last_id += <NUM_LIT:1> <EOL> form . populate_obj ( model ) <EOL> self . created_models . append ( model ) <EOL> self . all_models [ model . id ] = model <EOL> else : <EOL> form . populate_obj ( instance ) <EOL> self . updated_models . append ( instance ) <EOL> return True <EOL> def update_model ( self , form , model ) : <EOL> return True <EOL> def delete_models ( self , * pks ) : <EOL> for pk in pks : <EOL> self . deleted_models . append ( self . all_models . get ( int ( pk ) ) ) <EOL> return True <EOL> def setup ( ) : <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = False <EOL> app . secret_key = '<STR_LIT:1>' <EOL> admin = Admin ( app ) <EOL> return app , admin <EOL> def test_mockview ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . model , Model ) <EOL> eq_ ( view . name , '<STR_LIT>' ) <EOL> eq_ ( view . url , '<STR_LIT>' ) <EOL> eq_ ( view . endpoint , '<STR_LIT>' ) <EOL> ok_ ( view . blueprint is not None ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( col1 = '<STR_LIT>' , col2 = '<STR_LIT>' , col3 = '<STR_LIT>' ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> eq_ ( len ( view . created_models ) , <NUM_LIT:1> ) <EOL> model = view . created_models . pop ( ) <EOL> eq_ ( model . id , <NUM_LIT:3> ) <EOL> eq_ ( model . col1 , '<STR_LIT>' ) <EOL> eq_ ( model . col2 , '<STR_LIT>' ) <EOL> eq_ ( model . col3 , '<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> ok_ ( '<STR_LIT>' in rv . data ) <EOL> rv = client . post ( '<STR_LIT>' , <EOL> data = dict ( col1 = '<STR_LIT>' , col2 = '<STR_LIT>' , col3 = '<STR_LIT>' ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> eq_ ( len ( view . updated_models ) , <NUM_LIT:1> ) <EOL> model = view . updated_models . pop ( ) <EOL> eq_ ( model . col1 , '<STR_LIT>' ) <EOL> eq_ ( model . col2 , '<STR_LIT>' ) <EOL> eq_ ( model . col3 , '<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> rv = client . post ( '<STR_LIT>' , data = dict ( confirm_delete = True ) ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> eq_ ( rv . headers [ '<STR_LIT:location>' ] , '<STR_LIT>' ) <EOL> def test_permissions ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> client = app . test_client ( ) <EOL> view . can_create = False <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> view . can_edit = False <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT:200> ) <EOL> eq_ ( rv . data . count ( '<STR_LIT>' ) , <NUM_LIT:3> ) <EOL> view . can_delete = False <EOL> rv = client . post ( '<STR_LIT>' ) <EOL> eq_ ( rv . status_code , <NUM_LIT> ) <EOL> def test_permissions_and_add_delete_buttons ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> client = app . test_client ( ) <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> ok_ ( '<STR_LIT>' in resp . data ) <EOL> view . can_create = False <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> view . can_edit = False <EOL> view . can_delete = False <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> view . can_edit = False <EOL> view . can_delete = True <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> ok_ ( '<STR_LIT>' in resp . data ) <EOL> view . can_edit = True <EOL> view . can_delete = False <EOL> resp = client . get ( '<STR_LIT>' ) <EOL> eq_ ( resp . status_code , <NUM_LIT:200> ) <EOL> ok_ ( '<STR_LIT>' in resp . data ) <EOL> ok_ ( '<STR_LIT>' in resp . data ) <EOL> ok_ ( '<STR_LIT>' not in resp . data ) <EOL> def test_templates ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model ) <EOL> admin . add_view ( view ) <EOL> client = app . test_client ( ) <EOL> view . list_template = '<STR_LIT>' <EOL> view . add_template = '<STR_LIT>' <EOL> view . edit_template = '<STR_LIT>' <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . data , '<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . data , '<STR_LIT>' ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> eq_ ( rv . data , '<STR_LIT>' ) <EOL> def test_list_display_header ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , list_display = [ '<STR_LIT>' ] ) <EOL> admin . add_view ( view ) <EOL> eq_ ( len ( view . list_display ) , <NUM_LIT:1> ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> ok_ ( '<STR_LIT>' in rv . data ) <EOL> def test_search_fields ( ) : <EOL> app , admin = setup ( ) <EOL> view = MockModelView ( Model , search_fields = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> admin . add_view ( view ) <EOL> eq_ ( view . search_fields , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> client = app . test_client ( ) <EOL> rv = client . get ( '<STR_LIT>' ) <EOL> ok_ ( '<STR_LIT>' in rv . data ) </s>
