-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON_testing.py
More file actions
113 lines (92 loc) · 3.32 KB
/
Copy pathJSON_testing.py
File metadata and controls
113 lines (92 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Validate function
import json
import argparse
from nested_json import get_json_value
from cli_design import display_report
def validate_response(response, rules):
"""
Takes an API response in JSON format and checks it against a rules JSON file.
Args:
response: An API response doc in JSON format (i.e. response.json)
rules: An API rules doc in JSON format (i.e. rules.json)
Returns a dictionary containing overall pass/fail and all validation checks.
"""
check_list = []
required_keys = rules["required_keys"]
for element in required_keys:
path = element.split(".")
found, _ = get_json_value(path, response)
check_dict = {}
if found:
check_dict["path"] = element
check_dict["check_type"] = "required"
check_dict["passed"] = True
check_list.append(check_dict)
else:
check_dict["path"] = element
check_dict["check_type"] = "required"
check_dict["passed"] = False
check_dict["reason"] = "missing key"
check_list.append(check_dict)
# Mapping dictionary
type_dict = {
"string": str,
"integer": int,
"number": (int, float),
"object": dict,
"array": list,
"boolean": bool
}
type_check_list = []
expected_types = rules["expected_types"]
for element in expected_types:
expected_type_name = expected_types[element]
check_dict = {}
check_dict["path"] = element
check_dict["check_type"] = "type"
check_dict["expected_type"] = expected_type_name
path = element.split(".")
found, value = get_json_value(path, response)
if found:
if isinstance(value, type_dict[expected_type_name]):
check_dict["passed"] = True
type_check_list.append(check_dict)
else:
check_dict["passed"] = False
check_dict["reason"] = "wrong type"
check_dict["actual_type"] = type(value).__name__
type_check_list.append(check_dict)
else:
check_dict["passed"] = False
check_dict["reason"] = "missing key"
type_check_list.append(check_dict)
check_lists = check_list + type_check_list
final_dict = {}
overall_passed = True
for e in check_lists:
if not e["passed"]:
overall_passed = False
final_dict["overall_passed"] = overall_passed
final_dict["all_checks"] = check_lists
return final_dict
# CLI tool logic
def main():
parser = argparse.ArgumentParser(description="Validate an API JSON response against rules.")
parser.add_argument("response")
parser.add_argument("rules")
args = parser.parse_args()
def load_json(filename):
with open(filename) as f:
return json.load(f)
response_data = load_json(args.response)
print(response_data.keys())
rules_data = load_json(args.rules)
results = validate_response(response_data, rules_data)
overall_passed = results["overall_passed"]
all_checks = results["all_checks"]
# Pre-retro CLI code (commented out for now)
# results_json = json.dumps(results, indent=2)
# print(results_json)
display_report(all_checks, overall_passed)
if __name__ == "__main__":
main()