1

I built up a website by using django 1.3, Mongodb, and django-mongo-engine. When I run the server by using "manage.py runserver", it is successful. But when I try to do unit testing by "manage.py test", it somehow fails.

Here is the settings.py about database:

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine', 
        'NAME': 'sean_test',                     
        'USER': '',                    
        'PASSWORD': '',                  
        'HOST': '',                      
        'PORT': '',                      
    }
}

And this is the error message in testing:

....Problem installing fixture 'c:\Python27\lib\sitepackages\django\contrib\auth\fixtures\authtestdata.json': Traceback(most recent call last):

File "c:\Python27\lib\site-packages\django\core\management\commands\loaddata.py", line 174, in handleobj.save(using=using)

File "c:\Python27\lib\site-packages\django\core\serializers\base.py", line 165, in savemodels.Model.save_base(self.object, using=using, raw=True)

File "c:\Python27\lib\site-packages\django\db\models\base.py", line 573, in save_baseresult = manager._insert(values, return_id=update_pk, using=using)

File "c:\Python27\lib\site-packages\django\db\models\manager.py", line 195, in _insertreturn insert_query(self.model, values, **kwargs)

File "c:\Python27\lib\site-packages\django\db\models\query.py", line 1438, in insert_queryreturn query.get_compiler(using=using).execute_sql(return_id)

File "c:\Python27\lib\site-packages\djangotoolbox\db\basecompiler.py", line 369, in execute_sqlvalue = self.convert_value_for_db(db_type, value)

File "c:\Python27\lib\site-packages\django_mongodb_engine\compiler.py", line 67, in wrapperreturn func(*args, **kwargs)

File "c:\Python27\lib\site-packages\django_mongodb_engine\compiler.py", line 307, in convert_value_for_dbraise InvalidId(msg)

InvalidId: AutoField (default primary key) values must be strings representing an ObjectId on MongoDB (got u'1' instead)

I know that the django uses integer while mongodb uses string for the primary key, but I don't know why the test will fail when the "runserver" succeeds. Any idea about how to fix this problem?

Thanks!

Community
  • 1
  • 1
gogomac
  • 13
  • 4
  • I knew that using sqlite you don't need `USER` and `PASSWORD` in the `settings.py` file, but I don't know using `mongodb`, doesn't it require that? – eLRuLL Feb 04 '13 at 23:25

2 Answers2

0

So looks like you don't have the testing setup to connect to the mongodb instance when running tests because you are running it from commandline maybe try doing something like this

You have 2 settings files, lsettings.py that doesn't connect and settings.py that does

from lsettings import *
mongodb.connect()

So, while locally testing you can:

python manage.py test --settings=lsettings

Hopefully that gets your around the issue. :)

Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101
  • I don't think it works. The answer you provided seems to be same with the answer in [link](http://stackoverflow.com/questions/4774800/mongoengine-connect-in-settings-py-testing-problem), but we are facing different problems. I want to use mongodb in test as well – gogomac Feb 05 '13 at 21:03
0

The issue is with loading fixtures as described in this later question.

You can fix it by replacing the ids in your fixtures with proper ObjectIds as described in the corresponding answer.

From your traceback, the issue is with "authtestdata.json" fixtures provided by django.contrib.auth. As these are not compatible with ObjectIds, you need either to update your tests to use different fixtures, or skip the failing tests if you cannot update them. For example, Django's auth tests will not pass.

Community
  • 1
  • 1
Nicolas Cortot
  • 6,591
  • 34
  • 44