<s> import unittest <EOL> from flask_sqlalchemy import SQLAlchemy <EOL> from sqlalchemy . orm import backref <EOL> from flask_potion . routes import Relation <EOL> from flask_potion . contrib . alchemy import SQLAlchemyManager <EOL> from flask_potion import Api , fields <EOL> from flask_potion . resource import ModelResource <EOL> from tests import BaseTestCase <EOL> class SQLAlchemyTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SQLAlchemyTestCase , self ) . setUp ( ) <EOL> self . app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . api = Api ( self . app , default_manager = SQLAlchemyManager ) <EOL> self . sa = sa = SQLAlchemy ( self . app , session_options = { "<STR_LIT>" : False } ) <EOL> class Type ( sa . Model ) : <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> name = sa . Column ( sa . String ( <NUM_LIT> ) , nullable = False , unique = True ) <EOL> class Machine ( sa . Model ) : <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> name = sa . Column ( sa . String ( <NUM_LIT> ) , nullable = False ) <EOL> wattage = sa . Column ( sa . Float ) <EOL> type_id = sa . Column ( sa . Integer , sa . ForeignKey ( Type . id ) ) <EOL> type = sa . relationship ( Type , backref = backref ( '<STR_LIT>' , lazy = '<STR_LIT>' , uselist = True ) ) <EOL> sa . create_all ( ) <EOL> class MachineResource ( ModelResource ) : <EOL> class Meta : <EOL> model = Machine <EOL> include_id = True <EOL> include_type = True <EOL> class Schema : <EOL> type = fields . ToOne ( '<STR_LIT:type>' ) <EOL> class TypeResource ( ModelResource ) : <EOL> class Meta : <EOL> model = Type <EOL> include_id = True <EOL> include_type = True <EOL> class Schema : <EOL> machines = fields . ToMany ( '<STR_LIT>' ) <EOL> self . MachineResource = MachineResource <EOL> self . TypeResource = TypeResource <EOL> self . api . add_resource ( MachineResource ) <EOL> self . api . add_resource ( TypeResource ) <EOL> def tearDown ( self ) : <EOL> self . sa . drop_all ( ) <EOL> def test_field_discovery ( self ) : <EOL> self . assertEqual ( set ( self . MachineResource . schema . fields . keys ( ) ) , { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT:type>' , '<STR_LIT>' } ) <EOL> self . assertEqual ( set ( self . TypeResource . schema . fields . keys ( ) ) , { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' } ) <EOL> self . assertEqual ( self . MachineResource . meta . name , '<STR_LIT>' ) <EOL> self . assertEqual ( self . TypeResource . meta . name , '<STR_LIT:type>' ) <EOL> def test_create_no_json ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = '<STR_LIT>' ) <EOL> self . assert400 ( response ) <EOL> def test_create_json_string ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = '<STR_LIT>' , force_json = True ) <EOL> self . assert400 ( response ) <EOL> def test_conflict ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT:foo>" } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT:foo>" } ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> def test_create ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { } ) <EOL> self . assert400 ( response ) <EOL> self . assertEqual ( { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT:path>" : [ ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT:name>" <EOL> ] <EOL> } <EOL> } <EOL> ] , <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : <NUM_LIT> <EOL> } , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:type>' , '<STR_LIT>' : [ ] , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT:type>" : <NUM_LIT:1> } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:type>' : { "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" : None , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT:type>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT> } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:type>' : { "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT:type>' , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } <EOL> ] , <EOL> "<STR_LIT:name>" : "<STR_LIT>" <EOL> } , response . json ) <EOL> def test_get ( self ) : <EOL> type_ = lambda i : { "<STR_LIT>" : i , "<STR_LIT>" : "<STR_LIT:type>" , "<STR_LIT:name>" : "<STR_LIT>" . format ( i ) , "<STR_LIT>" : [ ] } <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" . format ( i ) , "<STR_LIT>" : [ ] } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( type_ ( i ) , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( i ) ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( type_ ( i ) , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ type_ ( i ) for i in range ( <NUM_LIT:1> , i + <NUM_LIT:1> ) ] , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' . format ( i + <NUM_LIT:1> ) ) <EOL> self . assert404 ( response ) <EOL> self . assertJSONEqual ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : i + <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:type>' } , <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : <NUM_LIT> <EOL> } , response . json ) <EOL> @ unittest . SkipTest <EOL> def test_pagination ( self ) : <EOL> pass <EOL> def test_update ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT:type>" : <NUM_LIT:1> } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:type>' : { "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" : None , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . patch ( '<STR_LIT>' , data = { } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . patch ( '<STR_LIT>' , data = { "<STR_LIT>" : <NUM_LIT> } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:type>' : { "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . patch ( '<STR_LIT>' , data = { "<STR_LIT:type>" : { "<STR_LIT>" : "<STR_LIT>" } } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:type>' : { "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . patch ( '<STR_LIT>' , data = { "<STR_LIT:type>" : None } ) <EOL> self . assert400 ( response ) <EOL> self . assertJSONEqual ( { <EOL> '<STR_LIT>' : [ <EOL> { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT:path>" : [ <EOL> "<STR_LIT:type>" <EOL> ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" <EOL> } <EOL> } , <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" <EOL> } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } <EOL> } <EOL> ] , <EOL> '<STR_LIT:message>' : '<STR_LIT>' , <EOL> '<STR_LIT:status>' : <NUM_LIT> <EOL> } , response . json ) <EOL> response = self . client . patch ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:type>' : { "<STR_LIT>" : "<STR_LIT>" } , "<STR_LIT>" : <NUM_LIT> , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> def test_delete ( self ) : <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assert404 ( response ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT>" : [ ] } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assert404 ( response ) <EOL> class SQLAlchemyRelationTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SQLAlchemyRelationTestCase , self ) . setUp ( ) <EOL> self . app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . api = Api ( self . app ) <EOL> self . sa = sa = SQLAlchemy ( self . app , session_options = { "<STR_LIT>" : False } ) <EOL> group_members = sa . Table ( '<STR_LIT>' , <EOL> sa . Column ( '<STR_LIT>' , sa . Integer ( ) , sa . ForeignKey ( '<STR_LIT>' ) ) , <EOL> sa . Column ( '<STR_LIT>' , sa . Integer ( ) , sa . ForeignKey ( '<STR_LIT>' ) ) ) <EOL> class User ( sa . Model ) : <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> parent_id = sa . Column ( sa . Integer , sa . ForeignKey ( id ) ) <EOL> name = sa . Column ( sa . String ( <NUM_LIT> ) , nullable = False ) <EOL> parent = sa . relationship ( '<STR_LIT>' , remote_side = [ id ] , backref = backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) ) <EOL> class Group ( sa . Model ) : <EOL> id = sa . Column ( sa . Integer , primary_key = True ) <EOL> name = sa . Column ( sa . String ( <NUM_LIT> ) , nullable = False ) <EOL> members = sa . relationship ( '<STR_LIT>' , secondary = group_members , <EOL> backref = backref ( '<STR_LIT>' , lazy = '<STR_LIT>' ) ) <EOL> sa . create_all ( ) <EOL> class UserResource ( ModelResource ) : <EOL> class Meta : <EOL> model = User <EOL> include_id = True <EOL> include_type = True <EOL> children = Relation ( '<STR_LIT>' ) <EOL> class GroupResource ( ModelResource ) : <EOL> class Meta : <EOL> model = Group <EOL> include_id = True <EOL> include_type = True <EOL> members = Relation ( '<STR_LIT:user>' ) <EOL> self . api . add_resource ( UserResource ) <EOL> self . api . add_resource ( GroupResource ) <EOL> def test_relationship_secondary ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:user>' , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ ] , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { "<STR_LIT>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ { "<STR_LIT>" : "<STR_LIT>" } ] , response . json ) <EOL> def test_relationship_secondary_delete_missing ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> def test_relationship_post ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:user>' , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : '<STR_LIT:user>' , "<STR_LIT:name>" : "<STR_LIT>" } , response . json ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { "<STR_LIT>" : "<STR_LIT>" } , response . json ) <EOL> def test_relationship_get ( self ) : <EOL> self . test_relationship_post ( ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ { "<STR_LIT>" : "<STR_LIT>" } ] , response . json ) <EOL> def test_relationship_delete ( self ) : <EOL> self . test_relationship_post ( ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ ] , response . json ) <EOL> def test_relationship_delete_missing ( self ) : <EOL> self . test_relationship_post ( ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ ] , response . json ) <EOL> def test_relationship_pagination ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> for i in range ( <NUM_LIT:2> , <NUM_LIT:50> ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : str ( i ) } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT>" : "<STR_LIT>" . format ( response . json [ '<STR_LIT>' ] ) } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ { "<STR_LIT>" : "<STR_LIT>" . format ( i ) } for i in range ( <NUM_LIT:2> , <NUM_LIT> ) ] , response . json ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( [ { "<STR_LIT>" : "<STR_LIT>" . format ( i ) } for i in range ( <NUM_LIT> , <NUM_LIT:50> ) ] , response . json ) <EOL> self . assertEqual ( '<STR_LIT>' , response . headers [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , response . headers [ '<STR_LIT>' ] ) <EOL> class SQLAlchemyInspectionTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SQLAlchemyInspectionTestCase , self ) . setUp ( ) <EOL> self . app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . api = Api ( self . app ) <EOL> self . sa = sa = SQLAlchemy ( self . app , session_options = { "<STR_LIT>" : False } ) <EOL> def test_inspection_auto_id_attribute_type ( self ) : <EOL> sa = self . sa <EOL> class User ( sa . Model ) : <EOL> username = sa . Column ( sa . String , primary_key = True ) <EOL> first_name = sa . Column ( sa . String ) <EOL> last_name = sa . Column ( sa . String ) <EOL> sa . create_all ( ) <EOL> class UserResource ( ModelResource ) : <EOL> class Schema : <EOL> username = fields . String ( ) <EOL> class Meta : <EOL> model = User <EOL> include_id = True <EOL> self . api . add_resource ( UserResource ) <EOL> self . assertEqual ( User . username , UserResource . manager . id_column ) <EOL> self . assertEqual ( '<STR_LIT:username>' , UserResource . manager . id_field . attribute ) <EOL> self . assertIsInstance ( UserResource . manager . id_field , fields . String ) <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:username>" : "<STR_LIT:foo>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { <EOL> "<STR_LIT>" : "<STR_LIT:foo>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT:username>" : "<STR_LIT:foo>" <EOL> } , response . json ) </s>
