Skip to content

Commit 17ea181

Browse files
authored
Merge pull request #202 from Geode-solutions/fix/test.src
fix(Tests): better handle relative paths
2 parents d72bfa4 + 9cb84a6 commit 17ea181

File tree

7 files changed

+40
-33
lines changed

7 files changed

+40
-33
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,3 @@ werkzeug==3.1.2
6060
# flask
6161
# flask-cors
6262

63-
opengeodeweb-microservice==1.*,>=1.0.6

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def upload_file() -> flask.Response:
4545

4646
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
4747
if not os.path.exists(UPLOAD_FOLDER):
48-
os.mkdir(UPLOAD_FOLDER)
48+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
49+
4950
file = flask.request.files["file"]
5051
filename = werkzeug.utils.secure_filename(os.path.basename(file.filename))
5152
file.save(os.path.join(UPLOAD_FOLDER, filename))

tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import pytest
1010

1111
# Local application imports
12-
from src.opengeodeweb_back.app import app
12+
from opengeodeweb_back.app import app
1313

14-
# from opengeodeweb_back import app_config
1514
from opengeodeweb_microservice.database.connection import init_database
1615

1716
TEST_ID = "1"

tests/test_create_routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from flask.testing import FlaskClient
99

1010
# Local application imports
11-
from src.opengeodeweb_back import test_utils
11+
from opengeodeweb_back import test_utils
1212

1313

1414
@pytest.fixture

tests/test_models_routes.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
from opengeodeweb_microservice.database.data import Data
77
from opengeodeweb_microservice.database.connection import get_session
88

9+
base_dir = os.path.abspath(os.path.dirname(__file__))
10+
data_dir = os.path.join(base_dir, "data")
11+
912

1013
def test_model_mesh_components(client, test_id):
1114
route = "/opengeodeweb_back/models/vtm_component_indices"
1215

1316
with client.application.app_context():
1417
data_path = geode_functions.data_file_path(test_id, "viewable.vtm")
1518
os.makedirs(os.path.dirname(data_path), exist_ok=True)
16-
shutil.copy("./tests/data/cube.vtm", data_path)
19+
shutil.copy(os.path.join(data_dir, "cube.vtm"), data_path)
1720

1821
response = client.post(route, json={"id": test_id})
1922
assert response.status_code == 200
@@ -30,7 +33,7 @@ def test_model_mesh_components(client, test_id):
3033

3134
def test_extract_brep_uuids(client, test_id):
3235
route = "/opengeodeweb_back/models/mesh_components"
33-
brep_filename = "cube.og_brep"
36+
brep_filename = os.path.join(data_dir, "cube.og_brep")
3437

