加载夹具时 django unittest 出错

发布于 2024-09-25 12:12:38 字数 1939 浏览 7 评论 0原文

我正在为 django 应用程序进行单元测试。我需要数据库中的一些数据进行测试,因此我使用 json 夹具。

我有两个固定装置:

  1. 供用户使用,并且工作正常。
  2. 对于某些网页,

固定装置 2 会导致以下错误:

Problem installing fixture 'C:\Users\luc\Dev\Hg\mnl-adminpub\website\fixtures\website-unittest.json': Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\django\core\management\commands\loaddata.py", line 169, in handle
    obj.save(using=using)
  File "C:\Python26\lib\site-packages\django\core\serializers\base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "C:\Python26\lib\site-packages\django\db\models\base.py", line 528, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "C:\Python26\lib\site-packages\django\db\models\manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 1479, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1366, "Incorrect string value: '\\xE2\\x80\\xA8<br...' for column 'html' at row 1")

我已使用转储数据创建了固定装置。我已经用 loaddata 测试过它并且工作正常。我正在使用mysql。

知道问题的可能原因吗?

感谢您的帮助

I am making unittests for a django app. I need some data in the database for my tests so I am using a json fixture.

I have two fixtures:

  1. for users and it works ok.
  2. for some webpages

The fixture 2 cause the following error:

Problem installing fixture 'C:\Users\luc\Dev\Hg\mnl-adminpub\website\fixtures\website-unittest.json': Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\django\core\management\commands\loaddata.py", line 169, in handle
    obj.save(using=using)
  File "C:\Python26\lib\site-packages\django\core\serializers\base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "C:\Python26\lib\site-packages\django\db\models\base.py", line 528, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "C:\Python26\lib\site-packages\django\db\models\manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "C:\Python26\lib\site-packages\django\db\models\query.py", line 1479, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1366, "Incorrect string value: '\\xE2\\x80\\xA8<br...' for column 'html' at row 1")

I've created the fixture with dumpdata. I've tested it with loaddata and it works fine. I am using mysql.

Any idea of the possible cause of the problem?

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女山的月亮 2024-10-02 12:12:38

您应该使用 TEST_CHARSET,但在内部 数据库配置。像这样:

DATABASES = {
      'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_sbet',                      
        'USER': 'test_sbet',                      
        'TEST_CHARSET': 'UTF8',
    }
}

You should use TEST_CHARSET, but inside DATABASE config. Like that:

DATABASES = {
      'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test_sbet',                      
        'USER': 'test_sbet',                      
        'TEST_CHARSET': 'UTF8',
    }
}
爱已欠费 2024-10-02 12:12:38

如果这只发生在测试期间,我会怀疑一些 TEST_ 设置,例如 TEST_CHARSET。也许您的(常规,而不是测试)数据库有一些非默认字符集?如果是这样,您需要指定测试数据库应设置的字符集。

顺便说一句,如果您使用的是旧版本(1.2 之前),那么应该使用 TEST_DATABASE_CHARSET

If this only happens during testing, I would suspect some of the TEST_ settings, like TEST_CHARSET. Maybe your (regular, not test) db has some non default charset? If so, you need to specify which charset the test database should be set up with.

BTW, If you are using an older (pre 1.2) version, then this should be done using TEST_DATABASE_CHARSET.

三人与歌 2024-10-02 12:12:38

有同样的问题。在 MySql 数据库中使用 utfmb4 并将 CHARSET 键添加到 TEST 字典中会有所帮助(Django 1.11):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            ...
            'charset': 'utf8mb4',
        },
        'TEST': {'CHARSET': 'utf8mb4',},
    }
}

Had the same issue. Using utfmb4 in MySql database and adding a CHARSET key to the TEST dictionary helped (Django 1.11):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'OPTIONS': {
            ...
            'charset': 'utf8mb4',
        },
        'TEST': {'CHARSET': 'utf8mb4',},
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文