Skip to content

Commit

Permalink
Updated test names, docstrings. Updated cosmos.config parsed fields t…
Browse files Browse the repository at this point in the history
…o properties. Fixed spelling
  • Loading branch information
tabmra committed Oct 11, 2023
1 parent 4be04e0 commit 56aff39
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
18 changes: 10 additions & 8 deletions cosmos/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
51 changes: 32 additions & 19 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -20,59 +19,73 @@ 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
print(err_info.value.args[0])
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
Expand Down

0 comments on commit 56aff39

Please sign in to comment.