Added Status
This commit is contained in:
0
status/__init__.py
Normal file
0
status/__init__.py
Normal file
3
status/admin.py
Normal file
3
status/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
4
status/apps.py
Normal file
4
status/apps.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
class StatusConfig (AppConfig):
|
||||
name = 'status'
|
||||
15
status/models.py
Normal file
15
status/models.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from django.db import models
|
||||
|
||||
class Status (models.Model):
|
||||
"""
|
||||
Status Model
|
||||
Defines the attributes of a status KeyPair
|
||||
"""
|
||||
key = models.CharField (primary_key = True, max_length = 20)
|
||||
value = models.IntegerField (null = False)
|
||||
|
||||
def get_value (self):
|
||||
return self.value
|
||||
|
||||
def __repr__(self):
|
||||
return self.key + ' is added.'
|
||||
8
status/serializers.py
Normal file
8
status/serializers.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Status
|
||||
|
||||
|
||||
class StatusSerializer (serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Status
|
||||
fields = ('key', 'value')
|
||||
0
status/tests/__init__.py
Normal file
0
status/tests/__init__.py
Normal file
21
status/tests/test_models.py
Normal file
21
status/tests/test_models.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from django.test import TestCase
|
||||
from ..models import Status
|
||||
|
||||
|
||||
class StatusTest (TestCase):
|
||||
""" Test module for Statusmodel """
|
||||
def setUp (self):
|
||||
Status.objects.create (key = 'TestStatus1',
|
||||
value = 123)
|
||||
|
||||
Status.objects.create (key = 'TestStatus2',
|
||||
value = 567)
|
||||
|
||||
|
||||
def test_parameter (self):
|
||||
state1 = Status.objects.get (key = 'TestStatus1')
|
||||
state2 = Status.objects.get (key = 'TestStatus2')
|
||||
self.assertEqual (
|
||||
state1.get_value (), 123)
|
||||
self.assertEqual (
|
||||
state2.get_value (), 567)
|
||||
84
status/tests/test_views.py
Normal file
84
status/tests/test_views.py
Normal file
@@ -0,0 +1,84 @@
|
||||
import json
|
||||
from rest_framework import status as rst_status
|
||||
from django.test import TestCase, Client
|
||||
from django.urls import reverse
|
||||
from ..models import Status
|
||||
from ..serializers import StatusSerializer
|
||||
|
||||
|
||||
# initialize the APIClient app
|
||||
client = Client ()
|
||||
|
||||
|
||||
class GetAllStatesTest (TestCase):
|
||||
""" Test module for GET all States API """
|
||||
|
||||
def setUp (self):
|
||||
Status.objects.create ( key = 'TestStatus1',
|
||||
value = 123)
|
||||
|
||||
Status.objects.create ( key = 'TestStatus2',
|
||||
value = 456)
|
||||
|
||||
def test_get_all_states (self):
|
||||
""" get API response """
|
||||
response = client.get (reverse ('get_post_status'))
|
||||
# get data from db
|
||||
states = Status.objects.all ()
|
||||
serializer = StatusSerializer (states, many = True)
|
||||
self.assertEqual (response.data, serializer.data)
|
||||
self.assertEqual (response.status_code, rst_status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GetSingleStateTest (TestCase):
|
||||
""" Test module for GET single State API """
|
||||
|
||||
def setUp (self):
|
||||
self.status1 = Status.objects.create ( key = 'TestStatus1',
|
||||
value = 123)
|
||||
|
||||
self.status2 = Status.objects.create ( key = 'TestStatus2',
|
||||
value = 456)
|
||||
|
||||
def test_get_valid_single_state (self):
|
||||
""" get API response """
|
||||
# get data from db
|
||||
states = Status.objects.all ()
|
||||
response = client.get (
|
||||
reverse ('get_delete_update_status', kwargs = {'key': self.status1.key}))
|
||||
status = Status.objects.get (key = self.status1.key)
|
||||
serializer = StatusSerializer (status)
|
||||
self.assertEqual (response.data, serializer.data)
|
||||
self.assertEqual (response.status_code, rst_status.HTTP_200_OK)
|
||||
|
||||
def test_get_invalid_single_state (self):
|
||||
response = client.get (
|
||||
reverse ('get_delete_update_status', kwargs = {'key': 'BadState'}))
|
||||
self.assertEqual (response.status_code, rst_status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
class CreateNewStatusTest (TestCase):
|
||||
""" Test module for inserting a new status """
|
||||
def setUp (self):
|
||||
self.valid_payload = { 'key' : 'Status',
|
||||
'value' : 123}
|
||||
|
||||
self.invalid_payload = { 'key' : 'Parameter2',
|
||||
'value' : ''}
|
||||
|
||||
|
||||
def test_create_valid_status (self):
|
||||
response = client.post (
|
||||
reverse ('get_post_status'),
|
||||
data = json.dumps (self.valid_payload),
|
||||
content_type = 'application/json'
|
||||
)
|
||||
self.assertEqual (response.status_code, rst_status.HTTP_201_CREATED)
|
||||
|
||||
def test_create_invalid_status (self):
|
||||
response = client.post (
|
||||
reverse ('get_post_status'),
|
||||
data = json.dumps (self.invalid_payload),
|
||||
content_type = 'application/json'
|
||||
)
|
||||
self.assertEqual (response.status_code, rst_status.HTTP_400_BAD_REQUEST)
|
||||
17
status/urls.py
Normal file
17
status/urls.py
Normal file
@@ -0,0 +1,17 @@
|
||||
#from django.conf.urls import url,path
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path (
|
||||
'api/status/<key>',
|
||||
views.get_delete_update_status,
|
||||
name = 'get_delete_update_status'
|
||||
),
|
||||
path (
|
||||
'api/status/',
|
||||
views.get_post_status,
|
||||
name = 'get_post_status'
|
||||
)
|
||||
]
|
||||
52
status/views.py
Normal file
52
status/views.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status as rst_status
|
||||
from .models import Status
|
||||
from .serializers import StatusSerializer
|
||||
|
||||
|
||||
#@api_view(['GET', 'DELETE', 'PUT'])
|
||||
@api_view (['GET', 'PUT'])
|
||||
def get_delete_update_status (request, key):
|
||||
try:
|
||||
status = Status.objects.get (key = key)
|
||||
except Status.DoesNotExist:
|
||||
return Response (status = rst_status.HTTP_404_NOT_FOUND)
|
||||
|
||||
# get value of a single Status
|
||||
if request.method == 'GET':
|
||||
serializer = StatusSerializer (status)
|
||||
return Response (serializer.data)
|
||||
# delete a single Status
|
||||
elif request.method == 'DELETE':
|
||||
return Response ({})
|
||||
# update details of a single Status
|
||||
elif request.method == 'PUT':
|
||||
serializer = StatusSerializer (status, data = request.data)
|
||||
if serializer.is_valid ():
|
||||
serializer.save ()
|
||||
return Response (serializer.data, status = rst_status.HTTP_204_NO_CONTENT)
|
||||
return Response (serializer.errors, status = rst_status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
@api_view (['GET', 'POST'])
|
||||
def get_post_status (request):
|
||||
# get all States
|
||||
if request.method == 'GET':
|
||||
states = Status.objects.all ()
|
||||
serializer = StatusSerializer (states, many = True)
|
||||
return Response (serializer.data)
|
||||
# insert a new record for a State
|
||||
elif request.method == 'POST':
|
||||
try:
|
||||
data = { 'key': request.data.get ('key'),
|
||||
'value': int (request.data.get ('value'))
|
||||
}
|
||||
except:
|
||||
return Response ('Value Bad', status = rst_status.HTTP_400_BAD_REQUEST)
|
||||
serializer = StatusSerializer (data = data)
|
||||
if serializer.is_valid () and data ['key'] > '':
|
||||
serializer.save ()
|
||||
return Response (serializer.data, status = rst_status.HTTP_201_CREATED)
|
||||
return Response (serializer.errors, status = rst_status.HTTP_400_BAD_REQUEST)
|
||||
Reference in New Issue
Block a user