Skip to content

Commit 3aeb276

Browse files
committed
fix(export_file): fix some tests and use native_file if there is not input_file
1 parent 7a01147 commit 3aeb276

File tree

4 files changed

+66
-32
lines changed

4 files changed

+66
-32
lines changed

src/opengeodeweb_back/geode_functions.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def geode_object_output_extensions(
5555
geode_object: GeodeObject,
5656
) -> dict[GeodeObjectType, dict[str, bool]]:
5757
results: dict[GeodeObjectType, dict[str, bool]] = {}
58-
for mixin_geode_object in geode_objects[geode_object.geode_object_type()].__mro__:
58+
current_type = geode_object.geode_object_type()
59+
for mixin_geode_object in geode_objects[current_type].__mro__:
5960
output_extensions_method = getattr(
6061
mixin_geode_object, "output_extensions", None
6162
)
@@ -66,11 +67,16 @@ def geode_object_output_extensions(
6667
continue
6768
object_output_extensions: dict[str, bool] = {}
6869
is_saveable_method = getattr(mixin_geode_object, "is_saveable")
70+
target_type = None
71+
if hasattr(mixin_geode_object, "geode_object_type"):
72+
target_type = mixin_geode_object.geode_object_type()
73+
if target_type and target_type != current_type:
74+
continue
6975
for output_extension in output_extensions:
7076
bool_is_saveable = is_saveable_method(
7177
geode_object, f"test.{output_extension}"
7278
)
7379
object_output_extensions[output_extension] = bool_is_saveable
74-
if hasattr(mixin_geode_object, "geode_object_type"):
75-
results[mixin_geode_object.geode_object_type()] = object_output_extensions
80+
if target_type:
81+
results[target_type] = object_output_extensions
7682
return results

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
routes = flask.Blueprint("routes", __name__, url_prefix="/opengeodeweb_back")
3131

32-
3332
routes.register_blueprint(
3433
blueprint_models.routes,
3534
url_prefix=blueprint_models.routes.url_prefix,
@@ -38,7 +37,6 @@
3837

3938
schemas_dict = get_schemas_dict(os.path.join(os.path.dirname(__file__), "schemas"))
4039

41-
4240
@routes.route(
4341
schemas_dict["allowed_files"]["route"],
4442
methods=schemas_dict["allowed_files"]["methods"],
@@ -51,7 +49,6 @@ def allowed_files() -> flask.Response:
5149
extensions.add(extension)
5250
return flask.make_response({"extensions": list(extensions)}, 200)
5351

54-
5552
@routes.route(
5653
schemas_dict["upload_file"]["route"],
5754
methods=schemas_dict["upload_file"]["methods"],
@@ -66,7 +63,6 @@ def upload_file() -> flask.Response:
6663
file.save(os.path.join(UPLOAD_FOLDER, filename))
6764
return flask.make_response({"message": "File uploaded"}, 201)
6865

69-
7066
@routes.route(
7167
schemas_dict["allowed_objects"]["route"],
7268
methods=schemas_dict["allowed_objects"]["methods"],
@@ -92,7 +88,6 @@ def allowed_objects() -> flask.Response:
9288
}
9389
return flask.make_response({"allowed_objects": allowed_objects}, 200)
9490

95-
9691
@routes.route(
9792
schemas_dict["missing_files"]["route"],
9893
methods=schemas_dict["missing_files"]["methods"],
@@ -131,7 +126,6 @@ def missing_files() -> flask.Response:
131126
200,
132127
)
133128

134-
135129
@routes.route(
136130
schemas_dict["geographic_coordinate_systems"]["route"],
137131
methods=schemas_dict["geographic_coordinate_systems"]["methods"],
@@ -156,7 +150,6 @@ def crs_converter_geographic_coordinate_systems() -> flask.Response:
156150
crs_list.append(crs)
157151
return flask.make_response({"crs_list": crs_list}, 200)
158152

159-
160153
@routes.route(
161154
schemas_dict["inspect_file"]["route"],
162155
methods=schemas_dict["inspect_file"]["methods"],
@@ -174,7 +167,6 @@ def inspect_file() -> flask.Response:
174167
inspection_result = extract_inspector_result(inspection_data)
175168
return flask.make_response({"inspection_result": inspection_result}, 200)
176169

177-
178170
def extract_inspector_result(inspection_data: Any) -> object:
179171
new_object = {}
180172

@@ -202,7 +194,6 @@ def extract_inspector_result(inspection_data: Any) -> object:
202194
new_object["issues"] = issues
203195
return new_object
204196

205-
206197
@routes.route(
207198
schemas_dict["geode_objects_and_output_extensions"]["route"],
208199
methods=schemas_dict["geode_objects_and_output_extensions"]["methods"],
@@ -224,7 +215,6 @@ def geode_objects_and_output_extensions() -> flask.Response:
224215
200,
225216
)
226217

227-
228218
@routes.route(
229219
schemas_dict["save_viewable_file"]["route"],
230220
methods=schemas_dict["save_viewable_file"]["methods"],
@@ -242,7 +232,6 @@ def save_viewable_file() -> flask.Response:
242232
200,
243233
)
244234

245-
246235
@routes.route(
247236
schemas_dict["texture_coordinates"]["route"],
248237
methods=schemas_dict["texture_coordinates"]["methods"],
@@ -258,7 +247,6 @@ def texture_coordinates() -> flask.Response:
258247
texture_coordinates = geode_object.texture_manager().texture_names()
259248
return flask.make_response({"texture_coordinates": texture_coordinates}, 200)
260249

261-
262250
@routes.route(
263251
schemas_dict["vertex_attribute_names"]["route"],
264252
methods=schemas_dict["vertex_attribute_names"]["methods"],
@@ -279,7 +267,6 @@ def vertex_attribute_names() -> flask.Response:
279267
200,
280268
)
281269

282-
283270
@routes.route(
284271
schemas_dict["cell_attribute_names"]["route"],
285272
methods=schemas_dict["cell_attribute_names"]["methods"],
@@ -300,7 +287,6 @@ def cell_attribute_names() -> flask.Response:
300287
200,
301288
)
302289

303-
304290
@routes.route(
305291
schemas_dict["polygon_attribute_names"]["route"],
306292
methods=schemas_dict["polygon_attribute_names"]["methods"],
@@ -321,7 +307,6 @@ def polygon_attribute_names() -> flask.Response:
321307
200,
322308
)
323309

324-
325310
@routes.route(
326311
schemas_dict["polyhedron_attribute_names"]["route"],
327312
methods=schemas_dict["polyhedron_attribute_names"]["methods"],
@@ -344,7 +329,6 @@ def polyhedron_attribute_names() -> flask.Response:
344329
200,
345330
)
346331

347-
348332
@routes.route(
349333
schemas_dict["ping"]["route"],
350334
methods=schemas_dict["ping"]["methods"],
@@ -354,14 +338,12 @@ def ping() -> flask.Response:
354338
flask.current_app.config.update(LAST_PING_TIME=time.time())
355339
return flask.make_response({"message": "Flask server is running"}, 200)
356340

357-
358341
@routes.route(schemas_dict["kill"]["route"], methods=schemas_dict["kill"]["methods"])
359342
def kill() -> flask.Response:
360343
print("Manual server kill, shutting down...", flush=True)
361344
os._exit(0)
362345
return flask.make_response({"message": "Flask server is dead"}, 200)
363346

364-
365347
@routes.route(
366348
schemas_dict["export_project"]["route"],
367349
methods=schemas_dict["export_project"]["methods"],
@@ -381,6 +363,14 @@ def export_project() -> flask.Response:
381363
export_vease_path = os.path.join(project_folder, filename)
382364

383365
with get_session() as session:
366+
session.query(Data).filter(
367+
(Data.input_file == None) | (Data.input_file == "")
368+
).update(
369+
{Data.input_file: Data.native_file},
370+
synchronize_session=False
371+
)
372+
session.commit()
373+
384374
rows = session.query(Data.id, Data.input_file, Data.additional_files).all()
385375

386376
with zipfile.ZipFile(
@@ -410,7 +400,6 @@ def export_project() -> flask.Response:
410400

411401
return utils_functions.send_file(project_folder, [export_vease_path], filename)
412402

413-
414403
@routes.route(
415404
schemas_dict["import_project"]["route"],
416405
methods=schemas_dict["import_project"]["methods"],
@@ -427,7 +416,6 @@ def import_project() -> flask.Response:
427416

428417
data_folder_path: str = flask.current_app.config["DATA_FOLDER_PATH"]
429418

430-
# 423 Locked bypass : remove stopped requests
431419
if connection.scoped_session_registry:
432420
connection.scoped_session_registry.remove()
433421
if connection.engine:

tests/test_geode_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ def test_input_output() -> None:
5959
geode_object,
6060
os.path.join(os.path.abspath(f"./output"), filename),
6161
)
62-
assert type(saved_files) is list
62+
assert type(saved_files) is list
6363
for saved_file in saved_files:
64-
os.remove(saved_file)
64+
os.remove(saved_file)

tests/test_models_routes.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
base_dir = os.path.abspath(os.path.dirname(__file__))
1515
data_dir = os.path.join(base_dir, "data")
1616

17-
1817
def test_model_mesh_components(client: FlaskClient, test_id: str) -> None:
1918
route = "/opengeodeweb_back/models/vtm_component_indices"
2019

@@ -35,7 +34,6 @@ def test_model_mesh_components(client: FlaskClient, test_id: str) -> None:
3534
for uuid in uuid_dict.keys():
3635
assert isinstance(uuid, str)
3736

38-
3937
def test_extract_brep_uuids(client: FlaskClient, test_id: str) -> None:
4038
route = "/opengeodeweb_back/models/mesh_components"
4139
brep_filename = os.path.join(data_dir, "cube.og_brep")
@@ -57,7 +55,6 @@ def test_extract_brep_uuids(client: FlaskClient, test_id: str) -> None:
5755
uuid_dict = response.get_json()["uuid_dict"]
5856
assert isinstance(uuid_dict, dict)
5957

60-
6158
def test_export_project_route(client: FlaskClient, tmp_path: Path) -> None:
6259
route = "/opengeodeweb_back/export_project"
6360
snapshot = {
@@ -69,6 +66,41 @@ def test_export_project_route(client: FlaskClient, tmp_path: Path) -> None:
6966
database_root_path = os.path.join(project_folder, "project.db")
7067
with open(database_root_path, "wb") as f:
7168
f.write(b"test_project_db")
69+
70+
with get_session() as session:
71+
session.query(Data).delete()
72+
session.commit()
73+
74+
data1 = Data(
75+
id="test_data_1",
76+
geode_object="BRep",
77+
viewer_object="BRep",
78+
input_file=None,
79+
native_file="test_native.txt",
80+
additional_files=[]
81+
)
82+
data2 = Data(
83+
id="test_data_2",
84+
geode_object="Section",
85+
viewer_object="Section",
86+
input_file="test_input.txt",
87+
native_file="test_native2.txt",
88+
additional_files=[]
89+
)
90+
session.add(data1)
91+
session.add(data2)
92+
session.commit()
93+
94+
data1_dir = os.path.join(project_folder, "test_data_1")
95+
os.makedirs(data1_dir, exist_ok=True)
96+
with open(os.path.join(data1_dir, "test_native.txt"), "w") as f:
97+
f.write("native file content")
98+
99+
data2_dir = os.path.join(project_folder, "test_data_2")
100+
os.makedirs(data2_dir, exist_ok=True)
101+
with open(os.path.join(data2_dir, "test_input.txt"), "w") as f:
102+
f.write("input file content")
103+
72104
response = client.post(route, json={"snapshot": snapshot, "filename": filename})
73105
assert response.status_code == 200
74106
assert response.headers.get("new-file-name") == filename
@@ -77,18 +109,28 @@ def test_export_project_route(client: FlaskClient, tmp_path: Path) -> None:
77109
zip_bytes = response.get_data()
78110
tmp_zip_path = tmp_path / filename
79111
tmp_zip_path.write_bytes(zip_bytes)
112+
80113
with zipfile.ZipFile(tmp_zip_path, "r") as zip_file:
81114
names = zip_file.namelist()
82115
assert "snapshot.json" in names
83116
parsed = json.loads(zip_file.read("snapshot.json").decode("utf-8"))
84117
assert parsed == snapshot
85118
assert "project.db" in names
119+
assert "test_data_1/test_native.txt" in names
120+
assert "test_data_2/test_input.txt" in names
121+
with get_session() as session:
122+
data1_updated = session.query(Data).filter_by(id="test_data_1").first()
123+
assert data1_updated.input_file == "test_native.txt"
124+
125+
data2_updated = session.query(Data).filter_by(id="test_data_2").first()
126+
assert data2_updated.input_file == "test_input.txt"
127+
86128
response.close()
129+
87130
export_path = os.path.join(project_folder, filename)
88131
if os.path.exists(export_path):
89132
os.remove(export_path)
90133

91-
92134
def test_import_project_route(client: FlaskClient, tmp_path: Path) -> None:
93135
route = "/opengeodeweb_back/import_project"
94136
snapshot = {
@@ -137,7 +179,6 @@ def test_import_project_route(client: FlaskClient, tmp_path: Path) -> None:
137179

138180
client.application.config["DATA_FOLDER_PATH"] = original_data_folder
139181

140-
141182
def test_save_viewable_workflow_from_file(client: FlaskClient) -> None:
142183
file = os.path.join(data_dir, "cube.og_brep")
143184
upload_resp = client.put(
@@ -164,7 +205,6 @@ def test_save_viewable_workflow_from_file(client: FlaskClient) -> None:
164205
refreshed = Data.get(data_id)
165206
assert refreshed is not None
166207

167-
168208
def test_save_viewable_workflow_from_object(client: FlaskClient) -> None:
169209
route = "/opengeodeweb_back/create/create_aoi"
170210
aoi_data = {
@@ -184,4 +224,4 @@ def test_save_viewable_workflow_from_object(client: FlaskClient) -> None:
184224
data_id = response.get_json()["id"]
185225
assert isinstance(data_id, str) and len(data_id) > 0
186226
assert response.get_json()["geode_object_type"] == "EdgedCurve3D"
187-
assert response.get_json()["viewable_file"].endswith(".vtp")
227+
assert response.get_json()["viewable_file"].endswith(".vtp")

0 commit comments

Comments
 (0)