diff options
| author | Karen Tracey <kmtracey@gmail.com> | 2011-08-07 00:43:26 +0000 |
|---|---|---|
| committer | Karen Tracey <kmtracey@gmail.com> | 2011-08-07 00:43:26 +0000 |
| commit | be87f0b0ec992e6f3e72735d8e95c654da969f6d (patch) | |
| tree | fa4d16b640e716dcd8271f2e6a7e3bb2953cbe5d /tests/regressiontests/backends | |
| parent | e3c89346d217fca92b62a6d11df6b4b6d5be28a2 (diff) | |
Fixed #3615: Added support for loading fixtures with forward references on database backends (such as MySQL/InnoDB) that do not support deferred constraint checking. Many thanks to jsdalton for coming up with a clever solution to this long-standing issue, and to jacob, ramiro, graham_king, and russellm for review/testing. (Apologies if I missed anyone else who helped here.)
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16590 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/backends')
| -rw-r--r-- | tests/regressiontests/backends/tests.py | 64 |
1 files changed, 61 insertions, 3 deletions
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index 29db6a705a..27d3dfdddc 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- # Unit and doctests for specific database backends. +from __future__ import with_statement import datetime from django.conf import settings from django.core.management.color import no_style -from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError +from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError, transaction from django.db.backends.signals import connection_created from django.db.backends.postgresql_psycopg2 import version as pg_version from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase @@ -328,7 +329,8 @@ class FkConstraintsTests(TransactionTestCase): try: a.save() except IntegrityError: - pass + return + self.skipTest("This backend does not support integrity checks.") def test_integrity_checks_on_update(self): """ @@ -343,4 +345,60 @@ class FkConstraintsTests(TransactionTestCase): try: a.save() except IntegrityError: - pass + return + self.skipTest("This backend does not support integrity checks.") + + def test_disable_constraint_checks_manually(self): + """ + When constraint checks are disabled, should be able to write bad data without IntegrityErrors. + """ + with transaction.commit_manually(): + # Create an Article. + models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) + # Retrive it from the DB + a = models.Article.objects.get(headline="Test article") + a.reporter_id = 30 + try: + connection.disable_constraint_checking() + a.save() + connection.enable_constraint_checking() + except IntegrityError: + self.fail("IntegrityError should not have occurred.") + finally: + transaction.rollback() + + def test_disable_constraint_checks_context_manager(self): + """ + When constraint checks are disabled (using context manager), should be able to write bad data without IntegrityErrors. + """ + with transaction.commit_manually(): + # Create an Article. + models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) + # Retrive it from the DB + a = models.Article.objects.get(headline="Test article") + a.reporter_id = 30 + try: + with connection.constraint_checks_disabled(): + a.save() + except IntegrityError: + self.fail("IntegrityError should not have occurred.") + finally: + transaction.rollback() + + def test_check_constraints(self): + """ + Constraint checks should raise an IntegrityError when bad data is in the DB. + """ + with transaction.commit_manually(): + # Create an Article. + models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) + # Retrive it from the DB + a = models.Article.objects.get(headline="Test article") + a.reporter_id = 30 + try: + with connection.constraint_checks_disabled(): + a.save() + with self.assertRaises(IntegrityError): + connection.check_constraints() + finally: + transaction.rollback() |
