#NEWLINE# def test_models_are_properly_hydrated( self ) : #NEWLINE# #INDENT# model1 = OrmBelongsToManyModelStub ( ) #NEWLINE# model1 . fill ( name = 'john' , pivot_user_id = 1 , pivot_role_id = 2 ) #NEWLINE# model2 = OrmBelongsToManyModelStub ( ) #NEWLINE# model2 . fill ( name = 'jane' , pivot_user_id = 3 , pivot_role_id = 4 ) #NEWLINE# models = [ model1 , model2 ] #NEWLINE# base_builder = flexmock ( Builder ( QueryBuilder ( MockConnection ( ) . prepare_mock ( ) , QueryGrammar ( ) , QueryProcessor ( ) ) ) ) #NEWLINE# relation = self . _get_relation ( ) #NEWLINE# relation . get_parent ( ) . should_receive ( 'get_connection_name' ) . and_return ( 'foo.connection' ) #NEWLINE# relation . get_query ( ) . get_query ( ) . should_receive ( 'add_select' ) . once ( ) . with_args ( * [ 'roles.*' , 'user_role.user_id AS pivot_user_id' , 'user_role.role_id AS pivot_role_id' ] ) . and_return ( relation . get_query ( ) ) #NEWLINE# relation . get_query ( ) . should_receive ( 'get_models' ) . once ( ) . and_return ( models ) #NEWLINE# relation . get_query ( ) . should_receive ( 'eager_load_relations' ) . once ( ) . with_args ( models ) . and_return ( models ) #NEWLINE# relation . get_related ( ) . should_receive ( 'new_collection' ) . replace_with ( ( lambda l : Collection ( l ) ) ) #NEWLINE# relation . get_query ( ) . should_receive ( 'get_query' ) . once ( ) . and_return ( base_builder ) #NEWLINE# results = relation . get ( ) #NEWLINE# self . assertIsInstance ( results , Collection ) #NEWLINE# self . assertEqual ( 'user_id' , results [ 0 ] . pivot . get_foreign_key ( ) ) #NEWLINE# self . assertEqual ( 'role_id' , results [ 0 ] . pivot . get_other_key ( ) ) #NEWLINE# self . assertEqual ( 'john' , results [ 0 ] . name ) #NEWLINE# self . assertEqual ( 1 , results [ 0 ] . pivot . user_id ) #NEWLINE# self . assertEqual ( 2 , results [ 0 ] . pivot . role_id ) #NEWLINE# self . assertEqual ( 'foo.connection' , results [ 0 ] . pivot . get_connection_name ( ) ) #NEWLINE# self . assertEqual ( 'jane' , results [ 1 ] . name ) #NEWLINE# self . assertEqual ( 3 , results [ 1 ] . pivot . user_id ) #NEWLINE# self . assertEqual ( 4 , results [ 1 ] . pivot . role_id ) #NEWLINE# self . assertEqual ( 'foo.connection' , results [ 1 ] . pivot . get_connection_name ( ) ) #NEWLINE# self . assertEqual ( 'user_role' , results [ 0 ] . pivot . get_table ( ) ) #NEWLINE# self . assertTrue ( models [ 0 ] . pivot . exists )