Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

calling a url with reverse with query string during unit testing

I have this URL that i want to test:

urlpatterns = [ path('produce<str:code>', ProduceSpreadSheet.as_view(), name="produce" ), ]

my test is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class ProduceSpreadSheetTest(TestCase):

    
    def setUp(self):
        self.code = "abcd"
        
    def test_valid_token(self):
        url = reverse('produce', query_kwargs={'code': self.code})
        
        response = client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

I get the error:

TypeError: reverse() got an unexpected keyword argument ‘query_kwargs’

when I change query_kwargs to kwargs: I get the error:

  TypeError: get() got an unexpected keyword argument 'code'

The method to be tested is:

class ProduceSpreadSheet(APIView):

   def get(self, request):

        code = request.query_params.get('code',None)
        // continue
        

I want to call it with

     .../produce?code=abcd

query_kwargs and kwargs and reverse(‘produce’) + ‘code=code’

>Solution :

For reverse it is just kwargs, not query_kwargs:

class ProduceSpreadSheetTest(TestCase):
    def setUp(self):
        self.code = 'abcd'

    def test_valid_token(self):
        url = reverse('produce', kwargs={'code': self.code})

        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading