Skip to content

Commit

Permalink
Move logging back to CLI call site
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted committed Jan 10, 2024
1 parent f2f4e2c commit cf13a66
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
5 changes: 5 additions & 0 deletions kpops/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ def is_in_steps(component: PipelineComponent) -> bool:
return component.name in component_names

pipeline.filter(is_in_steps, filter_type)

def get_step_names(steps_to_apply: list[PipelineComponent]) -> list[str]:
return [step.name for step in steps_to_apply]

log.info(f"Filtered pipeline:\n{get_step_names(pipeline.components)}")
if output:
print_yaml(pipeline.to_yaml())
return pipeline
Expand Down
5 changes: 0 additions & 5 deletions kpops/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ def filter(
case (FilterType.INCLUDE, False) | (FilterType.EXCLUDE, True):
self.remove(component)

def get_step_names(steps_to_apply: list[PipelineComponent]) -> list[str]:
return [step.name for step in steps_to_apply]

log.info(f"Filtered pipeline:\n{get_step_names(self.components)}")

def reverse(self) -> None:
self.root.reverse()

Expand Down
16 changes: 14 additions & 2 deletions tests/pipeline/test_generate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pathlib import Path
from unittest.mock import MagicMock

import pytest
import yaml
from pytest_mock import MockerFixture
from snapshottest.module import SnapshotTest
from typer.testing import CliRunner

Expand All @@ -16,6 +18,10 @@

@pytest.mark.usefixtures("mock_env", "load_yaml_file_clear_cache")
class TestGenerate:
@pytest.fixture(autouse=True)
def log_info(self, mocker: MockerFixture) -> MagicMock:
return mocker.patch("kpops.cli.main.log.info")

def test_python_api(self):
pipeline = kpops.generate(
RESOURCE_PATH / "first-pipeline" / "pipeline.yaml",
Expand All @@ -29,7 +35,7 @@ def test_python_api(self):
"filter",
]

def test_python_api_filter_include(self):
def test_python_api_filter_include(self, log_info: MagicMock):
pipeline = kpops.generate(
RESOURCE_PATH / "first-pipeline" / "pipeline.yaml",
defaults=RESOURCE_PATH,
Expand All @@ -39,8 +45,10 @@ def test_python_api_filter_include(self):
)
assert len(pipeline) == 1
assert pipeline.root[0].type == "converter"
assert log_info.call_count == 1
log_info.assert_any_call("Filtered pipeline:\n['converter']")

def test_python_api_filter_exclude(self):
def test_python_api_filter_exclude(self, log_info: MagicMock):
pipeline = kpops.generate(
RESOURCE_PATH / "first-pipeline" / "pipeline.yaml",
defaults=RESOURCE_PATH,
Expand All @@ -50,6 +58,10 @@ def test_python_api_filter_exclude(self):
)
assert len(pipeline) == 1
assert pipeline.root[0].type == "filter"
assert log_info.call_count == 1
log_info.assert_any_call(
"Filtered pipeline:\n['a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name-a-long-name']"
)

def test_load_pipeline(self, snapshot: SnapshotTest):
result = runner.invoke(
Expand Down
15 changes: 2 additions & 13 deletions tests/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from collections.abc import Callable
from dataclasses import dataclass
from typing import cast
from unittest.mock import MagicMock

import pytest
from pytest_mock import MockerFixture

from kpops.cli.options import FilterType
from kpops.components import PipelineComponent
Expand Down Expand Up @@ -45,30 +43,21 @@ def pipeline(self) -> Pipeline:
pipeline.add(cast(PipelineComponent, test_component_3))
return pipeline

@pytest.fixture(autouse=True)
def log_info(self, mocker: MockerFixture) -> MagicMock:
return mocker.patch("kpops.pipeline.log.info")

def test_filter_include(self, log_info: MagicMock, pipeline: Pipeline):
def test_filter_include(self, pipeline: Pipeline):
pipeline.filter(predicate({"example2", "example3"}), FilterType.INCLUDE)
assert len(pipeline.components) == 2
assert test_component_2 in pipeline.components
assert test_component_3 in pipeline.components
assert log_info.call_count == 1
log_info.assert_any_call("Filtered pipeline:\n['example2', 'example3']")

def test_filter_include_empty(self, pipeline: Pipeline):
pipeline.filter(predicate(set()), FilterType.INCLUDE)
assert len(pipeline.components) == 0

def test_filter_exclude(self, log_info: MagicMock, pipeline: Pipeline):
def test_filter_exclude(self, pipeline: Pipeline):
pipeline.filter(predicate({"example2", "example3"}), FilterType.EXCLUDE)
assert len(pipeline.components) == 1
assert test_component_1 in pipeline.components

assert log_info.call_count == 1
log_info.assert_any_call("Filtered pipeline:\n['example1']")

def test_filter_exclude_empty(self, pipeline: Pipeline):
pipeline.filter(predicate(set()), FilterType.EXCLUDE)
assert len(pipeline.components) == 3

0 comments on commit cf13a66

Please sign in to comment.