Browse Source

Add test for `docs_src.path_params.tutorial001.py`

pull/14569/head
Yurii Motov 7 months ago
parent
commit
07dbe03d22
  1. 116
      tests/test_tutorial/test_path_params/test_tutorial001.py

116
tests/test_tutorial/test_path_params/test_tutorial001.py

@ -0,0 +1,116 @@
import pytest
from fastapi.testclient import TestClient
from docs_src.path_params.tutorial001 import app
client = TestClient(app)
@pytest.mark.parametrize(
("item_id", "expected_response"),
[
(1, {"item_id": "1"}),
("alice", {"item_id": "alice"}),
],
)
def test_get_items(item_id, expected_response):
response = client.get(f"/items/{item_id}")
assert response.status_code == 200, response.text
assert response.json() == expected_response
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/{item_id}": {
"get": {
"operationId": "read_item_items__item_id__get",
"parameters": [
{
"in": "path",
"name": "item_id",
"required": True,
"schema": {
"title": "Item Id",
},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {},
},
},
"description": "Successful Response",
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError",
},
},
},
"description": "Validation Error",
},
},
"summary": "Read Item",
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError",
},
"title": "Detail",
"type": "array",
},
},
"title": "HTTPValidationError",
"type": "object",
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string",
},
{
"type": "integer",
},
],
},
"title": "Location",
"type": "array",
},
"msg": {
"title": "Message",
"type": "string",
},
"type": {
"title": "Error Type",
"type": "string",
},
},
"required": [
"loc",
"msg",
"type",
],
"title": "ValidationError",
"type": "object",
},
},
},
}
Loading…
Cancel
Save