Skip to content

Commit

Permalink
Resolve "The CLI is not tested so far" (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
btschwertfeger authored Jan 5, 2025
1 parent e286020 commit ae5e69e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 32 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ asyncio_default_fixture_loop_scope = "function"

[tool.coverage.run]
source = ["src/kraken_infinity_grid"]
omit = ["*tests*"]
omit = ["*tests*", "_version.py"]

[tool.coverage.report]
exclude_lines = ["coverage: disable", "if TYPE_CHECKING:"]
Expand Down
2 changes: 1 addition & 1 deletion src/kraken_infinity_grid/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def cancel(ctx: Context, **kwargs: dict) -> None:
"""Cancel all open orders."""
ctx.obj |= kwargs
if not ctx.obj["force"]:
print("Not canceling -f is required!") # noqa: T201
print("Not canceling unless '-f' was passed!") # noqa: T201
sys.exit(1)

from pprint import pprint # noqa: PLC0415
Expand Down
30 changes: 0 additions & 30 deletions tests/conftest.py

This file was deleted.

96 changes: 96 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2025 Benjamin Thomas Schwertfeger
# GitHub: https://github.com/btschwertfeger
#

import os
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from click.testing import CliRunner

from kraken_infinity_grid.cli import cli


@pytest.fixture
def runner() -> CliRunner:
return CliRunner()


# ==============================================================================


def test_cli_help(runner: CliRunner) -> None:
"""Test the help message"""
result = runner.invoke(cli, ["--help"])
assert result.exit_code == 0
assert "Usage:" in result.output


def test_cli_version(runner: CliRunner) -> None:
"""Test the version message"""
result = runner.invoke(cli, ["--version"])
assert result.exit_code == 0


@patch.dict(os.environ, {})
@patch("kraken_infinity_grid.gridbot.KrakenInfinityGridBot")
def test_cli_run(mock_bot: MagicMock, runner: CliRunner) -> None:
"""Test the run command"""
command = [
"--api-key",
"test_api_key",
"--secret-key",
"test_secret_key",
"run",
"--name",
"test_bot",
"--base-currency",
"BTC",
"--quote-currency",
"USD",
"--amount-per-grid",
"100",
"--interval",
"0.04",
"--n-open-buy-orders",
"3",
"--userref",
"123456",
"--db-user",
"test_db_user",
"--db-password",
"test_db_password",
"--db-name",
"test_db_name",
"--db-host",
"test_db_host",
"--db-port",
"5432",
"--strategy",
"DCA",
]
mock_bot.return_value.run = AsyncMock()
result = runner.invoke(cli, command)

assert result.exit_code == 0
mock_bot.assert_called_once()
mock_bot.return_value.run.assert_any_await()


@patch.dict(os.environ, {})
@patch("kraken.spot.Trade")
def test_cli_cancel(mock_trade: MagicMock, runner: CliRunner) -> None:
"""Test the cancel command"""
command = [
"--api-key",
"test_api_key",
"--secret-key",
"test_secret_key",
"cancel",
"--force",
]
result = runner.invoke(cli, command)
assert result.exit_code == 0
mock_trade.return_value.cancel_all_orders.assert_called_once()
17 changes: 17 additions & 0 deletions tests/test_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
from kraken_infinity_grid.telegram import Telegram


@pytest.fixture
def telegram() -> Telegram:
"""Fixture to create a Telegram instance for testing."""
strategy = mock.Mock()
telegram_token = "test_token" # noqa: S105
telegram_chat_id = "test_chat_id"
exception_token = "exception_token" # noqa: S105
exception_chat_id = "exception_chat_id"
return Telegram(
strategy,
telegram_token,
telegram_chat_id,
exception_token,
exception_chat_id,
)


@mock.patch("kraken_infinity_grid.telegram.requests.post")
def test_send_to_telegram(
mock_post: mock.Mock,
Expand Down

0 comments on commit ae5e69e

Please sign in to comment.