Skip to content

Commit becf0d7

Browse files
author
Gaurav Singh
committed
Added example of schema validation for Read all operation in people API
1 parent 701791c commit becf0d7

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pytest = "*"
1111
assertpy = "*"
1212
lxml = "*"
1313
jsonpath-ng = "*"
14+
cerberus = "*"
1415

1516
[requires]
1617
python_version = "3.8"

Pipfile.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/people_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_person_can_be_added_with_a_json_template(create_data):
6868
# Read full syntax: https://pypi.org/project/jsonpath-ng/
6969
jsonpath_expr = parse("$.[*].lname")
7070
result = [match.value for match in jsonpath_expr.find(peoples)]
71+
print(result)
7172

7273
expected_last_name = create_data['lname']
7374
assert_that(result).contains(expected_last_name)

tests/schema_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import json
2+
3+
import requests
4+
from cerberus import Validator
5+
6+
from config import BASE_URI
7+
8+
9+
def test_read_all_operation_has_expected_schema():
10+
schema = {
11+
"fname": {'type': 'string'},
12+
"lname": {'type': 'string'},
13+
"person_id": {'type': 'integer'},
14+
"timestamp": {'type': 'string'}
15+
}
16+
17+
response = requests.get(BASE_URI)
18+
persons = json.loads(response.text)
19+
20+
validator = Validator(schema)
21+
22+
for person in persons:
23+
is_valid = validator.validate(person)
24+
25+
if not is_valid:
26+
print(validator.errors)

0 commit comments

Comments
 (0)