| 1 |
import importlib |
| 2 |
from types import ModuleType |
| 3 |
|
| 4 |
import pytest |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
|
| 8 |
from ...utils import needs_py310 |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture( |
| 12 |
name="mod", |
| 13 |
params=[ |
| 14 |
pytest.param("tutorial001_py310", marks=needs_py310), |
| 15 |
pytest.param("tutorial001_an_py310", marks=needs_py310), |
| 16 |
], |
| 17 |
) |
| 18 |
def get_mod(request: pytest.FixtureRequest): |
| 19 |
mod = importlib.import_module(f"docs_src.cookie_params.{request.param}") |
| 20 |
|
| 21 |
return mod |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.parametrize( |
| 25 |
"path,cookies,expected_status,expected_response", |
| 26 |
[ |
| 27 |
("/items", None, 200, {"ads_id": None}), |
| 28 |
("/items", {"ads_id": "ads_track"}, 200, {"ads_id": "ads_track"}), |
| 29 |
( |
| 30 |
"/items", |
| 31 |
{"ads_id": "ads_track", "session": "cookiesession"}, |
| 32 |
200, |
| 33 |
{"ads_id": "ads_track"}, |
| 34 |
), |
| 35 |
("/items", {"session": "cookiesession"}, 200, {"ads_id": None}), |
| 36 |
], |
| 37 |
) |
| 38 |
def test(path, cookies, expected_status, expected_response, mod: ModuleType): |
| 39 |
client = TestClient(mod.app, cookies=cookies) |
| 40 |
response = client.get(path) |
| 41 |
assert response.status_code == expected_status |
| 42 |
assert response.json() == expected_response |
| 43 |
|
| 44 |
|
| 45 |
def test_openapi_schema(mod: ModuleType): |
| 46 |
client = TestClient(mod.app) |
| 47 |
response = client.get("/openapi.json") |
| 48 |
assert response.status_code == 200 |
| 49 |
assert response.json() == snapshot( |
| 50 |
{ |
| 51 |
"openapi": "3.1.0", |
| 52 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 53 |
"paths": { |
| 54 |
"/items/": { |
| 55 |
"get": { |
| 56 |
"responses": { |
| 57 |
"200": { |
| 58 |
"description": "Successful Response", |
| 59 |
"content": {"application/json": {"schema": {}}}, |
| 60 |
}, |
| 61 |
"422": { |
| 62 |
"description": "Validation Error", |
| 63 |
"content": { |
| 64 |
"application/json": { |
| 65 |
"schema": { |
| 66 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 67 |
} |
| 68 |
} |
| 69 |
}, |
| 70 |
}, |
| 71 |
}, |
| 72 |
"summary": "Read Items", |
| 73 |
"operationId": "read_items_items__get", |
| 74 |
"parameters": [ |
| 75 |
{ |
| 76 |
"required": False, |
| 77 |
"schema": { |
| 78 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 79 |
"title": "Ads Id", |
| 80 |
}, |
| 81 |
"name": "ads_id", |
| 82 |
"in": "cookie", |
| 83 |
} |
| 84 |
], |
| 85 |
} |
| 86 |
} |
| 87 |
}, |
| 88 |
"components": { |
| 89 |
"schemas": { |
| 90 |
"ValidationError": { |
| 91 |
"title": "ValidationError", |
| 92 |
"required": ["loc", "msg", "type"], |
| 93 |
"type": "object", |
| 94 |
"properties": { |
| 95 |
"loc": { |
| 96 |
"title": "Location", |
| 97 |
"type": "array", |
| 98 |
"items": { |
| 99 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 100 |
}, |
| 101 |
}, |
| 102 |
"msg": {"title": "Message", "type": "string"}, |
| 103 |
"type": {"title": "Error Type", "type": "string"}, |
| 104 |
"input": {"title": "Input"}, |
| 105 |
"ctx": {"title": "Context", "type": "object"}, |
| 106 |
}, |
| 107 |
}, |
| 108 |
"HTTPValidationError": { |
| 109 |
"title": "HTTPValidationError", |
| 110 |
"type": "object", |
| 111 |
"properties": { |
| 112 |
"detail": { |
| 113 |
"title": "Detail", |
| 114 |
"type": "array", |
| 115 |
"items": { |
| 116 |
"$ref": "#/components/schemas/ValidationError" |
| 117 |
}, |
| 118 |
} |
| 119 |
}, |
| 120 |
}, |
| 121 |
} |
| 122 |
}, |
| 123 |
} |
| 124 |
) |