Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayak-mehta committed Jan 24, 2025
1 parent c20b672 commit 2c39ce2
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,21 @@ def mock_clickhouse(mocker):
@pytest.fixture
def mock_houseplant(mocker, mock_clickhouse) -> Generator[None, None, None]:
"""Mock the Houseplant class to avoid actual operations during testing."""
mock_instance = mocker.Mock(spec=Houseplant)
# Create a real instance to get access to the original init method
real_instance = Houseplant()

# Set up the mock instance with a working db connection
# Create mock instance
mock_instance = mocker.Mock(spec=Houseplant)
mock_instance.db = mocker.Mock()
mock_instance.db.client = mock_clickhouse
mock_instance.db._check_clickhouse_connection.return_value = None
mock_instance.db.init_migrations_table.return_value = None
mock_instance.console = real_instance.console

# Mock both the Houseplant class constructor and get_houseplant function
# Create a mock that wraps the real init method
mock_instance.init = mocker.Mock(side_effect=real_instance.init)

# Mock both the constructor and get_houseplant
mocker.patch("houseplant.cli.Houseplant", return_value=mock_instance)
mocker.patch("houseplant.cli.get_houseplant", return_value=mock_instance)

Expand Down Expand Up @@ -71,12 +78,22 @@ def test_version_flag():
assert f"houseplant version {__version__}" in result.stdout


def test_init_command(mock_houseplant):
def test_init_command(tmp_path, mock_houseplant, monkeypatch):
"""Test the init command."""
# Change to temp directory safely using monkeypatch
monkeypatch.chdir(tmp_path)

result = runner.invoke(app, ["init"])
assert result.exit_code == 0
mock_houseplant.init.assert_called_once()

# Verify directories were created using absolute paths
migrations_dir = tmp_path / "ch" / "migrations"
schema_file = tmp_path / "ch" / "schema.sql"

assert migrations_dir.exists()
assert schema_file.exists()


def test_migrate_status_command(mock_houseplant):
"""Test the migrate:status command."""
Expand Down

0 comments on commit 2c39ce2

Please sign in to comment.