From e823fce1f57f1bd7319660c395cef6b22ee46b02 Mon Sep 17 00:00:00 2001 From: Sergey Vanyushkin Date: Mon, 20 May 2024 22:03:14 +0000 Subject: [PATCH] cli main help test --- src/clifinance/application/dto/selector.py | 6 +- .../persistence/json_gateway.py | 2 +- src/clifinance/presentation/cli/parsers.py | 41 +++++++++++- tests/test_cli_help.py | 63 +++++++++++++++++++ 4 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 tests/test_cli_help.py diff --git a/src/clifinance/application/dto/selector.py b/src/clifinance/application/dto/selector.py index 4e4ed1b..afdcc41 100644 --- a/src/clifinance/application/dto/selector.py +++ b/src/clifinance/application/dto/selector.py @@ -4,6 +4,6 @@ from datetime import datetime @dataclass(frozen=True) class SelectorDTO: - type: str - date: datetime - amount: float + type: str | None = None + date: datetime | None = None + amount: float | None = None diff --git a/src/clifinance/infrastructure/persistence/json_gateway.py b/src/clifinance/infrastructure/persistence/json_gateway.py index 1f05eed..413fec7 100644 --- a/src/clifinance/infrastructure/persistence/json_gateway.py +++ b/src/clifinance/infrastructure/persistence/json_gateway.py @@ -44,5 +44,5 @@ class JsonExpenseGateway(ExpenseGateway): if date is not None: result = [x for x in result if x.date == date] if amount is not None: - result = [x for x in result if x.amount == amount] + result = [x for x in result if x.amount.value == amount.value] return result diff --git a/src/clifinance/presentation/cli/parsers.py b/src/clifinance/presentation/cli/parsers.py index c0eb4f7..99ec03d 100644 --- a/src/clifinance/presentation/cli/parsers.py +++ b/src/clifinance/presentation/cli/parsers.py @@ -25,10 +25,10 @@ def get_select_parser(subparser: argparse._SubParsersAction) -> argparse.Argumen help="Select expenses by date", ) select_parser.add_argument( - "-p", - "--price", + "-a", + "--amount", type=float, - help="Select expenses by price", + help="Select expenses by amount", ) select_parser.add_argument( "-t", @@ -68,6 +68,41 @@ def get_add_parser(subparser: argparse._SubParsersAction) -> argparse.ArgumentPa return add_parser +def get_update_parser(subparser: argparse._SubParsersAction) -> argparse.ArgumentParser: + add_parser = subparser.add_parser(name="add", help="Add new expense") + add_parser.add_argument( + "-i", + "--id", + type=int, + help="Expense id", + ) + add_parser.add_argument( + "-t", + "--type", + type=str, + help="Expense type. Available values: income, expense", + ) + add_parser.add_argument( + "-d", + "--date", + type=datetime.datetime.fromisoformat, + help="Expense date", + ) + add_parser.add_argument( + "-a", + "--amount", + type=float, + help="Expense price", + ) + add_parser.add_argument( + "-D", + "--description", + type=str, + help="Expense description", + ) + return add_parser + + def provide_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description="Cli finance app") subparser = parser.add_subparsers(title="Availeble commands", dest="command") diff --git a/tests/test_cli_help.py b/tests/test_cli_help.py new file mode 100644 index 0000000..3670c0c --- /dev/null +++ b/tests/test_cli_help.py @@ -0,0 +1,63 @@ +import sys +from contextlib import nullcontext + +import pytest + +from clifinance.main.app import main + +app = "src/clifinance/main/app.py" + +main_help_out = [ + "usage:", + "app.py", + "[-h]", + "{balance,select,add}", + "...", + "Cli", + "finance", + "app", + "options:", + "-h,", + "--help", + "show", + "this", + "help", + "message", + "and", + "exit", + "Availeble", + "commands:", + "{balance,select,add}", + "balance", + "Get", + "balance", + "select", + "Get", + "expenses", + "by", + "filter", + "add", + "Add", + "new", + "expense", +] + +main_help_testdata = [ + (app, "-h", nullcontext(main_help_out)), + (app, "--help", nullcontext(main_help_out)), +] + + +@pytest.mark.parametrize("app, arg, expectation", main_help_testdata) +def test_main_help(capsys, app, arg, expectation): + with expectation as e: + sys.argv = [app, arg] + + with pytest.raises(SystemExit) as excinfo: + main() + + out, err = capsys.readouterr() + + assert excinfo.value.code == 0 + assert err == "" + assert out.split() == e