2
votes

I want to test some views in DRF project.

The problem comes when I try to check views that have arguments in the urls.

urls.py


    url(r'^(?Pcompany_hash>[\d\w]+)/(?Ptimestamp>[\.\d]*)/employees/$',
        EmployeeList.as_view(), name='employeelist'),

[edit: "<" in url has been deleted in purpose, just it isnt considered a tag and thus not shown]

views.py

    class EmployeeList(ListCreateAPIView):
        serializer_class = EmployeeDirectorySerializer

        def inner_company(self):
            company_hash = self.kwargs['company_hash']
            return get_company(company_hash)

        def get_queryset(self):
            return Employee.objects.filter(company=self.inner_company())

test.py


class ApiTests(APITestCase):
    def setUp(self):
        self.factory = APIRequestFactory()
        self.staff = mommy.make('directory.Employee', user__is_staff=True)
        self.employee = mommy.make('directory.Employee')

        self.hash = self.employee.company.company_hash

    def getResponse(self, url, myView, kwargs):
        view = myView.as_view()
        request = self.factory.get(url, kwargs)

        force_authenticate(request, user=user)

        response = view(request)
        return response

    def test_EmployeeList(self):
        kwargs = {'timestamp': 0, 'company_hash': self.hash}
        url = reverse('employeelist', kwargs=kwargs)
        testedView = EmployeeList

        response = self.getResponse(url, testedView,
                kwargs=kwargs)
        self.assertEqual(response.status_code, 200)

I'm getting this error

    company_hash = self.kwargs['company_hash']
KeyError: 'company_hash'

That is the args aren't been passed to the view.

I've tried in so many different ways to pass by the args, can't find a solution.

Any help is welcomed!

3
your urls.py entry is wrong. Try with url(r'^(?P<company_hash>[\d\w]+)/(?P<timestamp>[\.\d]*)/employees/$', EmployeeList.as_view(), name='employeelist'),Pynchia
Thanks Pynchia but it isnt wrong just had to quit the < because else was consider a tag and thus deleted. Could have scaped it thomonobot

3 Answers

4
votes

Check your regex syntax in your URL conf. You aren't capturing the named group correctly. You have

(?<P 

rather than

(?P<

https://docs.djangoproject.com/en/1.8/topics/http/urls/#named-groups

-James

-1
votes

Just found the problem!!

I was using APIRequestFactory() and should have been using the build in client factory from the APITestCase test class from Django Rest Framework

-1
votes

you just need to specify your kwargs again when calling get() method:

...
...
    def getResponse(self, url, myView, kwargs):
        view = myView.as_view()
        request = self.factory.get(url, kwargs)
        ...
        response = view(request, company_hash=kwargs['company_hash'])
        return response

So your argument will be passed correctly.

NB: i used this solution based on my experience with the mother class of APIRequestfactory, Django RequestFactory.