簡體   English   中英

Django測試 - 南遷移報告'沒有這樣的表',但我可以看到數據庫中的表

[英]Django Test - South migration reports 'no such table' but I can see said table in the db

我正在使用py2exe和Qt的Django實現。 有一個使用South管理的SQLite數據庫。

使用內存數據庫在源上運行django測試很好,但我也嘗試使用PhantomCSS(PhantomJS)執行測試以執行CSS回歸測試。

為此,我有一個LiveServerTestCase的子類( 源代碼 )。 我正在使用磁盤上的sqlite數據庫運行Django測試,在啟動自定義服務器進程時通過調用loaddata來加載fixture(請參閱最后的服務器函數)。

它的測試看起來像這樣;

class PhantomTestBackupRestore(PhantomTestCase):

    fixtures = ['basic_db.json',]

    def test_backup(self):
        self.assertTrue(
            self.phantom(RUNNER_PATH,
                screenID='lupyvAQL',
                host='http://127.0.0.1',
                port=buildconstants.PRODUCT_LISTENPORT)
        )

我正在使用以下命令創建的夾具 (這昨天加載所以似乎是loaddata一個氣質問題);

manage.py dumpdata -n --indent 4 --exclude=contenttypes --exclude=auth --format=json > phantom/fixtures/basic_db.json

我得到以下stacktrace;

 [exec] Traceback (most recent call last):
 [exec]   File "C:\Python27\lib\multiprocessing\process.py", line 258, in _bootstrap
 [exec]     self.run()
 [exec]   File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
 [exec]     self._target(*self._args, **self._kwargs)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django_offline\startuphelpers.py", line 124, in django_server_helper
 [exec]     *fixture_list
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\core\management\base.py", line 283, in execute
 [exec]     output = self.handle(*args, **options)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\core\management\commands\loaddata.py", line 55, in handle
 [exec]     self.loaddata(fixture_labels)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\core\management\commands\loaddata.py", line 84, in loaddata
 [exec]     self.load_label(fixture_label)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\core\management\commands\loaddata.py", line 140, in load_label
 [exec]     obj.save(using=self.using)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\core\serializers\base.py", line 164, in save
 [exec]     models.Model.save_base(self.object, using=using, raw=True)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\models\base.py", line 578, in save_base
 [exec]     updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\models\base.py", line 638, in _save_table
 [exec]     updated = self._do_update(base_qs, using, pk_val, values, update_fields)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\models\base.py", line 676, in _do_update
 [exec]     return base_qs.filter(pk=pk_val)._update(values) > 0
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\models\query.py", line 509, in _update
 [exec]     return query.get_compiler(self.db).execute_sql(None)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\models\sql\compiler.py", line 971, in execute_sql
 [exec]     cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)

 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\models\sql\compiler.py", line 777, in execute_sql
 [exec]     cursor.execute(sql, params)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\utils.py", line 105, in inner
 [exec]     return func(*args, **kwargs)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\utils.py", line 99, in __exit__
 [exec]     six.reraise(dj_exc_type, dj_exc_value, traceback)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\utils.py", line 105, in inner
 [exec]     return func(*args, **kwargs)
 [exec]   File "C:\Users\markw\work\bptrti3b\src\django\db\backends\sqlite3\base.py", line 445, in execute
 [exec]     return Database.Cursor.execute(self, query, params)
 [exec] OperationalError: Problem installing fixture 'C:\Users\markw\work\src\phantom\fixtures\basic_db.json': 
Could not load sites.Site(pk=1): no such table: django_site

因為我正在處理多處理,我不確定manage.py是否可能在發生這些錯誤時使用內存數據庫或其他東西,但是使用以下數據庫設置我不認為它使用內存進行測試;

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(
            APPDATA_DIR, 'sqlite3.db',
        ),
        'TEST_NAME': os.path.join(
            APPDATA_DIR, 'test.db',
        )
    }
}

如果它有用,啟動服務器的功能;

def django_server_helper(qt_pipe=None, fixture_list=None):
    """    
    This is for use in testing-only modes (e.g. PhantomCSS).

    If this process is a subprocess of the main application, qt_pipe will be
    supplied to allow us to access functionality provided by django_offline.
    The other end of this pipe comes out in the DjangoOfflineApp instance.

    @type qt_pipe: multiprocessing.Connection
    @param qt_pipe: Pipe for communicating with the main django_offline app.
    @type fixture_list: list or None
    @param fixture_list: List of JSON fixture files to load in for testing.

    @rtype: None
    """
    use_threading = True

    if fixture_list is not None:
        # A fixture list has been supplied, so we're in test mode.
        from django.core.management.commands import loaddata
        loaddata.Command().execute(
            verbosity = 0,
            database = "default",
            settings = "mysite.settings",
            *fixture_list
        )

    if qt_pipe is not None:
        # A pipe has been supplied, so we're a subprocess
        from django_offline import connector
        connector.QT_PIPE = qt_pipe

    # start the django server up
    from django_offline import settings_central
    try:
        from django.contrib.staticfiles.management.commands import runserver
        runserver.Command().execute(
            use_threading=use_threading,
            use_static_handler=True,
            insecure_serving=True,
            addrport='127.0.0.1:{0}'.format(settings_central.LISTEN_PORT),
        )
    except socket.error as e:
        logging.exception("Socket occupied; not starting a server.")
        sys.exit(1)
    sys.exit(0)

我是否需要在運行loaddata之前調用syncdb ,或者這可以通過使用fixture列表向South調用來完成?

問題是代碼在設置數據庫表之前運行loaddata ,因此無法寫入Site值。

通常manage.py test負責設置測試數據庫 - 您是否使用不同的/自定義測試運行器運行? 如果沒有,那么嘗試單步執行manage.py test ,看看測試代碼的初始化順序與syncdb的調用順序是什么。

如果您使用自己的測試運行器,則向syncdb添加適當的調用。 如果使用South,請確保選擇South版本,和/或設置SOUTH_TESTS_MIGRATE = False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM