Alexander Lamas
2018-08-21 09:08:56 UTC
Hi All,
How can I make a CRUD (GET, POST, DELETE, UPDATE) unit testing for a model
class
Here is my sample. I'm struggling with the update and delete
MODEL
from django.db import models
class Tenant(models.Model):
Name = models.CharField(max_length=200, null=False, default='', db_column='Name')
def __str__(self):
return self.Name
class Meta:
db_table = 'Tenant'
ordering = ['Name']
def save(self, *args, **kwargs):
Name = self.Name
super(Tenant, self).save(*args, **kwargs)
VIEW
from rest_framework import generics, permissions, renderers, viewsetsfrom rest_framework.decorators import api_view, detail_routefrom app.models import Tenantfrom app.serializers import TenantSerializer
class TenantViewSet(viewsets.ModelViewSet):
serializer_class = TenantSerializer
queryset = Tenant.objects.all()
@detail_route(renderer_classes=[renderers.JSONRenderer])
def perform_create(self, serializer):
serializer.save()
SERIALIZER
from rest_framework import serializersfrom app.models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = '__all__'
UNIT TEST
import djangodjango.setup()
from django.urls import reversefrom rest_framework import statusfrom rest_framework.test import APITestCasefrom app.models import Tenant
class TenantTests(APITestCase):
_value = 'Tenant_TEST'
def test_1_Tenant_ListAll_Return_0(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Tenant.objects.all().count(), 0)
def test_2_Tenant_ListSingle_Return_0(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Tenant.objects.filter(Name__contains=self._value).count(), 0)
def test_3_Tenant_Post_ReturnNothing(self):
url = reverse('tenant-list')
data = {'Name': self._value}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Tenant.objects.filter(Name__contains=self._value).count(), 1)
def test_4_Tenant_Update_ReturnRecordUpdated(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK,)
result = Tenant.objects.get(Name__contains=self._value).values_list('id')
self.assertEqual(Tenant.objects.count(), 1,)
def test_5_Tenant_Delete_ReturnNothing(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK,)
id = 1
Tenant.objects.get(pk = id).delete()
self.assertEqual(Tenant.objects.all().count(), 0)
Is that the correct way? Better question, is there a better way?
The intention is to test if my API is working in all aspects.
How can get the ID (PK) and make the update and delete happen?
Thank you very much!
Regards,
Alex
How can I make a CRUD (GET, POST, DELETE, UPDATE) unit testing for a model
class
Here is my sample. I'm struggling with the update and delete
MODEL
from django.db import models
class Tenant(models.Model):
Name = models.CharField(max_length=200, null=False, default='', db_column='Name')
def __str__(self):
return self.Name
class Meta:
db_table = 'Tenant'
ordering = ['Name']
def save(self, *args, **kwargs):
Name = self.Name
super(Tenant, self).save(*args, **kwargs)
VIEW
from rest_framework import generics, permissions, renderers, viewsetsfrom rest_framework.decorators import api_view, detail_routefrom app.models import Tenantfrom app.serializers import TenantSerializer
class TenantViewSet(viewsets.ModelViewSet):
serializer_class = TenantSerializer
queryset = Tenant.objects.all()
@detail_route(renderer_classes=[renderers.JSONRenderer])
def perform_create(self, serializer):
serializer.save()
SERIALIZER
from rest_framework import serializersfrom app.models import Tenant
class TenantSerializer(serializers.ModelSerializer):
class Meta:
model = Tenant
fields = '__all__'
UNIT TEST
import djangodjango.setup()
from django.urls import reversefrom rest_framework import statusfrom rest_framework.test import APITestCasefrom app.models import Tenant
class TenantTests(APITestCase):
_value = 'Tenant_TEST'
def test_1_Tenant_ListAll_Return_0(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Tenant.objects.all().count(), 0)
def test_2_Tenant_ListSingle_Return_0(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(Tenant.objects.filter(Name__contains=self._value).count(), 0)
def test_3_Tenant_Post_ReturnNothing(self):
url = reverse('tenant-list')
data = {'Name': self._value}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Tenant.objects.filter(Name__contains=self._value).count(), 1)
def test_4_Tenant_Update_ReturnRecordUpdated(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK,)
result = Tenant.objects.get(Name__contains=self._value).values_list('id')
self.assertEqual(Tenant.objects.count(), 1,)
def test_5_Tenant_Delete_ReturnNothing(self):
url = reverse('tenant-list')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK,)
id = 1
Tenant.objects.get(pk = id).delete()
self.assertEqual(Tenant.objects.all().count(), 0)
Is that the correct way? Better question, is there a better way?
The intention is to test if my API is working in all aspects.
How can get the ID (PK) and make the update and delete happen?
Thank you very much!
Regards,
Alex
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.