Skip to content

Commit c15603d

Browse files
committed
Add auto-format to generation
Closes #12
1 parent 153b5bc commit c15603d

File tree

12 files changed

+59
-40
lines changed

12 files changed

+59
-40
lines changed

openapi_python_client/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import shutil
6+
import subprocess
67
from importlib.metadata import version
78
from pathlib import Path
89
from typing import Any, Dict, Optional
@@ -68,6 +69,7 @@ def build(self) -> None:
6869
self._build_metadata()
6970
self._build_models()
7071
self._build_api()
72+
self._reformat()
7173

7274
def update(self) -> None:
7375
""" Update an existing project """
@@ -79,6 +81,11 @@ def update(self) -> None:
7981
self._create_package()
8082
self._build_models()
8183
self._build_api()
84+
self._reformat()
85+
86+
def _reformat(self) -> None:
87+
subprocess.run("isort --recursive --apply", cwd=self.project_dir, shell=True)
88+
subprocess.run("black .", cwd=self.project_dir, shell=True)
8289

8390
def _create_package(self) -> None:
8491
self.package_dir.mkdir()

poetry.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ typer = "^0.1.0"
2222
colorama = {version = "^0.4.3", markers = "sys_platform == 'win32'"}
2323
shellingham = "^1.3.2"
2424
httpx = "^0.12.1"
25+
black = "^19.10b0"
26+
isort = "^4.3.21"
2527

2628
[tool.poetry.scripts]
2729
openapi-python-client = "openapi_python_client.cli:app"
2830

2931
[tool.poetry.dev-dependencies]
3032
pytest = "*"
3133
pytest-mock = "*"
32-
black = {version = ">=19.10b", allow-prereleases = true}
3334
mypy = "^0.761"
3435
taskipy = "^1.1.3"
35-
isort = "^4.3.21"
3636
safety = "^1.8.5"
3737
pytest-cov = "^2.8.1"
3838
fastapi = "^0.52.0"

tests/test___init__.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def test_build(self, mocker):
135135
project._build_models = mocker.MagicMock()
136136
project._build_api = mocker.MagicMock()
137137
project._create_package = mocker.MagicMock()
138+
project._reformat = mocker.MagicMock()
138139

139140
project.build()
140141

@@ -143,6 +144,7 @@ def test_build(self, mocker):
143144
project._build_metadata.assert_called_once()
144145
project._build_models.assert_called_once()
145146
project._build_api.assert_called_once()
147+
project._reformat.assert_called_once()
146148

147149
def test_update(self, mocker):
148150
from openapi_python_client import _Project, shutil
@@ -154,13 +156,15 @@ def test_update(self, mocker):
154156
project._build_models = mocker.MagicMock()
155157
project._build_api = mocker.MagicMock()
156158
project._create_package = mocker.MagicMock()
159+
project._reformat = mocker.MagicMock()
157160

158161
project.update()
159162

160163
rmtree.assert_called_once_with(project.package_dir)
161164
project._create_package.assert_called_once()
162165
project._build_models.assert_called_once()
163166
project._build_api.assert_called_once()
167+
project._reformat.assert_called_once()
164168

165169
def test_update_missing_dir(self, mocker):
166170
from openapi_python_client import _Project
@@ -229,11 +233,9 @@ def test__build_metadata(self, mocker):
229233

230234
project._build_metadata()
231235

232-
project.env.get_template.assert_has_calls([
233-
mocker.call("pyproject.toml"),
234-
mocker.call("README.md"),
235-
mocker.call(".gitignore"),
236-
])
236+
project.env.get_template.assert_has_calls(
237+
[mocker.call("pyproject.toml"), mocker.call("README.md"), mocker.call(".gitignore"), ]
238+
)
237239

