Skip to content

Commit

Permalink
better tests use case for json validator
Browse files Browse the repository at this point in the history
  • Loading branch information
sonic182 committed Sep 16, 2017
1 parent 4c646ad commit cb99156
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
5 changes: 2 additions & 3 deletions app/validators/json_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def validate(self, data, field='', constrain=None, started=False):
return res, errors

if err:
return (False, {'payload': ERRORS[err]})
return (None, {'payload': ERRORS[err]})

my_field = field
for key in constrain:
Expand Down Expand Up @@ -64,8 +64,7 @@ def _key_match(self, obj, rules, key, field, started, res, errors):
obj, field, rules.get(
'properties', None), started)

if res2:
res[key] = res2
res[key] = res2 or {}

if errors2:
errors[key] = errors2
Expand Down
33 changes: 24 additions & 9 deletions tests/components/schema_validator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Json schemas validator."""

from json import dumps
from json import loads
from app.validators.json_schemas import JsonSchemaValidator


Expand Down Expand Up @@ -41,7 +42,7 @@ def test_constrain_primitive():
"""Test constrain primitive types"""
constrain = {
'string': {},
'integer': {'type': str},
'integer': {'type': int},
'float': {'type': float},
'boolean': {'type': bool},
'json': {
Expand All @@ -63,23 +64,24 @@ def test_constrain_primitive():
})

res, err = JsonSchemaValidator(constrain).validate('{as: "df"}')
assert err
assert err == {'payload': 'INVALID PAYLOAD'}

res, err = JsonSchemaValidator(constrain).validate(json)
assert err
assert err == {'extra_1': 'Missing field', 'extra_2': 'Missing field'}
del constrain['extra_1']
del constrain['extra_2']

constrain['integer']['type'] = str
res, err = JsonSchemaValidator(constrain).validate(json)
assert err
constrain['integer']['type'] = int
assert err == {'integer': 'Bad data type'}

constrain['integer']['type'] = int
res, err = JsonSchemaValidator(constrain).validate(json)
assert res and not err
assert res == loads(json)


def test_constrain_lists_dicts():
"""Test constrain primitive types."""
"""Test nested structures."""
constrain = {
'json': {
'type': dict,
Expand Down Expand Up @@ -124,7 +126,19 @@ def test_constrain_lists_dicts():
})

res, err = JsonSchemaValidator(constrain).validate(json)
assert res and err
assert res == {
'json': {'float': 12.12, 'integer': 42},
'list': [
{'lastname': 'mogollon', 'name': 'johan'},
{'lastname': 'mogollon', 'name': 'johan'},
{'age': [12, 24], 'lastname': 'paul', 'name': 'jean'}
]}
assert err == {
'list': [
{'list.0.age': 'Missing field'},
{'list.1.age': 'Missing field'}
]
}

json = dumps({
'json': {
Expand All @@ -135,4 +149,5 @@ def test_constrain_lists_dicts():
})

res, err = JsonSchemaValidator(constrain).validate(json)
assert res and err
assert res == {'json': {'float': 12.12, 'integer': 42}, 'list': []}
assert err == {'list': ['Bad data type', 'Bad data type', 'Bad data type']}

0 comments on commit cb99156

Please sign in to comment.