| 1 |
import pytest |
| 2 |
from fastapi import FastAPI |
| 3 |
from fastapi.testclient import TestClient |
| 4 |
from inline_snapshot import snapshot |
| 5 |
|
| 6 |
|
| 7 |
@pytest.fixture(name="client") |
| 8 |
def client_fixture() -> TestClient: |
| 9 |
from pydantic import BaseModel |
| 10 |
|
| 11 |
class Address(BaseModel): |
| 12 |
""" |
| 13 |
This is a public description of an Address |
| 14 |
\f |
| 15 |
You can't see this part of the docstring, it's private! |
| 16 |
""" |
| 17 |
|
| 18 |
line_1: str |
| 19 |
city: str |
| 20 |
state_province: str |
| 21 |
|
| 22 |
class Facility(BaseModel): |
| 23 |
id: str |
| 24 |
address: Address |
| 25 |
|
| 26 |
app = FastAPI() |
| 27 |
|
| 28 |
@app.get("/facilities/{facility_id}") |
| 29 |
def get_facility(facility_id: str) -> Facility: |
| 30 |
return Facility( |
| 31 |
id=facility_id, |
| 32 |
address=Address(line_1="123 Main St", city="Anytown", state_province="CA"), |
| 33 |
) |
| 34 |
|
| 35 |
client = TestClient(app) |
| 36 |
return client |
| 37 |
|
| 38 |
|
| 39 |
def test_get(client: TestClient): |
| 40 |
response = client.get("/facilities/42") |
| 41 |
assert response.status_code == 200, response.text |
| 42 |
assert response.json() == { |
| 43 |
"id": "42", |
| 44 |
"address": { |
| 45 |
"line_1": "123 Main St", |
| 46 |
"city": "Anytown", |
| 47 |
"state_province": "CA", |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
def test_openapi_schema(client: TestClient): |
| 53 |
""" |
| 54 |
Sanity check to ensure our app's openapi schema renders as we expect |
| 55 |
""" |
| 56 |
response = client.get("/openapi.json") |
| 57 |
assert response.status_code == 200, response.text |
| 58 |
assert response.json() == snapshot( |
| 59 |
{ |
| 60 |
"components": { |
| 61 |
"schemas": { |
| 62 |
"Address": { |
| 63 |
# NOTE: the description of this model shows only the public-facing text, before the `\f` in docstring |
| 64 |
"description": "This is a public description of an Address\n", |
| 65 |
"properties": { |
| 66 |
"city": {"title": "City", "type": "string"}, |
| 67 |
"line_1": {"title": "Line 1", "type": "string"}, |
| 68 |
"state_province": { |
| 69 |
"title": "State Province", |
| 70 |
"type": "string", |
| 71 |
}, |
| 72 |
}, |
| 73 |
"required": ["line_1", "city", "state_province"], |
| 74 |
"title": "Address", |
| 75 |
"type": "object", |
| 76 |
}, |
| 77 |
"Facility": { |
| 78 |
"properties": { |
| 79 |
"address": {"$ref": "#/components/schemas/Address"}, |
| 80 |
"id": {"title": "Id", "type": "string"}, |
| 81 |
}, |
| 82 |
"required": ["id", "address"], |
| 83 |
"title": "Facility", |
| 84 |
"type": "object", |
| 85 |
}, |
| 86 |
"HTTPValidationError": { |
| 87 |
"properties": { |
| 88 |
"detail": { |
| 89 |
"items": { |
| 90 |
"$ref": "#/components/schemas/ValidationError" |
| 91 |
}, |
| 92 |
"title": "Detail", |
| 93 |
"type": "array", |
| 94 |
} |
| 95 |
}, |
| 96 |
"title": "HTTPValidationError", |
| 97 |
"type": "object", |
| 98 |
}, |
| 99 |
"ValidationError": { |
| 100 |
"properties": { |
| 101 |
"ctx": {"title": "Context", "type": "object"}, |
| 102 |
"input": {"title": "Input"}, |
| 103 |
"loc": { |
| 104 |
"items": { |
| 105 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 106 |
}, |
| 107 |
"title": "Location", |
| 108 |
"type": "array", |
| 109 |
}, |
| 110 |
"msg": {"title": "Message", "type": "string"}, |
| 111 |
"type": {"title": "Error Type", "type": "string"}, |
| 112 |
}, |
| 113 |
"required": ["loc", "msg", "type"], |
| 114 |
"title": "ValidationError", |
| 115 |
"type": "object", |
| 116 |
}, |
| 117 |
} |
| 118 |
}, |
| 119 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 120 |
"openapi": "3.1.0", |
| 121 |
"paths": { |
| 122 |
"/facilities/{facility_id}": { |
| 123 |
"get": { |
| 124 |
"operationId": "get_facility_facilities__facility_id__get", |
| 125 |
"parameters": [ |
| 126 |
{ |
| 127 |
"in": "path", |
| 128 |
"name": "facility_id", |
| 129 |
"required": True, |
| 130 |
"schema": {"title": "Facility Id", "type": "string"}, |
| 131 |
} |
| 132 |
], |
| 133 |
"responses": { |
| 134 |
"200": { |
| 135 |
"content": { |
| 136 |
"application/json": { |
| 137 |
"schema": { |
| 138 |
"$ref": "#/components/schemas/Facility" |
| 139 |
} |
| 140 |
} |
| 141 |
}, |
| 142 |
"description": "Successful Response", |
| 143 |
}, |
| 144 |
"422": { |
| 145 |
"content": { |
| 146 |
"application/json": { |
| 147 |
"schema": { |
| 148 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 149 |
} |
| 150 |
} |
| 151 |
}, |
| 152 |
"description": "Validation Error", |
| 153 |
}, |
| 154 |
}, |
| 155 |
"summary": "Get Facility", |
| 156 |
} |
| 157 |
} |
| 158 |
}, |
| 159 |
} |
| 160 |
) |