<s> from __future__ import absolute_import <EOL> from django . db . models . signals import pre_save , post_save <EOL> from django . test import TestCase <EOL> from . models import Person , Employee , ProxyEmployee , Profile , Account <EOL> class UpdateOnlyFieldsTests ( TestCase ) : <EOL> def test_update_fields_basic ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> self . assertEqual ( s . gender , '<STR_LIT:F>' ) <EOL> s . gender = '<STR_LIT:M>' <EOL> s . name = '<STR_LIT>' <EOL> s . save ( update_fields = [ '<STR_LIT:name>' ] ) <EOL> s = Person . objects . get ( pk = s . pk ) <EOL> self . assertEqual ( s . gender , '<STR_LIT:F>' ) <EOL> self . assertEqual ( s . name , '<STR_LIT>' ) <EOL> def test_update_fields_deferred ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , pid = <NUM_LIT> ) <EOL> self . assertEqual ( s . gender , '<STR_LIT:F>' ) <EOL> s1 = Person . objects . defer ( "<STR_LIT>" , "<STR_LIT>" ) . get ( pk = s . pk ) <EOL> s1 . name = "<STR_LIT>" <EOL> s1 . gender = "<STR_LIT:M>" <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s1 . save ( ) <EOL> s2 = Person . objects . get ( pk = s1 . pk ) <EOL> self . assertEqual ( s2 . name , "<STR_LIT>" ) <EOL> self . assertEqual ( s2 . gender , "<STR_LIT:M>" ) <EOL> def test_update_fields_only_1 ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> self . assertEqual ( s . gender , '<STR_LIT:F>' ) <EOL> s1 = Person . objects . only ( '<STR_LIT:name>' ) . get ( pk = s . pk ) <EOL> s1 . name = "<STR_LIT>" <EOL> s1 . gender = "<STR_LIT:M>" <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s1 . save ( ) <EOL> s2 = Person . objects . get ( pk = s1 . pk ) <EOL> self . assertEqual ( s2 . name , "<STR_LIT>" ) <EOL> self . assertEqual ( s2 . gender , "<STR_LIT:M>" ) <EOL> def test_update_fields_only_2 ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , pid = <NUM_LIT> ) <EOL> self . assertEqual ( s . gender , '<STR_LIT:F>' ) <EOL> s1 = Person . objects . only ( '<STR_LIT:name>' ) . get ( pk = s . pk ) <EOL> s1 . name = "<STR_LIT>" <EOL> s1 . gender = "<STR_LIT:M>" <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> s1 . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> s2 = Person . objects . get ( pk = s1 . pk ) <EOL> self . assertEqual ( s2 . name , "<STR_LIT>" ) <EOL> self . assertEqual ( s2 . gender , "<STR_LIT:F>" ) <EOL> def test_update_fields_only_repeated ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> self . assertEqual ( s . gender , '<STR_LIT:F>' ) <EOL> s1 = Person . objects . only ( '<STR_LIT:name>' ) . get ( pk = s . pk ) <EOL> s1 . gender = '<STR_LIT:M>' <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s1 . save ( ) <EOL> s1 = Person . objects . only ( '<STR_LIT:name>' ) . get ( pk = s . pk ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s1 . save ( ) <EOL> def test_update_fields_inheritance_defer ( self ) : <EOL> profile_boss = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT> ) <EOL> e1 = Employee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , <EOL> employee_num = <NUM_LIT:1> , profile = profile_boss ) <EOL> e1 = Employee . objects . only ( '<STR_LIT:name>' ) . get ( pk = e1 . pk ) <EOL> e1 . name = '<STR_LIT>' <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> e1 . save ( ) <EOL> self . assertEqual ( Employee . objects . get ( pk = e1 . pk ) . name , <EOL> '<STR_LIT>' ) <EOL> def test_update_fields_fk_defer ( self ) : <EOL> profile_boss = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT> ) <EOL> profile_receptionist = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT:1000> ) <EOL> e1 = Employee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , <EOL> employee_num = <NUM_LIT:1> , profile = profile_boss ) <EOL> e1 = Employee . objects . only ( '<STR_LIT>' ) . get ( pk = e1 . pk ) <EOL> e1 . profile = profile_receptionist <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> e1 . save ( ) <EOL> self . assertEqual ( Employee . objects . get ( pk = e1 . pk ) . profile , profile_receptionist ) <EOL> e1 . profile_id = profile_boss . pk <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> e1 . save ( ) <EOL> self . assertEqual ( Employee . objects . get ( pk = e1 . pk ) . profile , profile_boss ) <EOL> def test_select_related_only_interaction ( self ) : <EOL> profile_boss = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT> ) <EOL> e1 = Employee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , <EOL> employee_num = <NUM_LIT:1> , profile = profile_boss ) <EOL> e1 = Employee . objects . only ( '<STR_LIT>' ) . select_related ( '<STR_LIT>' ) . get ( pk = e1 . pk ) <EOL> profile_boss . name = '<STR_LIT>' <EOL> profile_boss . salary = <NUM_LIT:1000> <EOL> profile_boss . save ( ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> e1 . profile . save ( ) <EOL> reloaded_profile = Profile . objects . get ( pk = profile_boss . pk ) <EOL> self . assertEqual ( reloaded_profile . name , profile_boss . name ) <EOL> self . assertEqual ( reloaded_profile . salary , <NUM_LIT> ) <EOL> def test_update_fields_m2m ( self ) : <EOL> profile_boss = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT> ) <EOL> e1 = Employee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , <EOL> employee_num = <NUM_LIT:1> , profile = profile_boss ) <EOL> a1 = Account . objects . create ( num = <NUM_LIT:1> ) <EOL> a2 = Account . objects . create ( num = <NUM_LIT:2> ) <EOL> e1 . accounts = [ a1 , a2 ] <EOL> with self . assertRaises ( ValueError ) : <EOL> e1 . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> def test_update_fields_inheritance ( self ) : <EOL> profile_boss = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT> ) <EOL> profile_receptionist = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT:1000> ) <EOL> e1 = Employee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , <EOL> employee_num = <NUM_LIT:1> , profile = profile_boss ) <EOL> e1 . name = '<STR_LIT>' <EOL> e1 . gender = '<STR_LIT:M>' <EOL> e1 . save ( update_fields = [ '<STR_LIT:name>' ] ) <EOL> e2 = Employee . objects . get ( pk = e1 . pk ) <EOL> self . assertEqual ( e2 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( e2 . gender , '<STR_LIT:F>' ) <EOL> self . assertEqual ( e2 . profile , profile_boss ) <EOL> e2 . profile = profile_receptionist <EOL> e2 . name = '<STR_LIT>' <EOL> e2 . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> e3 = Employee . objects . get ( pk = e1 . pk ) <EOL> self . assertEqual ( e3 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( e3 . profile , profile_receptionist ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> e3 . profile = profile_boss <EOL> e3 . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> e4 = Employee . objects . get ( pk = e3 . pk ) <EOL> self . assertEqual ( e4 . profile , profile_boss ) <EOL> self . assertEqual ( e4 . profile_id , profile_boss . pk ) <EOL> def test_update_fields_inheritance_with_proxy_model ( self ) : <EOL> profile_boss = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT> ) <EOL> profile_receptionist = Profile . objects . create ( name = '<STR_LIT>' , salary = <NUM_LIT:1000> ) <EOL> e1 = ProxyEmployee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' , <EOL> employee_num = <NUM_LIT:1> , profile = profile_boss ) <EOL> e1 . name = '<STR_LIT>' <EOL> e1 . gender = '<STR_LIT:M>' <EOL> e1 . save ( update_fields = [ '<STR_LIT:name>' ] ) <EOL> e2 = ProxyEmployee . objects . get ( pk = e1 . pk ) <EOL> self . assertEqual ( e2 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( e2 . gender , '<STR_LIT:F>' ) <EOL> self . assertEqual ( e2 . profile , profile_boss ) <EOL> e2 . profile = profile_receptionist <EOL> e2 . name = '<STR_LIT>' <EOL> e2 . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> e3 = ProxyEmployee . objects . get ( pk = e1 . pk ) <EOL> self . assertEqual ( e3 . name , '<STR_LIT>' ) <EOL> self . assertEqual ( e3 . profile , profile_receptionist ) <EOL> def test_update_fields_signals ( self ) : <EOL> p = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> pre_save_data = [ ] <EOL> def pre_save_receiver ( ** kwargs ) : <EOL> pre_save_data . append ( kwargs [ '<STR_LIT>' ] ) <EOL> pre_save . connect ( pre_save_receiver ) <EOL> post_save_data = [ ] <EOL> def post_save_receiver ( ** kwargs ) : <EOL> post_save_data . append ( kwargs [ '<STR_LIT>' ] ) <EOL> post_save . connect ( post_save_receiver ) <EOL> p . save ( update_fields = [ '<STR_LIT:name>' ] ) <EOL> self . assertEqual ( len ( pre_save_data ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( pre_save_data [ <NUM_LIT:0> ] ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT:name>' in pre_save_data [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( len ( post_save_data ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( post_save_data [ <NUM_LIT:0> ] ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( '<STR_LIT:name>' in post_save_data [ <NUM_LIT:0> ] ) <EOL> pre_save . disconnect ( pre_save_receiver ) <EOL> post_save . disconnect ( post_save_receiver ) <EOL> def test_update_fields_incorrect_params ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> s . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> with self . assertRaises ( ValueError ) : <EOL> s . save ( update_fields = "<STR_LIT:name>" ) <EOL> def test_empty_update_fields ( self ) : <EOL> s = Person . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> pre_save_data = [ ] <EOL> def pre_save_receiver ( ** kwargs ) : <EOL> pre_save_data . append ( kwargs [ '<STR_LIT>' ] ) <EOL> pre_save . connect ( pre_save_receiver ) <EOL> post_save_data = [ ] <EOL> def post_save_receiver ( ** kwargs ) : <EOL> post_save_data . append ( kwargs [ '<STR_LIT>' ] ) <EOL> post_save . connect ( post_save_receiver ) <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> s . save ( update_fields = [ ] ) <EOL> self . assertEqual ( len ( pre_save_data ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( post_save_data ) , <NUM_LIT:0> ) <EOL> pre_save . disconnect ( pre_save_receiver ) <EOL> post_save . disconnect ( post_save_receiver ) <EOL> def test_num_queries_inheritance ( self ) : <EOL> s = Employee . objects . create ( name = '<STR_LIT>' , gender = '<STR_LIT:F>' ) <EOL> s . employee_num = <NUM_LIT:1> <EOL> s . name = '<STR_LIT>' <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> s = Employee . objects . get ( pk = s . pk ) <EOL> self . assertEqual ( s . employee_num , <NUM_LIT:1> ) <EOL> self . assertEqual ( s . name , '<STR_LIT>' ) <EOL> s . employee_num = <NUM_LIT:2> <EOL> s . name = '<STR_LIT>' <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> s . save ( update_fields = [ '<STR_LIT:name>' ] ) <EOL> s = Employee . objects . get ( pk = s . pk ) <EOL> self . assertEqual ( s . name , '<STR_LIT>' ) <EOL> self . assertEqual ( s . employee_num , <NUM_LIT:1> ) <EOL> self . assertEqual ( Employee . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Person . objects . count ( ) , <NUM_LIT:1> ) <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> s . save ( update_fields = [ '<STR_LIT:name>' , '<STR_LIT>' ] ) </s>
