Skip to content

Commit 7dbb3fc

Browse files
feat(test_utils): automatic schema validation
closes #23
1 parent cd5597a commit 7dbb3fc

File tree

2 files changed

+71
-56
lines changed

2 files changed

+71
-56
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
def test_route_wrong_params(client, route, get_full_data):
4+
for key, value in get_full_data().items():
5+
json = get_full_data()
6+
json.pop(key)
7+
response = client.post(route, json=json)
8+
assert response.status_code == 400
9+
error_description = response.json["description"]
10+
assert error_description == f"Validation error: '{key}' is a required property"
11+
12+
json = get_full_data()
13+
json["dumb_key"] = "dumb_value"
14+
response = client.post(route, json=json)
15+
assert response.status_code == 400
16+
error_description = response.json["description"]
17+
assert error_description == "Validation error: Additional properties are not allowed ('dumb_key' was unexpected)"
18+

tests/test_routes.py

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import os
1+
# Standard library imports
22
import base64
3+
import os
4+
5+
# Third party imports
36
from werkzeug.datastructures import FileStorage
47

8+
# Local application imports
9+
from src.opengeodeweb_back import test_utils
10+
511

612
def test_allowed_files(client):
713
route = f"/allowed_files"
8-
response = client.post(route, json={"supported_feature": None})
14+
get_full_data = lambda: {"supported_feature": "None"}
15+
json = get_full_data()
16+
response = client.post(route, json=json)
917
assert response.status_code == 200
1018
extensions = response.json["extensions"]
1119
assert type(extensions) is list
1220
for extension in extensions:
1321
assert type(extension) is str
1422

23+
# Test all params
24+
test_utils.test_route_wrong_params(client, route, get_full_data)
25+
1526

1627
def test_allowed_objects(client):
1728
route = f"/allowed_objects"
@@ -30,14 +41,8 @@ def get_full_data():
3041
for allowed_object in allowed_objects:
3142
assert type(allowed_object) is str
3243

33-
for key, value in get_full_data().items():
34-
json = get_full_data()
35-
json.pop(key)
36-
response = client.post(route, json=json)
37-
assert response.status_code == 400
38-
error_description = response.json["description"]
39-
assert error_description == f"Validation error: '{key}' is a required property"
40-
44+
# Test all params
45+
test_utils.test_route_wrong_params(client, route, get_full_data)
4146

4247
def test_upload_file(client):
4348
response = client.put(
@@ -58,11 +63,7 @@ def get_full_data():
5863
}
5964

6065
json = get_full_data()
61-
response = client.post(
62-
route,
63-
json=json,
64-
)
65-
66+
response = client.post(route,json=json)
6667
assert response.status_code == 200
6768
has_missing_files = response.json["has_missing_files"]
6869
mandatory_files = response.json["mandatory_files"]
@@ -71,37 +72,25 @@ def get_full_data():
7172
assert type(mandatory_files) is list
7273
assert type(additional_files) is list
7374

74-
for key, value in get_full_data().items():
75-
json = get_full_data()
76-
json.pop(key)
77-
response = client.post(route, json=json)
78-
assert response.status_code == 400
79-
error_description = response.json["description"]
80-
assert error_description == f"Validation error: '{key}' is a required property"
81-
75+
# Test all params
76+
test_utils.test_route_wrong_params(client, route, get_full_data)
8277

8378
def test_geographic_coordinate_systems(client):
8479
route = f"/geographic_coordinate_systems"
85-
80+
get_full_data = lambda: {"input_geode_object": "BRep"}
8681
# Normal test with geode_object 'BRep'
87-
response = client.post(route, json={"input_geode_object": "BRep"})
82+
response = client.post(route, json=get_full_data())
8883
assert response.status_code == 200
8984
crs_list = response.json["crs_list"]
9085
assert type(crs_list) is list
9186
for crs in crs_list:
9287
assert type(crs) is dict
9388

94-
# Test without geode_object
95-
response = client.post(route, json={})
96-
assert response.status_code == 400
97-
error_message = response.json["description"]
98-
assert (
99-
error_message == "Validation error: 'input_geode_object' is a required property"
100-
)
101-
89+
# Test all params
90+
test_utils.test_route_wrong_params(client, route, get_full_data)
10291

10392
def test_inspect_file(client):
104-
route = f"/inspect_file"
93+
route = f"/inspect_file"
10594

10695
def get_full_data():
10796
return {
@@ -117,14 +106,8 @@ def get_full_data():
117106
inspection_result = response.json["inspection_result"]
118107
assert type(inspection_result) is dict
119108

120-
for key, value in get_full_data().items():
121-
json = get_full_data()
122-
json.pop(key)
123-
response = client.post(route, json=json)
124-
assert response.status_code == 400
125-
error_description = response.json["description"]
126-
assert error_description == f"Validation error: '{key}' is a required property"
127-
109+
# Test all params
110+
test_utils.test_route_wrong_params(client, route, get_full_data)
128111

129112
def test_geode_objects_and_output_extensions(client):
130113
route = "/geode_objects_and_output_extensions"
@@ -148,13 +131,8 @@ def get_full_data():
148131
assert type(value) is dict
149132
assert type(value["is_saveable"]) is bool
150133

151-
# Test without input_geode_object
152-
response = client.post(route, json={})
153-
assert response.status_code == 400
154-
error_message = response.json["description"]
155-
assert (
156-
error_message == "Validation error: 'input_geode_object' is a required property"
157-
)
134+
# Test all params
135+
test_utils.test_route_wrong_params(client, route, get_full_data)
158136

159137

160138
def test_save_viewable_file(client):
@@ -178,10 +156,29 @@ def get_full_data():
178156
id = response.json["id"]
179157
assert type(id) is str
180158

181-
for key, value in get_full_data().items():
182-
json = get_full_data()
183-
json.pop(key)
184-
response = client.post(route, json=json)
185-
assert response.status_code == 400
186-
error_description = response.json["description"]
187-
assert error_description == f"Validation error: '{key}' is a required property"
159+
test_utils.test_route_wrong_params(client, route, get_full_data)
160+
161+
def test_vertex_attribute_names(client):
162+
response = client.put(
163+
f"/upload_file",
164+
data={"file": FileStorage(open("./tests/vertex_attribute.vtp", "rb"))},
165+
)
166+
assert response.status_code == 201
167+
168+
route = f"/vertex_attribute_names"
169+
170+
def get_full_data():
171+
return {
172+
"input_geode_object": "PolygonalSurface3D",
173+
"filename": "vertex_attribute.vtp",
174+
}
175+
176+
# Normal test with filename 'vertex_attribute.vtp'
177+
response = client.post(route, json=get_full_data())
178+
assert response.status_code == 200
179+
vertex_attribute_names = response.json["vertex_attribute_names"]
180+
assert type(vertex_attribute_names) is list
181+
for vertex_attribute_name in vertex_attribute_names:
182+
assert type(vertex_attribute_name) is str
183+
184+
test_utils.test_route_wrong_params(client, route, get_full_data)

0 commit comments

Comments
 (0)