Skip to content

Commit

Permalink
🎨 [#50] added more tests for check_objecttype
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-maykin committed Aug 16, 2024
1 parent 4cc89b2 commit d7297da
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/objects/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ def check_objecttype(object_type, version, data):

try:
response = client.get(objecttype_version_url)
except requests.RequestException as exc:
msg = f"Object type version can not be retrieved: {exc.args[0]}"
except requests.RequestException:
msg = "Object type version can not be retrieved."
raise ValidationError(msg)

try:
response_data = response.json()
except requests.JSONDecodeError:
raise ValidationError("Object type doesn't have retrievable data")
raise ValidationError("Object type doesn't have retrievable data.")

try:
schema = response_data.get("jsonSchema")
schema = response_data["jsonSchema"]
except KeyError:
msg = f"{objecttype_version_url} does not appear to be a valid objecttype."
raise ValidationError(msg)
Expand Down
58 changes: 57 additions & 1 deletion src/objects/tests/v1/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid

import requests
import requests_mock
from rest_framework import status
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -101,7 +102,62 @@ def test_create_object_no_version(self, m):

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Object type doesn't have retrievable data"]
data["non_field_errors"], ["Object type doesn't have retrievable data."]
)

def test_create_object_objecttype_request_error(self, m):
mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
m.get(f"{self.object_type.url}/versions/10", exc=requests.HTTPError)

url = reverse("object-list")
data = {
"type": self.object_type.url,
"record": {
"typeVersion": 10,
"data": {"plantDate": "2020-04-12", "diameter": 30},
"startAt": "2020-01-01",
},
}

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

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Object.objects.count(), 0)

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Object type version can not be retrieved."]
)

def test_create_object_objecttype_with_no_jsonSchema(self, m):
mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
m.get(
f"{self.object_type.url}/versions/10",
status_code=200,
json={"key": "value"},
)

url = reverse("object-list")
data = {
"type": self.object_type.url,
"record": {
"typeVersion": 10,
"data": {"plantDate": "2020-04-12", "diameter": 30},
"startAt": "2020-01-01",
},
}

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

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Object.objects.count(), 0)

data = response.json()
self.assertEqual(
data["non_field_errors"],
[
f"{self.object_type.url}/versions/10 does not appear to be a valid objecttype."
],
)

def test_create_object_schema_invalid(self, m):
Expand Down
58 changes: 57 additions & 1 deletion src/objects/tests/v2/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid

import requests
import requests_mock
from rest_framework import status
from rest_framework.test import APITestCase
Expand Down Expand Up @@ -101,7 +102,62 @@ def test_create_object_no_version(self, m):

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Object type doesn't have retrievable data"]
data["non_field_errors"], ["Object type doesn't have retrievable data."]
)

def test_create_object_objecttype_request_error(self, m):
mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
m.get(f"{self.object_type.url}/versions/10", exc=requests.HTTPError)

url = reverse("object-list")
data = {
"type": self.object_type.url,
"record": {
"typeVersion": 10,
"data": {"plantDate": "2020-04-12", "diameter": 30},
"startAt": "2020-01-01",
},
}

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

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Object.objects.count(), 0)

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Object type version can not be retrieved."]
)

def test_create_object_objecttype_with_no_jsonSchema(self, m):
mock_service_oas_get(m, OBJECT_TYPES_API, "objecttypes")
m.get(
f"{self.object_type.url}/versions/10",
status_code=200,
json={"key": "value"},
)

url = reverse("object-list")
data = {
"type": self.object_type.url,
"record": {
"typeVersion": 10,
"data": {"plantDate": "2020-04-12", "diameter": 30},
"startAt": "2020-01-01",
},
}

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

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Object.objects.count(), 0)

data = response.json()
self.assertEqual(
data["non_field_errors"],
[
f"{self.object_type.url}/versions/10 does not appear to be a valid objecttype."
],
)

def test_create_object_schema_invalid(self, m):
Expand Down

0 comments on commit d7297da

Please sign in to comment.