Coverage for tests / test_tutorial / test_path_operation_advanced_configurations / test_tutorial007.py: 100%
28 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
8@pytest.fixture( 1abdc
9 name="client",
10 params=[
11 pytest.param("tutorial007_py310"),
12 ],
13)
14def get_client(request: pytest.FixtureRequest): 1abdc
15 mod = importlib.import_module( 1abc
16 f"docs_src.path_operation_advanced_configuration.{request.param}"
17 )
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23def test_post(client: TestClient): 1abdc
24 yaml_data = """ 1efg
25 name: Deadpoolio
26 tags:
27 - x-force
28 - x-men
29 - x-avengers
30 """
31 response = client.post("/items/", content=yaml_data) 1efg
32 assert response.status_code == 200, response.text 1efg
33 assert response.json() == { 1efg
34 "name": "Deadpoolio",
35 "tags": ["x-force", "x-men", "x-avengers"],
36 }
39def test_post_broken_yaml(client: TestClient): 1abdc
40 yaml_data = """ 1hij
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) 1hij
48 assert response.status_code == 422, response.text 1hij
49 assert response.json() == {"detail": "Invalid YAML"} 1hij
52def test_post_invalid(client: TestClient): 1abdc
53 yaml_data = """ 1klm
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) 1klm
62 assert response.status_code == 422, response.text 1klm
63 # insert_assert(response.json())
64 assert response.json() == { 1klm
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 }
76def test_openapi_schema(client: TestClient): 1abdc
77 response = client.get("/openapi.json") 1nop
78 assert response.status_code == 200, response.text 1nop
79 assert response.json() == snapshot( 1nop
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 )