<s> from flask_resty import Api , GenericModelView <EOL> from marshmallow import fields , Schema <EOL> import pytest <EOL> from sqlalchemy import Column , Integer , String <EOL> import helpers <EOL> @ pytest . yield_fixture <EOL> def models ( db ) : <EOL> class Widget ( db . Model ) : <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( Integer , primary_key = True ) <EOL> name = Column ( String , nullable = False ) <EOL> description = Column ( String ) <EOL> db . create_all ( ) <EOL> yield { <EOL> '<STR_LIT>' : Widget , <EOL> } <EOL> db . drop_all ( ) <EOL> @ pytest . fixture <EOL> def schemas ( ) : <EOL> class WidgetSchema ( Schema ) : <EOL> id = fields . Integer ( as_string = True ) <EOL> name = fields . String ( required = True ) <EOL> description = fields . String ( ) <EOL> return { <EOL> '<STR_LIT>' : WidgetSchema ( ) , <EOL> } <EOL> @ pytest . fixture ( autouse = True ) <EOL> def routes ( app , models , schemas ) : <EOL> class WidgetViewBase ( GenericModelView ) : <EOL> model = models [ '<STR_LIT>' ] <EOL> schema = schemas [ '<STR_LIT>' ] <EOL> class WidgetListView ( WidgetViewBase ) : <EOL> def get ( self ) : <EOL> return self . list ( ) <EOL> def post ( self ) : <EOL> return self . create ( ) <EOL> class WidgetView ( WidgetViewBase ) : <EOL> def get ( self , id ) : <EOL> return self . retrieve ( id ) <EOL> def patch ( self , id ) : <EOL> return self . update ( id , partial = True ) <EOL> def delete ( self , id ) : <EOL> return self . destroy ( id ) <EOL> api = Api ( app ) <EOL> api . add_resource ( <EOL> '<STR_LIT>' , WidgetListView , WidgetView , id_rule = '<STR_LIT>' <EOL> ) <EOL> @ pytest . fixture ( autouse = True ) <EOL> def data ( db , models ) : <EOL> db . session . add_all ( ( <EOL> models [ '<STR_LIT>' ] ( name = "<STR_LIT>" , description = "<STR_LIT>" ) , <EOL> models [ '<STR_LIT>' ] ( name = "<STR_LIT>" , description = "<STR_LIT>" ) , <EOL> models [ '<STR_LIT>' ] ( name = "<STR_LIT>" , description = "<STR_LIT>" ) , <EOL> ) ) <EOL> db . session . commit ( ) <EOL> def test_list ( client ) : <EOL> response = client . get ( '<STR_LIT>' ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> assert helpers . get_data ( response ) == [ <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:2>' , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:3>' , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } , <EOL> ] <EOL> def test_retrieve ( client ) : <EOL> response = client . get ( '<STR_LIT>' ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> assert helpers . get_data ( response ) == { <EOL> '<STR_LIT:id>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } <EOL> def test_create ( client ) : <EOL> response = helpers . request ( <EOL> client , <EOL> '<STR_LIT:POST>' , '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } , <EOL> ) <EOL> assert response . status_code == <NUM_LIT> <EOL> assert response . headers [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> assert helpers . get_data ( response ) == { <EOL> '<STR_LIT:id>' : '<STR_LIT:4>' , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } <EOL> def test_update ( client ) : <EOL> update_response = helpers . request ( <EOL> client , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT:id>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } , <EOL> ) <EOL> assert update_response . status_code == <NUM_LIT> <EOL> retrieve_response = client . get ( '<STR_LIT>' ) <EOL> assert retrieve_response . status_code == <NUM_LIT:200> <EOL> assert helpers . get_data ( retrieve_response ) == { <EOL> '<STR_LIT:id>' : '<STR_LIT:1>' , <EOL> '<STR_LIT:name>' : "<STR_LIT>" , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> } <EOL> def test_destroy ( client ) : <EOL> destroy_response = client . delete ( '<STR_LIT>' ) <EOL> assert destroy_response . status_code == <NUM_LIT> <EOL> retrieve_response = client . get ( '<STR_LIT>' ) <EOL> assert retrieve_response . status_code == <NUM_LIT> </s>
