Skip to content

Commit 0809514

Browse files
committed
Improve controller template
1 parent 327874a commit 0809514

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
import pytest
22
from fastapi.testclient import TestClient
3-
from {{package_name}}.app.asgi import get_app
3+
from {{package_name}}.app import get_application
44

55

6-
@pytest.fixture
7-
def client():
8-
# This is an example fixture for generated test sake.
9-
# By default there should be a 'app' fixture just like this under:
10-
# tests/unit/app/conftest.py
11-
app = get_app()
12-
with TestClient(app) as client:
13-
yield client
14-
{%- for endpoint, method in endpoints.items() %}
6+
class Test{{ controller_name.capitalize() }}Controller:
157

8+
@pytest.fixture
9+
def client(self):
10+
# This is an example fixture for generated test sake.
11+
# By default there should be a 'app_runner' fixture just like this under:
12+
# tests/unit/app/conftest.py
13+
app = get_application()
14+
with TestClient(app) as client:
15+
yield client
16+
{% for endpoint, method in endpoints.items() %}
17+
def test_should{{ endpoint.lower().replace('-','_') }}_return_ok(self, client):
18+
# given / when
19+
response = client.{{ method.lower() }}("/api/{{ controller_name }}/{{ endpoint.lower().replace('-','_') }}")
1620

17-
def test_{{ endpoint.lower().replace('-','_') }}(client):
18-
response = client.{{ method.lower() }}("/{{ controller_name }}/{{ endpoint.lower().replace('-','_') }}")
19-
assert response.status_code == 200
20-
assert response.json() == {"hello": "world"}
21-
22-
23-
{%- endfor %}
24-
{%- raw %}
25-
26-
{% endraw %}
21+
# then
22+
assert response.status_code == 200
23+
assert response.json() == {"hello": "world"}
24+
{% endfor %}

template/{{package_name}}/app/controllers/{{controller_name}}.py.jinja

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import logging
44
from fastapi import APIRouter
55

66

7-
router = APIRouter(
8-
prefix="/{{ controller_name }}"
9-
)
7+
router = APIRouter(prefix="/{{ controller_name }}")
108
log = logging.getLogger(__name__)
11-
{%- for endpoint, method in endpoints.items() %}
12-
9+
{% for endpoint, method in endpoints.items() %}
1310

1411
@router.{{ method.lower() }}(
1512
"/{{ endpoint.lower().replace('-','_') }}",
@@ -18,11 +15,7 @@ log = logging.getLogger(__name__)
1815
# https://fastapi.tiangolo.com/tutorial/path-operation-configuration/
1916
)
2017
async def {{ endpoint.lower().replace('-','_') }}():
18+
"""Define {{ controller_name.lower().replace('_', ' ') }} {{ endpoint.lower() }} endpoint."""
2119
# Implement endpoint logic here.
2220
return {"hello": "world"}
23-
24-
25-
{%- endfor %}
26-
{%- raw %}
27-
28-
{% endraw %}
21+
{% endfor %}

0 commit comments

Comments
 (0)