diff options
| author | Nick Martini <nmartini@orcasinc.com> | 2012-09-07 13:31:01 -0400 |
|---|---|---|
| committer | Nick Martini <nmartini@orcasinc.com> | 2012-09-07 13:31:01 -0400 |
| commit | 9e190e1c82a287c88636f8fffea125224526f0e5 (patch) | |
| tree | f4bf6f9ef87d43de90c3a8d095cab9ba151ddb68 /tests/modeltests | |
| parent | 9ce58906af4a5f5a0739598ac10103fef3642419 (diff) | |
| parent | 292322f977b1844e15ba25d59d1e985461571c15 (diff) | |
Merge branch 'master' of git://github.com/django/django
Diffstat (limited to 'tests/modeltests')
| -rw-r--r-- | tests/modeltests/basic/tests.py | 28 | ||||
| -rw-r--r-- | tests/modeltests/empty/tests.py | 7 | ||||
| -rw-r--r-- | tests/modeltests/fixtures/tests.py | 34 | ||||
| -rw-r--r-- | tests/modeltests/many_to_many/tests.py | 3 | ||||
| -rw-r--r-- | tests/modeltests/many_to_one/tests.py | 2 | ||||
| -rw-r--r-- | tests/modeltests/update_only_fields/tests.py | 3 | ||||
| -rw-r--r-- | tests/modeltests/validation/test_error_messages.py | 3 |
7 files changed, 42 insertions, 38 deletions
diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py index d96c60bbe8..6ec9ca03af 100644 --- a/tests/modeltests/basic/tests.py +++ b/tests/modeltests/basic/tests.py @@ -5,7 +5,7 @@ from datetime import datetime from django.core.exceptions import ObjectDoesNotExist from django.db.models.fields import Field, FieldDoesNotExist from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature -from django.utils.six import PY3 +from django.utils import six from django.utils.translation import ugettext_lazy from .models import Article @@ -82,7 +82,7 @@ class ModelTest(TestCase): # Django raises an Article.DoesNotExist exception for get() if the # parameters don't match any object. - self.assertRaisesRegexp( + six.assertRaisesRegex(self, ObjectDoesNotExist, "Article matching query does not exist. Lookup parameters were " "{'id__exact': 2000}", @@ -91,14 +91,14 @@ class ModelTest(TestCase): ) # To avoid dict-ordering related errors check only one lookup # in single assert. - self.assertRaisesRegexp( + six.assertRaisesRegex(self, ObjectDoesNotExist, ".*'pub_date__year': 2005.*", Article.objects.get, pub_date__year=2005, pub_date__month=8, ) - self.assertRaisesRegexp( + six.assertRaisesRegex(self, ObjectDoesNotExist, ".*'pub_date__month': 8.*", Article.objects.get, @@ -106,7 +106,7 @@ class ModelTest(TestCase): pub_date__month=8, ) - self.assertRaisesRegexp( + six.assertRaisesRegex(self, ObjectDoesNotExist, "Article matching query does not exist. Lookup parameters were " "{'pub_date__week_day': 6}", @@ -168,7 +168,7 @@ class ModelTest(TestCase): self.assertEqual(a4.headline, 'Fourth article') # Don't use invalid keyword arguments. - self.assertRaisesRegexp( + six.assertRaisesRegex(self, TypeError, "'foo' is an invalid keyword argument for this function", Article, @@ -259,13 +259,13 @@ class ModelTest(TestCase): "datetime.datetime(2005, 7, 28, 0, 0)"]) # dates() requires valid arguments. - self.assertRaisesRegexp( + six.assertRaisesRegex(self, TypeError, "dates\(\) takes at least 3 arguments \(1 given\)", Article.objects.dates, ) - self.assertRaisesRegexp( + six.assertRaisesRegex(self, FieldDoesNotExist, "Article has no field named 'invalid_field'", Article.objects.dates, @@ -273,7 +273,7 @@ class ModelTest(TestCase): "year", ) - self.assertRaisesRegexp( + six.assertRaisesRegex(self, AssertionError, "'kind' must be one of 'year', 'month' or 'day'.", Article.objects.dates, @@ -281,7 +281,7 @@ class ModelTest(TestCase): "bad_kind", ) - self.assertRaisesRegexp( + six.assertRaisesRegex(self, AssertionError, "'order' must be either 'ASC' or 'DESC'.", Article.objects.dates, @@ -323,7 +323,7 @@ class ModelTest(TestCase): "<Article: Third article>"]) # Slicing works with longs (Python 2 only -- Python 3 doesn't have longs). - if not PY3: + if not six.PY3: self.assertEqual(Article.objects.all()[long(0)], a) self.assertQuerysetEqual(Article.objects.all()[long(1):long(3)], ["<Article: Second article>", "<Article: Third article>"]) @@ -369,14 +369,14 @@ class ModelTest(TestCase): "<Article: Updated article 8>"]) # Also, once you have sliced you can't filter, re-order or combine - self.assertRaisesRegexp( + six.assertRaisesRegex(self, AssertionError, "Cannot filter a query once a slice has been taken.", Article.objects.all()[0:5].filter, id=a.id, ) - self.assertRaisesRegexp( + six.assertRaisesRegex(self, AssertionError, "Cannot reorder a query once a slice has been taken.", Article.objects.all()[0:5].order_by, @@ -411,7 +411,7 @@ class ModelTest(TestCase): # An Article instance doesn't have access to the "objects" attribute. # That's only available on the class. - self.assertRaisesRegexp( + six.assertRaisesRegex(self, AttributeError, "Manager isn't accessible via Article instances", getattr, diff --git a/tests/modeltests/empty/tests.py b/tests/modeltests/empty/tests.py index db464c632d..4c0c4409d8 100644 --- a/tests/modeltests/empty/tests.py +++ b/tests/modeltests/empty/tests.py @@ -1,10 +1,10 @@ from __future__ import absolute_import -from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db.models.loading import get_app from django.test import TestCase from django.test.utils import override_settings +from django.utils import six from .models import Empty @@ -14,12 +14,13 @@ class EmptyModelTests(TestCase): m = Empty() self.assertEqual(m.id, None) m.save() - m2 = Empty.objects.create() + Empty.objects.create() self.assertEqual(len(Empty.objects.all()), 2) self.assertTrue(m.id is not None) existing = Empty(m.id) existing.save() + class NoModelTests(TestCase): """ Test for #7198 to ensure that the proper error message is raised @@ -32,6 +33,6 @@ class NoModelTests(TestCase): """ @override_settings(INSTALLED_APPS=("modeltests.empty.no_models",)) def test_no_models(self): - with self.assertRaisesRegexp(ImproperlyConfigured, + with six.assertRaisesRegex(self, ImproperlyConfigured, 'App with label no_models is missing a models.py module.'): get_app('no_models') diff --git a/tests/modeltests/fixtures/tests.py b/tests/modeltests/fixtures/tests.py index b332348425..f9b0ac8a46 100644 --- a/tests/modeltests/fixtures/tests.py +++ b/tests/modeltests/fixtures/tests.py @@ -4,7 +4,7 @@ from django.contrib.sites.models import Site from django.core import management from django.db import connection, IntegrityError from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature -from django.utils.six import StringIO +from django.utils import six from .models import Article, Book, Spy, Tag, Visa @@ -21,16 +21,17 @@ class TestCaseFixtureLoadingTests(TestCase): '<Article: Poker has no place on ESPN>', ]) + class FixtureLoadingTests(TestCase): def _dumpdata_assert(self, args, output, format='json', natural_keys=False, use_base_manager=False, exclude_list=[]): - new_io = StringIO() - management.call_command('dumpdata', *args, **{'format':format, - 'stdout':new_io, - 'stderr':new_io, - 'use_natural_keys':natural_keys, - 'use_base_manager':use_base_manager, + new_io = six.StringIO() + management.call_command('dumpdata', *args, **{'format': format, + 'stdout': new_io, + 'stderr': new_io, + 'use_natural_keys': natural_keys, + 'use_base_manager': use_base_manager, 'exclude': exclude_list}) command_output = new_io.getvalue().strip() self.assertEqual(command_output, output) @@ -42,8 +43,6 @@ class FixtureLoadingTests(TestCase): ]) def test_loading_and_dumping(self): - new_io = StringIO() - Site.objects.all().delete() # Load fixture 1. Single JSON file, with two objects. management.call_command('loaddata', 'fixture1.json', verbosity=0, commit=False) @@ -184,12 +183,12 @@ class FixtureLoadingTests(TestCase): exclude_list=['fixtures.Article', 'fixtures.Book', 'sites']) # Excluding a bogus app should throw an error - with self.assertRaisesRegexp(management.CommandError, + with six.assertRaisesRegex(self, management.CommandError, "Unknown app in excludes: foo_app"): self._dumpdata_assert(['fixtures', 'sites'], '', exclude_list=['foo_app']) # Excluding a bogus model should throw an error - with self.assertRaisesRegexp(management.CommandError, + with six.assertRaisesRegex(self, management.CommandError, "Unknown model in excludes: fixtures.FooModel"): self._dumpdata_assert(['fixtures', 'sites'], '', exclude_list=['fixtures.FooModel']) @@ -199,7 +198,7 @@ class FixtureLoadingTests(TestCase): self.assertQuerysetEqual(Spy.objects.all(), ['<Spy: Paul>']) # Use the default manager - self._dumpdata_assert(['fixtures.Spy'],'[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]' % spy1.pk) + self._dumpdata_assert(['fixtures.Spy'], '[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]' % spy1.pk) # Dump using Django's base manager. Should return all objects, # even those normally filtered by the manager self._dumpdata_assert(['fixtures.Spy'], '[{"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": true}}, {"pk": %d, "model": "fixtures.spy", "fields": {"cover_blown": false}}]' % (spy2.pk, spy1.pk), use_base_manager=True) @@ -227,7 +226,7 @@ class FixtureLoadingTests(TestCase): def test_ambiguous_compressed_fixture(self): # The name "fixture5" is ambigous, so loading it will raise an error - with self.assertRaisesRegexp(management.CommandError, + with six.assertRaisesRegex(self, management.CommandError, "Multiple fixtures named 'fixture5'"): management.call_command('loaddata', 'fixture5', verbosity=0, commit=False) @@ -251,7 +250,7 @@ class FixtureLoadingTests(TestCase): # is closed at the end of each test. if connection.vendor == 'mysql': connection.cursor().execute("SET sql_mode = 'TRADITIONAL'") - with self.assertRaisesRegexp(IntegrityError, + with six.assertRaisesRegex(self, IntegrityError, "Could not load fixtures.Article\(pk=1\): .*$"): management.call_command('loaddata', 'invalid.json', verbosity=0, commit=False) @@ -290,10 +289,11 @@ class FixtureLoadingTests(TestCase): self._dumpdata_assert(['fixtures'], """<?xml version="1.0" encoding="utf-8"?> <django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16T12:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16T13:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="10" model="fixtures.book"><field type="CharField" name="name">Achieving self-awareness of Python programs</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"></field></object></django-objects>""", format='xml', natural_keys=True) + class FixtureTransactionTests(TransactionTestCase): def _dumpdata_assert(self, args, output, format='json'): - new_io = StringIO() - management.call_command('dumpdata', *args, **{'format':format, 'stdout':new_io}) + new_io = six.StringIO() + management.call_command('dumpdata', *args, **{'format': format, 'stdout': new_io}) command_output = new_io.getvalue().strip() self.assertEqual(command_output, output) @@ -308,7 +308,7 @@ class FixtureTransactionTests(TransactionTestCase): # Try to load fixture 2 using format discovery; this will fail # because there are two fixture2's in the fixtures directory - with self.assertRaisesRegexp(management.CommandError, + with six.assertRaisesRegex(self, management.CommandError, "Multiple fixtures named 'fixture2'"): management.call_command('loaddata', 'fixture2', verbosity=0) diff --git a/tests/modeltests/many_to_many/tests.py b/tests/modeltests/many_to_many/tests.py index 44bdde3aeb..7d30379b94 100644 --- a/tests/modeltests/many_to_many/tests.py +++ b/tests/modeltests/many_to_many/tests.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from django.test import TestCase +from django.utils import six from .models import Article, Publication @@ -52,7 +53,7 @@ class ManyToManyTests(TestCase): ]) # Adding an object of the wrong type raises TypeError - with self.assertRaisesRegexp(TypeError, "'Publication' instance expected, got <Article.*"): + with six.assertRaisesRegex(self, TypeError, "'Publication' instance expected, got <Article.*"): a6.publications.add(a5) # Add a Publication directly via publications.add by using keyword arguments. p4 = a6.publications.create(title='Highlights for Adults') diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py index 20bd1be0df..4fb19dbc69 100644 --- a/tests/modeltests/many_to_one/tests.py +++ b/tests/modeltests/many_to_one/tests.py @@ -70,7 +70,7 @@ class ManyToOneTests(TestCase): self.assertQuerysetEqual(self.r2.article_set.all(), ["<Article: Paul's story>"]) # Adding an object of the wrong type raises TypeError. - with self.assertRaisesRegexp(TypeError, "'Article' instance expected, got <Reporter.*"): + with six.assertRaisesRegex(self, TypeError, "'Article' instance expected, got <Reporter.*"): self.r.article_set.add(self.r2) self.assertQuerysetEqual(self.r.article_set.all(), [ diff --git a/tests/modeltests/update_only_fields/tests.py b/tests/modeltests/update_only_fields/tests.py index c904c97236..9ca0f968d1 100644 --- a/tests/modeltests/update_only_fields/tests.py +++ b/tests/modeltests/update_only_fields/tests.py @@ -1,7 +1,8 @@ from __future__ import absolute_import -from django.test import TestCase from django.db.models.signals import pre_save, post_save +from django.test import TestCase + from .models import Person, Employee, ProxyEmployee, Profile, Account diff --git a/tests/modeltests/validation/test_error_messages.py b/tests/modeltests/validation/test_error_messages.py index 62ca4e95c6..63c033a750 100644 --- a/tests/modeltests/validation/test_error_messages.py +++ b/tests/modeltests/validation/test_error_messages.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from django.core.exceptions import ValidationError from django.db import models +from django.utils import six from django.utils.unittest import TestCase @@ -18,7 +19,7 @@ class ValidationMessagesTest(TestCase): self._test_validation_messages(f, 'fõo', ["'fõo' value must be an integer."]) # primary_key must be True. Refs #12467. - with self.assertRaisesRegexp(AssertionError, + with six.assertRaisesRegex(self, AssertionError, "AutoFields must have primary_key=True."): models.AutoField(primary_key=False) |
