10
10
from django .core .files .base import ContentFile
11
11
from django .db import models
12
12
from django .db .models .fields .proxy import OrderWrt
13
- from django .test import TestCase , override_settings
13
+ from django .test import override_settings , TestCase
14
14
from django .urls import reverse
15
15
16
16
from simple_history .models import (
17
17
HistoricalRecords ,
18
18
ModelChange
19
19
)
20
+ from simple_history .signals import pre_create_historical_record
20
21
from simple_history .tests .tests .utils import middleware_override_settings
21
22
from simple_history .utils import update_change_reason
22
23
from ..external .models import ExternalModel2 , ExternalModel4
45
46
HistoricalChoice ,
46
47
HistoricalCustomFKError ,
47
48
HistoricalPoll ,
49
+ HistoricalPollWithHistoricalIPAddress ,
48
50
HistoricalState ,
49
51
Library ,
50
52
MultiOneToOne ,
51
53
Person ,
52
54
Place ,
53
55
Poll ,
54
56
PollInfo ,
55
- PollWithHistoricalIPAddress ,
56
57
PollWithExcludeFields ,
57
58
PollWithExcludedFKField ,
59
+ PollWithHistoricalIPAddress ,
58
60
Province ,
59
61
Restaurant ,
60
62
SelfFK ,
@@ -1052,14 +1054,32 @@ def test_changed_value_lost(self):
1052
1054
instance = historical .instance
1053
1055
self .assertEqual (instance .place , new_place )
1054
1056
1057
+ def add_static_history_ip_address (sender , ** kwargs ):
1058
+ history_instance = kwargs ['history_instance' ]
1059
+ history_instance .ip_address = '192.168.0.1'
1060
+
1061
+
1062
+ class ExtraFieldsStaticIPAddressTestCase (TestCase ):
1063
+ def setUp (self ):
1064
+ pre_create_historical_record .connect (
1065
+ add_static_history_ip_address ,
1066
+ sender = HistoricalPollWithHistoricalIPAddress ,
1067
+ dispatch_uid = 'add_static_history_ip_address'
1068
+ )
1069
+
1070
+ def tearDown (self ):
1071
+ pre_create_historical_record .disconnect (
1072
+ add_static_history_ip_address ,
1073
+ sender = HistoricalPollWithHistoricalIPAddress ,
1074
+ dispatch_uid = 'add_static_history_ip_address'
1075
+ )
1055
1076
1056
- class ExtraFieldsTestCase (TestCase ):
1057
1077
def test_extra_ip_address_field_populated_on_save (self ):
1058
1078
poll = PollWithHistoricalIPAddress .objects .create (
1059
1079
question = "Will it blend?" , pub_date = today
1060
1080
)
1061
1081
1062
- poll_history = poll .history .order_by ( 'history_date' )[ 0 ]
1082
+ poll_history = poll .history .first ()
1063
1083
1064
1084
self .assertEquals ('192.168.0.1' , poll_history .ip_address )
1065
1085
@@ -1072,13 +1092,31 @@ def test_extra_ip_address_field_not_present_on_poll(self):
1072
1092
poll .ip_address
1073
1093
1074
1094
1095
+ def add_dynamic_history_ip_address (sender , ** kwargs ):
1096
+ history_instance = kwargs ['history_instance' ]
1097
+ history_instance .ip_address = \
1098
+ HistoricalRecords .thread .request .META ['REMOTE_ADDR' ]
1099
+
1100
+
1075
1101
@override_settings (** middleware_override_settings )
1076
- class ExtraFieldsIPAddressTestCase (TestCase ):
1102
+ class ExtraFieldsDynamicIPAddressTestCase (TestCase ):
1103
+ def setUp (self ):
1104
+ pre_create_historical_record .connect (
1105
+ add_dynamic_history_ip_address ,
1106
+ sender = HistoricalPollWithHistoricalIPAddress ,
1107
+ dispatch_uid = 'add_dynamic_history_ip_address'
1108
+ )
1109
+
1110
+ def tearDown (self ):
1111
+ pre_create_historical_record .disconnect (
1112
+ add_dynamic_history_ip_address ,
1113
+ sender = HistoricalPollWithHistoricalIPAddress ,
1114
+ dispatch_uid = 'add_dynamic_history_ip_address'
1115
+ )
1116
+
1077
1117
def test_signal_is_able_to_retrieve_request_from_thread (self ):
1078
1118
data = {
1079
- # workaround for bad interplay with signals and mocking; see
1080
- # simple_history.tests.tests.utils.add_history_ip_address
1081
- 'question' : 'read IP from request' ,
1119
+ 'question' : 'Will it blend?' ,
1082
1120
'pub_date' : '2018-10-30'
1083
1121
}
1084
1122
0 commit comments