#NEWLINE# def test_m2m_cross_database_protection( self ) : #NEWLINE# #INDENT# 'M2M relations can cross databases if the database share a source' #NEWLINE# pro = Book . objects . using ( 'other' ) . create ( pk = 1 , title = 'Pro Django' , published = datetime . date ( 2008 , 12 , 16 ) ) #NEWLINE# marty = Person . objects . using ( 'other' ) . create ( pk = 1 , name = 'Marty Alchin' ) #NEWLINE# dive = Book . objects . using ( 'default' ) . create ( pk = 2 , title = 'Dive into Python' , published = datetime . date ( 2009 , 5 , 4 ) ) #NEWLINE# mark = Person . objects . using ( 'default' ) . create ( pk = 2 , name = 'Mark Pilgrim' ) #NEWLINE# pro . save ( using = 'default' ) #NEWLINE# marty . save ( using = 'default' ) #NEWLINE# dive . save ( using = 'other' ) #NEWLINE# mark . save ( using = 'other' ) #NEWLINE# self . assertEqual ( Book . objects . using ( 'default' ) . count ( ) , 2 ) #NEWLINE# self . assertEqual ( Book . objects . using ( 'other' ) . count ( ) , 2 ) #NEWLINE# self . assertEqual ( Person . objects . using ( 'default' ) . count ( ) , 2 ) #NEWLINE# self . assertEqual ( Person . objects . using ( 'other' ) . count ( ) , 2 ) #NEWLINE# try : #NEWLINE# #INDENT# marty . book_set = [ pro , dive ] #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across master/slave databases with a common source should be ok' ) #NEWLINE# #UNINDENT# self . assertEqual ( marty . _state . db , 'default' ) #NEWLINE# self . assertEqual ( pro . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'default' ) . count ( ) , 2 ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'other' ) . count ( ) , 0 ) #NEWLINE# Book . authors . through . objects . using ( 'default' ) . delete ( ) #NEWLINE# try : #NEWLINE# #INDENT# marty . book_set . add ( dive ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across master/slave databases with a common source should be ok' ) #NEWLINE# #UNINDENT# self . assertEqual ( marty . _state . db , 'default' ) #NEWLINE# self . assertEqual ( pro . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'default' ) . count ( ) , 1 ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'other' ) . count ( ) , 0 ) #NEWLINE# Book . authors . through . objects . using ( 'default' ) . delete ( ) #NEWLINE# try : #NEWLINE# #INDENT# dive . authors = [ pro , marty ] #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across master/slave databases with a common source should be ok' ) #NEWLINE# #UNINDENT# self . assertEqual ( marty . _state . db , 'default' ) #NEWLINE# self . assertEqual ( pro . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'default' ) . count ( ) , 2 ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'other' ) . count ( ) , 0 ) #NEWLINE# Book . authors . through . objects . using ( 'default' ) . delete ( ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'default' ) . count ( ) , 0 ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'other' ) . count ( ) , 0 ) #NEWLINE# try : #NEWLINE# #INDENT# dive . authors . add ( marty ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across master/slave databases with a common source should be ok' ) #NEWLINE# #UNINDENT# self . assertEqual ( marty . _state . db , 'default' ) #NEWLINE# self . assertEqual ( pro . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'default' ) . count ( ) , 1 ) #NEWLINE# self . assertEqual ( Book . authors . through . objects . using ( 'other' ) . count ( ) , 0 ) #NEWLINE# alice = dive . authors . create ( name = 'Alice' ) #NEWLINE# self . assertEqual ( alice . _state . db , 'default' ) #NEWLINE# ( alice , created ) = dive . authors . get_or_create ( name = 'Alice' ) #NEWLINE# self . assertEqual ( alice . _state . db , 'default' ) #NEWLINE# ( bob , created ) = dive . authors . get_or_create ( name = 'Bob' ) #NEWLINE# self . assertEqual ( bob . _state . db , 'default' )