forked from fastapiutils/fastapi-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cbv.py
More file actions
149 lines (116 loc) · 4.73 KB
/
test_cbv.py
File metadata and controls
149 lines (116 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from __future__ import annotations
from typing import Any, ClassVar, Optional
import pytest
from fastapi import APIRouter, Depends, Request
from starlette.testclient import TestClient
from fastapi_utils.cbv import cbv
class TestCBV:
@pytest.fixture(autouse=True)
def router(self) -> APIRouter:
return APIRouter()
def test_response_models(self, router: APIRouter) -> None:
expected_response = "home"
@cbv(router)
class CBV:
def __init__(self) -> None:
self.one = 1
self.two = 2
@router.get("/", response_model=str)
def string_response(self) -> str:
return expected_response
@router.get("/sum", response_model=int)
def int_response(self) -> int:
return self.one + self.two
client = TestClient(router)
response_1 = client.get("/")
assert response_1.status_code == 200
assert response_1.json() == expected_response
response_2 = client.get("/sum")
assert response_2.status_code == 200
assert response_2.content == b"3"
def test_dependencies(self, router: APIRouter) -> None:
def dependency_one() -> int:
return 1
def dependency_two() -> int:
return 2
@cbv(router)
class CBV:
one: int = Depends(dependency_one)
def __init__(self, two: int = Depends(dependency_two)):
self.two = two
@router.get("/", response_model=int)
def int_dependencies(self) -> int:
return self.one + self.two
client = TestClient(router)
response = client.get("/")
assert response.status_code == 200
assert response.content == b"3"
def test_class_var(self, router: APIRouter) -> None:
@cbv(router)
class CBV:
class_var: ClassVar[int]
@router.get("/", response_model=bool)
def g(self) -> bool:
return hasattr(self, "class_var")
client = TestClient(router)
response = client.get("/")
assert response.status_code == 200
assert response.content == b"false"
def test_routes_path_order_preserved(self, router: APIRouter) -> None:
@cbv(router)
class CBV:
@router.get("/test")
def get_test(self) -> int:
return 1
@router.get("/{any_path}")
def get_any_path(self) -> int: # Alphabetically before `get_test`
return 2
client = TestClient(router)
assert client.get("/test").json() == 1
assert client.get("/any_other_path").json() == 2
def test_multiple_paths(self, router: APIRouter) -> None:
@cbv(router)
class CBV:
@router.get("/items")
@router.get("/items/{custom_path:path}")
@router.get("/database/{custom_path:path}")
def root(self, custom_path: Optional[str] = None) -> Any:
return {"custom_path": custom_path} if custom_path else []
client = TestClient(router)
assert client.get("/items").json() == []
assert client.get("/items/1").json() == {"custom_path": "1"}
assert client.get("/database/abc").json() == {"custom_path": "abc"}
def test_query_parameters(self, router: APIRouter) -> None:
@cbv(router)
class CBV:
@router.get("/route")
def root(self, param: Optional[int] = None) -> int:
return param if param else 0
client = TestClient(router)
assert client.get("/route").json() == 0
assert client.get("/route?param=3").json() == 3
def test_prefix(self) -> None:
router = APIRouter(prefix="/api")
@cbv(router)
class CBV:
@router.get("/item")
def root(self) -> str:
return "hello"
client = TestClient(router)
response = client.get("/api/item")
assert response.status_code == 200
assert response.json() == "hello"
def test_url_for(self, router: APIRouter) -> None:
@cbv(router)
class Foo:
@router.get("/foo")
def example(self, request: Request) -> str:
return str(request.url_for("Bar.example"))
@cbv(router)
class Bar:
@router.get("/bar")
def example(self, request: Request) -> str:
return str(request.url_for("Foo.example"))
client = TestClient(router)
response = client.get("/foo")
assert response.json() == "http://testserver/bar"