2
2
from unittest .mock import Mock , patch
3
3
4
4
from django .contrib .auth import get_user_model
5
+ from django .core .exceptions import FieldError
5
6
from django .db import IntegrityError , transaction
6
7
from django .test import TestCase , TransactionTestCase , override_settings
7
8
from django .utils import timezone
@@ -124,8 +125,8 @@ def test_bulk_create_history_with_default_date(self):
124
125
all ([history .history_date == date for history in Poll .history .all ()])
125
126
)
126
127
127
- def test_bulk_create_history_num_queries_is_two (self ):
128
- with self .assertNumQueries (2 ):
128
+ def test_bulk_create_history_num_queries_is_three (self ):
129
+ with self .assertNumQueries (3 ):
129
130
bulk_create_with_history (self .data , Poll )
130
131
131
132
def test_bulk_create_history_on_model_without_history_raises_error (self ):
@@ -138,7 +139,7 @@ def test_bulk_create_history_on_model_without_history_raises_error(self):
138
139
bulk_create_with_history (self .data , Place )
139
140
140
141
def test_num_queries_when_batch_size_is_less_than_total (self ):
141
- with self .assertNumQueries (6 ):
142
+ with self .assertNumQueries (7 ):
142
143
bulk_create_with_history (self .data , Poll , batch_size = 2 )
143
144
144
145
def test_bulk_create_history_with_batch_size (self ):
@@ -211,7 +212,7 @@ def mock_bulk_create(*args, **kwargs):
211
212
with patch .object (
212
213
Poll ._default_manager , "bulk_create" , side_effect = mock_bulk_create
213
214
):
214
- with self .assertNumQueries (3 ):
215
+ with self .assertNumQueries (4 ):
215
216
result = bulk_create_with_history (objects , Poll )
216
217
self .assertEqual (
217
218
[poll .question for poll in result ], [poll .question for poll in objects ]
@@ -262,7 +263,7 @@ def test_bulk_create_history_rolls_back_when_last_exists(self):
262
263
self .assertEqual (Poll .history .count (), 1 )
263
264
264
265
def test_bulk_create_fails_with_wrong_model (self ):
265
- with self .assertRaises (AttributeError ):
266
+ with self .assertRaises (FieldError ):
266
267
bulk_create_with_history (self .data , Document )
267
268
268
269
self .assertEqual (Poll .objects .count (), 0 )
@@ -271,14 +272,11 @@ def test_bulk_create_fails_with_wrong_model(self):
271
272
@patch ("simple_history.utils.get_history_manager_for_model" )
272
273
def test_bulk_create_no_ids_return (self , hist_manager_mock ):
273
274
objects = [Place (id = 1 , name = "Place 1" )]
274
- model = Mock (
275
- _default_manager = Mock (
276
- bulk_create = Mock (return_value = [Place (name = "Place 1" )]),
277
- filter = Mock (return_value = Mock (order_by = Mock (return_value = objects ))),
278
- ),
279
- _meta = Mock (get_fields = Mock (return_value = [])),
275
+ Place ._default_manager .bulk_create = Mock (
276
+ side_effect = Place ._default_manager .bulk_create ,
277
+ return_value = [Place (name = "Place 1" )],
280
278
)
281
- result = bulk_create_with_history (objects , model )
279
+ result = bulk_create_with_history (objects , Place )
282
280
self .assertEqual (result , objects )
283
281
hist_manager_mock ().bulk_history_create .assert_called_with (
284
282
objects ,
@@ -288,6 +286,18 @@ def test_bulk_create_no_ids_return(self, hist_manager_mock):
288
286
default_date = None ,
289
287
)
290
288
289
+ def test_bulk_create_duplicate_objects_with_no_ids_return (self ):
290
+ Street ._default_manager .create (id = 1 , name = "Duplicate Place" )
291
+ obj = Street (name = "Duplicate Place" )
292
+ Street ._default_manager .bulk_create = Mock (
293
+ side_effect = Street ._default_manager .bulk_create ,
294
+ return_value = [Street (name = "Duplicate Place" )],
295
+ )
296
+ result = bulk_create_with_history ([obj ], Street )
297
+ self .assertEqual (result , [Street ._default_manager .last ()])
298
+ self .assertEqual (Street ._default_manager .count (), 2 )
299
+ self .assertEqual (Street .log .count (), 2 )
300
+
291
301
292
302
class BulkCreateWithManyToManyField (TestCase ):
293
303
def setUp (self ):
0 commit comments