Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarber-akamai committed Aug 17, 2023
1 parent 67dbdd5 commit 5e3537d
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ clean:
.PHONY: testunit
testunit: export LINODE_CLI_TEST_MODE = 1
testunit:
pytest tests/unit
pytest -v tests/unit

.PHONY: testint
testint:
Expand Down
24 changes: 22 additions & 2 deletions linodecli/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def print(
),
}

if len(columns) < 1:
raise ValueError(
"Expected a non-zero number of columns."
"This is always an error in the OpenAPI spec."
)

if isinstance(columns[0], OpenAPIResponseAttr):
header = [c.column_name for c in columns]
else:
Expand Down Expand Up @@ -148,7 +154,7 @@ def _pop_attrs_for_subtable(
Pops all attributes that belong to the given subtable
and returns them.
"""
results = [v for v in attrs if table + "." in v.name]
results = [v for v in attrs if v.name.startswith(table + ".")]

# Drop the corresponding entries from the root attrs
for v in results:
Expand All @@ -169,7 +175,7 @@ def _scope_data_to_subtable(data: List[Dict[str, Any]], table: str) -> Any:
if len(data) == 0:
return data

result = data[0]
result = data[0] if isinstance(data, list) else data

for seg in table.split("."):
if seg not in result:
Expand Down Expand Up @@ -245,6 +251,7 @@ def _table_output(

if title is not None:
tab.title = title
tab.min_width = self.column_width or len(title)

rprint(tab, file=to)

Expand Down Expand Up @@ -291,13 +298,26 @@ def _select_json_elements(keys, json_res):
paths to handle nested dicts
"""
ret = {}

for k, v in json_res.items():
if k in keys:
ret[k] = v
elif isinstance(v, dict):
v = OutputHandler._select_json_elements(keys, v)
if v:
ret[k] = v
elif isinstance(v, list):
results = []
for elem in v:
selected = OutputHandler._select_json_elements(keys, elem)
if not selected:
continue

results.append(selected)

if len(results) > 0:
ret[k] = results

return ret

def _build_output_content(
Expand Down
56 changes: 56 additions & 0 deletions tests/fixtures/subtable_test_get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
openapi: 3.0.1
info:
title: API Specification
version: 1.0.0
servers:
- url: http://localhost

paths:
/foo/bar:
get:
summary: get info with a complex structure
operationId: fooBarGet
description: This is description
responses:
'200':
description: Successful response
content:
application/json:
x-linode-cli-subtables:
- table
- foo.table
- foo.single_nested
schema:
type: object
properties:
table:
type: array
items:
type: object
properties:
foo:
type: string
bar:
type: integer
foo:
type: object
properties:
single_nested:
type: object
properties:
foo:
type: string
bar:
type: string
table:
type: array
items:
type: object
properties:
foobar:
type: array
format: ipv4
items:
type: string
foobar:
type: string
43 changes: 43 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,49 @@ def list_operation_for_response_test():
return cool_operation


@pytest.fixture
def get_operation_for_subtable_test():
"""
Creates the following CLI operation:
GET http://localhost/foo/bar
Returns {
"table": [
{
"foo": "",
"bar": 0
}
],
"foo": {
"single_nested": {
"foo": "",
"bar": ""
},
"table": [
{
"foobar": ["127.0.0.1"]
}
]
},
"foobar": ""
}
"""

spec = _get_parsed_spec("subtable_test_get.yaml")

dict_values = list(spec.paths.values())

# Get parameters for OpenAPIOperation() from yaml fixture
path = dict_values[0]

command = path.extensions.get("linode-cli-command", "default")
operation = getattr(path, "get")
method = "get"

return make_test_operation(command, operation, method, path.parameters)


@pytest.fixture
def mocked_config():
"""
Expand Down
Loading

0 comments on commit 5e3537d

Please sign in to comment.