Ticket #14223: 14223.2.diff
File 14223.2.diff, 4.5 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 150 150 cursor.execute("SET client_encoding to 'UNICODE'") 151 151 return UnicodeCursorWrapper(cursor, 'utf-8') 152 152 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 153 160 def typecast_string(s): 154 161 """ 155 162 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 189 189 finally: 190 190 self.isolation_level = level 191 191 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 54 54 class Meta: 55 55 db_table = 'CaseSensitive_Post' 56 56 57 58 class 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 66 class 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 57 75 qn = connection.ops.quote_name 58 76 59 77 __test__ = {'API_TESTS': """ -
tests/regressiontests/backends/tests.py
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
a b 4 4 import unittest 5 5 6 6 from django.conf import settings 7 from django.core import management8 7 from django.core.management.color import no_style 9 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS 8 from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, IntegrityError 10 9 from django.db.backends.signals import connection_created 11 10 from django.db.backends.postgresql import version as pg_version 12 from django.test import TestCase 11 from django.test import TestCase, TransactionTestCase 13 12 14 13 from regressiontests.backends import models 15 14 … … 186 185 data.clear() 187 186 cursor = connection.cursor() 188 187 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. 197 class 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))