#NEWLINE# def test_filter( self ) : #NEWLINE# #INDENT# Company . objects . create ( name = 'Example Inc.' , num_employees = 2300 , num_chairs = 5 , ceo = Employee . objects . create ( firstname = 'Joe' , lastname = 'Smith' ) ) #NEWLINE# Company . objects . create ( name = 'Foobar Ltd.' , num_employees = 3 , num_chairs = 4 , ceo = Employee . objects . create ( firstname = 'Frank' , lastname = 'Meyer' ) ) #NEWLINE# Company . objects . create ( name = 'Test GmbH' , num_employees = 32 , num_chairs = 1 , ceo = Employee . objects . create ( firstname = 'Max' , lastname = 'Mustermann' ) ) #NEWLINE# company_query = Company . objects . values ( 'name' , 'num_employees' , 'num_chairs' ) . order_by ( 'name' , 'num_employees' , 'num_chairs' ) #NEWLINE# self . assertQuerysetEqual ( company_query . filter ( num_employees__gt = F ( 'num_chairs' ) ) , [ { #NEWLINE# #INDENT# 'num_chairs' : 5 , #NEWLINE# 'name' : 'Example Inc.' , #NEWLINE# 'num_employees' : 2300 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 1 , #NEWLINE# 'name' : 'Test GmbH' , #NEWLINE# 'num_employees' : 32 , #NEWLINE# #UNINDENT# } ] , ( lambda o : o ) ) #NEWLINE# company_query . update ( num_chairs = F ( 'num_employees' ) ) #NEWLINE# self . assertQuerysetEqual ( company_query , [ { #NEWLINE# #INDENT# 'num_chairs' : 2300 , #NEWLINE# 'name' : 'Example Inc.' , #NEWLINE# 'num_employees' : 2300 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 3 , #NEWLINE# 'name' : 'Foobar Ltd.' , #NEWLINE# 'num_employees' : 3 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 32 , #NEWLINE# 'name' : 'Test GmbH' , #NEWLINE# 'num_employees' : 32 , #NEWLINE# #UNINDENT# } ] , ( lambda o : o ) ) #NEWLINE# company_query . update ( num_chairs = ( F ( 'num_employees' ) + 2 ) ) #NEWLINE# self . assertQuerysetEqual ( company_query , [ { #NEWLINE# #INDENT# 'num_chairs' : 2302 , #NEWLINE# 'name' : 'Example Inc.' , #NEWLINE# 'num_employees' : 2300 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 5 , #NEWLINE# 'name' : 'Foobar Ltd.' , #NEWLINE# 'num_employees' : 3 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 34 , #NEWLINE# 'name' : 'Test GmbH' , #NEWLINE# 'num_employees' : 32 , #NEWLINE# #UNINDENT# } ] , ( lambda o : o ) ) #NEWLINE# company_query . update ( num_chairs = ( F ( 'num_employees' ) + ( 2 * F ( 'num_employees' ) ) ) ) #NEWLINE# self . assertQuerysetEqual ( company_query , [ { #NEWLINE# #INDENT# 'num_chairs' : 6900 , #NEWLINE# 'name' : 'Example Inc.' , #NEWLINE# 'num_employees' : 2300 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 9 , #NEWLINE# 'name' : 'Foobar Ltd.' , #NEWLINE# 'num_employees' : 3 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 96 , #NEWLINE# 'name' : 'Test GmbH' , #NEWLINE# 'num_employees' : 32 , #NEWLINE# #UNINDENT# } ] , ( lambda o : o ) ) #NEWLINE# company_query . update ( num_chairs = ( ( F ( 'num_employees' ) + 2 ) * F ( 'num_employees' ) ) ) #NEWLINE# self . assertQuerysetEqual ( company_query , [ { #NEWLINE# #INDENT# 'num_chairs' : 5294600 , #NEWLINE# 'name' : 'Example Inc.' , #NEWLINE# 'num_employees' : 2300 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 15 , #NEWLINE# 'name' : 'Foobar Ltd.' , #NEWLINE# 'num_employees' : 3 , #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# 'num_chairs' : 1088 , #NEWLINE# 'name' : 'Test GmbH' , #NEWLINE# 'num_employees' : 32 , #NEWLINE# #UNINDENT# } ] , ( lambda o : o ) ) #NEWLINE# self . assertEqual ( Company . objects . update ( point_of_contact = F ( 'ceo' ) ) , 3 ) #NEWLINE# self . assertQuerysetEqual ( Company . objects . all ( ) , [ 'Joe Smith' , 'Frank Meyer' , 'Max Mustermann' ] , ( lambda c : six . text_type ( c . point_of_contact ) ) , ordered = False ) #NEWLINE# c = Company . objects . all ( ) [ 0 ] #NEWLINE# c . point_of_contact = Employee . objects . create ( firstname = 'Guido' , lastname = 'van Rossum' ) #NEWLINE# c . save ( ) #NEWLINE# self . assertQuerysetEqual ( Company . objects . filter ( ceo__firstname = F ( 'point_of_contact__firstname' ) ) , [ 'Foobar Ltd.' , 'Test GmbH' ] , ( lambda c : c . name ) , ordered = False ) #NEWLINE# Company . objects . exclude ( ceo__firstname = F ( 'point_of_contact__firstname' ) ) . update ( name = 'foo' ) #NEWLINE# self . assertEqual ( Company . objects . exclude ( ceo__firstname = F ( 'point_of_contact__firstname' ) ) . get ( ) . name , 'foo' ) #NEWLINE# with transaction . atomic ( ) : #NEWLINE# #INDENT# with self . assertRaises ( FieldError ) : #NEWLINE# #INDENT# Company . objects . exclude ( ceo__firstname = F ( 'point_of_contact__firstname' ) ) . update ( name = F ( 'point_of_contact__lastname' ) ) #NEWLINE# #UNINDENT# #UNINDENT# test_gmbh = Company . objects . get ( name = 'Test GmbH' ) #NEWLINE# self . assertEqual ( test_gmbh . num_employees , 32 ) #NEWLINE# test_gmbh . num_employees = ( F ( 'num_employees' ) + 4 ) #NEWLINE# test_gmbh . save ( ) #NEWLINE# test_gmbh = Company . objects . get ( pk = test_gmbh . pk ) #NEWLINE# self . assertEqual ( test_gmbh . num_employees , 36 ) #NEWLINE# test_gmbh . point_of_contact = None #NEWLINE# test_gmbh . save ( ) #NEWLINE# self . assertTrue ( ( test_gmbh . point_of_contact is None ) ) #NEWLINE# def test( ) : #NEWLINE# #INDENT# test_gmbh . point_of_contact = F ( 'ceo' ) #NEWLINE# #UNINDENT# self . assertRaises ( ValueError , test ) #NEWLINE# test_gmbh . point_of_contact = test_gmbh . ceo #NEWLINE# test_gmbh . save ( ) #NEWLINE# test_gmbh . name = F ( 'ceo__last_name' ) #NEWLINE# self . assertRaises ( FieldError , test_gmbh . save ) #NEWLINE# acme = Company ( name = 'The Acme Widget Co.' , num_employees = 12 , num_chairs = 5 , ceo = test_gmbh . ceo ) #NEWLINE# acme . num_employees = ( F ( 'num_employees' ) + 16 ) #NEWLINE# self . assertRaises ( TypeError , acme . save )