1
+ from unittest import skipIf
2
+
3
+ import django
4
+ from django .contrib .auth import get_user_model
1
5
from django .db import IntegrityError
2
6
from django .test import TestCase , TransactionTestCase
3
7
from django .utils import timezone
11
15
PollWithExcludeFields ,
12
16
Street ,
13
17
)
14
- from simple_history .utils import bulk_create_with_history , update_change_reason
18
+ from simple_history .utils import (
19
+ bulk_create_with_history ,
20
+ update_change_reason ,
21
+ bulk_update_with_history ,
22
+ )
23
+
24
+ User = get_user_model ()
15
25
16
26
17
27
class BulkCreateWithHistoryTestCase (TestCase ):
@@ -37,6 +47,29 @@ def test_bulk_create_history(self):
37
47
self .assertEqual (Poll .objects .count (), 5 )
38
48
self .assertEqual (Poll .history .count (), 5 )
39
49
50
+ def test_bulk_create_history_with_default_user (self ):
51
+ user = User .
objects .
create_user (
"tester" ,
"[email protected] " )
52
+
53
+ bulk_create_with_history (self .data , Poll , default_user = user )
54
+
55
+ self .assertTrue (
56
+ all ([history .history_user == user for history in Poll .history .all ()])
57
+ )
58
+
59
+ def test_bulk_create_history_with_default_change_reason (self ):
60
+ bulk_create_with_history (
61
+ self .data , Poll , default_change_reason = "my change reason"
62
+ )
63
+
64
+ self .assertTrue (
65
+ all (
66
+ [
67
+ history .history_change_reason == "my change reason"
68
+ for history in Poll .history .all ()
69
+ ]
70
+ )
71
+ )
72
+
40
73
def test_bulk_create_history_num_queries_is_two (self ):
41
74
with self .assertNumQueries (2 ):
42
75
bulk_create_with_history (self .data , Poll )
@@ -142,9 +175,95 @@ def test_bulk_create_no_ids_return(self, hist_manager_mock):
142
175
result = bulk_create_with_history (objects , model )
143
176
self .assertEqual (result , objects )
144
177
hist_manager_mock ().bulk_history_create .assert_called_with (
145
- objects , batch_size = None
178
+ objects , batch_size = None , default_user = None , default_change_reason = None
179
+ )
180
+
181
+
182
+ @skipIf (django .VERSION < (2 , 2 ,), reason = "bulk_update does not exist before 2.2" )
183
+ class BulkUpdateWithHistoryTestCase (TestCase ):
184
+ def setUp (self ):
185
+ self .data = [
186
+ Poll (id = 1 , question = "Question 1" , pub_date = timezone .now ()),
187
+ Poll (id = 2 , question = "Question 2" , pub_date = timezone .now ()),
188
+ Poll (id = 3 , question = "Question 3" , pub_date = timezone .now ()),
189
+ Poll (id = 4 , question = "Question 4" , pub_date = timezone .now ()),
190
+ Poll (id = 5 , question = "Question 5" , pub_date = timezone .now ()),
191
+ ]
192
+ bulk_create_with_history (self .data , Poll )
193
+
194
+ self .data [3 ].question = "Updated question"
195
+
196
+ def test_bulk_update_history (self ):
197
+ bulk_update_with_history (
198
+ self .data , Poll , fields = ["question" ],
199
+ )
200
+
201
+ self .assertEqual (Poll .objects .count (), 5 )
202
+ self .assertEqual (Poll .objects .get (id = 4 ).question , "Updated question" )
203
+ self .assertEqual (Poll .history .count (), 10 )
204
+ self .assertEqual (Poll .history .filter (history_type = "~" ).count (), 5 )
205
+
206
+ def test_bulk_update_history_with_default_user (self ):
207
+ user = User .
objects .
create_user (
"tester" ,
"[email protected] " )
208
+
209
+ bulk_update_with_history (
210
+ self .data , Poll , fields = ["question" ], default_user = user
211
+ )
212
+
213
+ self .assertTrue (
214
+ all (
215
+ [
216
+ history .history_user == user
217
+ for history in Poll .history .filter (history_type = "~" )
218
+ ]
219
+ )
220
+ )
221
+
222
+ def test_bulk_update_history_with_default_change_reason (self ):
223
+ bulk_update_with_history (
224
+ self .data ,
225
+ Poll ,
226
+ fields = ["question" ],
227
+ default_change_reason = "my change reason" ,
228
+ )
229
+
230
+ self .assertTrue (
231
+ all (
232
+ [
233
+ history .history_change_reason == "my change reason"
234
+ for history in Poll .history .filter (history_type = "~" )
235
+ ]
236
+ )
146
237
)
147
238
239
+ def test_bulk_update_history_num_queries_is_two (self ):
240
+ with self .assertNumQueries (2 ):
241
+ bulk_update_with_history (
242
+ self .data , Poll , fields = ["question" ],
243
+ )
244
+
245
+ def test_bulk_update_history_on_model_without_history_raises_error (self ):
246
+ self .data = [
247
+ Place (id = 1 , name = "Place 1" ),
248
+ Place (id = 2 , name = "Place 2" ),
249
+ Place (id = 3 , name = "Place 3" ),
250
+ ]
251
+ Place .objects .bulk_create (self .data )
252
+ self .data [0 ].name = "test"
253
+
254
+ with self .assertRaises (NotHistoricalModelError ):
255
+ bulk_update_with_history (self .data , Place , fields = ["name" ])
256
+
257
+ def test_num_queries_when_batch_size_is_less_than_total (self ):
258
+ with self .assertNumQueries (6 ):
259
+ bulk_update_with_history (self .data , Poll , fields = ["question" ], batch_size = 2 )
260
+
261
+ def test_bulk_update_history_with_batch_size (self ):
262
+ bulk_update_with_history (self .data , Poll , fields = ["question" ], batch_size = 2 )
263
+
264
+ self .assertEqual (Poll .objects .count (), 5 )
265
+ self .assertEqual (Poll .history .filter (history_type = "~" ).count (), 5 )
266
+
148
267
149
268
class UpdateChangeReasonTestCase (TestCase ):
150
269
def test_update_change_reason_with_excluded_fields (self ):
0 commit comments