~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/bulk_create/tests.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import absolute_import
 
2
 
 
3
from operator import attrgetter
 
4
 
 
5
from django.db import connection
 
6
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
 
7
from django.test.utils import override_settings
 
8
 
 
9
from .models import Country, Restaurant, Pizzeria, State, TwoFields
 
10
 
 
11
 
 
12
class BulkCreateTests(TestCase):
 
13
    def setUp(self):
 
14
        self.data = [
 
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")
 
19
        ]
 
20
 
 
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"))
 
27
 
 
28
        created = Country.objects.bulk_create([])
 
29
        self.assertEqual(created, [])
 
30
        self.assertEqual(Country.objects.count(), 4)
 
31
 
 
32
    @skipUnlessDBFeature('has_bulk_insert')
 
33
    def test_efficiency(self):
 
34
        with self.assertNumQueries(1):
 
35
            Country.objects.bulk_create(self.data)
 
36
 
 
37
    def test_inheritance(self):
 
38
        Restaurant.objects.bulk_create([
 
39
            Restaurant(name="Nicholas's")
 
40
        ])
 
41
        self.assertQuerysetEqual(Restaurant.objects.all(), [
 
42
            "Nicholas's",
 
43
        ], attrgetter("name"))
 
44
        with self.assertRaises(ValueError):
 
45
            Pizzeria.objects.bulk_create([
 
46
                Pizzeria(name="The Art of Pizza")
 
47
            ])
 
48
        self.assertQuerysetEqual(Pizzeria.objects.all(), [])
 
49
        self.assertQuerysetEqual(Restaurant.objects.all(), [
 
50
            "Nicholas's",
 
51
        ], attrgetter("name"))
 
52
 
 
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"]
 
57
        ])
 
58
        self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [
 
59
            "CA", "IL", "ME", "NY",
 
60
        ], attrgetter("two_letter_code"))
 
61
 
 
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"]
 
68
            ])
 
69
        self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [
 
70
            "CA", "IL", "ME", "NY",
 
71
        ], attrgetter("two_letter_code"))
 
72
 
 
73
    @skipIfDBFeature('allows_primary_key_0')
 
74
    def test_zero_as_autoval(self):
 
75
        """
 
76
        Zero as id for AutoField should raise exception in MySQL, because MySQL
 
77
        does not allow zero for automatic primary key.
 
78
        """
 
79
 
 
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])
 
84
 
 
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)
 
90
        ])
 
91
        self.assertEqual(Restaurant.objects.count(), 2)
 
92
 
 
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)
 
98
                ])
 
99
        self.assertEqual(TwoFields.objects.count(), 1001)
 
100
        self.assertEqual(
 
101
            TwoFields.objects.filter(f1__gte=450, f1__lte=550).count(),
 
102
            101)
 
103
        self.assertEqual(TwoFields.objects.filter(f2__gte=901).count(), 101)
 
104
 
 
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
 
108
        # query.
 
109
        Restaurant.objects.bulk_create([
 
110
            Restaurant() for i in range(0, 501)
 
111
        ])
 
112
 
 
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)
 
119
                ])
 
120
            self.assertTrue(len(connection.queries) < 10)
 
121
 
 
122
    def test_large_batch_mixed(self):
 
123
        """
 
124
        Test inserting a large batch with objects having primary key set
 
125
        mixed together with objects without PK set.
 
126
        """
 
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)
 
138
 
 
139
    @skipUnlessDBFeature('has_bulk_insert')
 
140
    def test_large_batch_mixed_efficiency(self):
 
141
        """
 
142
        Test inserting a large batch with objects having primary key set
 
143
        mixed together with objects without PK set.
 
144
        """
 
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)
 
151
 
 
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))
 
159
 
 
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))