我一直在尝试让我的 django 测试为我正在构建的 API 工作。我对 Python 比较陌生,并且已经坚持了一段时间。
一些简单的事实:
- 我正在使用 2 个数据库 - 我的用户使用 postgresql 数据库,其他所有数据库使用 mongo 数据库。
- 我正在使用 TastyPie、Django-MongoDB-Engine、Djangotoolbox、Django-Non-rel、Pymongo 和 psycopg2
- 我已成功连接到数据库。当我从 shell 中保存 auth_user 时,它会保存到 sql 数据库中,而我的应用程序中的模型会保存到 mongoDB
我必须将它与另一个 API 集成,因此仅限于我对数据库和库的选择。
我的大问题是测试 - 我无法让它工作。
当我尝试使用 ./manage.py 测试运行 Django 的测试时,我不断收到此错误:
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce', u'blahblah'), ('key', u'blahblah')]) failed: auth fails
经过广泛的搜索,我仍然不确定它是什么。我知道显然有些东西正在尝试使用我的 MongoDB 进行身份验证,然后它不允许它。我不介意使用这个数据库进行测试,因为它无论如何都是一个测试数据库。
请帮忙!我的配置和错误消息如下。
我将数据库设置设置为包括两个数据库,如下所示:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'main_db',
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
},
'mongo': {
'ENGINE': 'django_mongodb_engine',
'NAME': 'mongo_db',
'USER': os.environ['MONGO_USERNAME'],
'PASSWORD': os.environ['MONGO_PASSWORD'],
'HOST': os.environ['MONGO_HOSTNAME'],
'PORT': os.environ['MONGO_PORT'],
}
}
DATABASE_ROUTERS = ['myproject.database_router.AuthRouter']
如您所见,我创建了自己的自定义路由器:
sql_databases = ['admin', 'auth', 'contenttypes', 'tastypie'
'sessions', 'messages', 'staticfiles']
class AuthRouter(object):
"""
A router to control all database operations on models in the
auth application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read auth models go to auth_db.
"""
if model._meta.app_label in sql_databases:
return 'default'
return 'mongo'
def db_for_write(self, model, **hints):
"""
Attempts to write auth models go to auth_db.
"""
if model._meta.app_label in sql_databases:
return 'default'
return 'mongo'
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations only if model does includes auth app.
"""
if obj1._meta.app_label in sql_databases or \
obj2._meta.app_label in sql_databases:
return True
return False
def allow_migrate(self, db, model):
"""
Make sure the auth app only appears in the 'auth_db'
database.
"""
if db == 'default':
return model._meta.app_label in sql_databases
elif model._meta.app_label in sql_databases:
return False
return False
完整的跟踪在这里:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "myproject/djenv/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "myproject/djenv/lib/python2.7/site-packages/django/test/runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 196, in create_test_db
self.connection._reconnect()
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 279, in _reconnect
self._connect()
File "myproject/djenv/lib/python2.7/site-packages/django_mongodb_engine/base.py", line 268, in _connect
if not self.database.authenticate(user, password):
File "myproject/djenv/lib/python2.7/site-packages/pymongo/database.py", line 891, in authenticate
self.connection._cache_credentials(self.name, credentials)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 459, in _cache_credentials
auth.authenticate(credentials, sock_info, self.__simple_command)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/auth.py", line 243, in authenticate
auth_func(credentials[1:], sock_info, cmd_func)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/auth.py", line 222, in _authenticate_mongo_cr
cmd_func(sock_info, source, query)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/mongo_client.py", line 690, in __simple_command
helpers._check_command_response(response, None, msg)
File "myproject/djenv/lib/python2.7/site-packages/pymongo/helpers.py", line 178, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: command SON([('authenticate', 1), ('user', u'test'), ('nonce', u'blahblah'), ('key', u'blahblah')]) failed: auth fails
任何帮助表示赞赏!