Skip to content

Commit 96c7d6c

Browse files
committed
Start fixing CircleCI config
Adds first test. Progress for #3
1 parent 66e5494 commit 96c7d6c

File tree

5 files changed

+48
-36
lines changed

5 files changed

+48
-36
lines changed

.circleci/config.yml

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,22 @@ jobs:
88
keys:
99
- v1-dependencies-{{ checksum "poetry.lock" }}
1010
- run:
11-
command: 'pip install poetry --user --upgrade
12-
11+
name: install dependencies
12+
command: |
13+
pip install poetry --user --upgrade
1314
poetry config virtualenvs.in-project true
14-
15-
poetry config repositories.triaxtec https://pypi.fury.io/triaxtec/
16-
17-
poetry config http-basic.triaxtec $GEMFURY_PULL_TOKEN $GEMFURY_PULL_TOKEN
18-
1915
poetry install
2016
21-
'
22-
name: install dependencies
2317
- run:
24-
command: 'mkdir -p test-reports/safety test-reports/mypy test-reports/pytest
25-
26-
poetry run black . --check
27-
28-
poetry run safety check --json > test-reports/safety/results.json
29-
30-
poetry run mypy openapi_python_client --junit-xml=test-reports/mypy/results.xml
31-
32-
poetry run pytest --junitxml=test-reports/pytest/results.xml
33-
34-
'
3518
name: run tests
19+
command: |
20+
mkdir -p test-reports/safety test-reports/mypy test-reports/pytest
21+
poetry shell
22+
black . --check
23+
isort
24+
safety check --json > test-reports/safety/results.json
25+
mypy openapi_python_client --junit-xml=test-reports/mypy/results.xml
26+
pytest --junitxml=test-reports/pytest/results.xml
3627
- store_test_results:
3728
path: test-reports
3829
- run:

openapi_python_client/__init__.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" Generate modern Python clients from OpenAPI """
22
from pathlib import Path
3-
from typing import Any, Dict
3+
from typing import Any, Dict, Optional
44

55
import orjson
66
import requests
@@ -9,24 +9,24 @@
99
from .openapi_parser import OpenAPI, import_string_from_reference
1010

1111

12-
def main(url: str) -> None:
12+
def main(*, url: Optional[str], path: Optional[str]) -> None:
1313
""" Generate the client library """
14-
json = _get_json(url)
15-
data_dict = _parse_json(json)
14+
data_dict = _get_json(url=url, path=path)
1615
openapi = OpenAPI.from_dict(data_dict)
17-
_build_project(openapi)
16+
_build_project(openapi=openapi)
1817

1918

20-
def _get_json(url: str) -> bytes:
21-
response = requests.get(url)
22-
return response.content
19+
def _get_json(*, url: Optional[str], path: Optional[str]) -> Dict[str, Any]:
20+
json_bytes: bytes
21+
if url:
22+
response = requests.get(url)
23+
json_bytes = response.content
24+
else:
25+
json_bytes = Path(path).read_bytes()
26+
return orjson.loads(json_bytes)
2327

2428

25-
def _parse_json(json: bytes) -> Dict[str, Any]:
26-
return orjson.loads(json)
27-
28-
29-
def _build_project(openapi: OpenAPI) -> None:
29+
def _build_project(*, openapi: OpenAPI) -> None:
3030
env = Environment(loader=PackageLoader(__package__), trim_blocks=True, lstrip_blocks=True)
3131

3232
# Create output directories

openapi_python_client/cli.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import click
24

35
from . import main
@@ -10,7 +12,11 @@ def cli():
1012

1113

1214
@cli.command()
13-
@click.option("--url", required=True, help="The URL to the openapi.json file")
14-
def generate(url: str):
15+
@click.option("--url", help="The URL to the openapi.json file")
16+
@click.option("--path", help="The path to the openapi.json file")
17+
def generate(url: Optional[str], path: Optional[str]):
1518
""" Generate a new OpenAPI Client library """
16-
main(url=url)
19+
if not url and not path:
20+
print("You must either provide --url or --path")
21+
exit(1)
22+
main(url=url, path=path)

tests/__init__.py

Whitespace-only changes.

tests/test___init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def test_main(mocker):
2+
data_dict = mocker.MagicMock()
3+
_get_json = mocker.patch("openapi_python_client._get_json", return_value=data_dict)
4+
openapi = mocker.MagicMock()
5+
from_dict = mocker.patch("openapi_python_client.openapi_parser.OpenAPI.from_dict", return_value=openapi)
6+
_build_project = mocker.patch("openapi_python_client._build_project")
7+
url = mocker.MagicMock()
8+
path = mocker.MagicMock()
9+
10+
from openapi_python_client import main
11+
main(url=url, path=path)
12+
13+
_get_json.assert_called_once_with(url=url, path=path)
14+
from_dict.assert_called_once_with(data_dict)
15+
_build_project.assert_called_once_with(openapi=openapi)

0 commit comments

Comments
 (0)