3538
with client.application.app_context():
3639
data_entry = Data.create(
@@ -43,13 +46,6 @@ def test_extract_brep_uuids(client, test_id):
4346
if session:
4447
session.commit()
4548

46-
src_path = os.path.join("tests", "data", brep_filename)
47-
dest_path = os.path.join(
48-
flask.current_app.config["DATA_FOLDER_PATH"], data_entry.id, brep_filename
49-
)
50-
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
51-
shutil.copy2(src_path, dest_path)
52-
5349
response = client.post(route, json={"id": data_entry.id})
5450
assert response.status_code == 200
5551
assert "uuid_dict" in response.json

tests/test_routes.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Standard library imports
22
import os
3-
import shutil
43

54
# Third party imports
65
from werkzeug.datastructures import FileStorage
@@ -10,6 +9,9 @@
109
from opengeodeweb_microservice.database.connection import get_session
1110
from opengeodeweb_back import geode_functions, test_utils
1211

12+
base_dir = os.path.abspath(os.path.dirname(__file__))
13+
data_dir = os.path.join(base_dir, "data")
14+
1315

1416
def test_allowed_files(client):
1517
route = f"/opengeodeweb_back/allowed_files"
@@ -48,9 +50,11 @@ def get_full_data():
4850

4951

5052
def test_upload_file(client, filename="test.og_brep"):
53+
file = os.path.join(data_dir, filename)
54+
print(f"{file=}", flush=True)
5155
response = client.put(
5256
f"/opengeodeweb_back/upload_file",
53-
data={"file": FileStorage(open(f"./tests/data/{filename}", "rb"))},
57+
data={"file": FileStorage(open(file, "rb"))},
5458
)
5559
assert response.status_code == 201
5660

@@ -171,19 +175,19 @@ def get_full_data():
171175

172176
def test_texture_coordinates(client, test_id):
173177
with client.application.app_context():
178+
file = os.path.join(data_dir, "hat.vtp")
174179
data = Data.create(
175180
geode_object="PolygonalSurface3D",
176181
viewer_object=geode_functions.get_object_type("PolygonalSurface3D"),
177-
input_file="hat.vtp",
182+
input_file=file,
178183
)
179-
data.native_file_name = "hat.vtp"
184+
data.native_file_name = file
180185
session = get_session()
181186
if session:
182187
session.commit()
183188

184189
data_path = geode_functions.data_file_path(data.id, data.native_file_name)
185190
os.makedirs(os.path.dirname(data_path), exist_ok=True)
186-
shutil.copy("./tests/data/hat.vtp", data_path)
187191
assert os.path.exists(data_path), f"File not found at {data_path}"
188192
response = client.post(
189193
"/opengeodeweb_back/texture_coordinates", json={"id": data.id}
@@ -199,19 +203,19 @@ def test_vertex_attribute_names(client, test_id):
199203
route = f"/opengeodeweb_back/vertex_attribute_names"
200204

201205
with client.application.app_context():
206+
file = os.path.join(data_dir, "test.vtp")
202207
data = Data.create(
203208
geode_object="PolygonalSurface3D",
204209
viewer_object=geode_functions.get_object_type("PolygonalSurface3D"),
205-
input_file="test.vtp",
210+
input_file=file,
206211
)
207-
data.native_file_name = "test.vtp"
212+
data.native_file_name = file
208213
session = get_session()
209214
if session:
210215
session.commit()
211216

212217
data_path = geode_functions.data_file_path(data.id, data.native_file_name)
213218
os.makedirs(os.path.dirname(data_path), exist_ok=True)
214-
shutil.copy("./tests/data/test.vtp", data_path)
215219
assert os.path.exists(data_path), f"File not found at {data_path}"
216220
response = client.post(route, json={"id": data.id})
217221
assert response.status_code == 200
@@ -225,19 +229,19 @@ def test_polygon_attribute_names(client, test_id):
225229
route = f"/opengeodeweb_back/polygon_attribute_names"
226230

227231
with client.application.app_context():
232+
file = os.path.join(data_dir, "test.vtp")
228233
data = Data.create(
229234
geode_object="PolygonalSurface3D",
230235
viewer_object=geode_functions.get_object_type("PolygonalSurface3D"),
231-
input_file="test.vtp",
236+
input_file=file,
232237
)
233-
data.native_file_name = "test.vtp"
238+
data.native_file_name = file
234239
session = get_session()
235240
if session:
236241
session.commit()
237242

238243
data_path = geode_functions.data_file_path(data.id, data.native_file_name)
239244
os.makedirs(os.path.dirname(data_path), exist_ok=True)
240-
shutil.copy("./tests/data/test.vtp", data_path)
241245
assert os.path.exists(data_path), f"File not found at {data_path}"
242246
response = client.post(route, json={"id": data.id})
243247
assert response.status_code == 200
@@ -251,19 +255,19 @@ def test_polyhedron_attribute_names(client, test_id):
251255
route = f"/opengeodeweb_back/polyhedron_attribute_names"
252256

253257
with client.application.app_context():
258+
file = os.path.join(data_dir, "test.vtu")
254259
data = Data.create(
255260
geode_object="PolyhedralSolid3D",
256261
viewer_object=geode_functions.get_object_type("PolyhedralSolid3D"),
257-
input_file="test.vtu",
262+
input_file=file,
258263
)
259-
data.native_file_name = "test.vtu"
264+
data.native_file_name = file
260265
session = get_session()
261266
if session:
262267
session.commit()
263268

264269
data_path = geode_functions.data_file_path(data.id, data.native_file_name)
265270
os.makedirs(os.path.dirname(data_path), exist_ok=True)
266-
shutil.copy("./tests/data/test.vtu", data_path)
267271
assert os.path.exists(data_path), f"File not found at {data_path}"
268272
response = client.post(route, json={"id": data.id})
269273
print(response.json)

tests/test_utils_functions.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from opengeodeweb_microservice.database.connection import get_session
1313
from opengeodeweb_back import geode_functions, utils_functions
1414

15+
base_dir = os.path.abspath(os.path.dirname(__file__))
16+
data_dir = os.path.join(base_dir, "data")
17+
1518

1619
def test_increment_request_counter(app_context):
1720
assert flask.current_app.config.get("REQUEST_COUNTER") == 0
@@ -91,15 +94,16 @@ def test_create_data_folder_from_id(client):
9194
def test_save_all_viewables_and_return_info(client):
9295
app = client.application
9396
with app.app_context():
94-
base_dir = os.path.abspath(os.path.dirname(__file__))
95-
expected_db_path = os.path.join(base_dir, "data", "project.db")
97+
expected_db_path = os.path.join(data_dir, "project.db")
9698
expected_uri = f"sqlite:///{expected_db_path}"
9799

98100
assert app.config["SQLALCHEMY_DATABASE_URI"] == expected_uri
99101
assert os.path.exists(expected_db_path)
100102

101103
geode_object = "BRep"
102-
data = geode_functions.load(geode_object, "./tests/data/test.og_brep")
104+
data = geode_functions.load(
105+
geode_object, os.path.join(data_dir, "test.og_brep")
106+
)
103107
input_file = "test.og_brep"
104108
additional_files = ["additional_file.txt"]
105109

@@ -142,7 +146,9 @@ def test_save_all_viewables_commits_to_db(client):
142146
app = client.application
143147
with app.app_context():
144148
geode_object = "BRep"
145-
data = geode_functions.load(geode_object, "./tests/data/test.og_brep")
149+
data = geode_functions.load(
150+
geode_object, os.path.join(data_dir, "test.og_brep")
151+
)
146152
input_file = "test.og_brep"
147153

148154
data_entry = Data.create(
@@ -166,7 +172,9 @@ def test_generate_native_viewable_and_light_viewable_from_object(client):
166172
app = client.application
167173
with app.app_context():
168174
geode_object = "BRep"
169-
data = geode_functions.load(geode_object, "./tests/data/test.og_brep")
175+
data = geode_functions.load(
176+
geode_object, os.path.join(data_dir, "test.og_brep")
177+
)
170178

171179
result = (
172180
utils_functions.generate_native_viewable_and_light_viewable_from_object(

0 commit comments

Comments
 (0)