Fix parameterized tests with snapshots (#14875)

This commit is contained in:
Motov Yurii 2026-02-09 16:35:43 +01:00 committed by GitHub
parent cd31576d57
commit 227cb85a03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 28 additions and 29 deletions

View File

@ -3,7 +3,7 @@ from typing import Annotated
import pytest import pytest
from fastapi import FastAPI, Path from fastapi import FastAPI, Path
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import Is, snapshot
app = FastAPI() app = FastAPI()
@ -58,8 +58,8 @@ def test_schema(path: str, expected_name: str, expected_title: str):
[ [
{ {
"required": True, "required": True,
"schema": {"title": expected_title, "type": "string"}, "schema": {"title": Is(expected_title), "type": "string"},
"name": expected_name, "name": Is(expected_name),
"in": "path", "in": "path",
} }
] ]

View File

@ -3,7 +3,7 @@ import importlib
import pytest import pytest
from dirty_equals import IsList from dirty_equals import IsList
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import Is, snapshot
from ...utils import needs_py310 from ...utils import needs_py310
@ -212,7 +212,7 @@ def test_openapi_schema(client: TestClient, mod_name: str):
"title": "Tax", "title": "Tax",
"anyOf": [{"type": "number"}, {"type": "null"}], "anyOf": [{"type": "number"}, {"type": "null"}],
}, },
"tags": tags_schema, "tags": Is(tags_schema),
}, },
"required": [ "required": [
"name", "name",

View File

@ -2,7 +2,7 @@ import importlib
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import Is, snapshot
@pytest.fixture( @pytest.fixture(
@ -59,7 +59,7 @@ def test_openapi_schema(client: TestClient, mod_name: str):
"responses": { "responses": {
"200": { "200": {
"description": "Successful Response", "description": "Successful Response",
"content": response_content, "content": Is(response_content),
} }
}, },
"summary": "Read Items", "summary": "Read Items",

View File

@ -4,7 +4,7 @@ from textwrap import dedent
import pytest import pytest
from dirty_equals import IsList from dirty_equals import IsList
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import Is, snapshot
from ...utils import needs_py310 from ...utils import needs_py310
@ -75,7 +75,7 @@ def test_openapi_schema(client: TestClient, mod_name: str):
"/items/": { "/items/": {
"post": { "post": {
"summary": "Create an item", "summary": "Create an item",
"description": DESCRIPTIONS[mod_name], "description": Is(DESCRIPTIONS[mod_name]),
"operationId": "create_item_items__post", "operationId": "create_item_items__post",
"requestBody": { "requestBody": {
"content": { "content": {

View File

@ -3,7 +3,7 @@ import importlib
import pytest import pytest
from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot from inline_snapshot import Is, snapshot
from ...utils import needs_py310 from ...utils import needs_py310
@ -66,6 +66,23 @@ def test_query_params_str_validations_item_query_nonregexquery(client: TestClien
def test_openapi_schema(client: TestClient): def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json") response = client.get("/openapi.json")
assert response.status_code == 200, response.text assert response.status_code == 200, response.text
parameters_schema = {
"anyOf": [
{
"type": "string",
"minLength": 3,
"maxLength": 50,
"pattern": "^fixedquery$",
},
{"type": "null"},
],
"title": "Query string",
"description": "Query string for the items to search in the database that have a good match",
# See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34.
**({"deprecated": True} if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10) else {}),
}
assert response.json() == snapshot( assert response.json() == snapshot(
{ {
"openapi": "3.1.0", "openapi": "3.1.0",
@ -96,25 +113,7 @@ def test_openapi_schema(client: TestClient):
"description": "Query string for the items to search in the database that have a good match", "description": "Query string for the items to search in the database that have a good match",
"required": False, "required": False,
"deprecated": True, "deprecated": True,
"schema": { "schema": Is(parameters_schema),
"anyOf": [
{
"type": "string",
"minLength": 3,
"maxLength": 50,
"pattern": "^fixedquery$",
},
{"type": "null"},
],
"title": "Query string",
"description": "Query string for the items to search in the database that have a good match",
# See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34.
**(
{"deprecated": True}
if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10)
else {}
),
},
"name": "item-query", "name": "item-query",
"in": "query", "in": "query",
} }