@@ -60,7 +60,7 @@ def test_extract_brep_uuids(client, test_id):
6060 assert isinstance (uuid_dict , dict )
6161
6262
63- def test_export_project_route (client ):
63+ def test_export_project_route (client , tmp_path ):
6464 route = "/opengeodeweb_back/export_project"
6565 snapshot = {"styles" : {"1" : {"visibility" : True , "opacity" : 1.0 , "color" : [0.2 , 0.6 , 0.9 ]}}}
6666 filename = "export_project_test.zip"
@@ -69,15 +69,48 @@ def test_export_project_route(client):
6969 assert response .headers .get ("new-file-name" ) == filename
7070 assert response .mimetype == "application/octet-binary"
7171 response .direct_passthrough = False
72- data = response .get_data ()
73- with zipfile .ZipFile (io .BytesIO (data ), "r" ) as zf :
74- names = zf .namelist ()
72+ zip_bytes = response .get_data ()
73+ tmp_zip_path = tmp_path / filename
74+ tmp_zip_path .write_bytes (zip_bytes )
75+ with zipfile .ZipFile (tmp_zip_path , "r" ) as zip_file :
76+ names = zip_file .namelist ()
7577 assert "snapshot.json" in names
76- parsed = json .loads (zf .read ("snapshot.json" ).decode ("utf-8" ))
78+ parsed = json .loads (zip_file .read ("snapshot.json" ).decode ("utf-8" ))
7779 assert parsed == snapshot
7880 assert "1/project.db" in names
7981 response .close ()
8082 upload_folder = client .application .config ["UPLOAD_FOLDER" ]
8183 export_path = os .path .join (upload_folder , filename )
8284 if os .path .exists (export_path ):
8385 os .remove (export_path )
86+
87+
88+ def test_import_project_route (client , tmp_path ):
89+ route = "/opengeodeweb_back/import_project"
90+ snapshot = {"styles" : {"1" : {"visibility" : True , "opacity" : 1.0 , "color" : [0.2 , 0.6 , 0.9 ]}}}
91+
92+ data_folder = client .application .config ["DATA_FOLDER_PATH" ]
93+ pre_existing_db_path = os .path .join (data_folder , "1" , "project.db" )
94+ os .makedirs (os .path .dirname (pre_existing_db_path ), exist_ok = True )
95+ with open (pre_existing_db_path , "wb" ) as file :
96+ file .write (b"old_db_content" )
97+
98+ tmp_zip = tmp_path / "import_project_test.zip"
99+ new_database_bytes = b"new_db_content"
100+ with zipfile .ZipFile (tmp_zip , "w" , compression = zipfile .ZIP_DEFLATED ) as zip_file :
101+ zip_file .writestr ("snapshot.json" , json .dumps (snapshot ))
102+ zip_file .writestr ("1/project.db" , new_database_bytes )
103+
104+ with open (tmp_zip , "rb" ) as file :
105+ response = client .post (
106+ route ,
107+ data = {"file" : (file , "import_project_test.zip" )},
108+ content_type = "multipart/form-data" ,
109+ )
110+
111+ assert response .status_code == 200
112+ assert response .json .get ("snapshot" ) == snapshot
113+
114+ assert os .path .exists (pre_existing_db_path )
115+ with open (pre_existing_db_path , "rb" ) as file :
116+ assert file .read () == new_database_bytes
0 commit comments