-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# djangowiz/repo/templates/test.py.j2 | ||
|
||
from django.test import TestCase | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from {{ app_name }}.models import {{ model_name }} | ||
from rest_framework.test import APIClient | ||
|
||
class {{ model_name }}APITests(TestCase): | ||
fixtures = ['{{ model_name|lower }}_fixtures.json'] | ||
|
||
def setUp(self): | ||
self.client = APIClient() | ||
self.model_list_url = reverse('{{ model_name|lower }}-list') | ||
self.model_detail_url = lambda pk: reverse('{{ model_name|lower }}-detail', args=[pk]) | ||
self.instances = {{ model_name }}.objects.all() | ||
|
||
def test_list_{{ model_name|lower }}s(self): | ||
response = self.client.get(self.model_list_url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), len(self.instances)) | ||
|
||
def test_create_{{ model_name|lower }}(self): | ||
data = {'name': 'New Product'} | ||
response = self.client.post(self.model_list_url, data) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
self.assertEqual({{ model_name }}.objects.count(), len(self.instances) + 1) | ||
|
||
def test_retrieve_{{ model_name|lower }}(self): | ||
instance = self.instances.first() | ||
response = self.client.get(self.model_detail_url(instance.pk)) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data['name'], instance.name) | ||
|
||
def test_update_{{ model_name|lower }}(self): | ||
instance = self.instances.first() | ||
data = {'name': 'Updated Product'} | ||
response = self.client.put(self.model_detail_url(instance.pk), data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
instance.refresh_from_db() | ||
self.assertEqual(instance.name, 'Updated Product') | ||
|
||
def test_partial_update_{{ model_name|lower }}(self): | ||
instance = self.instances.first() | ||
data = {'name': 'Partially Updated Product'} | ||
response = self.client.patch(self.model_detail_url(instance.pk), data) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
instance.refresh_from_db() | ||
self.assertEqual(instance.name, 'Partially Updated Product') | ||
|
||
def test_delete_{{ model_name|lower }}(self): | ||
instance = self.instances.first() | ||
response = self.client.delete(self.model_detail_url(instance.pk)) | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
self.assertEqual({{ model_name }}.objects.count(), len(self.instances) - 1) | ||
|
||
def test_invalid_create(self): | ||
# Missing 'name' field in data | ||
data = {} | ||
response = self.client.post(self.model_list_url, data) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters