> What is the proper way to "reset" or "rollback" the connection in a
> test case after an IntegrityError has been raised? This only seems to
> be an issue with PostgresSQL; in sqlite the "current transaction is
> aborted, commands ignored" doesn't seem to come up.
To make this more concrete, here is an example model and test case.
What I'd like to do is catch an IntegrityError and then continue with
the rest of the test case. When using postgresql_psycopg2, I get
stuck in an InternalError once the IntegrityError is raised. SQLite
has no trouble.
from django.db import models, transaction, IntegrityError
from django.test import TestCase
class SomeModel(models.Model):
name = models.CharField(max_length=50, unique=True)
class TransactionRollbackTest(TestCase):
def test_transaction_rollback(self):
# first one should work fine
self.assertTrue(self.create_spam())
# second time violates unique constraint
self.assertRaises(IntegrityError, self.create_spam)
# have we recovered?
self.assertEquals(len(SomeModel.objects.all()), 1)
def create_spam(self):
spam = SomeModel(name="Spam")
try:
spam.save()
except IntegrityError:
transaction.rollback()
raise
return True
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---