11from __future__ import unicode_literals
22
3- from datetime import datetime , timedelta
43import unittest
54import warnings
5+ from datetime import datetime , timedelta
66
77import django
88from django .contrib .auth import get_user_model
99from django .core .files .base import ContentFile
1010from django .db import models
1111from django .db .models .fields .proxy import OrderWrt
1212from django .test import TestCase
13-
1413from 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+
2416from ..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 )
2525
2626try :
2727 from django .apps import apps
@@ -52,7 +52,7 @@ def assertRecordValues(self, record, klass, values_dict):
5252 self .assertEqual (getattr (record , key ), value )
5353 self .assertEqual (record .history_object .__class__ , klass )
5454 for key , value in values_dict .items ():
55- if key != 'history_type' :
55+ if key not in [ 'history_type' , 'history_change_reason' ] :
5656 self .assertEqual (getattr (record .history_object , key ), value )
5757
5858 def test_create (self ):
@@ -72,36 +72,63 @@ def test_update(self):
7272 p = Poll .objects .get ()
7373 p .pub_date = tomorrow
7474 p .save ()
75+ update_change_reason (p , 'future poll' )
7576 update_record , create_record = p .history .all ()
7677 self .assertRecordValues (create_record , Poll , {
7778 'question' : "what's up?" ,
7879 'pub_date' : today ,
7980 'id' : p .id ,
81+ 'history_change_reason' : None ,
8082 'history_type' : "+"
8183 })
8284 self .assertRecordValues (update_record , Poll , {
8385 'question' : "what's up?" ,
8486 'pub_date' : tomorrow ,
8587 'id' : p .id ,
88+ 'history_change_reason' : 'future poll' ,
8689 'history_type' : "~"
8790 })
8891 self .assertDatetimesEqual (update_record .history_date , datetime .now ())
8992
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 ):
91115 p = Poll .objects .create (question = "what's up?" , pub_date = today )
92116 poll_id = p .id
93117 p .delete ()
118+ update_change_reason (p , 'wrongEntry' )
94119 delete_record , create_record = Poll .history .all ()
95120 self .assertRecordValues (create_record , Poll , {
96121 'question' : "what's up?" ,
97122 'pub_date' : today ,
98123 'id' : poll_id ,
124+ 'history_change_reason' : None ,
99125 'history_type' : "+"
100126 })
101127 self .assertRecordValues (delete_record , Poll , {
102128 'question' : "what's up?" ,
103129 'pub_date' : today ,
104130 'id' : poll_id ,
131+ 'history_change_reason' : 'wrongEntry' ,
105132 'history_type' : "-"
106133 })
107134
@@ -492,7 +519,8 @@ def test_string_related(self):
492519 related_model = field_object .related .model
493520 self .assertEqual (related_model , HistoricalState )
494521
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" )
496524 def test_state_serialization_of_customfk (self ):
497525 from django .db .migrations import state
498526 state .ModelState .from_model (HistoricalCustomFKError )
@@ -644,7 +672,8 @@ def test_restore_object_with_changed_order(self):
644672 self .assertEqual (order [5 ], self .w_chair .pk )
645673 self .assertEqual (order [6 ], self .w_battle .pk )
646674
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" )
648677 def test_migrations_include_order (self ):
649678 from django .db .migrations import state
650679 model_state = state .ModelState .from_model (SeriesWork .history .model )
0 commit comments