Ticket #14223: 14223-r14315.diff
File 14223-r14315.diff, 4.9 KB (added by , 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 153 153 cursor.execute("SET client_encoding to 'UNICODE'") 154 154 return UnicodeCursorWrapper(cursor, 'utf-8') 155 155 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 156 163 def typecast_string(s): 157 164 """ 158 165 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 192 192 finally: 193 193 self.isolation_level = level 194 194 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 1 1 from django.contrib.contenttypes import generic 2 2 from django.contrib.contenttypes.models import ContentType 3 from django.conf import settings4 3 from django.db import models 5 from django.db import connection , DEFAULT_DB_ALIAS4 from django.db import connection 6 5 7 6 8 7 class Square(models.Model): … … 55 54 db_table = 'CaseSensitive_Post' 56 55 57 56 57 class 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 65 class 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 2 2 # Unit and doctests for specific database backends. 3 3 import datetime 4 4 5 from django.conf import settings6 from django.core import management7 5 from django.core.management.color import no_style 8 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS 6 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError 9 7 from django.db.backends.signals import connection_created 10 8 from django.db.backends.postgresql import version as pg_version 11 from django.test import TestCase, skipUnlessDBFeature 9 from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase 12 10 from django.utils import unittest 13 11 14 12 from regressiontests.backends import models … … 225 223 self.assertEqual(list(cursor.fetchmany(2)), [(u'Jane', u'Doe'), (u'John', u'Doe')]) 226 224 self.assertEqual(list(cursor.fetchall()), [(u'Mary', u'Agnelline'), (u'Peter', u'Parker')]) 227 225 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. 237 class 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