1

仮想環境内にセットアップがあります:

すべて正常に動作しますが、唯一の問題はテストの実行中です。テスト関数を実行した後、django がデータベースをフラッシュしようとするたびに、エラーがスローされます。

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.

したがって、テストケースごとに、想定どおりに合格または不合格になりますが、この厄介なエラーも発生します。

を実行しdjango-admin.py sqlflush --settings=dev_settings --pythonpath=.たところ、開発データベースが問題なくフラッシュされ、エラーは発生しませんでした。

いくつかのテスト機能で、データベースから取得したいくつかのモデルをチェックしましたが、オブジェクトを正常にフラッシュして再作成しているように見えるため、実際のテスト ケースには影響していません。

トレースバック全体を調べたところ、なぜそれが起こるのか理解できましたが、対処方法がわかりません。どんな助けでも大歓迎です。

編集

Django-nonrel-1.5 でテストを実行してみましたが、問題はありませんでした。1.6 バージョンのバグのようです。

4

1 に答える 1

1

使用SimpleTestCaseまたはカスタム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)

問題はtransaction.atomicコマンドflushにあるため、独自に作成する必要がある場合がありますflush

于 2015-06-04T05:17:31.787 に答える