6
votes

I am working on writing unit testcases for web based application which is written in django. For unittesting of django classes I am using TestCase class available from django.test.

Now When I test get_queryset() method by client.get() method it returns error:

raise DatabaseError('This query is not supported by the database.')
DatabaseError: This query is not supported by the database.

here is my method which I am trying to test:

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

and here is testcase I am writing for the same:

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)

There is no error at url reversing, that I can assure you. I have checked it from python shell. here is url pattern:

url(r'^allingredients$', views.IngredientAll.as_view(), name='ingredient-all'),

Error is showing at

response = self.client.get(url)

it would be great if someone help me with this, i will be very much thankful.

This is full traceback of error:

$python manage.py test lib.tests:IngredientAllTestCase

$ python manage.py test lib.tests:IngredientAllTestCase

E

ERROR: test_IngredientAll (lib.tests.IngredientAllTestCase)

Traceback (most recent call last):

File "C:\Apache2\htdocs\iLab\api\lib\tests.py", line 94, in test_IngredientAll response = self.client.get(url)

File "C:\Python27\lib\site-packages\django\test\client.py", line 442, in get response = super(Client, self).get(path, data=data, **extra)

File "C:\Python27\lib\site-packages\django\test\client.py", line 244, in get return self.request(**r)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 111, in get_response

response = callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py", line 127, in view return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py", line 39, in wrapped_view resp = view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py", line 52, in wrapped_view return view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py", line 399, in dispatch response = self.handle_exception(exc)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py", line 396, in dispatch response = handler(request, *args, **kwargs)

File "C:\Apache2\htdocs\iLab\api\lib\views.py", line 431, in get return Response(serializer.data)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\serializers.py", line 505, in data self._data = [self.to_native(item) for item in obj]

File "C:\Python27\lib\site-packages\django\db\models\query.py", line 107, in _result_iter self._fill_cache()

File "C:\Python27\lib\site-packages\django\db\models\query.py", line 774, in _fill_cache self._result_cache.append(self._iter.next())

File "C:\Python27\lib\site-packages\django\db\models\query.py", line 275, in iterator for row in compiler.results_iter(): File "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py", line 225, in results_iter self.check_query()

File "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py", line 273, in check_query raise DatabaseError('This query is not supported by the database.')

DatabaseError: This query is not supported by the database.

-------------------- >> begin captured logging << --------------------

django.request: ERROR: Internal Server Error: /allingredients

Traceback (most recent call last):

File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 111, in get_response response = callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py", line 127, in view return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py", line 39, in wrapped_view resp = view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py", line 52, in wrapped_view return view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py", line 399, in dispatch response = self.handle_exception(exc)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py", line 396, in dispatch response = handler(request, *args, **kwargs)

File "C:\Apache2\htdocs\iLab\api\lib\views.py", line 431, in get return Response(serializer.data)

File "C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\serializers.py", line 505, in data self._data = [self.to_native(item) for item in obj]

File "C:\Python27\lib\site-packages\django\db\models\query.py", line 107, in _result_iter self._fill_cache()

File "C:\Python27\lib\site-packanosetests lib.tests:IngredientAllTestCase --verbosity=1 Destroying test database for alias 'default'...

ges\django\db\models\query.py", line 774, in _fill_cache self._result_cache.append(self._iter.next())

File "C:\Python27\lib\site-packages\django\db\models\query.py", line 275, in iterator for row in compiler.results_iter():

File "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py", line 225, in results_iter self.check_query()

File "build\bdist.win-amd64\egg\djangotoolbox\db\basecompiler.py", line 273, in check_query

raise DatabaseError('This query is not supported by the database.')

DatabaseError: This query is not supported by the database.

--------------------- >> end captured logging << ---------------------


Ran 1 test in 0.900s

2
post the full error stack trace if you could please.ptr
trackback for error addedjimish
For POST method tests are running fine.. but problem is with GET method... that's what I have observed from all testcasesjimish
Wait... you're using a non-relational database?ptr
Yes, I am using mongoDB non-relational databasejimish

2 Answers

0
votes

Try this

def get_queryset(self):
    queryset = super(IngredientAll, self).get_queryset()
    userCompanyId = self.request.user.get_profile().companyId
    queryset = queryset.filter(company=userCompanyId)
    return queryset
0
votes

Thanks for your help I found solution.

There was another model UserProfile containing companyId and role and relation to django User class.

I just needed to add those attributes to my setUp() function.

Thanks again.