#NEWLINE# def test_foreign_key_cross_database_protection( self ) : #NEWLINE# #INDENT# 'Foreign keys can cross databases if they two databases have a common source' #NEWLINE# pro = Book . objects . using ( 'default' ) . create ( title = 'Pro Django' , published = datetime . date ( 2008 , 12 , 16 ) ) #NEWLINE# marty = Person . objects . using ( 'default' ) . create ( name = 'Marty Alchin' ) #NEWLINE# dive = Book . objects . using ( 'other' ) . create ( title = 'Dive into Python' , published = datetime . date ( 2009 , 5 , 4 ) ) #NEWLINE# mark = Person . objects . using ( 'other' ) . create ( name = 'Mark Pilgrim' ) #NEWLINE# try : #NEWLINE# #INDENT# dive . editor = marty #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across primary/replica 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# 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# dive = Book . objects . using ( 'other' ) . get ( title = 'Dive into Python' ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# try : #NEWLINE# #INDENT# marty . edited . set ( [ pro , dive ] , bulk = False ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across primary/replica 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 , 'default' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #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 ( dive . _state . db , 'other' ) #NEWLINE# try : #NEWLINE# #INDENT# marty . edited . add ( dive , bulk = False ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# self . fail ( 'Assignment across primary/replica 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 , 'default' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #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# chris = Person ( name = 'Chris Mills' ) #NEWLINE# html5 = Book ( title = 'Dive into HTML5' , published = datetime . date ( 2010 , 3 , 15 ) ) #NEWLINE# self . assertEqual ( chris . _state . db , None ) #NEWLINE# self . assertEqual ( html5 . _state . db , None ) #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# chris . save ( ) #NEWLINE# dive . editor = chris #NEWLINE# html5 . editor = mark #NEWLINE# self . assertEqual ( dive . _state . db , 'other' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #NEWLINE# self . assertEqual ( chris . _state . db , 'default' ) #NEWLINE# self . assertEqual ( created . _state . db , 'default' ) #NEWLINE# water = Book ( title = 'Dive into Water' , published = datetime . date ( 2001 , 1 , 1 ) , editor = mark ) #NEWLINE# self . assertEqual ( water . _state . db , 'default' ) #NEWLINE# mark . save ( using = 'default' ) #NEWLINE# mark . save ( using = 'other' ) #NEWLINE# self . assertEqual ( mark . _state . db , 'other' ) #NEWLINE# cheesecake = mark . edited . create ( title = 'Dive into Cheesecake' , published = datetime . date ( 2010 , 3 , 15 ) ) #NEWLINE# self . assertEqual ( cheesecake . _state . db , 'default' ) #NEWLINE# ( cheesecake , created ) = mark . edited . get_or_create ( title = 'Dive into Cheesecake' , published = datetime . date ( 2010 , 3 , 15 ) ) #NEWLINE# self . assertEqual ( cheesecake . _state . db , 'default' ) #NEWLINE# ( puddles , created ) = mark . edited . get_or_create ( title = 'Dive into Puddles' , published = datetime . date ( 2010 , 3 , 15 ) ) #NEWLINE# self . assertEqual ( puddles . _state . db , 'default' )