I have this URL that i want to test:
urlpatterns = [ path('produce<str:code>', ProduceSpreadSheet.as_view(), name="produce" ), ]
my test is:
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'
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)
if you want to add a querystring, you can use a QueryDict
:
from django.http import QueryDict
class ProduceSpreadSheetTest(TestCase):
def setUp(self):
self.code = 'abcd'
def test_valid_token(self):
url = reverse('produce')
url = f'{url}?{QueryDict({"code": self.code}).urlencode()}'
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)