1
1
from __future__ import unicode_literals
2
2
3
- from datetime import datetime , timedelta
4
3
import unittest
5
4
import warnings
5
+ from datetime import datetime , timedelta
6
6
7
7
import django
8
8
from django .contrib .auth import get_user_model
9
9
from django .core .files .base import ContentFile
10
10
from django .db import models
11
11
from django .db .models .fields .proxy import OrderWrt
12
12
from django .test import TestCase
13
-
14
13
from simple_history .models import HistoricalRecords , convert_auto_field
15
- from ..models import (
16
- AdminProfile , Bookcase , MultiOneToOne , Poll , Choice , Restaurant ,
17
- Person , FileModel , Document , Book , HistoricalPoll , Library , State ,
18
- AbstractBase , ConcreteAttr , ConcreteUtil , SelfFK , Temperature , WaterLevel ,
19
- ExternalModel1 , ExternalModel3 , UnicodeVerboseName , HistoricalChoice ,
20
- HistoricalState , HistoricalCustomFKError , Series , SeriesWork , PollInfo ,
21
- Employee , Country , Province ,
22
- City , Contact , ContactRegister ,
23
- )
14
+ from simple_history .utils import update_change_reason
15
+
24
16
from ..external .models import ExternalModel2 , ExternalModel4
17
+ from ..models import (AbstractBase , AdminProfile , Book , Bookcase , Choice , City ,
18
+ ConcreteAttr , ConcreteUtil , Contact , ContactRegister ,
19
+ Country , Document , Employee , ExternalModel1 ,
20
+ ExternalModel3 , FileModel , HistoricalChoice ,
21
+ HistoricalCustomFKError , HistoricalPoll , HistoricalState ,
22
+ Library , MultiOneToOne , Person , Poll , PollInfo , Province ,
23
+ Restaurant , SelfFK , Series , SeriesWork , State ,
24
+ Temperature , UnicodeVerboseName , WaterLevel )
25
25
26
26
try :
27
27
from django .apps import apps
@@ -52,7 +52,7 @@ def assertRecordValues(self, record, klass, values_dict):
52
52
self .assertEqual (getattr (record , key ), value )
53
53
self .assertEqual (record .history_object .__class__ , klass )
54
54
for key , value in values_dict .items ():
55
- if key != 'history_type' :
55
+ if key not in [ 'history_type' , 'history_change_reason' ] :
56
56
self .assertEqual (getattr (record .history_object , key ), value )
57
57
58
58
def test_create (self ):
@@ -72,36 +72,63 @@ def test_update(self):
72
72
p = Poll .objects .get ()
73
73
p .pub_date = tomorrow
74
74
p .save ()
75
+ update_change_reason (p , 'future poll' )
75
76
update_record , create_record = p .history .all ()
76
77
self .assertRecordValues (create_record , Poll , {
77
78
'question' : "what's up?" ,
78
79
'pub_date' : today ,
79
80
'id' : p .id ,
81
+ 'history_change_reason' : None ,
80
82
'history_type' : "+"
81
83
})
82
84
self .assertRecordValues (update_record , Poll , {
83
85
'question' : "what's up?" ,
84
86
'pub_date' : tomorrow ,
85
87
'id' : p .id ,
88
+ 'history_change_reason' : 'future poll' ,
86
89
'history_type' : "~"
87
90
})
88
91
self .assertDatetimesEqual (update_record .history_date , datetime .now ())
89
92
90
- def test_delete (self ):
93
+ def test_delete_verify_change_reason_implicitly (self ):
94
+ p = Poll .objects .create (question = "what's up?" , pub_date = today )
95
+ poll_id = p .id
96
+ p .changeReason = 'wrongEntry'
97
+ p .delete ()
98
+ delete_record , create_record = Poll .history .all ()
99
+ self .assertRecordValues (create_record , Poll , {
100
+ 'question' : "what's up?" ,
101
+ 'pub_date' : today ,
102
+ 'id' : poll_id ,
103
+ 'history_change_reason' : None ,
104
+ 'history_type' : "+"
105
+ })
106
+ self .assertRecordValues (delete_record , Poll , {
107
+ 'question' : "what's up?" ,
108
+ 'pub_date' : today ,
109
+ 'id' : poll_id ,
110
+ 'history_change_reason' : 'wrongEntry' ,
111
+ 'history_type' : "-"
112
+ })
113
+
114
+ def test_delete_verify_change_reason_explicity (self ):
91
115
p = Poll .objects .create (question = "what's up?" , pub_date = today )
92
116
poll_id = p .id
93
117
p .delete ()
118
+ update_change_reason (p , 'wrongEntry' )
94
119
delete_record , create_record = Poll .history .all ()
95
120
self .assertRecordValues (create_record , Poll , {
96
121
'question' : "what's up?" ,
97
122
'pub_date' : today ,
98
123
'id' : poll_id ,
124
+ 'history_change_reason' : None ,
99
125
'history_type' : "+"
100
126
})
101
127
self .assertRecordValues (delete_record , Poll , {
102
128
'question' : "what's up?" ,
103
129
'pub_date' : today ,
104
130
'id' : poll_id ,
131
+ 'history_change_reason' : 'wrongEntry' ,
105
132
'history_type' : "-"
106
133
})
107
134
@@ -492,7 +519,8 @@ def test_string_related(self):
492
519
related_model = field_object .related .model
493
520
self .assertEqual (related_model , HistoricalState )
494
521
495
- @unittest .skipUnless (django .get_version () >= "1.7" , "Requires 1.7 migrations" )
522
+ @unittest .skipUnless (django .get_version () >= "1.7" ,
523
+ "Requires 1.7 migrations" )
496
524
def test_state_serialization_of_customfk (self ):
497
525
from django .db .migrations import state
498
526
state .ModelState .from_model (HistoricalCustomFKError )
@@ -644,7 +672,8 @@ def test_restore_object_with_changed_order(self):
644
672
self .assertEqual (order [5 ], self .w_chair .pk )
645
673
self .assertEqual (order [6 ], self .w_battle .pk )
646
674
647
- @unittest .skipUnless (django .get_version () >= "1.7" , "Requires 1.7 migrations" )
675
+ @unittest .skipUnless (django .get_version () >= "1.7" ,
676
+ "Requires 1.7 migrations" )
648
677
def test_migrations_include_order (self ):
649
678
from django .db .migrations import state
650
679
model_state = state .ModelState .from_model (SeriesWork .history .model )
0 commit comments