From 56aff394148c3c6d0ccba04a0b77cbd43cbe924d Mon Sep 17 00:00:00 2001 From: tabmra Date: Wed, 11 Oct 2023 10:31:44 +0800 Subject: [PATCH] Updated test names, docstrings. Updated cosmos.config parsed fields to properties. Fixed spelling --- cosmos/config.py | 18 +++++++++------- tests/test_config.py | 51 +++++++++++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/cosmos/config.py b/cosmos/config.py index c5fe9f7d7..cc2255bc8 100644 --- a/cosmos/config.py +++ b/cosmos/config.py @@ -5,9 +5,11 @@ import contextlib import tempfile from dataclasses import dataclass, field +from functools import cached_property from pathlib import Path from typing import Iterator + from cosmos.constants import TestBehavior, ExecutionMode, LoadMode from cosmos.dbt.executable import get_system_dbt from cosmos.exceptions import CosmosValueError @@ -61,25 +63,25 @@ class ProjectConfig: seeds_relative_path: str | Path = "seeds" snapshots_relative_path: str | Path = "snapshots" manifest_path: str | Path | None = None + project_name: str | None = None - parsed_dbt_project_path: Path | None = None - parsed_manifest_path: Path | None = None + @cached_property + def parsed_dbt_project_path(self) -> Path | None: + return Path(self.dbt_project_path) if self.dbt_project_path else None - project_name: str | None = None + @cached_property + def parsed_manifest_path(self) -> Path | None: + return Path(self.manifest_path) if self.manifest_path else None def __post_init__(self) -> None: "Converts paths to `Path` objects." - if self.dbt_project_path: - self.parsed_dbt_project_path = Path(self.dbt_project_path) + if self.parsed_dbt_project_path: self.models_relative_path = self.parsed_dbt_project_path / Path(self.models_relative_path) self.seeds_relative_path = self.parsed_dbt_project_path / Path(self.seeds_relative_path) self.snapshots_relative_path = self.parsed_dbt_project_path / Path(self.snapshots_relative_path) if not self.project_name: self.project_name = self.parsed_dbt_project_path.stem - if self.manifest_path: - self.parsed_manifest_path = Path(self.manifest_path) - def validate_project(self) -> None: """ Validates necessary context is present for a project. diff --git a/tests/test_config.py b/tests/test_config.py index ad1c8c531..9dd936fba 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -10,7 +10,6 @@ PIPELINE_FOLDER = "jaffle_shop" -# Tests that a ProjectConfig object can be created with valid parameters def test_valid_parameters(): project_config = ProjectConfig(dbt_project_path="path/to/dbt/project") assert project_config.parsed_dbt_project_path == Path("path/to/dbt/project") @@ -20,37 +19,44 @@ def test_valid_parameters(): assert project_config.manifest_path is None -# Since dbt_project_path is now an optional parameter, we should test each combination for init and validation - - -# Passing a manifest AND project together should succeed, as previous -def test_init_with_manifest_and_project(): +def test_init_with_manifest_path_and_project_path_succeeds(): + """ + Passing a manifest path AND project path together should succeed, as previous + """ project_config = ProjectConfig(dbt_project_path="/tmp/some-path", manifest_path="target/manifest.json") assert project_config.parsed_manifest_path == Path("target/manifest.json") -# Since dbt_project_path is optional, we should be able to operate with only a manifest -def test_init_with_manifest_and_not_project(): +def test_init_with_manifest_path_and_not_project_path_succeeds(): + """ + Since dbt_project_path is optional, we should be able to operate with only a manifest + """ project_config = ProjectConfig(manifest_path="target/manifest.json") assert project_config.parsed_manifest_path == Path("target/manifest.json") -# supplying both project and manifest paths as previous should be permitted -def test_validate_project_success_project_and_manifest(): +def test_validate_with_project_path_and_manifest_path_succeeds(): + """ + Supplying both project and manifest paths as previous should be permitted + """ project_config = ProjectConfig( dbt_project_path=DBT_PROJECTS_ROOT_DIR, manifest_path=DBT_PROJECTS_ROOT_DIR / "manifest.json" ) assert project_config.validate_project() is None -# with updated logic, passing a project alone should be permitted -def test_validate_project_success_project_and_not_manifest(): +def test_validate_with_project_path_and_not_manifest_path_succeeds(): + """ + Passing a project with no manifest should be permitted + """ project_config = ProjectConfig(dbt_project_path=DBT_PROJECTS_ROOT_DIR) assert project_config.validate_project() is None -# with updated logic, passing a manifest alone should fail since we also require a project_name -def test_validate_project_failure_not_project_and_manifest(): +def test_validate_with_manifest_path_and_not_project_path_and_not_project_name_fails(): + """ + Passing a manifest alone should fail since we also require a project_name + """ project_config = ProjectConfig(manifest_path=DBT_PROJECTS_ROOT_DIR / "manifest.json") with pytest.raises(CosmosValueError) as err_info: assert project_config.validate_project() is None @@ -58,21 +64,28 @@ def test_validate_project_failure_not_project_and_manifest(): assert err_info.value.args[0] == "project_name required when manifest_path is present and dbt_project_path is not." -# with updated logic, passing a manifest and project name together should succeed. -def test_validate_project_success_not_project_and_manifest(): +def test_validate_with_manifest_path_and_project_name_and_not_project_path_succeeds(): + """ + Passing a manifest and project name together should succeed. + """ project_config = ProjectConfig(manifest_path=DBT_PROJECTS_ROOT_DIR / "manifest.json", project_name="test-project") assert project_config.validate_project() is None -# with updated logic, passing no manifest and no project directory should fail. -def test_validate_project_fail_none(): +def test_validate_no_paths_fails(): + """ + Passing no manifest and no project directory should fail. + """ project_config = ProjectConfig() with pytest.raises(CosmosValueError) as err_info: assert project_config.validate_project() is None assert err_info.value.args[0] == "dbt_project_path or manifest_path are required parameters." -def test_validate_project_fails(): +def test_validate_project_missing_fails(): + """ + Passing a project dir that does not exist where specified should fail + """ project_config = ProjectConfig(dbt_project_path=Path("/tmp")) with pytest.raises(CosmosValueError) as err_info: assert project_config.validate_project() is None