12
12
from django .test import TestCase
13
13
14
14
from simple_history .models import HistoricalRecords , convert_auto_field
15
+ from simple_history .utils import update_change_reason
15
16
from ..models import (
16
17
AdminProfile , Bookcase , MultiOneToOne , Poll , Choice , Restaurant ,
17
18
Person , FileModel , Document , Book , HistoricalPoll , Library , State ,
@@ -52,7 +53,7 @@ def assertRecordValues(self, record, klass, values_dict):
52
53
self .assertEqual (getattr (record , key ), value )
53
54
self .assertEqual (record .history_object .__class__ , klass )
54
55
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' ]:
56
57
self .assertEqual (getattr (record .history_object , key ), value )
57
58
58
59
def test_create (self ):
@@ -71,8 +72,8 @@ def test_update(self):
71
72
Poll .objects .create (question = "what's up?" , pub_date = today )
72
73
p = Poll .objects .get ()
73
74
p .pub_date = tomorrow
74
- p .changeReason = 'future poll'
75
75
p .save ()
76
+ update_change_reason (p , 'future poll' )
76
77
update_record , create_record = p .history .all ()
77
78
self .assertRecordValues (create_record , Poll , {
78
79
'question' : "what's up?" ,
@@ -90,7 +91,7 @@ def test_update(self):
90
91
})
91
92
self .assertDatetimesEqual (update_record .history_date , datetime .now ())
92
93
93
- def test_delete (self ):
94
+ def test_delete_verify_change_reason_implicitly (self ):
94
95
p = Poll .objects .create (question = "what's up?" , pub_date = today )
95
96
poll_id = p .id
96
97
p .changeReason = 'wrongEntry'
@@ -111,6 +112,27 @@ def test_delete(self):
111
112
'history_type' : "-"
112
113
})
113
114
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
+
114
136
def test_save_without_historical_record (self ):
115
137
pizza_place = Restaurant .objects .create (name = 'Pizza Place' , rating = 3 )
116
138
pizza_place .rating = 4
0 commit comments