cli main help test

main
Сергей Ванюшкин 2024-05-20 22:03:14 +00:00
parent 2b158c8b5f
commit e823fce1f5
4 changed files with 105 additions and 7 deletions

View File

@ -4,6 +4,6 @@ from datetime import datetime
@dataclass(frozen=True) @dataclass(frozen=True)
class SelectorDTO: class SelectorDTO:
type: str type: str | None = None
date: datetime date: datetime | None = None
amount: float amount: float | None = None

View File

@ -44,5 +44,5 @@ class JsonExpenseGateway(ExpenseGateway):
if date is not None: if date is not None:
result = [x for x in result if x.date == date] result = [x for x in result if x.date == date]
if amount is not None: 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 return result

View File

@ -25,10 +25,10 @@ def get_select_parser(subparser: argparse._SubParsersAction) -> argparse.Argumen
help="Select expenses by date", help="Select expenses by date",
) )
select_parser.add_argument( select_parser.add_argument(
"-p", "-a",
"--price", "--amount",
type=float, type=float,
help="Select expenses by price", help="Select expenses by amount",
) )
select_parser.add_argument( select_parser.add_argument(
"-t", "-t",
@ -68,6 +68,41 @@ def get_add_parser(subparser: argparse._SubParsersAction) -> argparse.ArgumentPa
return add_parser 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: def provide_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Cli finance app") parser = argparse.ArgumentParser(description="Cli finance app")
subparser = parser.add_subparsers(title="Availeble commands", dest="command") subparser = parser.add_subparsers(title="Availeble commands", dest="command")

63
tests/test_cli_help.py Normal file
View File

@ -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