Rrefactored schema

This commit is contained in:
Ken Tsang
2017-03-28 11:08:11 +01:00
parent 89e244ccd2
commit cb7cd233d1
2 changed files with 3 additions and 61 deletions

View File

@@ -1,5 +1,4 @@
from app.models import TEMPLATE_TYPES from app.models import TEMPLATE_TYPES
from app.schema_validation.definitions import uuid
from app.v2.template_schema import template from app.v2.template_schema import template
@@ -8,8 +7,7 @@ get_all_template_request = {
"description": "request schema for parameters allowed when getting all templates", "description": "request schema for parameters allowed when getting all templates",
"type": "object", "type": "object",
"properties": { "properties": {
"type": {"enum": TEMPLATE_TYPES}, "type": {"enum": TEMPLATE_TYPES}
"older_than": uuid
}, },
"additionalProperties": False, "additionalProperties": False,
} }
@@ -19,21 +17,6 @@ get_all_template_response = {
"description": "GET response schema when getting all templates", "description": "GET response schema when getting all templates",
"type": "object", "type": "object",
"properties": { "properties": {
"links": {
"type": "object",
"properties": {
"current": {
"type": "string",
"format": "uri"
},
"next": {
"type": "string",
"format": "uri"
}
},
"additionalProperties": False,
"required": ["current"],
},
"templates": { "templates": {
"type": "array", "type": "array",
"items": { "items": {
@@ -42,7 +25,7 @@ get_all_template_response = {
} }
} }
}, },
"required": ["links", "templates"], "required": ["templates"],
"definitions": { "definitions": {
"template": template "template": template
} }

View File

@@ -14,68 +14,46 @@ from jsonschema.exceptions import ValidationError
valid_json_get_all_response = [ valid_json_get_all_response = [
{ {
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [ "templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}, {"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"},
{"id": str(uuid.uuid4()), "version": 2, "uri": "http://template/id"} {"id": str(uuid.uuid4()), "version": 2, "uri": "http://template/id"}
] ]
}, },
{ {
'links': {'current': 'http://some.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}] "templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
}, },
{ {
'links': {'current': 'http://some.path'},
"templates": [] "templates": []
} }
] ]
invalid_json_get_all_response = [ invalid_json_get_all_response = [
({ ({
'links': {'current': 'invalid_uri'},
"templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}
]
}, ['links invalid_uri is not a valid URI.']),
({
'links': {'current': 'http://some.path'},
"templates": [ "templates": [
{"id": 'invalid_id', "version": 1, "uri": "http://template/id"} {"id": 'invalid_id', "version": 1, "uri": "http://template/id"}
] ]
}, ['templates is not a valid UUID']), }, ['templates is not a valid UUID']),
({ ({
'links': {'current': 'http://some.path'},
"templates": [ "templates": [
{"id": str(uuid.uuid4()), "version": 'invalid_version', "uri": "http://template/id"} {"id": str(uuid.uuid4()), "version": 'invalid_version', "uri": "http://template/id"}
] ]
}, ['templates invalid_version is not of type integer']), }, ['templates invalid_version is not of type integer']),
({ ({
'links': {'current': 'http://some.path'},
"templates": [ "templates": [
{"id": str(uuid.uuid4()), "version": 1, "uri": "invalid_uri"} {"id": str(uuid.uuid4()), "version": 1, "uri": "invalid_uri"}
] ]
}, ['templates invalid_uri is not a valid URI.']), }, ['templates invalid_uri is not a valid URI.']),
({}, ['templates is a required property']),
({ ({
'links': {'current': 'http://some.path'}
}, ['templates is a required property']),
({
'links': {'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1, "uri": "http://template/id"}]
}, ['links current is a required property']),
({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"version": 1, "uri": "http://template/id"}] "templates": [{"version": 1, "uri": "http://template/id"}]
}, ['templates id is a required property']), }, ['templates id is a required property']),
({ ({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "uri": "http://template/id"}] "templates": [{"id": str(uuid.uuid4()), "uri": "http://template/id"}]
}, ['templates version is a required property']), }, ['templates version is a required property']),
({ ({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"id": str(uuid.uuid4()), "version": 1}] "templates": [{"id": str(uuid.uuid4()), "version": 1}]
}, ['templates uri is a required property']), }, ['templates uri is a required property']),
({ ({
'links': {'current': 'http://some.path', 'next': 'http://some.other.path'},
"templates": [{"version": 1}] "templates": [{"version": 1}]
}, ['templates id is a required property', 'templates uri is a required property']), }, ['templates id is a required property', 'templates uri is a required property']),
] ]
@@ -93,12 +71,6 @@ def test_get_all_template_request_schema_against_valid_args_is_valid(template_ty
assert validate(data, get_all_template_request) == data assert validate(data, get_all_template_request) == data
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_valid_args_with_optional_is_valid(template_type, fake_uuid):
data = {'type': template_type, 'older_than': fake_uuid}
assert validate(data, get_all_template_request) == data
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES) @pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_invalid_args_is_invalid(template_type): def test_get_all_template_request_schema_against_invalid_args_is_invalid(template_type):
data = {'type': 'unknown'} data = {'type': 'unknown'}
@@ -112,19 +84,6 @@ def test_get_all_template_request_schema_against_invalid_args_is_invalid(templat
assert errors['errors'][0]['message'] == 'type unknown is not one of [sms, email, letter]' assert errors['errors'][0]['message'] == 'type unknown is not one of [sms, email, letter]'
@pytest.mark.parametrize("template_type", TEMPLATE_TYPES)
def test_get_all_template_request_schema_against_invalid_args_with_optional_is_invalid(template_type):
data = {'type': template_type, 'older_than': 'invalid_uuid'}
with pytest.raises(ValidationError) as e:
validate(data, get_all_template_request)
errors = json.loads(str(e.value))
assert errors['status_code'] == 400
assert len(errors['errors']) == 1
assert errors['errors'][0]['message'] == 'older_than is not a valid UUID'
@pytest.mark.parametrize("response", valid_json_get_all_response) @pytest.mark.parametrize("response", valid_json_get_all_response)
def test_valid_get_all_templates_response_schema_is_valid(response): def test_valid_get_all_templates_response_schema_is_valid(response):
assert validate(response, get_all_template_response) == response assert validate(response, get_all_template_response) == response