Skip to content

Commit

Permalink
Merge pull request #12 from tobias-watzel/bugfix
Browse files Browse the repository at this point in the history
Bugfix in Path class
  • Loading branch information
ModeSevenIndustrialSolutions authored Aug 12, 2024
2 parents 7ea5edb + 9f8d1e6 commit 65cd031
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion osc_extraction_utils/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def s3_settings() -> S3Settings:
# TODO add test mode paths?
@pytest.fixture(scope="session")
def project_paths(main_settings: MainSettings) -> ProjectPaths:
return ProjectPaths("test_project", main_settings)
return ProjectPaths("test_project", main_settings, Path(__file__).parents[1].resolve())


@pytest.fixture(scope="session")
Expand Down
24 changes: 22 additions & 2 deletions osc_extraction_utils/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class ProjectPaths(BaseSettings):
_PATH_FOLDER_ROOT: Path = Path(__file__).parents[1].resolve()
_PATH_FOLDER_ROOT: Path = Path()
_PATH_FOLDER_NLP: Path = _PATH_FOLDER_ROOT
_PATH_FOLDER_MODEL: Path = _PATH_FOLDER_ROOT / "models"
_PATH_FOLDER_DATA: Path = _PATH_FOLDER_ROOT / "data"
Expand Down Expand Up @@ -44,16 +44,19 @@ class ProjectPaths(BaseSettings):
path_folder_text_3434: Path = Field(default=Path("interim/ml"))
path_folder_relevance: Path = Field(default=Path("output/RELEVANCE/Text"))

def __init__(self, string_project_name: str, main_settings: MainSettings, **kwargs):
def __init__(self, string_project_name: str, main_settings: MainSettings, path_folder_root: Path, **kwargs):
super().__init__(**kwargs)
if not isinstance(string_project_name, str):
raise TypeError
self._string_project_name: str = string_project_name
self._PATH_FOLDER_ROOT = path_folder_root.resolve()
self._path_project_data_folder: Path = self._PATH_FOLDER_DATA / Path(string_project_name)
self._path_project_model_folder: Path = self._PATH_FOLDER_MODEL / Path(string_project_name)
self._main_settings: MainSettings = main_settings
self._update_all_paths_depending_on_path_project_data_folder()
self._update_all_paths_depending_on_path_project_model_folder()
self._update_all_root_related_paths()
self._create_all_root_related_folders()

@property
def string_project_name(self) -> str:
Expand Down Expand Up @@ -89,6 +92,14 @@ def main_settings(self, main_settings_new: MainSettings) -> None:
def PATH_FOLDER_ROOT(self) -> Path:
return self._PATH_FOLDER_ROOT

@PATH_FOLDER_ROOT.setter
def PATH_FOLDER_ROOT(self, path_folder_root_new: Path) -> None:
if not isinstance(path_folder_root_new, Path):
raise TypeError
self._PATH_FOLDER_ROOT = path_folder_root_new
self._update_all_root_related_paths()
self._create_all_root_related_folders()

@property
def PATH_FOLDER_NLP(self) -> Path:
return self._PATH_FOLDER_NLP
Expand Down Expand Up @@ -116,6 +127,15 @@ def _update_all_paths_depending_on_path_project_data_folder(self) -> None:
self, f"{path_field}", self._PATH_FOLDER_DATA / Path(self._string_project_name) / path_field_default
)

def _update_all_root_related_paths(self) -> None:
self._PATH_FOLDER_NLP: Path = self.PATH_FOLDER_ROOT
self._PATH_FOLDER_MODEL: Path = self.PATH_FOLDER_ROOT / "models"
self._PATH_FOLDER_DATA: Path = self.PATH_FOLDER_ROOT / "data"

def _create_all_root_related_folders(self) -> None:
self._PATH_FOLDER_MODEL.mkdir(parents=True, exist_ok=True) # includes root folder
self._PATH_FOLDER_DATA.mkdir(exist_ok=True)

def _update_all_paths_depending_on_path_project_model_folder(self) -> None:
list_string_paths_depending_on_path_project_model_folder: list[str] = [
"path_folder_destination_saved_models_relevance",
Expand Down
39 changes: 36 additions & 3 deletions osc_extraction_utils/tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def path_folder_config_path() -> Path:

@pytest.fixture
def paths_project(main_settings: MainSettings) -> ProjectPaths:
return ProjectPaths(string_project_name="test_project", main_settings=main_settings)
return ProjectPaths(
string_project_name="test_project",
main_settings=main_settings,
path_folder_root=Path(__file__).parents[2].resolve(),
)


def test_root_folder_set(path_folder_config_path: Path, paths_project: ProjectPaths):
Expand Down Expand Up @@ -59,7 +63,9 @@ def test_check_that_all_required_paths_exist_in_project_path_object(main_setting
with patch.object(ProjectPaths, "_update_all_paths_depending_on_path_project_data_folder"), patch.object(
ProjectPaths, "_update_all_paths_depending_on_path_project_model_folder"
):
paths_project: ProjectPaths = ProjectPaths("new_test_project", main_settings)
paths_project: ProjectPaths = ProjectPaths(
"new_test_project", main_settings, Path(__file__).parents[1].resolve()
)

for path_field in paths_project.model_fields.keys():
path_field_attribute: Path = getattr(paths_project, f"{path_field}")
Expand All @@ -71,7 +77,7 @@ def test_project_paths_update_methods_are_called(main_settings: MainSettings):
patch.object(ProjectPaths, "_update_all_paths_depending_on_path_project_data_folder") as mocked_update_data,
patch.object(ProjectPaths, "_update_all_paths_depending_on_path_project_model_folder") as mocked_update_model,
):
ProjectPaths("new_test_project", main_settings)
ProjectPaths("new_test_project", main_settings, Path(__file__).parents[1].resolve())

mocked_update_data.assert_called_once()
mocked_update_model.assert_called_once()
Expand Down Expand Up @@ -110,6 +116,33 @@ def test_set_main_settings(paths_project: ProjectPaths, main_settings: Settings)
assert paths_project.main_settings != main_settings


def test_update_all_root_related_paths(main_settings: MainSettings, path_folder_temporary: Path):
string_test_project: str = "test_project"
path_folder_project_root = path_folder_temporary / "test_project"

project_paths = ProjectPaths(string_test_project, main_settings, path_folder_project_root)

assert project_paths._PATH_FOLDER_ROOT == path_folder_project_root
assert project_paths._PATH_FOLDER_NLP == path_folder_project_root
assert project_paths._PATH_FOLDER_MODEL == path_folder_project_root / "models"
assert project_paths._PATH_FOLDER_DATA == path_folder_project_root / "data"


def test_create_all_root_related_paths(main_settings: MainSettings, path_folder_temporary: Path):
string_test_project: str = "test_project"
path_folder_project_root = path_folder_temporary / "test_project"
list_paths_expected = [
path_folder_project_root,
path_folder_project_root / "models",
path_folder_project_root / "data",
]

ProjectPaths(string_test_project, main_settings, path_folder_project_root)

for path in list_paths_expected:
assert path.exists()


def test_update_all_paths_depending_on_path_project_data_folder(paths_project: ProjectPaths):
string_test_project: str = "test_project"
paths_project.string_project_name = string_test_project
Expand Down

0 comments on commit 65cd031

Please sign in to comment.