Skip to content

Commit

Permalink
✅ test publish endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
annashamray committed Nov 18, 2020
1 parent 167a00c commit 39b78c2
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 87 deletions.
89 changes: 2 additions & 87 deletions src/objecttypes/tests/test_objecttype_api.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
from datetime import date

from django.urls import reverse

from rest_framework import status
from rest_framework.test import APITestCase

from objecttypes.core.constants import (
DataClassificationChoices,
ObjectVersionStatus,
UpdateFrequencyChoices,
)
from objecttypes.core.models import ObjectType, ObjectVersion
from objecttypes.core.constants import DataClassificationChoices, UpdateFrequencyChoices
from objecttypes.core.models import ObjectType
from objecttypes.core.tests.factories import ObjectTypeFactory, ObjectVersionFactory
from objecttypes.utils.test import TokenAuthMixin

JSON_SCHEMA = {
"type": "object",
"title": "Tree",
"$schema": "http://json-schema.org/draft-07/schema#",
"required": ["diameter"],
"properties": {"diameter": {"type": "integer", "description": "size in cm."}},
}


class ObjectTypeAPITests(TokenAuthMixin, APITestCase):
def test_get_objecttypes(self):
Expand Down Expand Up @@ -121,74 +107,3 @@ def test_delete_objecttype_not_supported(self):
response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)


class ObjectVersionAPITests(TokenAuthMixin, APITestCase):
def test_get_versions(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse("objectversion-list", args=[object_type.uuid])

response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.json(),
[
{
"url": f"http://testserver{reverse('objectversion-detail', args=[object_type.uuid, object_version.version])}",
"version": object_version.version,
"objectType": f"http://testserver{reverse('objecttype-detail', args=[object_version.object_type.uuid])}",
"publicationDate": object_version.publication_date.isoformat(),
"status": object_version.status,
"jsonSchema": JSON_SCHEMA,
}
],
)

def test_create_version(self):
object_type = ObjectTypeFactory.create()
data = {"jsonSchema": JSON_SCHEMA}
url = reverse("objectversion-list", args=[object_type.uuid])

response = self.client.post(url, data)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ObjectVersion.objects.count(), 1)

object_version = ObjectVersion.objects.get()

self.assertEqual(object_version.object_type, object_type)
self.assertEqual(object_version.json_schema, JSON_SCHEMA)
self.assertEqual(object_version.version, 1)
self.assertEqual(object_version.publication_date, date.today())
self.assertEqual(object_version.status, ObjectVersionStatus.draft)

def test_update_version(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse(
"objectversion-detail", args=[object_type.uuid, object_version.version]
)
new_json_schema = {
"type": "object",
"title": "Tree",
"$schema": "http://json-schema.org/draft-07/schema#",
"required": ["diameter"],
"properties": {"diameter": {"type": "number"}},
}

response = self.client.put(url, {"jsonSchema": new_json_schema})

self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_delete_version_not_supported(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse(
"objectversion-detail", args=[object_type.uuid, object_version.version]
)

response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
105 changes: 105 additions & 0 deletions src/objecttypes/tests/test_version_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from datetime import date

from django.urls import reverse

from rest_framework import status
from rest_framework.test import APITestCase

from objecttypes.core.constants import ObjectVersionStatus
from objecttypes.core.models import ObjectVersion
from objecttypes.core.tests.factories import ObjectTypeFactory, ObjectVersionFactory
from objecttypes.utils.test import TokenAuthMixin

JSON_SCHEMA = {
"type": "object",
"title": "Tree",
"$schema": "http://json-schema.org/draft-07/schema#",
"required": ["diameter"],
"properties": {"diameter": {"type": "integer", "description": "size in cm."}},
}


class ObjectVersionAPITests(TokenAuthMixin, APITestCase):
def test_get_versions(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse("objectversion-list", args=[object_type.uuid])

response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.json(),
[
{
"url": f"http://testserver{reverse('objectversion-detail', args=[object_type.uuid, object_version.version])}",
"version": object_version.version,
"objectType": f"http://testserver{reverse('objecttype-detail', args=[object_version.object_type.uuid])}",
"publicationDate": object_version.publication_date.isoformat(),
"status": object_version.status,
"jsonSchema": JSON_SCHEMA,
}
],
)

def test_create_version(self):
object_type = ObjectTypeFactory.create()
data = {"jsonSchema": JSON_SCHEMA}
url = reverse("objectversion-list", args=[object_type.uuid])

response = self.client.post(url, data)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(ObjectVersion.objects.count(), 1)

object_version = ObjectVersion.objects.get()

self.assertEqual(object_version.object_type, object_type)
self.assertEqual(object_version.json_schema, JSON_SCHEMA)
self.assertEqual(object_version.version, 1)
self.assertEqual(object_version.publication_date, date.today())
self.assertEqual(object_version.status, ObjectVersionStatus.draft)

def test_update_version(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse(
"objectversion-detail", args=[object_type.uuid, object_version.version]
)
new_json_schema = {
"type": "object",
"title": "Tree",
"$schema": "http://json-schema.org/draft-07/schema#",
"required": ["diameter"],
"properties": {"diameter": {"type": "number"}},
}

response = self.client.put(url, {"jsonSchema": new_json_schema})

self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_delete_version_not_supported(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse(
"objectversion-detail", args=[object_type.uuid, object_version.version]
)

response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

def test_publish_version(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse(
"objectversion-publish", args=[object_type.uuid, object_version.version]
)

response = self.client.post(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

object_version.refresh_from_db()

self.assertEqual(object_version.status, ObjectVersionStatus.published)

0 comments on commit 39b78c2

Please sign in to comment.