加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Django测试 – 南迁移报告’没有这样的表’,但我可以

发布时间:2020-12-20 13:37:05 所属栏目:Python 来源:网络整理
导读:我正在使用py2exe和amp;的Django实现. Qt的.有一个使用South管理的SQLite数据库. 使用内存数据库在源上运行django测试很好,但我也尝试使用PhantomCSS(PhantomJS)执行测试以执行CSS回归测试. 为此,我有一个LiveServerTestCase(source)的子类.我正在使用磁盘上
我正在使用py2exe和amp;的Django实现. Qt的.有一个使用South管理的SQLite数据库.

使用内存数据库在源上运行django测试很好,但我也尝试使用PhantomCSS(PhantomJS)执行测试以执行CSS回归测试.

为此,我有一个LiveServerTestCase(source)的子类.我正在使用磁盘上的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)
        )

我用以下命令创建的0??7001(这昨天加载所以似乎是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:Python27libmultiprocessingprocess.py",line 258,in _bootstrap
 [exec]     self.run()
 [exec]   File "C:Python27libmultiprocessingprocess.py",line 114,in run
 [exec]     self._target(*self._args,**self._kwargs)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjango_offlinestartuphelpers.py",line 124,in django_server_helper
 [exec]     *fixture_list
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangocoremanagementbase.py",line 283,in execute
 [exec]     output = self.handle(*args,**options)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangocoremanagementcommandsloaddata.py",line 55,in handle
 [exec]     self.loaddata(fixture_labels)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangocoremanagementcommandsloaddata.py",line 84,in loaddata
 [exec]     self.load_label(fixture_label)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangocoremanagementcommandsloaddata.py",line 140,in load_label
 [exec]     obj.save(using=self.using)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangocoreserializersbase.py",line 164,in save
 [exec]     models.Model.save_base(self.object,using=using,raw=True)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbmodelsbase.py",line 578,in save_base
 [exec]     updated = self._save_table(raw,cls,force_insert,force_update,using,update_fields)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbmodelsbase.py",line 638,in _save_table
 [exec]     updated = self._do_update(base_qs,pk_val,values,line 676,in _do_update
 [exec]     return base_qs.filter(pk=pk_val)._update(values) > 0
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbmodelsquery.py",line 509,in _update
 [exec]     return query.get_compiler(self.db).execute_sql(None)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbmodelssqlcompiler.py",line 971,in execute_sql
 [exec]     cursor = super(SQLUpdateCompiler,self).execute_sql(result_type)

 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbmodelssqlcompiler.py",line 777,in execute_sql
 [exec]     cursor.execute(sql,params)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbutils.py",line 105,in inner
 [exec]     return func(*args,**kwargs)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbutils.py",line 99,in __exit__
 [exec]     six.reraise(dj_exc_type,dj_exc_value,traceback)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbutils.py",**kwargs)
 [exec]   File "C:Usersmarkwworkbptrti3bsrcdjangodbbackendssqlite3base.py",line 445,in execute
 [exec]     return Database.Cursor.execute(self,query,params)
 [exec] OperationalError: Problem installing fixture 'C:Usersmarkwworksrcphantomfixturesbasic_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测试负责设置测试数据库 – 您是否使用不同的/自定义测试运行器运行?如果没有,那么尝试单步执行manage.py测试,看看测试代码的初始化顺序与syncdb的调用顺序是什么.

如果您使用自己的测试运行器,则向syncdb添加适当的调用.如果使用South,请确保选择South版本,和/或设置SOUTH_TESTS_MIGRATE = False

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读