Skip to content

Commit 3c832dd

Browse files
authored
Merge pull request #1 from fastapi-mvc/improve_template
Improve controller template
2 parents 327874a + 84533e0 commit 3c832dd

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
This file documents changes to [fastapi-mvc/copier-controller](https://github.com/fastapi-mvc/copier-controller). The release numbering uses [semantic versioning](http://semver.org).
44

5+
## 0.2.0 (01.01.2023)
6+
7+
* Improve controller template. PR [#1](https://github.com/fastapi-mvc/copier-controller/pull/1)
58

69
## 0.1.0 (09.10.2022)
710

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

+4-11
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)