Jim
Jim

Reputation: 4669

How to unit test an HTTP delete with Django Rest Framework

I have a simple ViewSet to represent a model in my database, defined as such

class EventViewSet(viewsets.ModelViewSet)

I have the following code to unit test an HTTP GET request:

    client = APIClient()
    client.credentials(username="test", password="test")
    response = client.get("/api/events/")
    self.assertEqual(response.status_code, 200)
    self.assertEqual(len(response.data), 2)

My question is this: How do I make the same type of test, but for HTTP delete?

Upvotes: 5

Views: 2812

Answers (1)

falsetru
falsetru

Reputation: 368904

If you use rest_framework.test.APIClient, you can use .get(), .post(), .put(), .patch(), .delete(), .head() and .options() methods.

data = {...}
response = client.delete("/api/events/", data=data)

Upvotes: 4

Related Questions