-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconftest.py
More file actions
83 lines (63 loc) · 1.9 KB
/
Copy pathconftest.py
File metadata and controls
83 lines (63 loc) · 1.9 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
from unittest.mock import Mock
import httpx
from asgi_lifespan import LifespanManager
from fastapi import Depends, FastAPI, HTTPException
from pydantic import BaseModel
from pytest import fixture
from sqlalchemy import text
@fixture(scope="module", autouse=True)
def setup_tear_down(sqla_connection):
with sqla_connection.begin():
sqla_connection.execute(
text(
"""
CREATE TABLE IF NOT EXISTS public.user (
id integer primary key,
first_name varchar,
last_name varchar
)
"""
)
)
yield
with sqla_connection.begin():
sqla_connection.execute(text("DROP TABLE public.user"))
@fixture
def User():
from fastapi_sqla import Base
class User(Base):
__tablename__ = "user"
return User
@fixture
def app(User):
from fastapi_sqla import Session, setup
app = FastAPI()
setup(app)
class UserIn(BaseModel):
id: int
first_name: str
last_name: str
@app.post("/users")
def create_user(user: UserIn, session: Session = Depends()):
session.add(User(**dict(user)))
@app.get("/404")
def get_users(session: Session = Depends(Session)):
raise HTTPException(status_code=404, detail="YOLO")
return app
@fixture
def mock_middleware(app: FastAPI):
mock_middleware = Mock()
@app.middleware("http")
async def a_middleware(request, call_next):
res = await call_next(request)
mock_middleware()
return res
return mock_middleware
@fixture
async def client(app, mock_middleware):
async with LifespanManager(app):
transport = httpx.ASGITransport(app=app, raise_app_exceptions=False)
async with httpx.AsyncClient(
transport=transport, base_url="http://example.local"
) as client:
yield client