From 6e3cccd4c97916479ddd7bb2587808200ddb2d66 Mon Sep 17 00:00:00 2001 From: Michael Rest Date: Tue, 16 Jul 2019 11:09:20 +0200 Subject: [PATCH] Added Status --- status/__init__.py | 0 status/admin.py | 3 ++ status/apps.py | 4 ++ status/models.py | 15 +++++++ status/serializers.py | 8 ++++ status/tests/__init__.py | 0 status/tests/test_models.py | 21 ++++++++++ status/tests/test_views.py | 84 +++++++++++++++++++++++++++++++++++++ status/urls.py | 17 ++++++++ status/views.py | 52 +++++++++++++++++++++++ 10 files changed, 204 insertions(+) create mode 100644 status/__init__.py create mode 100644 status/admin.py create mode 100644 status/apps.py create mode 100644 status/models.py create mode 100644 status/serializers.py create mode 100644 status/tests/__init__.py create mode 100644 status/tests/test_models.py create mode 100644 status/tests/test_views.py create mode 100644 status/urls.py create mode 100644 status/views.py diff --git a/status/__init__.py b/status/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/status/admin.py b/status/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/status/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/status/apps.py b/status/apps.py new file mode 100644 index 0000000..7b1b2f4 --- /dev/null +++ b/status/apps.py @@ -0,0 +1,4 @@ +from django.apps import AppConfig + +class StatusConfig (AppConfig): + name = 'status' diff --git a/status/models.py b/status/models.py new file mode 100644 index 0000000..a590f56 --- /dev/null +++ b/status/models.py @@ -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.' diff --git a/status/serializers.py b/status/serializers.py new file mode 100644 index 0000000..9af568d --- /dev/null +++ b/status/serializers.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import Status + + +class StatusSerializer (serializers.ModelSerializer): + class Meta: + model = Status + fields = ('key', 'value') diff --git a/status/tests/__init__.py b/status/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/status/tests/test_models.py b/status/tests/test_models.py new file mode 100644 index 0000000..45e6dee --- /dev/null +++ b/status/tests/test_models.py @@ -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) diff --git a/status/tests/test_views.py b/status/tests/test_views.py new file mode 100644 index 0000000..706d23a --- /dev/null +++ b/status/tests/test_views.py @@ -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) diff --git a/status/urls.py b/status/urls.py new file mode 100644 index 0000000..4a537fd --- /dev/null +++ b/status/urls.py @@ -0,0 +1,17 @@ +#from django.conf.urls import url,path +from django.urls import path +from . import views + + +urlpatterns = [ + path ( + 'api/status/', + views.get_delete_update_status, + name = 'get_delete_update_status' + ), + path ( + 'api/status/', + views.get_post_status, + name = 'get_post_status' + ) +] diff --git a/status/views.py b/status/views.py new file mode 100644 index 0000000..3df2596 --- /dev/null +++ b/status/views.py @@ -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)