Skip to content

Commit

Permalink
style(archive-output): improved typing in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-laporte-pro committed Jun 5, 2024
1 parent aa2acb5 commit df81b17
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions tests/storage/business/test_raw_study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import platform
import re
import time
import typing as t
from pathlib import Path
from typing import Callable
from unittest.mock import Mock

import py7zr
Expand All @@ -23,7 +23,7 @@ def build_config(
study_path: Path,
workspace_name: str = DEFAULT_WORKSPACE_NAME,
allow_deletion: bool = False,
):
) -> Config:
return Config(
storage=StorageConfig(
workspaces={workspace_name: WorkspaceConfig(path=study_path)},
Expand All @@ -33,7 +33,7 @@ def build_config(


@pytest.mark.unit_test
def test_get(tmp_path: str, project_path) -> None:
def test_get(tmp_path: str, project_path: Path) -> None:
"""
path_to_studies
|_study1 (d)
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_get_cache(tmp_path: str) -> None:
config=Mock(),
cache=cache,
study_factory=study_factory,
path_resources="",
path_resources=Path(),
patch_service=Mock(),
)

Expand All @@ -120,7 +120,7 @@ def test_get_cache(tmp_path: str) -> None:


@pytest.mark.unit_test
def test_check_errors():
def test_check_errors() -> None:
study = Mock()
study.check_errors.return_value = ["Hello"]

Expand All @@ -144,7 +144,7 @@ def test_check_errors():


@pytest.mark.unit_test
def test_assert_study_exist(tmp_path: str, project_path) -> None:
def test_assert_study_exist(tmp_path: str, project_path: Path) -> None:
tmp = Path(tmp_path)
(tmp / "study1").mkdir()
(tmp / "study.antares").touch()
Expand All @@ -170,7 +170,7 @@ def test_assert_study_exist(tmp_path: str, project_path) -> None:


@pytest.mark.unit_test
def test_assert_study_not_exist(tmp_path: str, project_path) -> None:
def test_assert_study_not_exist(tmp_path: str, project_path: Path) -> None:
# Create folders
tmp = Path(tmp_path)
(tmp / "study1").mkdir()
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_create(tmp_path: Path, project_path: Path) -> None:


@pytest.mark.unit_test
def test_create_study_versions(tmp_path: str, project_path) -> None:
def test_create_study_versions(tmp_path: str, project_path: Path) -> None:
path_studies = Path(tmp_path)

study = Mock()
Expand All @@ -252,7 +252,7 @@ def test_create_study_versions(tmp_path: str, project_path) -> None:
patch_service=Mock(),
)

def create_study(version: str):
def create_study(version: str) -> RawStudy:
metadata = RawStudy(
id=f"study{version}",
workspace=DEFAULT_WORKSPACE_NAME,
Expand Down Expand Up @@ -398,7 +398,7 @@ def create_study(version: str):
@pytest.mark.unit_test
def test_copy_study(
tmp_path: str,
clean_ini_writer: Callable,
clean_ini_writer: t.Callable[[Path, str], None],
) -> None:
path_studies = Path(tmp_path)
source_name = "study1"
Expand All @@ -420,8 +420,7 @@ def test_copy_study(
study.get.return_value = value
study_factory = Mock()

config = Mock()
study_factory.create_from_fs.return_value = FileStudy(config, study)
study_factory.create_from_fs.return_value = FileStudy(Mock(spec=Config), study)
study_factory.create_from_config.return_value = study

url_engine = Mock()
Expand Down Expand Up @@ -451,6 +450,17 @@ def test_copy_study(
study.get.assert_called_once_with(["study"])


GENERAL_SECTION = """\
[general]
version = 700
name = 11mc
mode = Economy
date = 2020.09.07 - 16:15
title = 2020.09.07 - 16:15
timestamp = 1599488150
"""


@pytest.mark.unit_test
def test_archived_output(tmp_path: Path) -> None:
if not platform.platform().startswith("Windows"):
Expand All @@ -474,17 +484,7 @@ def test_archived_output(tmp_path: Path) -> None:

archived_output = tmp_path / "output.7z"
with py7zr.SevenZipFile(archived_output, "w") as output_data:
output_data.writestr(
"""[general]
version = 700
name = 11mc
mode = Economy
date = 2020.09.07 - 16:15
title = 2020.09.07 - 16:15
timestamp = 1599488150
""",
"info.antares-output",
)
output_data.writestr(GENERAL_SECTION, "info.antares-output")

expected_output_name = "20200907-1615eco-11mc"
output_name = study_service.import_output(md, archived_output)
Expand All @@ -501,6 +501,7 @@ def test_archived_output(tmp_path: Path) -> None:
assert not (study_path / "output" / expected_output_name).exists()

output_name = study_service.import_output(md, archived_output)
assert output_name is not None
study_service.unarchive_study_output(md, expected_output_name, True)
assert (study_path / "output" / (expected_output_name + ".7z")).exists()
os.unlink(study_path / "output" / (expected_output_name + ".7z"))
Expand Down Expand Up @@ -577,7 +578,7 @@ def test_initialize_additional_data(tmp_path: Path) -> None:

assert not study_service.initialize_additional_data(raw_study)

study_service._read_additional_data_from_files = Mock(return_value=study_additional_data)
study_service._read_additional_data_from_files = Mock(return_value=study_additional_data) # type: ignore
assert study_service.initialize_additional_data(raw_study)


Expand Down

0 comments on commit df81b17

Please sign in to comment.