ycdemo / fastapi / tests/ test_regex_deprecated_params.py

from typing import Annotated

import pytest
from fastapi import FastAPI, Query
from fastapi.exceptions import FastAPIDeprecationWarning
from fastapi.testclient import TestClient
from inline_snapshot import snapshot

from .utils import needs_py310


def get_client():
    app = FastAPI()
    with pytest.warns(FastAPIDeprecationWarning):

        @app.get("/items/")
        async def read_items(
            q: Annotated[str | None, Query(regex="^fixedquery$")] = None,
        ):
            if q:
                return f"Hello {q}"
            else:
                return "Hello World"

    client = TestClient(app)
    return client


@needs_py310
def test_query_params_str_validations_no_query():
    client = get_client()
    response = client.get("/items/")
    assert response.status_code == 200
    assert response.json() == "Hello World"


@needs_py310
def test_query_params_str_validations_q_fixedquery():
    client = get_client()
    response = client.get("/items/", params={"q": "fixedquery"})
    assert response.status_code == 200
    assert response.json() == "Hello fixedquery"


@needs_py310
def test_query_params_str_validations_item_query_nonregexquery():
    client = get_client()
    response = client.get("/items/", params={"q": "nonregexquery"})
    assert response.status_code == 422
    assert response.json() == {
        "detail": [
            {
                "type": "string_pattern_mismatch",
                "loc": ["query", "q"],
                "msg": "String should match pattern '^fixedquery
		
		
		
		
	

",
                "input": "nonregexquery",
                "ctx": {"pattern": "^fixedquery$"},
            }
        ]
    }


@needs_py310
def test_openapi_schema():
    client = get_client()
    response = client.get("/openapi.json")
    assert response.status_code == 200, response.text
    assert response.json() == snapshot(
        {
            "openapi": "3.1.0",
            "info": {"title": "FastAPI", "version": "0.1.0"},
            "paths": {
                "/items/": {
                    "get": {
                        "summary": "Read Items",
                        "operationId": "read_items_items__get",
                        "parameters": [
                            {
                                "name": "q",
                                "in": "query",
                                "required": False,
                                "schema": {
                                    "anyOf": [
                                        {"type": "string", "pattern": "^fixedquery$"},
                                        {"type": "null"},
                                    ],
                                    "title": "Q",
                                },
                            }
                        ],
                        "responses": {
                            "200": {
                                "description": "Successful Response",
                                "content": {"application/json": {"schema": {}}},
                            },
                            "422": {
                                "description": "Validation Error",
                                "content": {
                                    "application/json": {
                                        "schema": {
                                            "$ref": "#/components/schemas/HTTPValidationError"
                                        }
                                    }
                                },
                            },
                        },
                    }
                }
            },
            "components": {
                "schemas": {
                    "HTTPValidationError": {
                        "properties": {
                            "detail": {
                                "items": {
                                    "$ref": "#/components/schemas/ValidationError"
                                },
                                "type": "array",
                                "title": "Detail",
                            }
                        },
                        "type": "object",
                        "title": "HTTPValidationError",
                    },
                    "ValidationError": {
                        "properties": {
                            "ctx": {"title": "Context", "type": "object"},
                            "input": {"title": "Input"},
                            "loc": {
                                "items": {
                                    "anyOf": [{"type": "string"}, {"type": "integer"}]
                                },
                                "type": "array",
                                "title": "Location",
                            },
                            "msg": {"type": "string", "title": "Message"},
                            "type": {"type": "string", "title": "Error Type"},
                        },
                        "type": "object",
                        "required": ["loc", "msg", "type"],
                        "title": "ValidationError",
                    },
                }
            },
        }
    )
\",\n \"input\": \"nonregexquery\",\n \"ctx\": {\"pattern\": \"^fixedquery$\"},\n }\n ]\n }\n\n\n@needs_py310\ndef test_openapi_schema():\n client = get_client()\n response = client.get(\"/openapi.json\")\n assert response.status_code == 200, response.text\n assert response.json() == snapshot(\n {\n \"openapi\": \"3.1.0\",\n \"info\": {\"title\": \"FastAPI\", \"version\": \"0.1.0\"},\n \"paths\": {\n \"/items/\": {\n \"get\": {\n \"summary\": \"Read Items\",\n \"operationId\": \"read_items_items__get\",\n \"parameters\": [\n {\n \"name\": \"q\",\n \"in\": \"query\",\n \"required\": False,\n \"schema\": {\n \"anyOf\": [\n {\"type\": \"string\", \"pattern\": \"^fixedquery$\"},\n {\"type\": \"null\"},\n ],\n \"title\": \"Q\",\n },\n }\n ],\n \"responses\": {\n \"200\": {\n \"description\": \"Successful Response\",\n \"content\": {\"application/json\": {\"schema\": {}}},\n },\n \"422\": {\n \"description\": \"Validation Error\",\n \"content\": {\n \"application/json\": {\n \"schema\": {\n \"$ref\": \"#/components/schemas/HTTPValidationError\"\n }\n }\n },\n },\n },\n }\n }\n },\n \"components\": {\n \"schemas\": {\n \"HTTPValidationError\": {\n \"properties\": {\n \"detail\": {\n \"items\": {\n \"$ref\": \"#/components/schemas/ValidationError\"\n },\n \"type\": \"array\",\n \"title\": \"Detail\",\n }\n },\n \"type\": \"object\",\n \"title\": \"HTTPValidationError\",\n },\n \"ValidationError\": {\n \"properties\": {\n \"ctx\": {\"title\": \"Context\", \"type\": \"object\"},\n \"input\": {\"title\": \"Input\"},\n \"loc\": {\n \"items\": {\n \"anyOf\": [{\"type\": \"string\"}, {\"type\": \"integer\"}]\n },\n \"type\": \"array\",\n \"title\": \"Location\",\n },\n \"msg\": {\"type\": \"string\", \"title\": \"Message\"},\n \"type\": {\"type\": \"string\", \"title\": \"Error Type\"},\n },\n \"type\": \"object\",\n \"required\": [\"loc\", \"msg\", \"type\"],\n \"title\": \"ValidationError\",\n },\n }\n },\n }\n )\n","preview":null,"history":null,"version":"2069004f3c9417e65b16c0e01e643ec35c621253"},"/_session?shell=1":null}