|
| 1 | +from typing import List, Optional |
| 2 | + |
| 3 | +from dirty_equals import IsDict |
| 4 | +from fastapi import FastAPI, Form |
| 5 | +from fastapi.testclient import TestClient |
| 6 | +from pydantic import BaseModel |
| 7 | +from typing_extensions import Annotated |
| 8 | + |
| 9 | +app = FastAPI() |
| 10 | + |
| 11 | + |
| 12 | +class FormModel(BaseModel): |
| 13 | + username: str |
| 14 | + lastname: str |
| 15 | + age: Optional[int] = None |
| 16 | + tags: List[str] = ["foo", "bar"] |
| 17 | + |
| 18 | + |
| 19 | +@app.post("/form/") |
| 20 | +def post_form(user: Annotated[FormModel, Form()]): |
| 21 | + return user |
| 22 | + |
| 23 | + |
| 24 | +client = TestClient(app) |
| 25 | + |
| 26 | + |
| 27 | +def test_send_all_data(): |
| 28 | + response = client.post( |
| 29 | + "/form/", |
| 30 | + data={ |
| 31 | + "username": "Rick", |
| 32 | + "lastname": "Sanchez", |
| 33 | + "age": "70", |
| 34 | + "tags": ["plumbus", "citadel"], |
| 35 | + }, |
| 36 | + ) |
| 37 | + assert response.status_code == 200, response.text |
| 38 | + assert response.json() == { |
| 39 | + "username": "Rick", |
| 40 | + "lastname": "Sanchez", |
| 41 | + "age": 70, |
| 42 | + "tags": ["plumbus", "citadel"], |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +def test_defaults(): |
| 47 | + response = client.post("/form/", data={"username": "Rick", "lastname": "Sanchez"}) |
| 48 | + assert response.status_code == 200, response.text |
| 49 | + assert response.json() == { |
| 50 | + "username": "Rick", |
| 51 | + "lastname": "Sanchez", |
| 52 | + "age": None, |
| 53 | + "tags": ["foo", "bar"], |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +def test_invalid_data(): |
| 58 | + response = client.post( |
| 59 | + "/form/", |
| 60 | + data={ |
| 61 | + "username": "Rick", |
| 62 | + "lastname": "Sanchez", |
| 63 | + "age": "seventy", |
| 64 | + "tags": ["plumbus", "citadel"], |
| 65 | + }, |
| 66 | + ) |
| 67 | + assert response.status_code == 422, response.text |
| 68 | + assert response.json() == IsDict( |
| 69 | + { |
| 70 | + "detail": [ |
| 71 | + { |
| 72 | + "type": "int_parsing", |
| 73 | + "loc": ["body", "age"], |
| 74 | + "msg": "Input should be a valid integer, unable to parse string as an integer", |
| 75 | + "input": "seventy", |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + ) | IsDict( |
| 80 | + # TODO: remove when deprecating Pydantic v1 |
| 81 | + { |
| 82 | + "detail": [ |
| 83 | + { |
| 84 | + "loc": ["body", "age"], |
| 85 | + "msg": "value is not a valid integer", |
| 86 | + "type": "type_error.integer", |
| 87 | + } |
| 88 | + ] |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def test_no_data(): |
| 94 | + response = client.post("/form/") |
| 95 | + assert response.status_code == 422, response.text |
| 96 | + assert response.json() == IsDict( |
| 97 | + { |
| 98 | + "detail": [ |
| 99 | + { |
| 100 | + "type": "missing", |
| 101 | + "loc": ["body", "username"], |
| 102 | + "msg": "Field required", |
| 103 | + "input": {"tags": ["foo", "bar"]}, |
| 104 | + }, |
| 105 | + { |
| 106 | + "type": "missing", |
| 107 | + "loc": ["body", "lastname"], |
| 108 | + "msg": "Field required", |
| 109 | + "input": {"tags": ["foo", "bar"]}, |
| 110 | + }, |
| 111 | + ] |
| 112 | + } |
| 113 | + ) | IsDict( |
| 114 | + # TODO: remove when deprecating Pydantic v1 |
| 115 | + { |
| 116 | + "detail": [ |
| 117 | + { |
| 118 | + "loc": ["body", "username"], |
| 119 | + "msg": "field required", |
| 120 | + "type": "value_error.missing", |
| 121 | + }, |
| 122 | + { |
| 123 | + "loc": ["body", "lastname"], |
| 124 | + "msg": "field required", |
| 125 | + "type": "value_error.missing", |
| 126 | + }, |
| 127 | + ] |
| 128 | + } |
| 129 | + ) |
0 commit comments