Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions linodecli/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def linode_types_with_region_prices(

output = Table(
header_style="bold",
show_header=output_handler.headers,
show_lines=True,
)

Expand All @@ -169,7 +170,9 @@ def linode_types_with_region_prices(
row = []
for h in headers:
if h == "region_prices":
sub_table = format_region_prices(linode[h])
sub_table = format_region_prices(
linode[h], show_header=output_handler.headers
)
row.append(sub_table)

elif h in ("price.hourly", "price.monthly"):
Expand Down Expand Up @@ -201,13 +204,15 @@ def format_prices(prices, data: Dict[str, any]) -> any:
return str(data[price_headers[0]][price_headers[1]])


def format_region_prices(data: Dict[str, any]) -> any:
def format_region_prices(
data: Dict[str, any], *, show_header: bool = True
) -> any:
"""
Format nested region price entry into a sub-table.
"""
subheaders = ["id", "hourly", "monthly"]

sub_table = Table(box=box.SIMPLE_HEAVY)
sub_table = Table(box=box.SIMPLE_HEAVY, show_header=show_header)

for header in subheaders:
sub_table.add_column(header, justify="center")
Expand Down
44 changes: 29 additions & 15 deletions tests/unit/test_overrides.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import contextlib
import io
from argparse import ArgumentParser
from unittest.mock import patch

import pytest

from linodecli import OutputMode
from linodecli.arg_helpers import register_args
from linodecli.overrides import OUTPUT_OVERRIDES


Expand Down Expand Up @@ -60,8 +64,14 @@ def patch_func(*a):

assert stdout_buf.getvalue() != "line 1\nline 2\n"

@pytest.mark.parametrize("no_headers", [False, True])
def test_types_region_prices_list(
self, mock_cli, list_operation_for_overrides_test
self,
mock_cli,
list_operation_for_overrides_test,
capsys,
monkeypatch,
no_headers,
):
response_json = {
"data": [
Expand Down Expand Up @@ -99,19 +109,23 @@ def test_types_region_prices_list(
"results": 1,
}

override_signature = ("linodes", "types", OutputMode.table)

list_operation_for_overrides_test.command = "linodes"
list_operation_for_overrides_test.action = "types"
mock_cli.output_handler.mode = OutputMode.table

stdout_buf = io.StringIO()

with contextlib.redirect_stdout(stdout_buf):
list_operation_for_overrides_test.process_response_json(
response_json, mock_cli.output_handler
)

rows = stdout_buf.getvalue().split("\n")
# assert that the overridden table has the new columns
assert len(rows[1].split(rows[1][0])) == 15
args = ["linodes", "types"]
if no_headers:
args.append("--no-headers")
parsed = register_args(ArgumentParser(add_help=False)).parse_args(args)
mock_cli.output_handler.configure(parsed)
monkeypatch.setenv("COLUMNS", "240")

list_operation_for_overrides_test.process_response_json(
response_json, mock_cli.output_handler
)

cells = capsys.readouterr().out.split()
for value in ("g6-standard-2", "0.03", "20", "us-east", "0.036", "24"):
assert value in cells
price_headers = {"price.hourly", "price.monthly", "hourly", "monthly"}
assert price_headers.intersection(cells) == (
set() if no_headers else price_headers
)