On 23 elo, 08:30, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
> This ... [1]
>
> from django.db import connection
> connection._rollback()
>
> ... solved a problem for me in unit testing when I catch a deliberate
> DatabaseError.
>
> Django 1.4
> Python 2.7
> PostgreSQL 9.1
>
> My question: Is it safe enough to use a method with a leading underscore?

Generally no. In this specific case you might be safe. However, if you
use _commit() in Django's standard TestCase you might make your
testing DB dirty and some other test could break because of that. This
kind of bug is horrible to debug.

> Is there a better method?

Use TransactionTestCase which is meant for this kind of testing. In
TransactionTestCase you can safely use .commit() and .rollback(). The
cost is slower test execution. See: [https://docs.djangoproject.com/en/
dev/topics/testing/#django.test.TransactionTestCase].

 - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to