0

How can I test query_params? I have provided a response and a request, but it still doesn't work. Initially, the error was like this:

price = int(self.context.get('request').query_params.get('price', None))
AttributeError: 'NoneType' object has no attribute 'query_params'

test_api.py

class IpotekaApiTestCase(APITestCase):
    def setUp(self) -> None:
        self.offer_1 = Ipoteka.objects.create(bank_name='test',
                                          term_min=1,
                                          term_max=3,
                                          rate_min=1.5,
                                          rate_max=4.8,
                                          payment_min=12345,
                                          payment_max=123456)
        self.offer_2 = Ipoteka.objects.create(bank_name='test_1',
                                          term_min=1,
                                          term_max=3,
                                          rate_min=1.5,
                                          rate_max=4.8,
                                          payment_min=12345,
                                          payment_max=123456)   
    def test_get(self):
        url = reverse('offer-list')
        response = self.client.get(url)
        view = IpotekaModelViewSet.as_view({'get': 'list'})
        request = RequestFactory().get('/')
        context = {'request': view(request), 'response': response}
        serializer_data = IpotekaSerializer([self.offer_1, self.offer_2], 
        context=context, many=True).data
        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assertEqual(serializer_data, response.data['results'])

serializers.py

    def get_payment(self, obj) -> int:
        try:
            price = int(self.context.get('request').query_params.get('price', None))
            deposit = int(self.context.get('request').query_params.get('deposit', 
            None))
            term = int(self.context.get('request').query_params.get('term', None))
            if price == 0 or term == 0:
                return 0
            return self._get_offer_result(price, deposit, term, obj)
        except (ValueError, TypeError):
            return 0

Got this error:

  File "D:\Work\Django\Test_ipoteka\ipoteka\serializers.py", line 22, in get_payment
    price = int(self.context.get('request').query_params.get('price', None))
AttributeError: 'Response' object has no attribute 'query_params'

----------------------------------------------------------------------
Ran 1 test in 0.014s

FAILED (errors=1)

I also deleted response but it didn't help.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Ramil
  • 28
  • 3
  • It looks like you provide a Response object where a Request object is expected. – physicalattraction Jun 22 '22 at 08:45
  • @physicalattraction i also provided only request object but i got this error again price = int(self.context.get('request').query_params.get('price', None)) AttributeError: 'Response' object has no attribute 'query_params' – Ramil Jun 22 '22 at 09:10
  • The erorr message means there is a Response object in `self.context['request']`. I am unfamiliar with RequestFactory. Are you sure that this line returns a Request and not a Response: `request = RequestFactory().get('/')`? – physicalattraction Jun 22 '22 at 09:26
  • The issue here is that you're passing `IpotekaModelViewSet` in `context['request']` and expecting that you can extract the `request` object in your serializer. **Viewsets** return `Response` object which means you cannot pass it in your `context`. What you need to do is rework your serializer and not access `request` there. – Christopher Tabula Jun 26 '22 at 13:16

2 Answers2

0

I think you can set the query parameters like the following.

def test_get(self):
    url = reverse('offer-list')
    response = self.client.get(url, { 'price': ..., 'deposit': ..., 'term': ... })
    ...
Metalgear
  • 3,391
  • 1
  • 7
  • 16
  • No it doesn't work Still getting this error: price = int(self.context.get('request').query_params.get('price', None)) AttributeError: 'NoneType' object has no attribute 'query_params' – Ramil Jun 22 '22 at 18:46
0

i m writing test case of my project and in my every apps there is using queryparms so basically query parms is use in my views is

employee_id = self.request.query_params.get('employee_id', None)

and i pass this in this way in my test case

url = f'/api/v1/education_information/?employee_id={self.employee.id}'

f'/api/v1/education_information/' this is the base url and remaining part is query parms that i passed

Tanveer Ahmad
  • 706
  • 4
  • 12