Skip to content

Commit 227cb85

Browse files
authored
✅ Fix parameterized tests with snapshots (#14875)
1 parent cd31576 commit 227cb85

5 files changed

Lines changed: 28 additions & 29 deletions

File tree

tests/test_request_params/test_path/test_required_str.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from fastapi import FastAPI, Path
55
from fastapi.testclient import TestClient
6-
from inline_snapshot import snapshot
6+
from inline_snapshot import Is, snapshot
77

88
app = FastAPI()
99

@@ -58,8 +58,8 @@ def test_schema(path: str, expected_name: str, expected_title: str):
5858
[
5959
{
6060
"required": True,
61-
"schema": {"title": expected_title, "type": "string"},
62-
"name": expected_name,
61+
"schema": {"title": Is(expected_title), "type": "string"},
62+
"name": Is(expected_name),
6363
"in": "path",
6464
}
6565
]

tests/test_tutorial/test_body_nested_models/test_tutorial001_tutorial002_tutorial003.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from dirty_equals import IsList
55
from fastapi.testclient import TestClient
6-
from inline_snapshot import snapshot
6+
from inline_snapshot import Is, snapshot
77

88
from ...utils import needs_py310
99

@@ -212,7 +212,7 @@ def test_openapi_schema(client: TestClient, mod_name: str):
212212
"title": "Tax",
213213
"anyOf": [{"type": "number"}, {"type": "null"}],
214214
},
215-
"tags": tags_schema,
215+
"tags": Is(tags_schema),
216216
},
217217
"required": [
218218
"name",

tests/test_tutorial/test_custom_response/test_tutorial002_tutorial003_tutorial004.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44
from fastapi.testclient import TestClient
5-
from inline_snapshot import snapshot
5+
from inline_snapshot import Is, snapshot
66

77

88
@pytest.fixture(
@@ -59,7 +59,7 @@ def test_openapi_schema(client: TestClient, mod_name: str):
5959
"responses": {
6060
"200": {
6161
"description": "Successful Response",
62-
"content": response_content,
62+
"content": Is(response_content),
6363
}
6464
},
6565
"summary": "Read Items",

tests/test_tutorial/test_path_operation_configurations/test_tutorial003_tutorial004.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from dirty_equals import IsList
66
from fastapi.testclient import TestClient
7-
from inline_snapshot import snapshot
7+
from inline_snapshot import Is, snapshot
88

99
from ...utils import needs_py310
1010

@@ -75,7 +75,7 @@ def test_openapi_schema(client: TestClient, mod_name: str):
7575
"/items/": {
7676
"post": {
7777
"summary": "Create an item",
78-
"description": DESCRIPTIONS[mod_name],
78+
"description": Is(DESCRIPTIONS[mod_name]),
7979
"operationId": "create_item_items__post",
8080
"requestBody": {
8181
"content": {

tests/test_tutorial/test_query_params_str_validations/test_tutorial010.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE
55
from fastapi.testclient import TestClient
6-
from inline_snapshot import snapshot
6+
from inline_snapshot import Is, snapshot
77

88
from ...utils import needs_py310
99

@@ -66,6 +66,23 @@ def test_query_params_str_validations_item_query_nonregexquery(client: TestClien
6666
def test_openapi_schema(client: TestClient):
6767
response = client.get("/openapi.json")
6868
assert response.status_code == 200, response.text
69+
70+
parameters_schema = {
71+
"anyOf": [
72+
{
73+
"type": "string",
74+
"minLength": 3,
75+
"maxLength": 50,
76+
"pattern": "^fixedquery$",
77+
},
78+
{"type": "null"},
79+
],
80+
"title": "Query string",
81+
"description": "Query string for the items to search in the database that have a good match",
82+
# See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34.
83+
**({"deprecated": True} if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10) else {}),
84+
}
85+
6986
assert response.json() == snapshot(
7087
{
7188
"openapi": "3.1.0",
@@ -96,25 +113,7 @@ def test_openapi_schema(client: TestClient):
96113
"description": "Query string for the items to search in the database that have a good match",
97114
"required": False,
98115
"deprecated": True,
99-
"schema": {
100-
"anyOf": [
101-
{
102-
"type": "string",
103-
"minLength": 3,
104-
"maxLength": 50,
105-
"pattern": "^fixedquery$",
106-
},
107-
{"type": "null"},
108-
],
109-
"title": "Query string",
110-
"description": "Query string for the items to search in the database that have a good match",
111-
# See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34.
112-
**(
113-
{"deprecated": True}
114-
if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10)
115-
else {}
116-
),
117-
},
116+
"schema": Is(parameters_schema),
118117
"name": "item-query",
119118
"in": "query",
120119
}

0 commit comments

Comments
 (0)