File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,14 @@ def test_explicit_batch_size_respects_max_batch_size(self):
293
293
with self .assertNumQueries (ceil (len (objs ) / max_batch_size )):
294
294
Country .objects .bulk_create (objs , batch_size = max_batch_size + 1 )
295
295
296
+ @skipUnlessDBFeature ("has_bulk_insert" )
297
+ def test_max_batch_size (self ):
298
+ objs = [Country (name = f"Country { i } " ) for i in range (1000 )]
299
+ fields = ["name" , "iso_two_letter" , "description" ]
300
+ max_batch_size = connection .ops .bulk_batch_size (fields , objs )
301
+ with self .assertNumQueries (ceil (len (objs ) / max_batch_size )):
302
+ Country .objects .bulk_create (objs )
303
+
296
304
@skipUnlessDBFeature ("has_bulk_insert" )
297
305
def test_bulk_insert_expressions (self ):
298
306
Restaurant .objects .bulk_create (
Original file line number Diff line number Diff line change 1
1
import datetime
2
+ from math import ceil
2
3
3
4
from django .core .exceptions import FieldDoesNotExist
5
+ from django .db import connection
4
6
from django .db .models import F
5
7
from django .db .models .functions import Lower
6
8
from django .db .utils import IntegrityError
@@ -69,6 +71,15 @@ def test_batch_size(self):
69
71
with self .assertNumQueries (len (self .notes )):
70
72
Note .objects .bulk_update (self .notes , fields = ["note" ], batch_size = 1 )
71
73
74
+ def test_max_batch_size (self ):
75
+ max_batch_size = connection .ops .bulk_batch_size (
76
+ # PK is used twice, see comment in bulk_update().
77
+ [Note ._meta .pk , Note ._meta .pk , Note ._meta .get_field ("note" )],
78
+ self .notes ,
79
+ )
80
+ with self .assertNumQueries (ceil (len (self .notes ) / max_batch_size )):
81
+ Note .objects .bulk_update (self .notes , fields = ["note" ])
82
+
72
83
def test_unsaved_models (self ):
73
84
objs = self .notes + [Note (note = "test" , misc = "test" )]
74
85
msg = "All bulk_update() objects must have a primary key set."
You can’t perform that action at this time.
0 commit comments