1
from __future__ import absolute_import
3
from operator import attrgetter
5
from django.db import connection
6
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
7
from django.test.utils import override_settings
9
from .models import Country, Restaurant, Pizzeria, State, TwoFields
12
class BulkCreateTests(TestCase):
15
Country(name="United States of America", iso_two_letter="US"),
16
Country(name="The Netherlands", iso_two_letter="NL"),
17
Country(name="Germany", iso_two_letter="DE"),
18
Country(name="Czech Republic", iso_two_letter="CZ")
21
def test_simple(self):
22
created = Country.objects.bulk_create(self.data)
23
self.assertEqual(len(created), 4)
24
self.assertQuerysetEqual(Country.objects.order_by("-name"), [
25
"United States of America", "The Netherlands", "Germany", "Czech Republic"
26
], attrgetter("name"))
28
created = Country.objects.bulk_create([])
29
self.assertEqual(created, [])
30
self.assertEqual(Country.objects.count(), 4)
32
@skipUnlessDBFeature('has_bulk_insert')
33
def test_efficiency(self):
34
with self.assertNumQueries(1):
35
Country.objects.bulk_create(self.data)
37
def test_inheritance(self):
38
Restaurant.objects.bulk_create([
39
Restaurant(name="Nicholas's")
41
self.assertQuerysetEqual(Restaurant.objects.all(), [
43
], attrgetter("name"))
44
with self.assertRaises(ValueError):
45
Pizzeria.objects.bulk_create([
46
Pizzeria(name="The Art of Pizza")
48
self.assertQuerysetEqual(Pizzeria.objects.all(), [])
49
self.assertQuerysetEqual(Restaurant.objects.all(), [
51
], attrgetter("name"))
53
def test_non_auto_increment_pk(self):
54
State.objects.bulk_create([
55
State(two_letter_code=s)
56
for s in ["IL", "NY", "CA", "ME"]
58
self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [
59
"CA", "IL", "ME", "NY",
60
], attrgetter("two_letter_code"))
62
@skipUnlessDBFeature('has_bulk_insert')
63
def test_non_auto_increment_pk_efficiency(self):
64
with self.assertNumQueries(1):
65
State.objects.bulk_create([
66
State(two_letter_code=s)
67
for s in ["IL", "NY", "CA", "ME"]
69
self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [
70
"CA", "IL", "ME", "NY",
71
], attrgetter("two_letter_code"))
73
@skipIfDBFeature('allows_primary_key_0')
74
def test_zero_as_autoval(self):
76
Zero as id for AutoField should raise exception in MySQL, because MySQL
77
does not allow zero for automatic primary key.
80
valid_country = Country(name='Germany', iso_two_letter='DE')
81
invalid_country = Country(id=0, name='Poland', iso_two_letter='PL')
82
with self.assertRaises(ValueError):
83
Country.objects.bulk_create([valid_country, invalid_country])
85
def test_batch_same_vals(self):
86
# Sqlite had a problem where all the same-valued models were
87
# collapsed to one insert.
88
Restaurant.objects.bulk_create([
89
Restaurant(name='foo') for i in range(0, 2)
91
self.assertEqual(Restaurant.objects.count(), 2)
93
def test_large_batch(self):
94
with override_settings(DEBUG=True):
95
connection.queries = []
96
TwoFields.objects.bulk_create([
97
TwoFields(f1=i, f2=i+1) for i in range(0, 1001)
99
self.assertEqual(TwoFields.objects.count(), 1001)
101
TwoFields.objects.filter(f1__gte=450, f1__lte=550).count(),
103
self.assertEqual(TwoFields.objects.filter(f2__gte=901).count(), 101)
105
@skipUnlessDBFeature('has_bulk_insert')
106
def test_large_single_field_batch(self):
107
# SQLite had a problem with more than 500 UNIONed selects in single
109
Restaurant.objects.bulk_create([
110
Restaurant() for i in range(0, 501)
113
@skipUnlessDBFeature('has_bulk_insert')
114
def test_large_batch_efficiency(self):
115
with override_settings(DEBUG=True):
116
connection.queries = []
117
TwoFields.objects.bulk_create([
118
TwoFields(f1=i, f2=i+1) for i in range(0, 1001)
120
self.assertTrue(len(connection.queries) < 10)
122
def test_large_batch_mixed(self):
124
Test inserting a large batch with objects having primary key set
125
mixed together with objects without PK set.
127
with override_settings(DEBUG=True):
128
connection.queries = []
129
TwoFields.objects.bulk_create([
130
TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i+1)
131
for i in range(100000, 101000)])
132
self.assertEqual(TwoFields.objects.count(), 1000)
133
# We can't assume much about the ID's created, except that the above
134
# created IDs must exist.
135
id_range = range(100000, 101000, 2)
136
self.assertEqual(TwoFields.objects.filter(id__in=id_range).count(), 500)
137
self.assertEqual(TwoFields.objects.exclude(id__in=id_range).count(), 500)
139
@skipUnlessDBFeature('has_bulk_insert')
140
def test_large_batch_mixed_efficiency(self):
142
Test inserting a large batch with objects having primary key set
143
mixed together with objects without PK set.
145
with override_settings(DEBUG=True):
146
connection.queries = []
147
TwoFields.objects.bulk_create([
148
TwoFields(id=i if i % 2 == 0 else None, f1=i, f2=i+1)
149
for i in range(100000, 101000)])
150
self.assertTrue(len(connection.queries) < 10)
152
def test_explicit_batch_size(self):
153
objs = [TwoFields(f1=i, f2=i) for i in range(0, 4)]
154
TwoFields.objects.bulk_create(objs, 2)
155
self.assertEqual(TwoFields.objects.count(), len(objs))
156
TwoFields.objects.all().delete()
157
TwoFields.objects.bulk_create(objs, len(objs))
158
self.assertEqual(TwoFields.objects.count(), len(objs))
160
@skipUnlessDBFeature('has_bulk_insert')
161
def test_explicit_batch_size_efficiency(self):
162
objs = [TwoFields(f1=i, f2=i) for i in range(0, 100)]
163
with self.assertNumQueries(2):
164
TwoFields.objects.bulk_create(objs, 50)
165
TwoFields.objects.all().delete()
166
with self.assertNumQueries(1):
167
TwoFields.objects.bulk_create(objs, len(objs))