Error in django unittest when loading the device

I am doing unittests for a django application. I need some database data for my tests, so I use json fixture.

I have two lights:

  • for users, and it works fine.
  • for some web pages

Device 2 causes 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 created a device with dumps. I tested it with loaddata and it works great. I am using mysql.

Any idea on a possible cause of the problem?

thanks for the help

+4
source share
3 answers

You must use TEST_CHARSET , but inside the DATABASE configuration. For instance:

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

If this happens only during testing, I would suspect some TEST_ settings, for example TEST_CHARSET . Perhaps your (regular, non-test) db has the wrong encoding? If so, you need to specify what encoding the test database should set.

BTW. If you are using an older version (pre 1.2), then you need to do this using TEST_DATABASE_CHARSET .

+1
source

There was the same problem. Using utfmb4 in the MySql database and adding the CHARSET key to the TEST dictionary helped (Django 1.11):

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

All Articles