Ticket #14223: 14223-r14315.diff

File 14223-r14315.diff, 4.9 KB (added by Ramiro Morales, 14 years ago)
  • django/db/backends/postgresql/base.py

    diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
    a b  
    153153            cursor.execute("SET client_encoding to 'UNICODE'")
    154154        return UnicodeCursorWrapper(cursor, 'utf-8')
    155155
     156    def _commit(self):
     157        if self.connection is not None:
     158            try:
     159                return self.connection.commit()
     160            except Database.IntegrityError, e:
     161                raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
     162
    156163def typecast_string(s):
    157164    """
    158165    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  
    192192        finally:
    193193            self.isolation_level = level
    194194            self.features.uses_savepoints = bool(level)
     195
     196    def _commit(self):
     197        if self.connection is not None:
     198            try:
     199                return self.connection.commit()
     200            except Database.IntegrityError, e:
     201                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  
    11from django.contrib.contenttypes import generic
    22from django.contrib.contenttypes.models import ContentType
    3 from django.conf import settings
    43from django.db import models
    5 from django.db import connection, DEFAULT_DB_ALIAS
     4from django.db import connection
    65
    76
    87class Square(models.Model):
     
    5554        db_table = 'CaseSensitive_Post'
    5655
    5756
     57class Reporter(models.Model):
     58    first_name = models.CharField(max_length=30)
     59    last_name = models.CharField(max_length=30)
     60
     61    def __unicode__(self):
     62        return u"%s %s" % (self.first_name, self.last_name)
     63
     64
     65class Article(models.Model):
     66    headline = models.CharField(max_length=100)
     67    pub_date = models.DateField()
     68    reporter = models.ForeignKey(Reporter)
     69
     70    def __unicode__(self):
     71        return self.headline
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    a b  
    22# Unit and doctests for specific database backends.
    33import datetime
    44
    5 from django.conf import settings
    6 from django.core import management
    75from django.core.management.color import no_style
    8 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
     6from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError
    97from django.db.backends.signals import connection_created
    108from django.db.backends.postgresql import version as pg_version
    11 from django.test import TestCase, skipUnlessDBFeature
     9from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase
    1210from django.utils import unittest
    1311
    1412from regressiontests.backends import models
     
    225223        self.assertEqual(list(cursor.fetchmany(2)), [(u'Jane', u'Doe'), (u'John', u'Doe')])
    226224        self.assertEqual(list(cursor.fetchall()), [(u'Mary', u'Agnelline'), (u'Peter', u'Parker')])
    227225
     226
     227# We don't make these tests conditional because that means we would need to
     228# check and differentiate between:
     229# * MySQL+InnoDB, MySQL+MYISAM (something we currently can't do).
     230# * if sqlite3 (if/once we get #14204 fixed) has referential integrity turned
     231#   on or not, something that would be controlled by runtime support and user
     232#   preference.
     233# * and possibly Oracle?. I don't know if it enforces referential integrity.
     234#
     235# Instead, we check if an exception is raised by the backend and if so, we
     236# verify if its type is django.database.db.IntegrityError.
     237class FkConstraintsTests(TransactionTestCase):
     238
     239    def setUp(self):
     240        # Create a Reporter.
     241        self.r = models.Reporter.objects.create(first_name='John', last_name='Smith')
     242
     243    def test_integrity_checks_on_creation(self):
     244        """Try to create a model instance that violates a FK constraint. Should fail"""
     245        a = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
     246        try:
     247            a.save()
     248        except IntegrityError:
     249            pass
     250
     251    def test_integrity_checks_on_update(self):
     252        """Try to update a model instance introducing a FK constraint violation. Should fail"""
     253        # Create an Article.
     254        models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
     255        # Retrive it from the DB
     256        a = models.Article.objects.get(headline="Test article")
     257        a.reporter_id = 30
     258        try:
     259            a.save()
     260        except IntegrityError:
     261            pass
Back to Top