<s> from functools import partial <EOL> from peewee import CharField , ForeignKeyField <EOL> from flask_potion . contrib . peewee import PeeweeManager <EOL> from flask_potion import signals <EOL> from flask_potion . routes import Relation <EOL> from flask_potion . resource import ModelResource <EOL> from flask_potion import Api <EOL> from tests import BaseTestCase <EOL> from blinker . _utilities import contextmanager <EOL> from blinker import ANY <EOL> from tests . contrib . peewee import PeeweeTestDB <EOL> class PeeweeSignalTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( PeeweeSignalTestCase , self ) . setUp ( ) <EOL> app = self . app <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . db = db = PeeweeTestDB ( self . app ) <EOL> self . api = Api ( self . app , default_manager = PeeweeManager ) <EOL> class User ( db . Model ) : <EOL> name = CharField ( max_length = <NUM_LIT> ) <EOL> gender = CharField ( max_length = <NUM_LIT:1> , null = True ) <EOL> parent = ForeignKeyField ( '<STR_LIT>' , null = True , related_name = '<STR_LIT>' ) <EOL> def __eq__ ( self , other ) : <EOL> return self . name == other . name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . name ) <EOL> class Group ( db . Model ) : <EOL> name = CharField ( max_length = <NUM_LIT> ) <EOL> class GroupMembership ( db . Model ) : <EOL> user = ForeignKeyField ( User ) <EOL> group = ForeignKeyField ( Group ) <EOL> db . database . connect ( ) <EOL> db . database . create_tables ( [ User , Group , GroupMembership ] ) <EOL> class UserResource ( ModelResource ) : <EOL> class Meta : <EOL> model = User <EOL> children = Relation ( '<STR_LIT>' ) <EOL> class GroupResource ( ModelResource ) : <EOL> class Meta : <EOL> model = Group <EOL> members = Relation ( UserResource ) <EOL> self . User = User <EOL> self . Group = Group <EOL> self . UserResource = UserResource <EOL> self . GroupResource = GroupResource <EOL> self . api . add_resource ( UserResource ) <EOL> self . api . add_resource ( GroupResource ) <EOL> @ contextmanager <EOL> def assertSignals ( self , expected_events , sender = ANY ) : <EOL> events = [ ] <EOL> def receiver_ ( signal , sender , ** kwargs ) : <EOL> events . append ( ( signal , sender , kwargs ) ) <EOL> receivers = { <EOL> signal : partial ( receiver_ , signal ) for signal in [ <EOL> signals . before_create , <EOL> signals . after_create , <EOL> signals . before_update , <EOL> signals . after_update , <EOL> signals . before_delete , <EOL> signals . after_delete , <EOL> signals . before_add_to_relation , <EOL> signals . after_add_to_relation , <EOL> signals . before_remove_from_relation , <EOL> signals . after_remove_from_relation <EOL> ] <EOL> } <EOL> for signal , receiver in receivers . items ( ) : <EOL> signal . connect ( receiver , sender = sender , weak = False ) <EOL> try : <EOL> yield None <EOL> except : <EOL> for signal , receiver in receivers . items ( ) : <EOL> signal . disconnect ( receiver ) <EOL> raise <EOL> else : <EOL> for signal , receiver in receivers . items ( ) : <EOL> signal . disconnect ( receiver ) <EOL> self . assertEqual ( events , expected_events ) <EOL> def test_create_signal ( self ) : <EOL> with self . assertSignals ( [ <EOL> ( signals . before_create , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . after_create , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) <EOL> ] ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : '<STR_LIT>' , "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT>" : None } , response . json ) <EOL> def test_update_signal ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> with self . assertSignals ( [ <EOL> ( signals . before_update , self . UserResource , { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:M>' , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . after_update , self . UserResource , { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT:M>' , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) <EOL> ] ) : <EOL> response = self . client . patch ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT:M>" } ) <EOL> self . assert200 ( response ) <EOL> self . assertJSONEqual ( { '<STR_LIT>' : '<STR_LIT>' , "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT:M>" } , response . json ) <EOL> def test_delete_signal ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> with self . assertSignals ( [ <EOL> ( signals . before_delete , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . after_delete , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) <EOL> ] ) : <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) <EOL> def test_relation_signal ( self ) : <EOL> response = self . client . post ( '<STR_LIT>' , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> with self . assertSignals ( [ <EOL> ( signals . before_create , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . after_create , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . before_add_to_relation , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . after_add_to_relation , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . before_remove_from_relation , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) , <EOL> ( signals . after_remove_from_relation , self . UserResource , { '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . User ( name = "<STR_LIT>" ) } ) <EOL> ] ) : <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>" : "<STR_LIT>" } ) <EOL> self . assert200 ( response ) <EOL> response = self . client . delete ( '<STR_LIT>' ) <EOL> self . assertStatus ( response , <NUM_LIT> ) </s>
