refactor unit test to avoid Python check twice

This commit is contained in:
svlandeg 2024-08-23 16:35:37 +02:00
parent fd9034e05e
commit 9b1cbc5b28
3 changed files with 30 additions and 33 deletions

View File

@ -1,34 +1,32 @@
from __future__ import annotations
import sys
from typing import Optional
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
from ..utils import needs_py310
from .login_tool import login_required
if sys.version_info > (3, 10):
from typing import Optional
app = FastAPI()
client = TestClient(app)
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
from .loging_tool import login_required
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
app = FastAPI()
client = TestClient(app)
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
@app.get("/items/")
@login_required
def get_item(id: int) -> Item:
return Item(name="name", price=42.42)
@app.get("/items/")
@login_required
def get_item(item_id: int) -> Item:
return Item(name="name", price=42.42)
@needs_py310
def test_future_6465():
res = client.get("/items?id=3")
res = client.get("/items?item_id=3")
assert res.status_code == 200

View File

@ -1,25 +1,24 @@
from __future__ import annotations
import sys
from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient
from starlette.requests import Request
from ..utils import needs_py310
if sys.version_info > (3, 10):
from fastapi import Depends, FastAPI
from fastapi.testclient import TestClient
from starlette.requests import Request
app = FastAPI()
app = FastAPI()
client = TestClient(app)
client = TestClient(app)
class Test:
def __call__(self, request: Request):
return "test"
class Test:
def __call__(self, request: Request):
return "test"
@app.get("/test/")
def call(test: str = Depends(Test())):
return {"test": test}
@app.get("/test/")
def call(test: str = Depends(Test())):
return {"test": test}
@needs_py310