Validate category creation requests, unit test (#15)

* Validate category creation requests, unit test

* unit tests
This commit is contained in:
Ethan Roseman
2022-08-27 02:34:59 -04:00
committed by GitHub
parent d5978edd29
commit 5d619698c4
9 changed files with 179 additions and 37 deletions

View File

@@ -0,0 +1,14 @@
from rest_framework import serializers
from frog_api.models import AUTH_KEY_LEN
class ApiKeySerializer(serializers.CharField):
required = True
max_length = AUTH_KEY_LEN
class CreateCategoriesSerializer(serializers.Serializer): # type:ignore
api_key = ApiKeySerializer()
categories = serializers.DictField(
required=True, allow_empty=False, child=serializers.CharField()
)

View File

@@ -1,3 +1,39 @@
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
# Create your tests here.
from frog_api.models import Category, Project, Version
class CreateCategoriesTests(APITestCase):
def test_create_categories(self) -> None:
"""
Ensure that the category creation endpoint works
"""
create_json = {
"api_key": "test_key_123",
"categories": {
"total": "Total",
"actors": "Actors",
},
}
# Create a test Project and Version
project = Project(slug="oot", name="Ocarina of Time", auth_key="test_key_123")
project.save()
version = Version(slug="us", name="US", project=project)
version.save()
self.assertEqual(Category.objects.count(), 0)
response = self.client.post(
reverse("category-structure", args=[project.slug, version.slug]),
create_json,
format="json",
)
# Confirm we created the categories and that they are in the DB
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Category.objects.count(), len(create_json["categories"]))

View File

@@ -10,6 +10,7 @@ urlpatterns = [
re_path(
"projects/(?P<project_slug>.+)/(?P<version_slug>.+)/$",
structure.CategoryStructureView.as_view(),
name="category-structure",
),
# data (/data)
re_path(

View File

@@ -7,7 +7,7 @@ from frog_api.exceptions import (
NoEntriesException,
)
from frog_api.models import Entry, Measure, Project, Version
from frog_api.serializers import EntrySerializer
from frog_api.serializers.model_serializers import EntrySerializer
from frog_api.views.common import (
get_category,
get_project,

View File

@@ -1,9 +1,10 @@
from typing import Any
from django.db import models
from frog_api.exceptions import AlreadyExistsException, MissingAPIKeyException
from frog_api.exceptions import AlreadyExistsException
from frog_api.models import Category, Project
from frog_api.serializers import ProjectSerializer
from frog_api.serializers.model_serializers import ProjectSerializer
from frog_api.serializers.request_serializers import CreateCategoriesSerializer
from frog_api.views.common import get_project, get_version, validate_api_key
from rest_framework import status
from rest_framework.request import Request
@@ -34,23 +35,25 @@ class CategoryStructureView(APIView):
def create_categories(
req_data: dict[str, Any], project_slug: str, version_slug: str
) -> int:
request_ser = CreateCategoriesSerializer(data=req_data)
request_ser.is_valid(raise_exception=True)
data = request_ser.data
project = get_project(project_slug)
validate_api_key(request_ser.data["api_key"], project)
version = get_version(version_slug, project)
if "api_key" not in req_data:
raise MissingAPIKeyException()
validate_api_key(req_data["api_key"], project)
categories = req_data["data"]
categories: dict[str, str] = data["categories"]
to_save: list[models.Model] = []
for cat in categories:
for cat, name in categories.items():
if Category.objects.filter(slug=cat, version=version).exists():
raise AlreadyExistsException(
f"Category {cat} already exists for project '{project_slug}', version '{version_slug}'"
)
to_save.append(Category(version=version, slug=cat, name=categories[cat]))
to_save.append(Category(version=version, slug=cat, name=name))
for s in to_save:
s.save()