Browse Source

Use `snapshot` in tests

pull/14874/head
Yurii Motov 4 months ago
parent
commit
1a18de1153
  1. 173
      tests/test_nested_annotated_in_sequence.py

173
tests/test_nested_annotated_in_sequence.py

@ -3,6 +3,7 @@ from typing import Annotated
from dirty_equals import IsList from dirty_equals import IsList
from fastapi import FastAPI, Query from fastapi import FastAPI, Query
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from pydantic import Field from pydantic import Field
MaxSizedSet = Annotated[set[str], Field(max_length=3)] MaxSizedSet = Annotated[set[str], Field(max_length=3)]
@ -33,104 +34,110 @@ def test_endpoint_valid():
def test_endpoint_too_long(): def test_endpoint_too_long():
response = client.get("/", params={"foo": ["a", "b", "c", "d"]}) response = client.get("/", params={"foo": ["a", "b", "c", "d"]})
assert response.status_code == 422 assert response.status_code == 422
assert response.json() == { assert response.json() == snapshot(
"detail": [ {
{ "detail": [
"type": "too_long", {
"loc": ["query", "foo"], "type": "too_long",
"msg": "Set should have at most 3 items after validation, not more", "loc": ["query", "foo"],
"input": IsList("a", "b", "c", "d", check_order=False), "msg": "Set should have at most 3 items after validation, not more",
"ctx": { "input": IsList("a", "b", "c", "d", check_order=False),
"actual_length": None, "ctx": {
"field_type": "Set", "actual_length": None,
"max_length": 3, "field_type": "Set",
}, "max_length": 3,
} },
] }
} ]
}
)
def test_openapi(): def test_openapi():
assert app.openapi() == { assert app.openapi() == snapshot(
"components": { {
"schemas": { "components": {
"HTTPValidationError": { "schemas": {
"properties": { "HTTPValidationError": {
"detail": { "properties": {
"items": {"$ref": "#/components/schemas/ValidationError"}, "detail": {
"title": "Detail", "items": {
"type": "array", "$ref": "#/components/schemas/ValidationError"
},
"title": "Detail",
"type": "array",
},
}, },
"title": "HTTPValidationError",
"type": "object",
}, },
"title": "HTTPValidationError", "ValidationError": {
"type": "object", "properties": {
}, "ctx": {"title": "Context", "type": "object"},
"ValidationError": { "input": {"title": "Input"},
"properties": { "loc": {
"ctx": {"title": "Context", "type": "object"}, "items": {
"input": {"title": "Input"}, "anyOf": [{"type": "string"}, {"type": "integer"}],
"loc": { },
"items": { "title": "Location",
"anyOf": [{"type": "string"}, {"type": "integer"}], "type": "array",
}, },
"title": "Location", "msg": {"title": "Message", "type": "string"},
"type": "array", "type": {"title": "Error Type", "type": "string"},
}, },
"msg": {"title": "Message", "type": "string"}, "required": ["loc", "msg", "type"],
"type": {"title": "Error Type", "type": "string"}, "title": "ValidationError",
"type": "object",
}, },
"required": ["loc", "msg", "type"],
"title": "ValidationError",
"type": "object",
}, },
}, },
}, "info": {
"info": { "title": "FastAPI",
"title": "FastAPI", "version": "0.1.0",
"version": "0.1.0", },
}, "openapi": "3.1.0",
"openapi": "3.1.0", "paths": {
"paths": { "/": {
"/": { "get": {
"get": { "operationId": "read_root__get",
"operationId": "read_root__get", "parameters": [
"parameters": [ {
{ "in": "query",
"in": "query", "name": "foo",
"name": "foo", "required": False,
"required": False, "schema": {
"schema": { "anyOf": [
"anyOf": [ {
{ "items": {"type": "string"},
"items": {"type": "string"}, "maxItems": 3,
"maxItems": 3, "type": "array",
"type": "array", "uniqueItems": True,
"uniqueItems": True, },
}, {"type": "null"},
{"type": "null"}, ],
], "title": "Foo",
"title": "Foo", },
}, },
}, ],
], "responses": {
"responses": { "200": {
"200": { "content": {"application/json": {"schema": {}}},
"content": {"application/json": {"schema": {}}}, "description": "Successful Response",
"description": "Successful Response", },
}, "422": {
"422": { "content": {
"content": { "application/json": {
"application/json": { "schema": {
"schema": { "$ref": "#/components/schemas/HTTPValidationError",
"$ref": "#/components/schemas/HTTPValidationError", },
}, },
}, },
"description": "Validation Error",
}, },
"description": "Validation Error",
}, },
"summary": "Read Root",
}, },
"summary": "Read Root",
}, },
}, },
}, }
} )

Loading…
Cancel
Save