python - How can i correctly pass arguments to classbasedviews testing Django Rest Framework? -
i want test views in drf project.
the problem comes when try check views have arguments in 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, isnt considered tag , 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 error
company_hash = self.kwargs['company_hash'] keyerror: 'company_hash'
that args aren't been passed view.
i've tried in many different ways pass args, can't find solution.
any welcomed!
check regex syntax in url conf. aren't capturing named group correctly. have
(?<p
rather
(?p<
https://docs.djangoproject.com/en/1.8/topics/http/urls/#named-groups
-james
Comments
Post a Comment