diff options
| author | Alex Gaynor <alex.gaynor@gmail.com> | 2011-09-09 19:22:28 +0000 |
|---|---|---|
| committer | Alex Gaynor <alex.gaynor@gmail.com> | 2011-09-09 19:22:28 +0000 |
| commit | 7deb25b8dd5aa1ed02b5e30cbc67cd1fb0c3d6e6 (patch) | |
| tree | 09ed3208f309d71b5710b8287bccdb92ddf40c58 /tests | |
| parent | e55bbf4c3c42b17d52c21bc3a460b4981fdc190c (diff) | |
Fixed #7596. Added Model.objects.bulk_create, and make use of it in several places. This provides a performance benefit when inserting multiple objects. THanks to Russ for the review, and Simon Meers for the MySQl implementation.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16739 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/bulk_create/__init__.py | 0 | ||||
| -rw-r--r-- | tests/regressiontests/bulk_create/models.py | 21 | ||||
| -rw-r--r-- | tests/regressiontests/bulk_create/tests.py | 54 | ||||
| -rw-r--r-- | tests/regressiontests/db_typecasts/tests.py | 4 |
4 files changed, 77 insertions, 2 deletions
diff --git a/tests/regressiontests/bulk_create/__init__.py b/tests/regressiontests/bulk_create/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/regressiontests/bulk_create/__init__.py diff --git a/tests/regressiontests/bulk_create/models.py b/tests/regressiontests/bulk_create/models.py new file mode 100644 index 0000000000..a4c611d537 --- /dev/null +++ b/tests/regressiontests/bulk_create/models.py @@ -0,0 +1,21 @@ +from django.db import models + + +class Country(models.Model): + name = models.CharField(max_length=255) + iso_two_letter = models.CharField(max_length=2) + +class Place(models.Model): + name = models.CharField(max_length=100) + + class Meta: + abstract = True + +class Restaurant(Place): + pass + +class Pizzeria(Restaurant): + pass + +class State(models.Model): + two_letter_code = models.CharField(max_length=2, primary_key=True)
\ No newline at end of file diff --git a/tests/regressiontests/bulk_create/tests.py b/tests/regressiontests/bulk_create/tests.py new file mode 100644 index 0000000000..8d3faa2379 --- /dev/null +++ b/tests/regressiontests/bulk_create/tests.py @@ -0,0 +1,54 @@ +from __future__ import with_statement + +from operator import attrgetter + +from django.test import TestCase, skipUnlessDBFeature + +from models import Country, Restaurant, Pizzeria, State + + +class BulkCreateTests(TestCase): + def setUp(self): + self.data = [ + Country(name="United States of America", iso_two_letter="US"), + Country(name="The Netherlands", iso_two_letter="NL"), + Country(name="Germany", iso_two_letter="DE"), + Country(name="Czech Republic", iso_two_letter="CZ") + ] + + def test_simple(self): + Country.objects.bulk_create(self.data) + self.assertQuerysetEqual(Country.objects.order_by("-name"), [ + "United States of America", "The Netherlands", "Germany", "Czech Republic" + ], attrgetter("name")) + + @skipUnlessDBFeature("has_bulk_insert") + def test_efficiency(self): + with self.assertNumQueries(1): + Country.objects.bulk_create(self.data) + + def test_inheritance(self): + Restaurant.objects.bulk_create([ + Restaurant(name="Nicholas's") + ]) + self.assertQuerysetEqual(Restaurant.objects.all(), [ + "Nicholas's", + ], attrgetter("name")) + with self.assertRaises(ValueError): + Pizzeria.objects.bulk_create([ + Pizzeria(name="The Art of Pizza") + ]) + self.assertQuerysetEqual(Pizzeria.objects.all(), []) + self.assertQuerysetEqual(Restaurant.objects.all(), [ + "Nicholas's", + ], attrgetter("name")) + + def test_non_auto_increment_pk(self): + with self.assertNumQueries(1): + State.objects.bulk_create([ + State(two_letter_code=s) + for s in ["IL", "NY", "CA", "ME"] + ]) + self.assertQuerysetEqual(State.objects.order_by("two_letter_code"), [ + "CA", "IL", "ME", "NY", + ], attrgetter("two_letter_code"))
\ No newline at end of file diff --git a/tests/regressiontests/db_typecasts/tests.py b/tests/regressiontests/db_typecasts/tests.py index 8c71c8f809..1d3bbfa101 100644 --- a/tests/regressiontests/db_typecasts/tests.py +++ b/tests/regressiontests/db_typecasts/tests.py @@ -53,10 +53,10 @@ TEST_CASES = { class DBTypeCasts(unittest.TestCase): def test_typeCasts(self): - for k, v in TEST_CASES.items(): + for k, v in TEST_CASES.iteritems(): for inpt, expected in v: got = getattr(typecasts, k)(inpt) - assert got == expected, "In %s: %r doesn't match %r. Got %r instead." % (k, inpt, expected, got) + self.assertEqual(got, expected, "In %s: %r doesn't match %r. Got %r instead." % (k, inpt, expected, got)) if __name__ == '__main__': unittest.main() |