238240
pyproject_template.render.assert_called_once_with(
239241
project_name=project.project_name,
@@ -386,7 +388,23 @@ def test__build_api(self, mocker):
386388
errors_template.render.assert_called_once()
387389
api_errors.write_text.assert_called_once_with(errors_template.render())
388390
endpoint_template.render.assert_has_calls(
389-
[mocker.call(collection=collection_1), mocker.call(collection=collection_2),]
391+
[mocker.call(collection=collection_1), mocker.call(collection=collection_2), ]
390392
)
391393
collection_1_path.write_text.assert_called_once_with(endpoint_renders[collection_1])
392394
collection_2_path.write_text.assert_called_once_with(endpoint_renders[collection_2])
395+
396+
397+
def test__reformat(mocker):
398+
from openapi_python_client import _Project, OpenAPI
399+
400+
sub_run = mocker.patch("subprocess.run")
401+
openapi = mocker.MagicMock(autospec=OpenAPI, title="My Test API")
402+
project = _Project(openapi=openapi)
403+
project.project_dir = mocker.MagicMock(autospec=pathlib.Path)
404+
405+
project._reformat()
406+
407+
sub_run.assert_has_calls([
408+
mocker.call("isort --recursive --apply", cwd=project.project_dir, shell=True),
409+
mocker.call("black .", cwd=project.project_dir, shell=True),
410+
])

tests/test_end_to_end/fastapi/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
from fastapi import FastAPI
66
from pydantic import BaseModel
77

8-
app = FastAPI(
9-
title="My Test API",
10-
description="An API for testing openapi-python-client",
11-
)
8+
app = FastAPI(title="My Test API", description="An API for testing openapi-python-client",)
129

1310

1411
class PingResponse(BaseModel):
@@ -20,6 +17,7 @@ async def ping():
2017
""" A quick check to see if the system is running """
2118
return {"success": True}
2219

23-
if __name__ == '__main__':
20+
21+
if __name__ == "__main__":
2422
path = Path(__file__).parent / "openapi.json"
2523
path.write_text(json.dumps(app.openapi()))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
""" A client library for accessing My Test API """
2-
from .client import AuthenticatedClient, Client
2+
from .client import AuthenticatedClient, Client
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
""" Contains all methods for accessing the API """
1+
""" Contains all methods for accessing the API """

tests/test_end_to_end/golden-master/my_test_api_client/api/default.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@
44
import httpx
55

66
from ..client import AuthenticatedClient, Client
7-
from .errors import ApiResponseError
8-
97
from ..models.ping_response import PingResponse
8+
from .errors import ApiResponseError
109

1110

1211
def ping_ping_get(
13-
*,
14-
client: Client,
12+
*, client: Client,
1513
) -> Union[
1614
PingResponse,
1715
]:
1816
""" A quick check to see if the system is running """
1917
url = f"{client.base_url}/ping"
2018

21-
22-
response = httpx.get(
23-
url=url,
24-
headers=client.get_headers(),
25-
)
19+
response = httpx.get(url=url, headers=client.get_headers(),)
2620

2721
if response.status_code == 200:
2822
return PingResponse.from_dict(response.json())
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from httpx import Response
22

3+
34
class ApiResponseError(Exception):
45
""" An exception raised when an unknown response occurs """
56

67
def __init__(self, *, response: Response):
78
super().__init__()
8-
self.response: Response = response
9+
self.response: Response = response

tests/test_end_to_end/golden-master/my_test_api_client/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dataclasses import dataclass
22
from typing import Dict
33

4+
45
@dataclass
56
class Client:
67
""" A class for keeping track of data related to the API """
@@ -11,6 +12,7 @@ def get_headers(self) -> Dict[str, str]:
1112
""" Get headers to be used in all endpoints """
1213
return {}
1314

15+
1416
@dataclass
1517
class AuthenticatedClient(Client):
1618
""" A Client which has been authenticated for use on secured endpoints """
@@ -19,4 +21,4 @@ class AuthenticatedClient(Client):
1921

2022
def get_headers(self) -> Dict[str, str]:
2123
""" Get headers to be used in authenticated endpoints """
22-
return {"Authorization": f"Bearer {self.token}"}
24+
return {"Authorization": f"Bearer {self.token}"}

tests/test_end_to_end/golden-master/my_test_api_client/models/ping_response.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from typing import Dict, List, Optional, cast
66

77

8-
98
@dataclass
109
class PingResponse:
1110
""" """
11+
1212
success: bool
1313

1414
def to_dict(self) -> Dict:
@@ -20,6 +20,4 @@ def to_dict(self) -> Dict:
2020
def from_dict(d: Dict) -> PingResponse:
2121

2222
success = d["success"]
23-
return PingResponse(
24-
success=success,
25-
)
23+
return PingResponse(success=success,)

tests/test_end_to_end/test_end_to_end.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import os
22
import shutil
3-
from filecmp import dircmp, cmpfiles
3+
from filecmp import cmpfiles, dircmp
44
from pathlib import Path
55

66
import pytest
77
from typer.testing import CliRunner
8+
89
from openapi_python_client.cli import app
910

1011

0 commit comments

Comments
 (0)