1212from django .test import TestCase
1313
1414from simple_history .models import HistoricalRecords , convert_auto_field
15+ from simple_history .utils import update_change_reason
1516from ..models import (
1617 AdminProfile , Bookcase , MultiOneToOne , Poll , Choice , Restaurant ,
1718 Person , FileModel , Document , Book , HistoricalPoll , Library , State ,
@@ -52,7 +53,7 @@ def assertRecordValues(self, record, klass, values_dict):
5253 self .assertEqual (getattr (record , key ), value )
5354 self .assertEqual (record .history_object .__class__ , klass )
5455 for key , value in values_dict .items ():
55- if key not in ['history_type' , 'history_change_reason' ] :
56+ if key not in ['history_type' , 'history_change_reason' ]:
5657 self .assertEqual (getattr (record .history_object , key ), value )
5758
5859 def test_create (self ):
@@ -71,8 +72,8 @@ def test_update(self):
7172 Poll .objects .create (question = "what's up?" , pub_date = today )
7273 p = Poll .objects .get ()
7374 p .pub_date = tomorrow
74- p .changeReason = 'future poll'
7575 p .save ()
76+ update_change_reason (p , 'future poll' )
7677 update_record , create_record = p .history .all ()
7778 self .assertRecordValues (create_record , Poll , {
7879 'question' : "what's up?" ,
@@ -90,7 +91,7 @@ def test_update(self):
9091 })
9192 self .assertDatetimesEqual (update_record .history_date , datetime .now ())
9293
93- def test_delete (self ):
94+ def test_delete_verify_change_reason_implicitly (self ):
9495 p = Poll .objects .create (question = "what's up?" , pub_date = today )
9596 poll_id = p .id
9697 p .changeReason = 'wrongEntry'
@@ -111,6 +112,27 @@ def test_delete(self):
111112 'history_type' : "-"
112113 })
113114
115+ def test_delete_verify_change_reason_explicity (self ):
116+ p = Poll .objects .create (question = "what's up?" , pub_date = today )
117+ poll_id = p .id
118+ p .delete ()
119+ update_change_reason (p , 'wrongEntry' )
120+ delete_record , create_record = Poll .history .all ()
121+ self .assertRecordValues (create_record , Poll , {
122+ 'question' : "what's up?" ,
123+ 'pub_date' : today ,
124+ 'id' : poll_id ,
125+ 'history_change_reason' : None ,
126+ 'history_type' : "+"
127+ })
128+ self .assertRecordValues (delete_record , Poll , {
129+ 'question' : "what's up?" ,
130+ 'pub_date' : today ,
131+ 'id' : poll_id ,
132+ 'history_change_reason' : 'wrongEntry' ,
133+ 'history_type' : "-"
134+ })
135+
114136 def test_save_without_historical_record (self ):
115137 pizza_place = Restaurant .objects .create (name = 'Pizza Place' , rating = 3 )
116138 pizza_place .rating = 4
0 commit comments