Skip to content

Commit dd4732d

Browse files
Add unit test
1 parent 8f4af0a commit dd4732d

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

linodecli/cli.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ def __init__(self, version, base_url, skip_config=False):
4949
self.config = CLIConfig(self.base_url, skip_config=skip_config)
5050
self.load_baked()
5151

52-
def bake(self, spec_location: str):
52+
def bake(self, spec_location: str, save: bool = True):
5353
"""
5454
Generates ops and bakes them to a pickle.
5555
5656
:param spec_location: The URL or file path of the OpenAPI spec to parse.
57+
:param save: Whether the pickled operations should be saved to a file.
58+
This is primarily intended for unit testing.
5759
"""
5860

5961
try:
@@ -74,10 +76,6 @@ def bake(self, spec_location: str):
7476

7577
for path in spec.paths.values():
7678
command = path.extensions.get(ext["command"], None)
77-
if command is None:
78-
raise KeyError(
79-
f"Path {path} is missing {ext['command']} extension"
80-
)
8179

8280
for m in METHODS:
8381
operation = getattr(path, m)
@@ -94,12 +92,19 @@ def bake(self, spec_location: str):
9492

9593
if ext["skip"] in operation.extensions:
9694
logger.debug(
97-
"%s: Skipping operation due to %s extension",
95+
"%s: Skipping operation due to x-%s extension",
9896
operation_log_fmt,
9997
ext["skip"],
10098
)
10199
continue
102100

101+
# We don't do this in the parent loop because certain paths
102+
# may only have skipped operations
103+
if command is None:
104+
raise KeyError(
105+
f"{operation_log_fmt}: Missing x-{ext['command']} extension"
106+
)
107+
103108
action = operation.extensions.get(ext["action"], None)
104109

105110
if action is None:
@@ -150,7 +155,8 @@ def bake(self, spec_location: str):
150155

151156
# finish the baking
152157
data_file = self._get_data_file()
153-
with open(data_file, "wb") as f:
158+
159+
with open(data_file, "wb") if save else os.devnull as f:
154160
pickle.dump(self.ops, f)
155161

156162
def load_baked(self):
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
openapi: 3.0.1
2+
info:
3+
title: API Specification
4+
version: 1.0.0
5+
servers:
6+
- url: http://localhost/v4
7+
paths:
8+
/foo/bar:
9+
get:
10+
summary: get info
11+
operationId: fooBarGet
12+
description: This is description
13+
responses:
14+
'200':
15+
description: Successful response
16+
content:
17+
application/json:
18+
schema:
19+
type: object
20+
properties:
21+
data:
22+
type: array
23+
items:
24+
$ref: '#/components/schemas/OpenAPIResponseAttr'
25+
page:
26+
$ref: '#/components/schemas/PaginationEnvelope/properties/page'
27+
pages:
28+
$ref: '#/components/schemas/PaginationEnvelope/properties/pages'
29+
results:
30+
$ref: '#/components/schemas/PaginationEnvelope/properties/results'
31+
32+
components:
33+
schemas:
34+
OpenAPIResponseAttr:
35+
type: object
36+
properties:
37+
filterable_result:
38+
x-linode-filterable: true
39+
type: string
40+
description: Filterable result value
41+
filterable_list_result:
42+
x-linode-filterable: true
43+
type: array
44+
items:
45+
type: string
46+
description: Filterable result value
47+
PaginationEnvelope:
48+
type: object
49+
properties:
50+
pages:
51+
type: integer
52+
readOnly: true
53+
description: The total number of pages.
54+
example: 1
55+
page:
56+
type: integer
57+
readOnly: true
58+
description: The current page.
59+
example: 1
60+
results:
61+
type: integer
62+
readOnly: true
63+
description: The total number of results.
64+
example: 1

tests/unit/test_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ def test_load_openapi_spec_yaml(self):
122122
assert m.call_count == 1
123123
assert parsed_json_http.raw_element == parsed_json_local.raw_element
124124

125+
def test_bake_missing_cmd_ext(self, mock_cli: CLI):
126+
try:
127+
mock_cli.bake(
128+
str(
129+
os.path.join(
130+
FIXTURES_PATH, "cli_test_bake_missing_cmd_ext.yaml"
131+
)
132+
),
133+
save=False,
134+
)
135+
except KeyError as err:
136+
assert (
137+
str(err)
138+
== "'GET /foo/bar: Missing x-linode-cli-command extension'"
139+
)
140+
else:
141+
raise AssertionError("Expected a KeyError exception")
142+
125143

126144
def test_get_all_pages(
127145
mock_cli: CLI, list_operation: OpenAPIOperation, monkeypatch: MonkeyPatch

0 commit comments

Comments
 (0)