Skip to content

Commit 53d1762

Browse files
committed
Fixed CLI tests, started OpenAPI tests
#3
1 parent 185c8e9 commit 53d1762

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

openapi_python_client/openapi_parser/openapi.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,12 @@ class OpenAPI:
189189
title: str
190190
description: str
191191
version: str
192-
# security_schemes: Dict
193192
schemas: Dict[str, Schema]
194193
endpoint_collections_by_tag: Dict[str, EndpointCollection]
195194
enums: Dict[str, EnumProperty]
196195

197196
@staticmethod
198-
def check_enums(schemas: Iterable[Schema], collections: Iterable[EndpointCollection]) -> Dict[str, EnumProperty]:
197+
def _check_enums(schemas: Iterable[Schema], collections: Iterable[EndpointCollection]) -> Dict[str, EnumProperty]:
199198
enums: Dict[str, EnumProperty] = {}
200199

201200
def _iterate_properties() -> Generator[Property, None, None]:
@@ -225,14 +224,13 @@ def from_dict(d: Dict[str, Dict[str, Any]], /) -> OpenAPI:
225224
""" Create an OpenAPI from dict """
226225
schemas = Schema.dict(d["components"]["schemas"])
227226
endpoint_collections_by_tag = EndpointCollection.from_dict(d["paths"])
228-
enums = OpenAPI.check_enums(schemas.values(), endpoint_collections_by_tag.values())
227+
enums = OpenAPI._check_enums(schemas.values(), endpoint_collections_by_tag.values())
229228

230229
return OpenAPI(
231230
title=d["info"]["title"],
232231
description=d["info"]["description"],
233232
version=d["info"]["version"],
234233
endpoint_collections_by_tag=endpoint_collections_by_tag,
235234
schemas=schemas,
236-
# security_schemes=d["components"]["securitySchemes"],
237235
enums=enums,
238236
)

tests/test_cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
from click.testing import CliRunner
22

33

4-
def test_generate_no_params():
4+
def test_generate_no_params(mocker):
5+
main = mocker.patch("openapi_python_client.cli.main")
56
from openapi_python_client.cli import generate
67

78
runner = CliRunner()
89
result = runner.invoke(generate)
910

1011
assert result.exit_code == 1
1112
assert result.output == "You must either provide --url or --path\n"
13+
main.assert_not_called()
1214

1315

14-
def test_generate_url_and_path():
16+
def test_generate_url_and_path(mocker):
17+
main = mocker.patch("openapi_python_client.cli.main")
1518
from openapi_python_client.cli import generate
1619

1720
runner = CliRunner()
1821
result = runner.invoke(generate, ["--url=blah", "--path=blahblah"])
1922

2023
assert result.exit_code == 1
2124
assert result.output == "Provide either --url or --path, not both\n"
25+
main.assert_not_called()
2226

2327

2428
def test_generate_url(mocker):
25-
main = mocker.patch("openapi_python_client.main")
29+
main = mocker.patch("openapi_python_client.cli.main")
2630
url = mocker.MagicMock()
2731
from openapi_python_client.cli import generate
2832

@@ -34,7 +38,7 @@ def test_generate_url(mocker):
3438

3539

3640
def test_generate_path(mocker):
37-
main = mocker.patch("openapi_python_client.main")
41+
main = mocker.patch("openapi_python_client.cli.main")
3842
path = mocker.MagicMock()
3943
from openapi_python_client.cli import generate
4044

tests/test_openapi_parser/__init__.py

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
MODULE_NAME = "openapi_python_client.openapi_parser.openapi"
2+
3+
4+
class TestOpenAPI:
5+
def test_from_dict(self, mocker):
6+
Schema = mocker.patch(f"{MODULE_NAME}.Schema")
7+
schemas = mocker.MagicMock()
8+
Schema.dict.return_value = schemas
9+
EndpointCollection = mocker.patch(f"{MODULE_NAME}.EndpointCollection")
10+
endpoint_collections_by_tag = mocker.MagicMock()
11+
EndpointCollection.from_dict.return_value = endpoint_collections_by_tag
12+
in_dict = {
13+
"components": {"schemas": mocker.MagicMock()},
14+
"paths": mocker.MagicMock(),
15+
"info": {"title": mocker.MagicMock(), "description": mocker.MagicMock(), "version": mocker.MagicMock()},
16+
}
17+
enums = mocker.MagicMock()
18+
_check_enums = mocker.patch(f"{MODULE_NAME}.OpenAPI._check_enums", return_value=enums)
19+
20+
from openapi_python_client.openapi_parser.openapi import OpenAPI
21+
22+
openapi = OpenAPI.from_dict(in_dict)
23+
24+
Schema.dict.assert_called_once_with(in_dict["components"]["schemas"])
25+
EndpointCollection.from_dict.assert_called_once_with(in_dict["paths"])
26+
_check_enums.assert_called_once_with(schemas.values(), endpoint_collections_by_tag.values())
27+
assert openapi == OpenAPI(
28+
title=in_dict["info"]["title"],
29+
description=in_dict["info"]["description"],
30+
version=in_dict["info"]["version"],
31+
endpoint_collections_by_tag=endpoint_collections_by_tag,
32+
schemas=schemas,
33+
enums=enums,
34+
)

0 commit comments

Comments
 (0)