Skip to content

Commit

Permalink
TEST
Browse files Browse the repository at this point in the history
  • Loading branch information
tacgomes committed Dec 5, 2024
1 parent cfcf32a commit 3cc2b67
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
- name: Run unit tests
run: |
pytest
pytest -vv
env:
COVERAGE_FILE: .coverage.${{ matrix.os }}-py${{ matrix.python-version }}

Expand Down
33 changes: 16 additions & 17 deletions src/investir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def orders_command( # noqa: PLR0913
disposals_only: Annotated[
bool, typer.Option("--disposals", help="Show only disposals.")
] = False,
output_format: OutputFormatOpt = OutputFormat.TEXT,
format: OutputFormatOpt = OutputFormat.TEXT,
) -> None:
"""
Show share buy/sell orders.
Expand All @@ -265,23 +265,23 @@ def orders_command( # noqa: PLR0913

filters = create_filters(tax_year=tax_year, ticker=ticker, tr_type=tr_type)
if table := tr_hist.get_orders_table(filters):
print(table.to_string(output_format, leading_nl=config.logging_enabled))
print(table.to_string(format, leading_nl=config.logging_enabled))


@app.command("dividends")
def dividends_command(
files: FilesArg,
tax_year: TaxYearOpt = None,
ticker: TickerOpt = None,
output_format: OutputFormatOpt = OutputFormat.TEXT,
format: OutputFormatOpt = OutputFormat.TEXT,
) -> None:
"""
Show share dividends paid out.
"""
tr_hist, _ = parse(files)
filters = create_filters(tax_year=tax_year, ticker=ticker)
if table := tr_hist.get_dividends_table(filters):
print(table.to_string(output_format, leading_nl=config.logging_enabled))
print(table.to_string(format, leading_nl=config.logging_enabled))


@app.command("transfers")
Expand All @@ -294,7 +294,7 @@ def transfers_command(
withdrawals_only: Annotated[
bool, typer.Option("--withdrawals", help="Show only withdrawals.")
] = False,
output_format: OutputFormatOpt = OutputFormat.TEXT,
format: OutputFormatOpt = OutputFormat.TEXT,
) -> None:
"""
Show cash deposits and cash withdrawals.
Expand All @@ -313,22 +313,22 @@ def transfers_command(

filters = create_filters(tax_year=tax_year, amount_op=amount_op)
if table := tr_hist.get_transfers_table(filters):
print(table.to_string(output_format, leading_nl=config.logging_enabled))
print(table.to_string(format, leading_nl=config.logging_enabled))


@app.command("interest")
def interest_command(
files: FilesArg,
tax_year: TaxYearOpt = None,
output_format: OutputFormatOpt = OutputFormat.TEXT,
format: OutputFormatOpt = OutputFormat.TEXT,
) -> None:
"""
Show interest earned on cash.
"""
tr_hist, _ = parse(files)
filters = create_filters(tax_year=tax_year)
if table := tr_hist.get_interest_table(filters):
print(table.to_string(output_format, leading_nl=config.logging_enabled))
print(table.to_string(format, leading_nl=config.logging_enabled))


@app.command("capital-gains")
Expand All @@ -342,18 +342,17 @@ def capital_gains_command( # noqa: PLR0913
] = False,
tax_year: TaxYearOpt = None,
ticker: TickerOpt = None,
output_format: OutputFormatOpt = OutputFormat.TEXT,
format: OutputFormatOpt = OutputFormat.TEXT,
) -> None:
"""
Show capital gains report.
"""
if gains_only and losses_only:
raise MutuallyExclusiveOption("--gains", "--losses")

if output_format != OutputFormat.TEXT and tax_year is None:
if format != OutputFormat.TEXT and tax_year is None:
raise click.exceptions.UsageError(
f"The {output_format.value} format requires the option "
f"--tax-year to be used"
f"The {format.value} format requires the option " f"--tax-year to be used"
)

_, tax_calculator = parse(files)
Expand All @@ -376,15 +375,15 @@ def capital_gains_command( # noqa: PLR0913
if table:
print(end="\n" if tax_year_idx == 0 and config.logging_enabled else "")

if output_format == OutputFormat.TEXT:
if format == OutputFormat.TEXT:
print(
boldify(f"Capital Gains Tax Report {tax_year_short_date(tax_year)}")
)
print(tax_year_full_date(tax_year))
print(table.to_string(output_format))
print(table.to_string(format))
print(summary)
else:
print(table.to_string(output_format, leading_nl=False))
print(table.to_string(format, leading_nl=False))


@app.command("holdings")
Expand All @@ -394,15 +393,15 @@ def holdings_command(
show_gain_loss: Annotated[
bool, typer.Option("--show-gain-loss", help="Show unrealised gain/loss.")
] = False,
output_format: OutputFormatOpt = OutputFormat.TEXT,
format: OutputFormatOpt = OutputFormat.TEXT,
) -> None:
"""
Show current holdings.
"""
_, tax_calculator = parse(files)
ticker = Ticker(ticker) if ticker else None
if table := tax_calculator.get_holdings_table(ticker, show_gain_loss):
print(table.to_string(output_format, leading_nl=config.logging_enabled))
print(table.to_string(format, leading_nl=config.logging_enabled))


def main() -> None:
Expand Down
12 changes: 6 additions & 6 deletions src/investir/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ def __init__(self, *args, **kwargs) -> None:
def hide_field(self, field_name: str) -> None:
self.invisible_fields.add(field_name)

def to_string(self, output_format: OutputFormat, leading_nl: bool = True) -> str:
self._apply_formatting(output_format == OutputFormat.TEXT)
def to_string(self, format: OutputFormat, leading_nl: bool = True) -> str:
self._apply_formatting(format == OutputFormat.TEXT)

leading = "\n" if leading_nl else ""
trailing = "\n" if output_format == OutputFormat.TEXT else ""
trailing = "\n" if format == OutputFormat.TEXT else ""

fields = [
f for f in self.field_names if unboldify(f) not in self.invisible_fields
]

kwargs: dict[str, Any] = {"fields": fields}
if output_format == OutputFormat.JSON:
if format == OutputFormat.JSON:
kwargs["default"] = str

table_str = self.get_formatted_string(output_format.value, **kwargs)
table_str = self.get_formatted_string(format.value, **kwargs)

if output_format == OutputFormat.CSV:
if format == OutputFormat.CSV:
table_str = table_str.rstrip()

return f"{leading}{table_str}{trailing}"
Expand Down
9 changes: 8 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ def test_cli_output(request, execute, cmd, expected):
assert result.exit_code == EX_OK

assert expected_output.exists()
assert result.stdout == expected_output.read_text(encoding="utf-8")
print("A")
print(repr(result.stdout))
print("B")
print(repr(expected_output.read_text(encoding="utf-8")))
assert (
result.stdout.splitlines()
== expected_output.read_text(encoding="utf-8").splitlines()
)


def test_capital_gains_multiple_input_files(execute):
Expand Down

0 comments on commit 3cc2b67

Please sign in to comment.