[Answered ]-Test Cases For PUT in DJANGO REST and mongoengine

1👍

Finally Figured it out

def test_delete_client(self):
    """
    Ensure we can delete new clients in mongo database.
    """
    url = '/client_management/client/'
    data = {'client_name'='XX'}
    factory = APIRequestFactory()
    user = User.objects.get(username='jacob')
    view = views.ClientList.as_view()

    # Make an authenticated request to the view...
    request = factory.post(url, data=data,)
    force_authenticate(request, user=user)
    response = view(request)
    ##self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    g_id = response.data['id']
    urls = '/client_management/client/?id=' + g_id
    ## even tried
    ## urls = '/client_management/client/ + g_id + /
    data['id'] = g_id
    #data['client_name'] = 'MAA'
    view = views.ClientDetails.as_view()
    request = factory.delete(urls)
    ## tried this also
    ## request = factory.delete('/client_management/client/')
    print request
    force_authenticate(request, user=user)
    response = view(request, id=str(g_id))
    self.assertEqual(response.status_code, 201)

You have to send ID when calling view(request) as response = view(request, id=str(g_id)).

1👍

The docs seems to indicate that the default lookup_field is pk and not id. I would try to either change the url pattern to

...
url(r'^client/(?P<pk>[\w]{24})/$', views.ClientDetails.as_view(),
    name='client_details'),
...

or add a lookup_field attribute to your view

class ClientDetails(generics.RetrieveUpdateDestroyAPIView):
    serializer_class = ClientSerializer
    queryset = Client.objects.all()
    lookup_field = ('id',) # Note the parenthesis, they are needed so the object is a tuple
👤pingul

0👍

Change your code and reposting

urls.py

urlpatterns = [
    url(r'^client/(?P<id>[\w]{24})/$', views.ClientDetails.as_view(),
    name='client_details'),
]

test.py
In the url you are passing the pk value. In the test case also you need to pass the pk value of id. In the above code i absorbed that you passing id as query-parameter so i’m passing it as pk.

def test_delete_client(self):
    """
    Ensure we can delete new clients in mongo database.
    """
    g_id = response.data['id']
    urls = '/client_management/client/' + str(g_id) + '/'
    view = views.ClientDetails.as_view()

Leave a comment