7

Can somebody please explain to me how to properly test Postgres DB errors, particularly IntegrityError. For example i have next test:

class TestSlugs(TestCase):
    # This slug must be unique
    b = BookPublisher(slug=self.duplicate_slug)
    self.assertRaises(IntegrityError, b.save)

    #check if there's only one BookPublisher
    self.assertEquals(BookPublisher.objects.count(), 1)

Here it catches the IntegrityError but then all operations will fail, because that's how postgres works, ok. I see in docs that i can use transaction.rollback() but where: in test or in save() method?

Also, i don't like the idea of writing rollbacks by hand, why can't django just try to save, and if it fails - give me IntegrityError and let me continue to work.

I'm using django 1.1

2 Answers 2

2
+250

Don't alter the save() method, since you want it to propagate through under normal operations. You should catch the exception in your Test class and rollback there (note that since you're testing transactions you must subclass TransactionalTestCase instead of the normal TestCase).

Sign up to request clarification or add additional context in comments.

2 Comments

Ok thanks. So in live code, everything will be fine after catching IntegrityError? Or should i still rollback there explicitly?
You should avoid ever causing IntegrityError in your own code, and treat any SQL error as a bug. So you should check before trying some modification whether that modification will be sensible. If you find that the modification cannot be made, you can rollback and report the problem to the user. Or you can decide to skip the modification, or make a different modification instead.
2

not 100% sure that this is valid, but you could do:

def save(self):
    transaction.commit()
    try:
        super(MyModel, self).save()
    except IntegrityError:
        transaction.rollback()
    else:
        transaction.commit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.