| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture( |
| 9 |
name="client", |
| 10 |
params=[ |
| 11 |
pytest.param("tutorial007_py310"), |
| 12 |
], |
| 13 |
) |
| 14 |
def get_client(request: pytest.FixtureRequest): |
| 15 |
mod = importlib.import_module( |
| 16 |
f"docs_src.path_operation_advanced_configuration.{request.param}" |
| 17 |
) |
| 18 |
|
| 19 |
client = TestClient(mod.app) |
| 20 |
return client |
| 21 |
|
| 22 |
|
| 23 |
def test_post(client: TestClient): |
| 24 |
yaml_data = """ |
| 25 |
name: Deadpoolio |
| 26 |
tags: |
| 27 |
- x-force |
| 28 |
- x-men |
| 29 |
- x-avengers |
| 30 |
""" |
| 31 |
response = client.post("/items/", content=yaml_data) |
| 32 |
assert response.status_code == 200, response.text |
| 33 |
assert response.json() == { |
| 34 |
"name": "Deadpoolio", |
| 35 |
"tags": ["x-force", "x-men", "x-avengers"], |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
def test_post_broken_yaml(client: TestClient): |
| 40 |
yaml_data = """ |
| 41 |
name: Deadpoolio |
| 42 |
tags: |
| 43 |
x - x-force |
| 44 |
x - x-men |
| 45 |
x - x-avengers |
| 46 |
""" |
| 47 |
response = client.post("/items/", content=yaml_data) |
| 48 |
assert response.status_code == 422, response.text |
| 49 |
assert response.json() == {"detail": "Invalid YAML"} |
| 50 |
|
| 51 |
|
| 52 |
def test_post_invalid(client: TestClient): |
| 53 |
yaml_data = """ |
| 54 |
name: Deadpoolio |
| 55 |
tags: |
| 56 |
- x-force |
| 57 |
- x-men |
| 58 |
- x-avengers |
| 59 |
- sneaky: object |
| 60 |
""" |
| 61 |
response = client.post("/items/", content=yaml_data) |
| 62 |
assert response.status_code == 422, response.text |
| 63 |
# insert_assert(response.json()) |
| 64 |
assert response.json() == { |
| 65 |
"detail": [ |
| 66 |
{ |
| 67 |
"type": "string_type", |
| 68 |
"loc": ["tags", 3], |
| 69 |
"msg": "Input should be a valid string", |
| 70 |
"input": {"sneaky": "object"}, |
| 71 |
} |
| 72 |
] |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
def test_openapi_schema(client: TestClient): |
| 77 |
response = client.get("/openapi.json") |
| 78 |
assert response.status_code == 200, response.text |
| 79 |
assert response.json() == snapshot( |
| 80 |
{ |
| 81 |
"openapi": "3.1.0", |
| 82 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 83 |
"paths": { |
| 84 |
"/items/": { |
| 85 |
"post": { |
| 86 |
"summary": "Create Item", |
| 87 |
"operationId": "create_item_items__post", |
| 88 |
"requestBody": { |
| 89 |
"content": { |
| 90 |
"application/x-yaml": { |
| 91 |
"schema": { |
| 92 |
"title": "Item", |
| 93 |
"required": ["name", "tags"], |
| 94 |
"type": "object", |
| 95 |
"properties": { |
| 96 |
"name": {"title": "Name", "type": "string"}, |
| 97 |
"tags": { |
| 98 |
"title": "Tags", |
| 99 |
"type": "array", |
| 100 |
"items": {"type": "string"}, |
| 101 |
}, |
| 102 |
}, |
| 103 |
} |
| 104 |
} |
| 105 |
}, |
| 106 |
"required": True, |
| 107 |
}, |
| 108 |
"responses": { |
| 109 |
"200": { |
| 110 |
"description": "Successful Response", |
| 111 |
"content": {"application/json": {"schema": {}}}, |
| 112 |
} |
| 113 |
}, |
| 114 |
} |
| 115 |
} |
| 116 |
}, |
| 117 |
} |
| 118 |
) |