refactor: simplify CLI command logic by extracting helper functions #13
+17
-7
@@ -1,28 +1,33 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from typer.testing import CliRunner
|
from typer.testing import CliRunner
|
||||||
from unittest.mock import patch, MagicMock
|
|
||||||
from pathlib import Path
|
|
||||||
from photools3.cli import app
|
from photools3.cli import app
|
||||||
|
|
||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_photo_logic(tmp_path):
|
def mock_photo_logic(tmp_path):
|
||||||
"""Fixture to mock the core logic of keywords command."""
|
"""Fixture to mock the core logic of keywords command."""
|
||||||
with patch("photools3.cli.LlmSessionCaptionsAndKeywords") as mocked_session, \
|
with (
|
||||||
patch("photools3.cli.Photo") as mocked_photo:
|
patch("photools3.cli.LlmSessionCaptionsAndKeywords") as mocked_session,
|
||||||
|
patch("photools3.cli.Photo") as mocked_photo,
|
||||||
|
):
|
||||||
|
|
||||||
# Configure session mock
|
# Configure session mock
|
||||||
session_instance = mocked_session.return_value
|
session_instance = mocked_session.return_value
|
||||||
session_instance.call.return_value = {
|
session_instance.call.return_value = {
|
||||||
"caption": "A beautiful test image",
|
"caption": "A beautiful test image",
|
||||||
"args": ["test", "mock", "image"] # Wait, the code uses 'tags'
|
"args": ["test", "mock", "image"], # Wait, the code uses 'tags'
|
||||||
}
|
}
|
||||||
# Let me re-check the code in cli.py
|
# Let me re-check the code in cli.py
|
||||||
# result.get("tags", [])
|
# result.get("tags", [])
|
||||||
session_instance.call.return_value = {
|
session_instance.call.return_value = {
|
||||||
"caption": "A beautiful test image",
|
"caption": "A beautiful test image",
|
||||||
"tags": ["test", "mock", "image"]
|
"tags": ["test", "mock", "image"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Configure photo mock
|
# Configure photo mock
|
||||||
@@ -32,9 +37,10 @@ def mock_photo_logic(tmp_path):
|
|||||||
yield {
|
yield {
|
||||||
"session": session_instance,
|
"session": session_instance,
|
||||||
"photo_class": mocked_photo,
|
"photo_class": mocked_photo,
|
||||||
"photo_instance": photo_instance
|
"photo_instance": photo_instance,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def dummy_file(tmp_path):
|
def dummy_file(tmp_path):
|
||||||
"""Fixture to create a dummy file for CLI testing."""
|
"""Fixture to create a dummy file for CLI testing."""
|
||||||
@@ -42,6 +48,7 @@ def dummy_file(tmp_path):
|
|||||||
dummy_path.write_text("dummy content")
|
dummy_path.write_text("dummy content")
|
||||||
return dummy_path
|
return dummy_path
|
||||||
|
|
||||||
|
|
||||||
def test_keywords_command_success(mock_photo_logic, dummy_file):
|
def test_keywords_command_success(mock_photo_logic, dummy_file):
|
||||||
"""Test the 'keywords' command with a valid file path."""
|
"""Test the 'keywords' command with a valid file path."""
|
||||||
# Ensure the mock photo instance points to our dummy file
|
# Ensure the mock photo instance points to our dummy file
|
||||||
@@ -54,6 +61,7 @@ def test_keywords_command_success(mock_photo_logic, dummy_file):
|
|||||||
assert "Caption: A beautiful test image" in result.stdout
|
assert "Caption: A beautiful test image" in result.stdout
|
||||||
assert "Tags: test, mock, image" in result.stdout
|
assert "Tags: test, mock, image" in result.stdout
|
||||||
|
|
||||||
|
|
||||||
def test_keywords_command_file_not_found():
|
def test_keywords_command_file_not_found():
|
||||||
"""Test the 'keywords' command with a non-existent file path."""
|
"""Test the 'keywords' command with a non-existent file path."""
|
||||||
non_existent_file = Path("non_existent_file.jpg")
|
non_existent_file = Path("non_existent_file.jpg")
|
||||||
@@ -62,6 +70,7 @@ def test_keywords_command_file_not_found():
|
|||||||
assert result.exit_code == 1
|
assert result.exit_code == 1
|
||||||
assert f"Error: File '{non_existent_file}' does not exist." in result.stderr
|
assert f"Error: File '{non_existent_file}' does not exist." in result.stderr
|
||||||
|
|
||||||
|
|
||||||
def test_keywords_command_error_during_processing(mock_photo_logic, dummy_file):
|
def test_keywords_command_error_during_processing(mock_photo_logic, dummy_file):
|
||||||
"""Test the 'keywords' command when an exception occurs during processing."""
|
"""Test the 'keywords' command when an exception occurs during processing."""
|
||||||
# Configure the session mock to raise an exception
|
# Configure the session mock to raise an exception
|
||||||
@@ -72,6 +81,7 @@ def test_keywords_command_error_during_processing(mock_photo_logic, dummy_file):
|
|||||||
assert result.exit_code == 1
|
assert result.exit_code == 1
|
||||||
assert "Error processing image: LLM error" in result.stderr
|
assert "Error processing image: LLM error" in result.stderr
|
||||||
|
|
||||||
|
|
||||||
def test_version_command():
|
def test_version_command():
|
||||||
"""Test the --version option."""
|
"""Test the --version option."""
|
||||||
result = runner.invoke(app, ["--version"])
|
result = runner.invoke(app, ["--version"])
|
||||||
|
|||||||
Reference in New Issue
Block a user