1

I have a setup inside a virtual environment:

Everything works fine, the only problem is during running tests. Every time django tries to flush database after running a test function it throws an error:

Traceback (most recent call last):
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 187, in __call__
    self._post_teardown()
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 796, in _post_teardown
    self._fixture_teardown()
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 889, in _fixture_teardown
    return super(TestCase, self)._fixture_teardown()
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/test/testcases.py", line 817, in _fixture_teardown
    inhibit_post_syncdb=self.available_apps is not None)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 79, in handle_noargs
    six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2])
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/core/management/commands/flush.py", line 67, in handle_noargs
    savepoint=connection.features.can_rollback_ddl):
  File "/home/lehins/.virtualenvs/studentpal/local/lib/python2.7/site-packages/django/db/transaction.py", line 251, in __enter__
    "The outermost 'atomic' block cannot use "
CommandError: Database test_dev_db couldn't be flushed. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the expected database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: The outermost 'atomic' block cannot use savepoint = False when autocommit is off.

So for each test case, I have a pass or a fail, like it suppose to, but I also get this annoying error.

I did run django-admin.py sqlflush --settings=dev_settings --pythonpath=., which flushed my development database just fine, with no errors.

In a couple test functions I checked a few models pulled from database, and it seems to be flushing and recreating objects just fine, so that's why it is not affecting actual test cases.

I went though the whole traceback and I kind of understand why it happens, but I cannot figure out how to deal with. Any help is appreciated.

Edit

Just tried running tests with Django-nonrel-1.5, there was no problems. It seems like a bug in 1.6 version.

lehins
  • 9,642
  • 2
  • 35
  • 49
  • Did you found solution of your problem ? – bux Oct 03 '15 at 15:15
  • @bux Unfortunately, I have not. I am no longer working on a project using Django-nonrel so I cannot even confirm if it was fixed or not. Post a solution here if you find one. – lehins Oct 03 '15 at 23:17

1 Answers1

1

Use SimpleTestCase or a custom TestCase

class CustomTestCase(TestCase):

    def _fixture_teardown(self):
        for db_name in self._databases_names(include_mirrors=False):
            call_command('custom_flush', verbosity=0, interactive=False,
                         database=db_name, skip_validation=True,
                         reset_sequences=False,
                         allow_cascade=self.available_apps is not None,
                         inhibit_post_syncdb=self.available_apps is not None)

Since the problem is transaction.atomic in command flush, you may have to write your own flush.

Daishi
  • 93
  • 2