Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add command line interface with basic commands. #62

Merged
merged 10 commits into from
Oct 4, 2024
Prev Previous commit
Next Next commit
tests: Improve test coverage.
  • Loading branch information
bvanelli committed Oct 4, 2024
commit f1ff7d4f6e94cf05c66a9f83225ca551e5a81a0a
66 changes: 42 additions & 24 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import datetime
import pathlib
from typing import List

import pytest
from click.testing import Result
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for_logs
from typer.testing import CliRunner

from actual import Actual, __version__
from actual.cli.config import Config, default_config_path
from actual.queries import create_account, create_transaction

runner = CliRunner()
Expand All @@ -15,7 +18,10 @@

def base_dataset(actual: Actual):
actual.create_budget("Test")
bank = create_account(actual.session, "Bank", 150)
bank = create_account(actual.session, "Bank")
create_transaction(
actual.session, datetime.date(2024, 9, 5), bank, "Starting Balance", category="Starting", amount=150
)
create_transaction(
actual.session, datetime.date(2024, 12, 24), bank, "Shopping Center", "Christmas Gifts", "Gifts", -100
)
Expand Down Expand Up @@ -54,26 +60,32 @@ def actual_server(request, module_mocker, tmp_path_factory):
yield container


def test_app(actual_server):
def invoke(command: List[str]) -> Result:
from actual.cli.main import app

result = runner.invoke(app, ["version"])
return runner.invoke(app, command)


def test_load_config(actual_server):
cfg = Config.load()
assert cfg.default_context == "test"
assert str(default_config_path()).endswith(".actual/config.yaml")


def test_app(actual_server):
result = invoke(["version"])
assert result.exit_code == 0
assert result.stdout == f"Library Version: {__version__}\nServer Version: {server_version}\n"


def test_metadata(actual_server):
from actual.cli.main import app

result = runner.invoke(app, ["metadata"])
result = invoke(["metadata"])
assert result.exit_code == 0
assert "budgetName" in result.stdout


def test_accounts(actual_server):
from actual.cli.main import app

result = runner.invoke(app, ["accounts"])
result = invoke(["accounts"])
assert result.exit_code == 0
assert result.stdout == (
" Accounts \n"
Expand All @@ -86,27 +98,21 @@ def test_accounts(actual_server):


def test_transactions(actual_server):
from actual.cli.main import app

result = runner.invoke(app, ["transactions"])
result = invoke(["transactions"])
assert result.exit_code == 0
assert result.stdout == (
" "
"Transactions \n"
"┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓\n"
"┃ Date ┃ Payee ┃ Notes ┃ Category ┃ Amount ┃\n"
"┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩\n"
"│ 2024-12-24 │ Shopping Center │ Christmas Gifts │ Gifts │ -100.00 │\n"
"│ 2024-09-05 │ Starting Balance │ │ Starting │ 150.00 │\n"
"│ │ │ │ Balances │ │\n"
"└────────────┴──────────────────┴─────────────────┴──────────────────┴─────────┘\n"
" Transactions \n"
"┏━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━┓\n"
"┃ Date ┃ Payee ┃ Notes ┃ Category ┃ Amount ┃\n"
"┡━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━┩\n"
"│ 2024-12-24 │ Shopping Center │ Christmas Gifts │ Gifts │ -100.00 │\n"
"│ 2024-09-05 │ Starting Balance │ │ Starting │ 150.00 │\n"
"└────────────┴──────────────────┴─────────────────┴──────────┴─────────┘\n"
)


def test_payees(actual_server):
from actual.cli.main import app

result = runner.invoke(app, ["payees"])
result = invoke(["payees"])
assert result.exit_code == 0
assert result.stdout == (
" Payees \n"
Expand All @@ -118,3 +124,15 @@ def test_payees(actual_server):
"│ Shopping Center │ -100.00 │\n"
"└──────────────────┴─────────┘\n"
)


def test_export(actual_server, mocker):
export_data = mocker.patch("actual.Actual.export_data")
invoke(["export"])
export_data.assert_called_once()
assert export_data.call_args[0][0].name.endswith("Test.zip")

# test normal file name
invoke(["export", "Test.zip"])
assert export_data.call_count == 2
assert export_data.call_args[0][0].name == "Test.zip"
Loading