Ticket #30827: ticket30827RegressionTests.diff

File ticket30827RegressionTests.diff, 2.1 KB (added by Chetan Khanna, 5 years ago)

Adding regression tests

  • tests/bulk_create/tests.py

    diff --git a/tests/bulk_create/tests.py b/tests/bulk_create/tests.py
    index 08c753a826..56b9850a96 100644
    a b  
     1from math import ceil
    12from operator import attrgetter
    23
    3 from django.db import IntegrityError, NotSupportedError, connection
     4from django.db import IntegrityError, NotSupportedError, OperationalError, connection, connections
    45from django.db.models import FileField, Value
     6from django.db.models.fields import AutoField
    57from django.db.models.functions import Lower
    68from django.test import (
    79    TestCase, override_settings, skipIfDBFeature, skipUnlessDBFeature,
    class BulkCreateTests(TestCase):  
    295297        # Without ignore_conflicts=True, there's a problem.
    296298        with self.assertRaises(IntegrityError):
    297299            TwoFields.objects.bulk_create(conflicting_objects)
     300
     301    # Regression tests for #30827
     302    def test_batched_insert_on_model_without_pk(self):
     303        objs = [Country(name=str(i), iso_two_letter=str(i), description=str(i))
     304                for i in range(1000)]
     305        ops = connections[Country.objects.db].ops
     306        opts = Country.objects.model._meta
     307        fields = [f for f in opts.concrete_fields if not isinstance(f, AutoField)]
     308        max_batch_size = max(ops.bulk_batch_size(fields, objs), 1)
     309        with self.assertNumQueries(ceil(1000/max_batch_size)):
     310            Country.objects.bulk_create(objs, batch_size = max_batch_size + 1)
     311
     312    # Regression tests for #30827
     313    def test_batched_insert_upper_limit(self):
     314        ops = connections[State.objects.db].ops
     315        opts = State.objects.model._meta
     316        fields = opts.concrete_fields
     317        objs = [State(two_letter_code=str(i))
     318                for i in range(1000)]
     319        max_batch_size = max(ops.bulk_batch_size(fields, objs), 1)
     320        try:
     321            State.objects.bulk_create(objs, batch_size = max_batch_size + 1)
     322        except OperationalError as e:
     323            self.fail("Unexpected failure while performing regresion test in "
     324                      "bulk_create: %s" % e)
Back to Top