#NEWLINE# def test_generic_key_cross_database_protection( self ) : #NEWLINE# #INDENT# 'Generic Key operations can span databases if they share a source' #NEWLINE# copy_content_types_from_default_to_other ( ) #NEWLINE# pro = Book . objects . using ( 'default' ) . create ( title = 'Pro Django' , published = datetime . date ( 2008 , 12 , 16 ) ) #NEWLINE# review1 = Review . objects . using ( 'default' ) . create ( source = 'Python Monthly' , content_object = pro ) #NEWLINE# dive = Book . objects . using ( 'other' ) . create ( title = 'Dive into Python' , published = datetime . date ( 2009 , 5 , 4 ) ) #NEWLINE# review2 = Review . objects . using ( 'other' ) . create ( source = 'Python Weekly' , content_object = dive ) #NEWLINE# try : #NEWLINE# #INDENT# review1 . content_object = 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 ( pro . _state . db , 'default' ) #NEWLINE# self . assertEqual ( review1 . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( review2 . _state . db , 'other' ) #NEWLINE# dive . save ( ) #NEWLINE# self . assertEqual ( review1 . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'default' ) #NEWLINE# try : #NEWLINE# #INDENT# Book . objects . using ( 'default' ) . get ( title = 'Dive into Python' ) . delete ( ) #NEWLINE# #UNINDENT# except Book . DoesNotExist : #NEWLINE# #INDENT# self . fail ( 'Source database should have a copy of saved object' ) #NEWLINE# #UNINDENT# dive = Book . objects . using ( 'other' ) . get ( title = 'Dive into Python' ) #NEWLINE# self . assertEqual ( review2 . _state . db , 'other' ) #NEWLINE# try : #NEWLINE# #INDENT# dive . reviews . add ( review1 ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across master/slave databases with a common source should be ok' ) #NEWLINE# #UNINDENT# self . assertEqual ( pro . _state . db , 'default' ) #NEWLINE# self . assertEqual ( review1 . _state . db , 'default' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( review2 . _state . db , 'other' ) #NEWLINE# dive . save ( ) #NEWLINE# self . assertEqual ( dive . _state . db , 'default' ) #NEWLINE# try : #NEWLINE# #INDENT# Book . objects . using ( 'default' ) . get ( title = 'Dive into Python' ) . delete ( ) #NEWLINE# #UNINDENT# except Book . DoesNotExist : #NEWLINE# #INDENT# self . fail ( 'Source database should have a copy of saved object' ) #NEWLINE# #UNINDENT# review3 = Review ( source = 'Python Daily' ) #NEWLINE# self . assertEqual ( review3 . _state . db , None ) #NEWLINE# review3 . content_object = dive #NEWLINE# self . assertEqual ( review3 . _state . db , 'default' ) #NEWLINE# dive = Book . objects . using ( 'other' ) . get ( title = 'Dive into Python' ) #NEWLINE# nyt = dive . reviews . create ( source = 'New York Times' , content_object = dive ) #NEWLINE# self . assertEqual ( nyt . _state . db , 'default' )