1

How do you test Django code that uses commit_manually() from a unittest?

I have a long-running method that deletes large numbers of records, enough so that it can't keep all those changes in a single transaction without consuming all system memory. So I wrap it in @commit_manually and periodically call commit().

However, in Django 1.6, all unittests are now wrapped in transaction.atomic(), so if I try and make any manual commits, I get the error:

django.db.transaction.TransactionManagementError: This is forbidden when an 'atomic' block is active.

What's a work-around for this?

Cerin
  • 60,957
  • 96
  • 316
  • 522

1 Answers1

1

Using TransactionTestCase instead of TestCase should give you this ability.

https://docs.djangoproject.com/en/1.6/topics/testing/tools/#django.test.TransactionTestCase

https://docs.djangoproject.com/en/1.6/topics/testing/advanced/#advanced-features-of-transactiontestcase

Loic Duros
  • 5,472
  • 10
  • 43
  • 56
  • @Cerin did using TransactionTestCase fix your issue? – Loic Duros May 01 '14 at 19:35
  • Unfortunately, no. All that gives me is "TransactionManagementError: Your database backend doesn't behave properly when autocommit is off. Turn it on before using 'atomic'." I'm using Sqlite3 as my unittest db backend. – Cerin Sep 02 '14 at 02:01