Skip to content

Commit

Permalink
v1.0.1 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle authored Jul 31, 2024
1 parent 4df7c62 commit 16c9e05
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 34 deletions.
16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ packages = ["src/antares"]

[tool.hatch.envs.default]
dependencies = [
"coverage[toml]>=6.5",
"pytest<8",
"pydantic<2",
"pytest-freezer<0.5",
"coverage[toml]>=7.6.0",
"pytest>=8.2.2",
"pydantic>=2.8.2",
"freezegun~=1.5.1"
]


Expand All @@ -77,10 +77,10 @@ python = ["3.8", "3.9", "3.10", "3.11", "3.12"]

[tool.hatch.envs.types]
dependencies = [
"pytest<8",
"pydantic<2",
"pytest-freezer<0.5",
"mypy>=1.0.0",
"pytest>=8.2.2",
"pydantic>=2.8.2",
"mypy>=1.10.1",
"freezegun~=1.5.1"
]


Expand Down
7 changes: 5 additions & 2 deletions src/antares/study/version/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
- antares-study-version create: create a new study.
"""

from pathlib import Path

import click

from antares.study.version import StudyVersion
from antares.study.version.__about__ import __date__, __version__
from antares.study.version.create_app import CreateApp, available_versions
from antares.study.version.exceptions import ApplicationError
Expand Down Expand Up @@ -38,7 +41,7 @@ def show(study_dir: str) -> None:
STUDY_DIR: The directory containing the study.
"""
try:
app = ShowApp(study_dir) # type: ignore
app = ShowApp(Path(study_dir))
except (ValueError, FileNotFoundError) as e:
click.echo(f"Error: {e}", err=True)
raise click.Abort()
Expand Down Expand Up @@ -102,7 +105,7 @@ def create(study_dir: str, caption: str, version: str, author: str) -> None:
STUDY_DIR: The directory where the study will be created.
"""
try:
app = CreateApp(study_dir, caption=caption, version=version, author=author) # type: ignore
app = CreateApp(Path(study_dir), caption=caption, version=StudyVersion.parse(version), author=author)
except (ValueError, FileExistsError) as e:
click.echo(f"Error: {e}", err=True)
raise click.Abort()
Expand Down
13 changes: 7 additions & 6 deletions src/antares/study/version/model/study_antares.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ def __post_init__(self) -> None:

@classmethod
def _validate_caption(cls, value: t.Any) -> str:
value = str(value).strip()
if value:
return value
raise ValueError("Caption cannot be empty")
return str(value).strip()

@classmethod
def _validate_version(cls, value: t.Any) -> StudyVersion:
Expand All @@ -108,7 +105,11 @@ def _validate_date(cls, value: t.Any) -> datetime.datetime:
if isinstance(value, datetime.datetime):
return value
if isinstance(value, str):
value = float(value)
try:
value = float(value)
except ValueError:
return datetime.datetime.utcnow()

return datetime.datetime.utcfromtimestamp(value)

_validate_created_date = _validate_date
Expand Down Expand Up @@ -146,7 +147,7 @@ def from_ini_file(cls, study_dir: t.Union[str, Path]) -> "StudyAntares":
section = parser["antares"]
return cls(
caption=section["caption"],
version=section["version"], # type: ignore
version=StudyVersion.parse(section["version"]),
created_date=section["created"], # type: ignore
last_save_date=section["lastsave"], # type: ignore
author=section["author"],
Expand Down
3 changes: 3 additions & 0 deletions src/antares/study/version/model/study_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def __lt__(self, other):
else:
return NotImplemented

def __le__(self, other):
return self.__lt__(other) or self.__eq__(other)

# Format method

def __format__(self, format_spec: str) -> str:
Expand Down
4 changes: 2 additions & 2 deletions src/antares/study/version/upgrade_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def study_antares(self) -> StudyAntares:
try:
return StudyAntares.from_ini_file(self.study_dir)
except ValidationError as e:
raise ApplicationError(str(e)) from e
raise ApplicationError(e.args[0]) from e

@property
def upgrade_methods(self) -> t.List[UpgradeMethod]:
Expand All @@ -83,7 +83,7 @@ def upgrade_methods(self) -> t.List[UpgradeMethod]:
try:
return scenarios[start:end] # type: ignore
except KeyError as e:
raise ApplicationError(str(e)) from e
raise ApplicationError(e.args[0]) from e

@property
def should_denormalize(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/antares/study/version/upgrade_app/scenario_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def _iter_upgrade_methods(
# Then, find the next upgrade methods until the end version
curr = meth.new
for meth in iter_methods:
if meth.can_upgrade(curr) and (end is None or meth.new <= end): # type: ignore
if meth.can_upgrade(curr) and (end is None or meth.new <= end):
yield meth
curr = meth.new
else:
Expand Down
2 changes: 1 addition & 1 deletion src/antares/study/version/upgrade_app/upgrade_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __post_init__(self):
self.new = StudyVersion.parse(self.new)

def can_upgrade(self, version: StudyVersion) -> bool:
return self.old <= version < self.new # type: ignore
return self.old <= version < self.new

@classmethod
def upgrade(cls, study_dir: Path) -> None:
Expand Down
10 changes: 6 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_cli__version(self) -> None:

def test_cli__show(self, study_dir: Path) -> None:
runner = CliRunner()
result = runner.invoke(cli, ["show", str(study_dir)])
result = runner.invoke(t.cast(click.BaseCommand, cli), ["show", str(study_dir)])
assert result.exit_code == 0
show_str = result.output.strip()
assert "Caption: Thermal fleet optimization" in show_str
Expand All @@ -45,7 +45,7 @@ def test_cli__create(self, tmp_path: Path, study_version: StudyVersion) -> None:
study_dir = tmp_path / "My Study"
runner = CliRunner()
args = ["create", str(study_dir), f"--version={study_version:2d}", "--author=Jane Doe", "--caption=New Study"]
result = runner.invoke(cli, args)
result = runner.invoke(t.cast(click.BaseCommand, cli), args)
assert result.exit_code == 0, result.output

study_antares_file = study_dir / "study.antares"
Expand All @@ -69,7 +69,7 @@ def test_cli__create(self, tmp_path: Path, study_version: StudyVersion) -> None:
def test_cli__create__versions(self) -> None:
runner = CliRunner()
args = ["create", "--versions"]
result = runner.invoke(cli, args)
result = runner.invoke(t.cast(click.BaseCommand, cli), args)
assert result.exit_code == 0

show_str = result.output.strip()
Expand All @@ -78,7 +78,9 @@ def test_cli__create__versions(self) -> None:
def test_upgrade__nominal_case(self, study_assets: StudyAssets) -> None:
runner = CliRunner()
target_version = "8.8"
result = runner.invoke(cli, ["upgrade", str(study_assets.study_dir), f"--version={target_version}"])
result = runner.invoke(
t.cast(click.BaseCommand, cli), ["upgrade", str(study_assets.study_dir), f"--version={target_version}"]
)
assert result.exit_code == 0

# compare the content of the input directory
Expand Down
10 changes: 5 additions & 5 deletions tests/test_using_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import uuid
from unittest import mock

import freezegun # type: ignore
from pydantic import BaseModel, Field, validator
import freezegun
from pydantic import BaseModel, Field, field_validator

from antares.study.version import StudyVersion

Expand All @@ -16,7 +16,7 @@ class StudyDTO(BaseModel):
version: StudyVersion
created: datetime.datetime = Field(default_factory=lambda: datetime.datetime.now(tz=datetime.timezone.utc))

@validator("version", pre=True)
@field_validator("version", mode="before")
def _validate_version(cls, v: t.Any) -> StudyVersion:
return StudyVersion.parse(v)

Expand All @@ -43,11 +43,11 @@ def test_init_with_string(self) -> None:
def test_to_json(self) -> None:
with mock.patch("uuid.uuid4", return_value=uuid.UUID("4930a577-63d2-4ea9-b0b9-581110d97475")):
study = StudyDTO(name="foo", version=StudyVersion(4, 5))
result = study.json()
result = study.model_dump_json()
assert isinstance(result, str)
# Compare dicts, since the order of the keys is not guaranteed
assert json.loads(result) == {
"created": "2024-12-31T12:30:00+00:00",
"created": "2024-12-31T12:30:00Z",
"id": "4930a577-63d2-4ea9-b0b9-581110d97475",
"name": "foo",
"version": {"major": 4, "minor": 5, "patch": 0},
Expand Down
11 changes: 6 additions & 5 deletions tests/upgrade_app/test_filter_out_child_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing as t
from pathlib import Path

import pytest

Expand All @@ -14,16 +15,16 @@
["document.txt"],
),
(
["input", "input/other", "input/other/other"],
["input", str(Path("input").joinpath("other")), str(Path("input") / "other" / "other")],
["input"],
),
(
["input/other1.txt", "input/other2.txt"],
["input/other1.txt", "input/other2.txt"],
[str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt")],
[str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt")],
),
(
["input/other1.txt", "input/other2.txt", "other3.txt"],
["input/other1.txt", "input/other2.txt", "other3.txt"],
[str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt"), str(Path("input") / "other3.txt")],
[str(Path("input") / "other1.txt"), str(Path("input") / "other2.txt"), str(Path("input") / "other3.txt")],
),
],
)
Expand Down

0 comments on commit 16c9e05

Please sign in to comment.