我正在编写用于基于web的应用程序的单元测试用例,该应用程序是用django编写的 . 对于django类的单元测试,我使用的是来自django.test的TestCase类 .
现在,当我通过client.get()方法测试get_queryset()方法时,它返回错误:
引发DatabaseError('This query is not supported by the database.')
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)
网址倒车时没有错误,我可以向你保证 . 我从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响应= 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行,获取返回self.request(** r)
在get_response中的文件“C:\ Python27 \ lib \ site-packages \ django \ core \ handlers \ base.py”,第111行
response = callback(request, *callback_args, **callback_kwargs)
文件“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)
文件“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行,在dispatch response = handler(request,* args,** kwargs)
文件“C:\ Apache2 \ htdocs \ iLab \ api \ lib \ views.py”,第431行,获取返回响应(serializer.data)
文件“C:\ Python27 \ lib \ site-packages \ djangorestframework-2.3.8-py2.7.egg \ rest_framework \ serializers.py”,第505行,数据self._data = [self.to_native(item)for item在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行,在compile.results_iter()中的行的迭代器中:文件“build \ bdist.win-amd64 \ egg \ djangotoolbox \ db \ basecompiler.py“,第225行,在result_iter self.check_query()中
文件“build \ bdist.win-amd64 \ egg \ djangotoolbox \ db \ basecompiler.py”,第273行,在check_query中引发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行,在视图中返回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)
文件“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行,在dispatch response = handler(request,* args,** kwargs)
文件“C:\ Apache2 \ htdocs \ iLab \ api \ lib \ views.py”,第431行,获取返回响应(serializer.data)
文件“C:\ Python27 \ lib \ site-packages \ djangorestframework-2.3.8-py2.7.egg \ rest_framework \ serializers.py”,第505行,数据self._data = [self.to_native(item)for item在obj]
文件“C:\ Python27 \ lib \ site-packages \ django \ db \ models \ query.py”,第107行,在_result_iter self._fill_cache()中
文件“C:\ Python27 \ lib \ site-packanosetests lib.tests:IngredientAllTestCase --verbosity = 1销毁别名'default'的测试数据库...
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行,incompile.results_iter()中行的迭代器:
文件“build \ bdist.win-amd64 \ egg \ djangotoolbox \ db \ basecompiler.py”,第225行,在result_iter 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.900s中进行1次测试
2 回答
试试这个
谢谢你的帮助,我找到了解决方案 .
还有另一个模型UserProfile包含companyId以及与django User类的角色和关系 .
我只需要将这些属性添加到我的setUp()函数中 .
再次感谢 .