Skip to content

Commit d13008c

Browse files
committed
test
1 parent bcd349f commit d13008c

File tree

6 files changed

+144
-3
lines changed

6 files changed

+144
-3
lines changed

src/opengeodeweb_back/geode_functions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,14 @@ def create_coordinate_system(
291291
create_crs(
292292
geode_object, data, name, input_coordiante_system, output_coordiante_system
293293
)
294+
295+
296+
def file_exists_in_upload(filename: str) -> bool:
297+
"""Vérifie si un fichier existe dans le dossier d'upload."""
298+
file_path = upload_file_path(filename)
299+
return os.path.exists(file_path)
300+
301+
def file_exists_in_data(data_id: str, filename: str) -> bool:
302+
"""Vérifie si un fichier existe dans le dossier de données."""
303+
file_path = data_file_path(data_id, filename)
304+
return os.path.exists(file_path)

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,31 @@ def upload_file():
7676
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
7777
if not os.path.exists(UPLOAD_FOLDER):
7878
os.mkdir(UPLOAD_FOLDER)
79+
7980
file = flask.request.files["file"]
8081
filename = werkzeug.utils.secure_filename(os.path.basename(file.filename))
81-
file.save(os.path.join(UPLOAD_FOLDER, filename))
82-
return flask.make_response({"message": "File uploaded"}, 201)
82+
file_path = os.path.join(UPLOAD_FOLDER, filename)
83+
84+
# Vérifier si le fichier existe déjà
85+
file_existed = os.path.exists(file_path)
86+
if file_existed:
87+
replace_if_exists = flask.request.form.get('replace_if_exists', 'false').lower() == 'true'
88+
if not replace_if_exists:
89+
return flask.make_response({
90+
"error": "File already exists",
91+
"message": f"Le fichier '{filename}' existe déjà. Utilisez 'replace_if_exists=true' pour le remplacer.",
92+
"existing_file": filename
93+
}, 409) # 409 Conflict
94+
95+
file.save(file_path)
96+
97+
response_data = {
98+
"message": "File uploaded",
99+
"filename": filename,
100+
"replaced": file_existed
101+
}
102+
103+
return flask.make_response(response_data, 201)
83104

84105

85106
with open(

src/opengeodeweb_back/routes/schemas/upload_file.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
"filename": {
1010
"type": "string",
1111
"minLength": 1
12+
},
13+
"replace_if_exists": {
14+
"type": "boolean",
15+
"default": false
1216
}
1317
},
18+
"required": ["filename"],
1419
"additionalProperties": false
1520
}

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313

1414
@pytest.fixture(scope="session", autouse=True)
1515
def copy_data():
16+
# Nettoyer les dossiers de test
1617
shutil.rmtree("./data", ignore_errors=True)
18+
shutil.rmtree("./uploads", ignore_errors=True)
19+
20+
# Copier les données de test
1721
shutil.copytree("./tests/data/", f"./data/{TEST_ID}/", dirs_exist_ok=True)
22+
23+
# Créer le dossier uploads pour les tests
24+
os.makedirs("./uploads", exist_ok=True)
1825

1926

2027
@pytest.fixture

tests/test_geode_functions.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,41 @@ def test_geode_objects_output_extensions():
327327
output_extension_value,
328328
) in output_geode_object_value.items():
329329
assert type(output_extension_value["is_saveable"]) is bool
330+
331+
332+
def test_file_exists_in_upload():
333+
"""Test de la fonction file_exists_in_upload."""
334+
# Test avec un fichier qui n'existe pas
335+
assert geode_functions.file_exists_in_upload("nonexistent.txt") == False
336+
337+
# Test avec un fichier qui existe (après l'avoir créé)
338+
test_filename = "test_exists.txt"
339+
test_path = geode_functions.upload_file_path(test_filename)
340+
os.makedirs(os.path.dirname(test_path), exist_ok=True)
341+
with open(test_path, 'w') as f:
342+
f.write("test")
343+
344+
assert geode_functions.file_exists_in_upload(test_filename) == True
345+
346+
# Nettoyer
347+
os.remove(test_path)
348+
349+
def test_file_exists_in_data():
350+
"""Test de la fonction file_exists_in_data."""
351+
test_data_id = "test_data_id"
352+
test_filename = "test_data.txt"
353+
354+
# Test avec un fichier qui n'existe pas
355+
assert geode_functions.file_exists_in_data(test_data_id, test_filename) == False
356+
357+
# Test avec un fichier qui existe (après l'avoir créé)
358+
test_path = geode_functions.data_file_path(test_data_id, test_filename)
359+
os.makedirs(os.path.dirname(test_path), exist_ok=True)
360+
with open(test_path, 'w') as f:
361+
f.write("test")
362+
363+
assert geode_functions.file_exists_in_data(test_data_id, test_filename) == True
364+
365+
# Nettoyer
366+
os.remove(test_path)
367+
os.rmdir(os.path.dirname(test_path))

tests/test_routes.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,70 @@ def get_full_data():
4646

4747

4848
def test_upload_file(client, filename="corbi.og_brep"):
49+
# Test upload normal
4950
response = client.put(
5051
f"/upload_file",
51-
data={"file": FileStorage(open(f"./tests/{filename}", "rb"))},
52+
data={"file": FileStorage(open(f"./tests/data/{filename}", "rb"))},
5253
)
54+
assert response.status_code == 201
55+
assert response.json["message"] == "File uploaded"
56+
assert response.json["filename"] == filename
57+
assert response.json["replaced"] == False
58+
59+
# Test upload du même fichier sans remplacement (doit échouer)
60+
response = client.put(
61+
f"/upload_file",
62+
data={
63+
"file": FileStorage(open(f"./tests/data/{filename}", "rb")),
64+
"replace_if_exists": "false"
65+
},
66+
)
67+
assert response.status_code == 409
68+
assert "File already exists" in response.json["error"]
69+
70+
# Test upload du même fichier avec remplacement (doit réussir)
71+
response = client.put(
72+
f"/upload_file",
73+
data={
74+
"file": FileStorage(open(f"./tests/data/{filename}", "rb")),
75+
"replace_if_exists": "true"
76+
},
77+
)
78+
assert response.status_code == 201
79+
assert response.json["message"] == "File uploaded"
80+
assert response.json["filename"] == filename
81+
assert response.json["replaced"] == True
5382

83+
84+
def test_upload_file_new_parameter_validation(client):
85+
"""Test que le paramètre replace_if_exists est optionnel."""
86+
route = "/upload_file"
87+
88+
# Test avec replace_if_exists = true
89+
response = client.put(
90+
route,
91+
data={
92+
"file": FileStorage(open("./tests/data/corbi.og_brep", "rb")),
93+
"replace_if_exists": "true"
94+
},
95+
)
96+
assert response.status_code == 201
97+
98+
# Test avec replace_if_exists = false
99+
response = client.put(
100+
route,
101+
data={
102+
"file": FileStorage(open("./tests/data/test.og_brep", "rb")),
103+
"replace_if_exists": "false"
104+
},
105+
)
106+
assert response.status_code == 201
107+
108+
# Test sans replace_if_exists (doit utiliser la valeur par défaut false)
109+
response = client.put(
110+
route,
111+
data={"file": FileStorage(open("./tests/data/cube.og_brep", "rb"))},
112+
)
54113
assert response.status_code == 201
55114

56115

0 commit comments

Comments
 (0)