Django get_queryset 方法的单元测试失败
我正在为一个基于网页的应用程序编写单元测试,这个应用程序是用Django写的。为了测试Django类,我使用了django.test中的TestCase类。
现在,当我通过client.get()方法测试get_queryset()方法时,它返回了一个错误:
raise DatabaseError('这个查询不被数据库支持。')
DatabaseError: 这个查询不被数据库支持。
这是我正在尝试测试的方法:
class IngredientAll(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
model = Ingredient
serializer_class = IngredientListSerializer
def get_queryset(self):
userCompanyId = self.request.user.get_profile().companyId
ingredients = Ingredient.objects.filter(company = userCompanyId)
return ingredients
这是我为这个方法写的测试用例:
class IngredientAllTestCase(unittest.TestCase):
def setUp(self):
self.user=User(username='jimish')
password = 'password'
self.user.set_password(password)
self.user.save()
self.client = Client()
self.client.login(username=self.user.username, password=password)
def test_IngredientAll(self):
url = reverse('lib:ingredient-all')
response = self.client.get(url)
self.assertEqual(response.status_code,status.HTTP_200_OK)
在URL反转方面没有错误,我可以向你保证。我已经在Python shell中检查过了。这里是URL模式:
url(r'^allingredients$', views.IngredientAll.as_view(), name='ingredient-all'),
错误出现在
response = self.client.get(url)
如果有人能帮我解决这个问题,我将非常感激。
这是错误的完整追踪信息:
$python manage.py test lib.tests:IngredientAllTestCase
$ python manage.py test lib.tests:IngredientAllTestCase
E
错误: test_IngredientAll (lib.tests.IngredientAllTestCase)
追踪信息(最近的调用在最前面):
文件 "C:\Apache2\htdocs\iLab\api\lib\tests.py",第94行,在 test_IngredientAll 中 response = self.client.get(url)
文件 "C:\Python27\lib\site-packages\django\test\client.py",第442行,在 get 中 response = super(Client, self).get(path, data=data, **extra)
文件 "C:\Python27\lib\site-packages\django\test\client.py",第244行,在 get 中 return self.request(**r)
文件 "C:\Python27\lib\site-packages\django\core\handlers\base.py",第111行,在 get_response 中
response = callback(request, *callback_args, **callback_kwargs)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py",第127行,在 view 中 return self.dispatch(request, *args, **kwargs)
文件 "C:\Python27\lib\site-packages\django\views\decorators\csrf.py",第39行,在 wrapped_view 中 resp = view_func(*args, **kwargs)
文件 "C:\Python27\lib\site-packages\django\views\decorators\csrf.py",第52行,在 wrapped_view 中 return view_func(*args, **kwargs)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py",第399行,在 dispatch 中 response = self.handle_exception(exc)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py",第396行,在 dispatch 中 response = handler(request, *args, **kwargs)
文件 "C:\Apache2\htdocs\iLab\api\lib\views.py",第431行,在 get 中 return Response(serializer.data)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\serializers.py",第505行,在 data 中 self._data = [self.to_native(item) for item in obj]
文件 "C:\Python27\lib\site-packages\django\db\models\query.py",第107行,在 _result_iter 中 self._fill_cache()
文件 "C:\Python27\lib\site-packages\django\db\models\query.py",第774行,在 _fill_cache 中 self._result_cache.append(self._iter.next())
文件 "C:\Python27\lib\site-packages\django\db\models\query.py",第275行,在 iterator 中 for row in compiler.results_iter(): 文件 "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py",第225行,在 results_iter 中 self.check_query()
文件 "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py",第273行,在 check_query 中 raise DatabaseError('这个查询不被数据库支持。')
DatabaseError: 这个查询不被数据库支持。
-------------------- >> 开始捕获日志 << --------------------
django.request: 错误: 内部服务器错误: /allingredients
追踪信息(最近的调用在最前面):
文件 "C:\Python27\lib\site-packages\django\core\handlers\base.py",第111行,在 get_response 中 response = callback(request, *callback_args, **callback_kwargs)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py",第127行,在 view 中 return self.dispatch(request, *args, **kwargs)
文件 "C:\Python27\lib\site-packages\django\views\decorators\csrf.py",第39行,在 wrapped_view 中 resp = view_func(*args, **kwargs)
文件 "C:\Python27\lib\site-packages\django\views\decorators\csrf.py",第52行,在 wrapped_view 中 return view_func(*args, **kwargs)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py",第399行,在 dispatch 中 response = self.handle_exception(exc)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py",第396行,在 dispatch 中 response = handler(request, *args, **kwargs)
文件 "C:\Apache2\htdocs\iLab\api\lib\views.py",第431行,在 get 中 return Response(serializer.data)
文件 "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\serializers.py",第505行,在 data 中 self._data = [self.to_native(item) for item in obj]
文件 "C:\Python27\lib\site-packages\django\db\models\query.py",第107行,在 _result_iter 中 self._fill_cache()
文件 "C:\Python27\lib\site-packages\django\db\models\query.py",第774行,在 _fill_cache 中 self._result_cache.append(self._iter.next())
文件 "C:\Python27\lib\site-packages\django\db\models\query.py",第275行,在 iterator 中 for row in compiler.results_iter():
文件 "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py",第225行,在 results_iter 中 self.check_query()
文件 "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py",第273行,在 check_query 中
raise DatabaseError('This query is not supported by the database.')
DatabaseError: 这个查询不被数据库支持。
--------------------- >> 结束捕获日志 << ---------------------
运行了1个测试,耗时0.900秒
相关问题:
2 个回答
试试这个
def get_queryset(self):
queryset = super(IngredientAll, self).get_queryset()
userCompanyId = self.request.user.get_profile().companyId
queryset = queryset.filter(company=userCompanyId)
return queryset
谢谢你的帮助,我找到了解决办法。
原来有另一个模型叫做 UserProfile,它里面包含了公司ID和角色,并且和 Django 的 User 类有关联。
我只需要把这些属性加到我的 setUp() 函数里就可以了。
再次感谢你。