Skip to content

Commit

Permalink
feat(study): normalize study path (#2316)
Browse files Browse the repository at this point in the history
  • Loading branch information
smailio authored Feb 3, 2025
1 parent 1263f5a commit b85fe72
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
20 changes: 19 additions & 1 deletion antarest/study/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import typing as t
import uuid
from datetime import datetime, timedelta
from pathlib import Path
from pathlib import Path, PurePath

from antares.study.version import StudyVersion
from pydantic import BeforeValidator, ConfigDict, Field, PlainSerializer, computed_field, field_validator
Expand All @@ -31,6 +31,7 @@
String,
)
from sqlalchemy.orm import relationship # type: ignore
from sqlalchemy.orm import validates
from typing_extensions import override

from antarest.core.exceptions import ShouldNotHappenException
Expand Down Expand Up @@ -288,6 +289,23 @@ def __eq__(self, other: t.Any) -> bool:
def to_json_summary(self) -> t.Any:
return {"id": self.id, "name": self.name}

@validates("folder") # type: ignore
def validate_folder(self, key: str, folder: t.Optional[str]) -> t.Optional[str]:
"""
We want to store the path in posix format in the database, even on windows.
"""
return normalize_path(folder)


def normalize_path(path: t.Optional[str]) -> t.Optional[str]:
"""
Turns any path including a windows path (with \ separator) to a posix path (with / separator).
"""
if not path:
return path
pure_path = PurePath(path)
return pure_path.as_posix()


class RawStudy(Study):
"""
Expand Down
4 changes: 0 additions & 4 deletions tests/storage/business/test_raw_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ def test_get(tmp_path: str, project_path) -> None:
data = {"titi": 43}
sub_route = "settings"

path = path_study / "settings"
key = "titi"

study = Mock()
study.get.return_value = data
study_factory = Mock()
Expand Down Expand Up @@ -100,7 +97,6 @@ def test_get_cache(tmp_path: str) -> None:
path_study.mkdir()
(path_study / "settings").mkdir()
(path_study / "study.antares").touch()
path = path_study / "settings"

data = {"titi": 43}
study = Mock()
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_partial_sync_studies_from_disk() -> None:
id=ANY,
path=f"directory{os.sep}f",
name="f",
folder=f"directory{os.sep}f",
folder="directory/f",
created_at=ANY,
missing=None,
public_mode=PublicMode.FULL,
Expand Down

0 comments on commit b85fe72

Please sign in to comment.