From e0fd6b1b758504efc862fa05ad6509905f7cb83f Mon Sep 17 00:00:00 2001 From: belthlemar Date: Mon, 23 Sep 2024 11:44:30 +0200 Subject: [PATCH] fix(cache): update cache after deleting file --- antarest/study/service.py | 7 ++++++- .../test_fetch_raw_data.py | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/antarest/study/service.py b/antarest/study/service.py index 662f20670c..25f73e7469 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -48,7 +48,7 @@ ) from antarest.core.filetransfer.model import FileDownloadTaskDTO from antarest.core.filetransfer.service import FileTransferManager -from antarest.core.interfaces.cache import ICache +from antarest.core.interfaces.cache import CacheConstants, ICache from antarest.core.interfaces.eventbus import Event, EventType, IEventBus from antarest.core.jwt import DEFAULT_ADMIN_USER, JWTGroup, JWTUser from antarest.core.model import JSON, SUB_JSON, PermissionInfo, PublicMode, StudyPermissionType @@ -2670,3 +2670,8 @@ def delete_file_or_folder(self, study_id: str, path: str, current_user: JWTUser) study_tree["user"].delete(url[1:]) except ChildNotFoundError as e: raise FileDeletionNotAllowed(f"the given path doesn't exist: {e.detail}") + + # update cache + cache_id = f"{CacheConstants.RAW_STUDY}/{study.id}" + updated_tree = self.storage_service.raw_study_service.get_raw(study, False).tree.get() + self.storage_service.get_storage(study).cache.put(cache_id, updated_tree) # type: ignore diff --git a/tests/integration/raw_studies_blueprint/test_fetch_raw_data.py b/tests/integration/raw_studies_blueprint/test_fetch_raw_data.py index 8b294cadc5..b36b33b720 100644 --- a/tests/integration/raw_studies_blueprint/test_fetch_raw_data.py +++ b/tests/integration/raw_studies_blueprint/test_fetch_raw_data.py @@ -276,7 +276,8 @@ def test_delete_raw(client: TestClient, user_access_token: str, internal_study_i content = io.BytesIO(b"This is the end!") file_1_path = "user/file_1.txt" file_2_path = "user/folder/file_2.txt" - for f in [file_1_path, file_2_path]: + file_3_path = "user/folder_2/file_3.txt" + for f in [file_1_path, file_2_path, file_3_path]: # Creates a file / folder inside user folder. res = client.put( f"/v1/studies/{internal_study_id}/raw", params={"path": f, "create_missing": True}, files={"file": content} @@ -284,6 +285,8 @@ def test_delete_raw(client: TestClient, user_access_token: str, internal_study_i assert res.status_code == 204, res.json() # Deletes the file / folder + if f == file_2_path: + f = "user/folder" res = client.delete(f"/v1/studies/{internal_study_id}/raw?path={f}") assert res.status_code == 200 # Asserts it doesn't exist anymore @@ -291,6 +294,18 @@ def test_delete_raw(client: TestClient, user_access_token: str, internal_study_i assert res.status_code == 404 assert "not a child of" in res.json()["description"] + # checks debug view + res = client.get(f"/v1/studies/{internal_study_id}/raw?path=&depth=-1") + assert res.status_code == 200 + tree = res.json()["user"] + if f == file_3_path: + # asserts the folder that wasn't deleted is still here. + assert list(tree.keys()) == ["expansion", "folder_2"] + assert tree["folder_2"] == {} + else: + # asserts deleted files cannot be seen inside the debug view + assert list(tree.keys()) == ["expansion"] + # ============================= # ERRORS # =============================