diff --git a/tests/test_cli.py b/tests/test_cli.py index 9544a56..f62f63f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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) @@ -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."""