我正在为使用django编写的基于Web的应用程序编写单元测试用例。对于django类的单元测试,我使用django.test中提供的TestCase类。
现在,当我通过client.get()方法测试get_queryset()方法时,它将返回错误:
引发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(r'^allingredients$', views.IngredientAll.as_view(), name='ingredient-all'),
错误显示在
response = self.client.get(url)
有人帮助我会很棒,我将非常感谢。
这是错误的完整回溯:
$python manage.py test lib.tests:IngredientAllTestCase
$ python manage.py测试lib.tests:IngredientAllTestCase
E
错误:test_IngredientAll(lib.tests.IngredientAllTestCase)
追溯(最近一次通话):
在test_IngredientAll中的文件“C:\Apache2\htdocs\iLab\api\lib\tests.py”,第94行
响应= self.client.get(URL)
在get中的文件“C:\Python27\lib\site-packages\django\test\client.py”,第442行
响应= super (客户,自我).get(路径,数据=数据,**额外)
在get中的文件“C:\Python27\lib\site-packages\django\test\client.py”,第244行
返回self.request(** r)
get_response中的第111行的文件“C:\Python27\lib\site-packages\django\core\handlers\base.py”
response = callback(request, *callback_args, **callback_kwargs)
View 中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py”,第127行
返回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中
返回view_func(* args,** kwargs)
在分派(dispatch)中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py”,行399
响应= self.handle_exception(exc)
调度中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py”,行396
响应=处理程序(请求,* args,** kwargs)
在get中的文件“C:\Apache2\htdocs\iLab\api\lib\views.py”,第431行
返回Response(serializer.data)
文件505中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\serializers.py”
self._data = [obj中项目的self.to_native(item)]
_result_iter中的文件“C:\Python27\lib\site-packages\django\db\models\query.py”,第107行
self._fill_cache()
_fill_cache中的文件“C:\Python27\lib\site-packages\django\db\models\query.py”,第774行
self._result_cache.append(self._iter.next())
迭代器中的文件“C:\Python27\lib\site-packages\django\db\models\query.py”,行275
对于compile.results_iter()中的行:
在results_iter中的第225行,文件“build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py”
self.check_query()
在check_query中,文件“build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py”,第273行
引发DatabaseError('数据库不支持此查询。')
DatabaseError:数据库不支持此查询。
-------------------- >>开始捕获日志<< --------------------
django.request:错误:内部服务器错误:/allingredients
追溯(最近一次通话):
get_response中的第111行的文件“C:\Python27\lib\site-packages\django\core\handlers\base.py”
响应=回调(请求,* callback_args,** callback_kwargs)
View 中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py”,第127行
返回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中
返回view_func(* args,** kwargs)
在分派(dispatch)中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py”,行399
响应= self.handle_exception(exc)
调度中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py”,行396
响应=处理程序(请求,* args,** kwargs)
在get中的文件“C:\Apache2\htdocs\iLab\api\lib\views.py”,第431行
返回Response(serializer.data)
文件505中的文件“C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\serializers.py”
self._data = [obj中项目的self.to_native(item)]
_result_iter中的文件“C:\Python27\lib\site-packages\django\db\models\query.py”,第107行
self._fill_cache()
文件“C:\Python27\lib\site-packanosetests lib.tests:IngredientAllTestCase --verbosity = 1
正在销毁测试数据库的别名“默认” ...
ges\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
对于compile.results_iter()中的行:
在results_iter中的第225行,文件“build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py”
self.check_query()
在check_query中,文件“build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py”,第273行
raise DatabaseError('This query is not supported by the database.')
DatabaseError:数据库不支持此查询。
--------------------- >>结束捕获的日志<< ---------------------
在0.900秒内进行了1次测试
最佳答案
试试这个
def get_queryset(self):
queryset = super(IngredientAll, self).get_queryset()
userCompanyId = self.request.user.get_profile().companyId
queryset = queryset.filter(company=userCompanyId)
return queryset
关于python - Django get_queryset方法的单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398026/