Ticket #14223: 14223.2.diff

File 14223.2.diff, 4.5 KB (added by Ramiro Morales, 14 years ago)

Enhanced patch, check for django.db.IntegrityError instead of django.db.utils IntegrityError in the tests, added a comment

  • django/db/backends/postgresql/base.py

    diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
    a b  
    150150            cursor.execute("SET client_encoding to 'UNICODE'")
    151151        return UnicodeCursorWrapper(cursor, 'utf-8')
    152152
     153    def _commit(self):
     154        if self.connection is not None:
     155            try:
     156                return self.connection.commit()
     157            except Database.IntegrityError, e:
     158                raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
     159
    153160def typecast_string(s):
    154161    """
    155162    Cast all returned strings to unicode strings.
  • django/db/backends/postgresql_psycopg2/base.py

    diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
    a b  
    189189        finally:
    190190            self.isolation_level = level
    191191            self.features.uses_savepoints = bool(level)
     192
     193    def _commit(self):
     194        if self.connection is not None:
     195            try:
     196                return self.connection.commit()
     197            except Database.IntegrityError, e:
     198                raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
  • tests/regressiontests/backends/models.py

    diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py
    a b  
    5454    class Meta:
    5555        db_table = 'CaseSensitive_Post'
    5656
     57
     58class Reporter(models.Model):
     59    first_name = models.CharField(max_length=30)
     60    last_name = models.CharField(max_length=30)
     61
     62    def __unicode__(self):
     63        return u"%s %s" % (self.first_name, self.last_name)
     64
     65
     66class Article(models.Model):
     67    headline = models.CharField(max_length=100)
     68    pub_date = models.DateField()
     69    reporter = models.ForeignKey(Reporter)
     70
     71    def __unicode__(self):
     72        return self.headline
     73
     74
    5775qn = connection.ops.quote_name
    5876
    5977__test__ = {'API_TESTS': """
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    a b  
    44import unittest
    55
    66from django.conf import settings
    7 from django.core import management
    87from django.core.management.color import no_style
    9 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
     8from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError
    109from django.db.backends.signals import connection_created
    1110from django.db.backends.postgresql import version as pg_version
    12 from django.test import TestCase
     11from django.test import TestCase, TransactionTestCase
    1312
    1413from regressiontests.backends import models
    1514
     
    186185            data.clear()
    187186            cursor = connection.cursor()
    188187            self.assertTrue(data == {})
     188
     189
     190# We don't enclose these tests inside a big 'if' test because that would mean
     191# we need to differentiate between MySQL+InnoDB, MySQL+MYISAM, if sqlite3
     192# (if/once we get #14204 fixed) has referential integrity turned on or not,
     193# something that would be controlled by runtime support and user preference
     194# (and possibly Oracle? I don't know if it enforces referential integrity).
     195# Instead, we test if an exception is raised by the backend and if so, we
     196# verify if its type is django.database.db.IntegrtyError.
     197class FkConstraintsTest(TransactionTestCase):
     198
     199    def setUp(self):
     200        # Create a Reporter.
     201        self.r = models.Reporter.objects.create(first_name='John', last_name='Smith')
     202
     203    def test_integrity_checks_on_creation(self):
     204        """Try to create a model instance that violates a FK constraint. Should fail"""
     205        a = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
     206        try:
     207            a.save()
     208        except Exception, e:
     209            self.assertEqual(IntegrityError, type(e))
     210
     211    def test_integrity_checks_on_update(self):
     212        """Try to update a model instance introducing a FK constraint violation. Should fail"""
     213        # Create an Article.
     214        models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
     215        # Retrive it from the DB
     216        a = models.Article.objects.get(headline="Test article")
     217        a.reporter_id = 30
     218        try:
     219            a.save()
     220        except Exception, e:
     221            self.assertEqual(IntegrityError, type(e))
Back to Top