Skip to content

Commit

Permalink
feat(move): adapt back-end code and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle authored and skamril committed Dec 16, 2024
1 parent 187e930 commit c7cb378
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
6 changes: 5 additions & 1 deletion antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,15 @@ def copy_task(notifier: ITaskNotifier) -> TaskResult:

return task_or_study_id

def move_study(self, study_id: str, new_folder: str, params: RequestParameters) -> None:
def move_study(self, study_id: str, folder_dest: str, params: RequestParameters) -> None:
study = self.get_study(study_id)
assert_permission(params.user, study, StudyPermissionType.WRITE)
if not is_managed(study):
raise NotAManagedStudyException(study_id)
if folder_dest:
new_folder = folder_dest.rstrip("/") + f"/{study.id}"
else:
new_folder = None
study.folder = new_folder
self.repository.save(study, update_modification_date=False)
self.event_bus.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def create_variant_study(self, uuid: str, name: str, params: RequestParameters)
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
version=study.version,
folder=(re.sub(f"/?{study.id}", "", study.folder) if study.folder is not None else None),
folder=(re.sub(study.id, new_id, study.folder) if study.folder is not None else None),
groups=study.groups, # Create inherit_group boolean
owner_id=params.user.impersonator if params.user else None,
snapshot=None,
Expand Down
51 changes: 51 additions & 0 deletions tests/integration/studies_blueprint/test_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from starlette.testclient import TestClient


class TestMove:
def test_move_endpoint(self, client: TestClient, internal_study_id: str, user_access_token: str) -> None:
client.headers = {"Authorization": f"Bearer {user_access_token}"}

res = client.post("/v1/studies?name=study_test")
assert res.status_code == 201
study_id = res.json()

# asserts move with a given folder adds the /study_id at the end of the path
res = client.put(f"/v1/studies/{study_id}/move", params={"folder_dest": "folder1"})
res.raise_for_status()
res = client.get(f"/v1/studies/{study_id}")
assert res.json()["folder"] == f"folder1/{study_id}"

# asserts move to a folder with //// removes the unwanted `/`
res = client.put(f"/v1/studies/{study_id}/move", params={"folder_dest": "folder2///////"})
res.raise_for_status()
res = client.get(f"/v1/studies/{study_id}")
assert res.json()["folder"] == f"folder2/{study_id}"

# asserts the created variant has the same parent folder
res = client.post(f"/v1/studies/{study_id}/variants?name=Variant1")
variant_id = res.json()
res = client.get(f"/v1/studies/{variant_id}")
assert res.json()["folder"] == f"folder2/{variant_id}"

# asserts move doesn't work on un-managed studies
res = client.put(f"/v1/studies/{internal_study_id}/move", params={"folder_dest": "folder1"})
assert res.status_code == 422
assert res.json()["exception"] == "NotAManagedStudyException"

# asserts users can put back a study at the root folder
res = client.put(f"/v1/studies/{study_id}/move", params={"folder_dest": ""})
res.raise_for_status()
res = client.get(f"/v1/studies/{study_id}")
assert res.json()["folder"] is None
3 changes: 2 additions & 1 deletion tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ def test_main(client: TestClient, admin_access_token: str) -> None:
headers={"Authorization": f'Bearer {george_credentials["access_token"]}'},
)
assert len(res.json()) == 3
assert filter(lambda s: s["id"] == copied.json(), res.json().values()).__next__()["folder"] == "foo/bar"
moved_study = filter(lambda s: s["id"] == copied.json(), res.json().values()).__next__()
assert moved_study["folder"] == f"foo/bar/{moved_study['id']}"

# Study delete
client.delete(
Expand Down

0 comments on commit c7cb378

Please sign in to comment.