2015-08-15 24 views
-1

我正在尝试配置Django应用程序的unittest。 需要设置一些值作为数据库,所以我想我可以使用override_settings函数。当我运行此代码时,出现以下错误。SyntaxError在Django中使用override_settings

代码:

from zope.interface import implements 
from twisted.python import failure, log 
from twisted.cred import portal, checkers, error, credentials 
from twisted.internet import defer 

from django.test import TestCase 
from django.test.utils import override_settings 
from django.conf import settings 
settings.configure() 

from django.contrib.auth.models import User, check_password 

@override_settings(DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}) 
class DjangoAuthChecker: 
    implements(checkers.ICredentialsChecker) 
    credentialInterfaces = (credentials.IUsernamePassword, 
    credentials.IUsernameHashedPassword) 

    def _passwordMatch(self, matched, user): 
     if matched: 
      return user 
     else: 
      return failure.Failure(error.UnauthorizedLogin()) 

    def requestAvatarId(self, credentials, connection): 

     try: 
      user = User.objects.get(username=credentials.username) 
      return defer.maybeDeferred(
       check_password, 
       credentials.password, 
       user.password).addCallback(self._passwordMatch, user) 
     except User.DoesNotExist: 
      return defer.fail(error.UnauthorizedLogin()) 

你能告诉我什么是错的?我还需要一些关于Django测试的良好建议。

错误:

(.venv_test)[email protected]:~/Dev/client/protocol/tests$ python test_creds.py 
Traceback (most recent call last): 
    File "test_creds.py", line 48, in <module> 
    from ampauth.testing import DjangoAuthChecker 
    File "/home/sgongar/Dev/client/protocol/ampauth/testing.py", line 36 
    @override_settings(DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}) 
SyntaxError: keyword can't be an expression 

回答

2

你应该覆盖整个词典如

@override_settings(DATABASES = {'default':{'ENGINE': 'django.db.backends.sqlite3'}}) 

,或者您可以使用替代设置为上下文管理。

+0

谢谢你的回答! ;) –