-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_validate_models.py
More file actions
128 lines (107 loc) · 3.74 KB
/
Copy pathtest_validate_models.py
File metadata and controls
128 lines (107 loc) · 3.74 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
"""Tests for validate_models.py"""
import json
import os
import tempfile
import pytest
from validate_models import (
ModelValidator,
ValidationReport,
ValidationIssue,
find_model_files,
)
@pytest.fixture
def tmp_dir():
with tempfile.TemporaryDirectory() as d:
yield d
def write_json(directory, filename, data):
path = os.path.join(directory, filename)
with open(path, "w") as f:
json.dump(data, f)
return path
class TestValidationReport:
def test_add_error(self):
report = ValidationReport()
report.add_issue(ValidationIssue("f.json", "/", "error", "test", "msg"))
assert report.total_errors == 1
def test_add_warning(self):
report = ValidationReport()
report.add_issue(ValidationIssue("f.json", "/", "warning", "test", "msg"))
assert report.total_warnings == 1
class TestModelValidator:
def test_invalid_json(self, tmp_dir):
path = os.path.join(tmp_dir, "bad.json")
with open(path, "w") as f:
f.write("{invalid}")
v = ModelValidator(tmp_dir)
v.validate_file("bad.json")
assert v.report.total_errors >= 1
def test_missing_swagger_fields(self, tmp_dir):
write_json(tmp_dir, "api.json", {"swagger": "2.0"})
v = ModelValidator(tmp_dir)
v.validate_file("api.json")
errs = [i for i in v.report.issues if i.category == "required_field"]
assert len(errs) >= 1
def test_valid_swagger(self, tmp_dir):
write_json(tmp_dir, "api.json", {
"swagger": "2.0",
"info": {"title": "Test", "version": "1.0.0"},
"paths": {}
})
v = ModelValidator(tmp_dir)
v.validate_file("api.json")
assert v.report.total_errors == 0
def test_invalid_type(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"properties": {
"name": {"type": "invalid_type"}
}
})
v = ModelValidator(tmp_dir)
v.validate_file("model.json")
errs = [i for i in v.report.issues if i.category == "type"]
assert len(errs) >= 1
def test_array_missing_items(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"properties": {
"tags": {"type": "array"}
}
})
v = ModelValidator(tmp_dir)
v.validate_file("model.json")
warns = [i for i in v.report.issues if "items" in i.message]
assert len(warns) >= 1
def test_required_prop_not_defined(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"required": ["name"],
"properties": {}
})
v = ModelValidator(tmp_dir)
v.validate_file("model.json")
errs = [i for i in v.report.issues if "Required property" in i.message]
assert len(errs) >= 1
def test_naming_convention(self, tmp_dir):
write_json(tmp_dir, "model.json", {
"type": "object",
"properties": {
"BadName": {"type": "string"}
}
})
v = ModelValidator(tmp_dir, naming_convention="camel")
v.validate_file("model.json")
infos = [i for i in v.report.issues if i.category == "naming"]
assert len(infos) >= 1
class TestFindModelFiles:
def test_finds_json(self, tmp_dir):
write_json(tmp_dir, "model.json", {})
files = find_model_files(tmp_dir)
assert len(files) == 1
def test_ignores_hidden(self, tmp_dir):
write_json(tmp_dir, ".hidden.json", {})
files = find_model_files(tmp_dir)
assert len(files) == 0
if __name__ == "__main__":
pytest.main([__file__, "-v"])