Skip to content

Commit 7afe531

Browse files
committed
update tests and import_project
1 parent ef26ea5 commit 7afe531

2 files changed

Lines changed: 46 additions & 32 deletions

File tree

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,28 +369,36 @@ def import_project() -> flask.Response:
369369
if not os.path.isfile(database_root_path):
370370
flask.abort(400, "Missing project.db at project root")
371371

372-
# Reset database to the imported project.db
373372
connection.init_database(database_root_path, create_tables=False)
374373

374+
try:
375+
with get_session() as session:
376+
rows = session.query(Data).all()
377+
except Exception:
378+
connection.init_database(database_root_path, create_tables=True)
379+
with get_session() as session:
380+
rows = session.query(Data).all()
381+
375382
with get_session() as session:
376-
for data_entry in session.query(Data).all():
383+
for data_entry in rows:
377384
data_path = geode_functions.data_file_path(data_entry.id)
378-
379385
viewable_name = data_entry.viewable_file_name
380386
if viewable_name:
381387
vpath = geode_functions.data_file_path(data_entry.id, viewable_name)
382388
if os.path.isfile(vpath):
383389
continue
384-
390+
385391
input_file = str(data_entry.input_file or "")
386392
if not input_file:
387393
continue
388-
394+
389395
input_full = geode_functions.data_file_path(data_entry.id, input_file)
390396
if not os.path.isfile(input_full):
391397
continue
392-
393-
data_object = geode_functions.load(data_entry.geode_object, input_full)
398+
399+
data_object = geode_functions.load(
400+
data_entry.geode_object, input_full
401+
)
394402
utils_functions.save_all_viewables_and_return_info(
395403
data_entry.geode_object, data_object, data_entry, data_path
396404
)

tests/test_models_routes.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,42 @@ def test_export_project_route(client, tmp_path):
9292

9393
def test_import_project_route(client, tmp_path):
9494
route = "/opengeodeweb_back/import_project"
95-
snapshot = {
96-
"styles": {"1": {"visibility": True, "opacity": 1.0, "color": [0.2, 0.6, 0.9]}}
97-
}
98-
99-
client.application.config["DATA_FOLDER_PATH"] = os.path.join(
100-
str(tmp_path), "project_data"
95+
snapshot = {"styles": {"1": {"visibility": True, "opacity": 1.0, "color": [0.2, 0.6, 0.9]}}}
96+
97+
original_data_folder = client.application.config["DATA_FOLDER_PATH"]
98+
client.application.config["DATA_FOLDER_PATH"] = os.path.join(str(tmp_path), "project_data")
99+
db_path = os.path.join(client.application.config["DATA_FOLDER_PATH"], "project.db")
100+
101+
import sqlite3, zipfile, json
102+
temp_db = tmp_path / "temp_project.db"
103+
conn = sqlite3.connect(str(temp_db))
104+
conn.execute(
105+
"CREATE TABLE datas (id TEXT PRIMARY KEY, geode_object TEXT, viewer_object TEXT, native_file_name TEXT, "
106+
"viewable_file_name TEXT, light_viewable TEXT, input_file TEXT, additional_files TEXT)"
101107
)
102-
data_folder = client.application.config["DATA_FOLDER_PATH"]
103-
pre_existing_db_path = os.path.join(data_folder, "project.db")
104-
105-
tmp_zip = tmp_path / "import_project_test.zip"
106-
new_database_bytes = b"new_db_content"
107-
with zipfile.ZipFile(tmp_zip, "w", compression=zipfile.ZIP_DEFLATED) as zip_file:
108-
zip_file.writestr("snapshot.json", json.dumps(snapshot))
109-
zip_file.writestr("project.db", new_database_bytes)
110-
111-
with open(tmp_zip, "rb") as file:
112-
response = client.post(
113-
route,
114-
data={"file": (file, "import_project_test.zip")},
115-
content_type="multipart/form-data",
108+
conn.commit(); conn.close()
109+
110+
z = tmp_path / "import_project_test.zip"
111+
with zipfile.ZipFile(z, "w", compression=zipfile.ZIP_DEFLATED) as zipf:
112+
zipf.writestr("snapshot.json", json.dumps(snapshot))
113+
zipf.write(str(temp_db), "project.db")
114+
115+
with open(z, "rb") as f:
116+
resp = client.post(
117+
route, data={"file": (f, "import_project_test.zip")}, content_type="multipart/form-data"
116118
)
117119

118-
assert response.status_code == 200
119-
assert response.json.get("snapshot") == snapshot
120+
assert resp.status_code == 200
121+
assert resp.json.get("snapshot") == snapshot
122+
assert os.path.exists(db_path)
120123

121-
assert os.path.exists(pre_existing_db_path)
122-
with open(pre_existing_db_path, "rb") as file:
123-
assert file.read() == new_database_bytes
124+
from opengeodeweb_microservice.database import connection
125+
client.application.config["DATA_FOLDER_PATH"] = original_data_folder
126+
test_db_path = os.environ.get("TEST_DB_PATH")
127+
if test_db_path:
128+
connection.init_database(test_db_path, create_tables=True)
124129

130+
client.application.config["DATA_FOLDER_PATH"] = original_data_folder
125131

126132
def test_save_viewable_workflow_from_file(client):
127133
route = "/opengeodeweb_back/save_viewable_file"

0 commit comments

Comments
 (0)