Django method get_queryset unittest failing

I am working on writing test modules for web applications written in django. For unittesting django classes, I use the TestCase class available from django.test.

Now, when I test the get_queryset () method with the client.get () method, it returns an error:

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

DatabaseError: this query is not supported by the database.

here is my method I'm 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 the test file that I am writing for it:

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 accessing the URL that I can assure you. I checked it from the python shell. here is the url pattern:

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

The error is displayed on

response = self.client.get(url)

It would be great if someone helped me with this, I will be very grateful.

This is a complete error trace:

$python manage.py test lib.tests: IngredientAllTestCase

$python manage.py test lib.tests: IngredientAllTestCase

: test_IngredientAll (lib.tests.IngredientAllTestCase)

Traceback ( ):

"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(, = , ** )

"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,   return self.dispatch(, * 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,   response = self.handle_exception (exc)

"C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py", 396,   response = (, * 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,   self._data = [self.to_native (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,    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    DatabaseError (' .')

DatabaseError: .

-------------------- → < < --------------------

django.request: : :/allingredients

Traceback ( ):

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

"C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\compat.py", 127,   return self.dispatch(, * 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,   response = self.handle_exception (exc)

"C:\Python27\lib\site-packages\djangorestframework-2.3.8-py2.7.egg\rest_framework\views.py", 396,   response = (, * 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,   self._data = [self.to_native (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 " "...

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,    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: .

--------------------- → < < ---------------------


Ran 1 0.900s

+4
2

. .

UserProfile, companyId, django.

setUp().

.

0

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

Source: https://habr.com/ru/post/1531710/


All Articles