Motov Yurii
1 week ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with
3 additions and
8 deletions
-
tests/test_response_model_default_factory.py
|
|
@ -1,18 +1,13 @@ |
|
|
|
import functools |
|
|
|
import random |
|
|
|
|
|
|
|
from fastapi import FastAPI |
|
|
|
from fastapi.testclient import TestClient |
|
|
|
from pydantic import BaseModel, Field |
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
messages = ["操作成功.", "Successful operation."] |
|
|
|
|
|
|
|
|
|
|
|
class ResponseModel(BaseModel): |
|
|
|
code: int = 200 |
|
|
|
message: str = Field(default_factory=functools.partial(random.choice, messages)) |
|
|
|
message: str = Field(default_factory=lambda: "Successful operation.") |
|
|
|
|
|
|
|
|
|
|
|
@app.get( |
|
|
@ -40,7 +35,7 @@ def test_response_model_has_default_factory_return_dict(): |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
|
|
|
|
assert response.json()["code"] == 200 |
|
|
|
assert response.json()["message"] in messages |
|
|
|
assert response.json()["message"] == "Successful operation." |
|
|
|
|
|
|
|
|
|
|
|
def test_response_model_has_default_factory_return_model(): |
|
|
@ -49,4 +44,4 @@ def test_response_model_has_default_factory_return_model(): |
|
|
|
assert response.status_code == 200, response.text |
|
|
|
|
|
|
|
assert response.json()["code"] == 200 |
|
|
|
assert response.json()["message"] in messages |
|
|
|
assert response.json()["message"] == "Successful operation." |
|
|
|