+import importlib
+
import pytest
from dirty_equals import IsDict
from fastapi.testclient import TestClient
-from docs_src.dependencies.tutorial001 import app
+from ...utils import needs_py39, needs_py310
+
+
+@pytest.fixture(
+ name="client",
+ params=[
+ "tutorial001",
+ pytest.param("tutorial001_py310", marks=needs_py310),
+ "tutorial001_an",
+ pytest.param("tutorial001_an_py39", marks=needs_py39),
+ pytest.param("tutorial001_an_py310", marks=needs_py310),
+ ],
+)
+def get_client(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
-client = TestClient(app)
+ client = TestClient(mod.app)
+ return client
@pytest.mark.parametrize(
("/users", 200, {"q": None, "skip": 0, "limit": 100}),
],
)
-def test_get(path, expected_status, expected_response):
+def test_get(path, expected_status, expected_response, client: TestClient):
response = client.get(path)
assert response.status_code == expected_status
assert response.json() == expected_response
-def test_openapi_schema():
+def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from docs_src.dependencies.tutorial001_an import app
-
-client = TestClient(app)
-
-
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- ("/items", 200, {"q": None, "skip": 0, "limit": 100}),
- ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}),
- ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}),
- ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}),
- ("/users", 200, {"q": None, "skip": 0, "limit": 100}),
- ],
-)
-def test_get(path, expected_status, expected_response):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-def test_openapi_schema():
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- "/users/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Users",
- "operationId": "read_users_users__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial001_an_py310 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py310
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- ("/items", 200, {"q": None, "skip": 0, "limit": 100}),
- ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}),
- ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}),
- ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}),
- ("/users", 200, {"q": None, "skip": 0, "limit": 100}),
- ],
-)
-def test_get(path, expected_status, expected_response, client: TestClient):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-@needs_py310
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- "/users/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Users",
- "operationId": "read_users_users__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial001_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- ("/items", 200, {"q": None, "skip": 0, "limit": 100}),
- ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}),
- ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}),
- ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}),
- ("/users", 200, {"q": None, "skip": 0, "limit": 100}),
- ],
-)
-def test_get(path, expected_status, expected_response, client: TestClient):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-@needs_py39
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- "/users/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Users",
- "operationId": "read_users_users__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial001_py310 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py310
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- ("/items", 200, {"q": None, "skip": 0, "limit": 100}),
- ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}),
- ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}),
- ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}),
- ("/users", 200, {"q": None, "skip": 0, "limit": 100}),
- ],
-)
-def test_get(path, expected_status, expected_response, client: TestClient):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-@needs_py310
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- "/users/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Users",
- "operationId": "read_users_users__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- },
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+import importlib
+
import pytest
from dirty_equals import IsDict
from fastapi.testclient import TestClient
-from docs_src.dependencies.tutorial004 import app
+from ...utils import needs_py39, needs_py310
+
+
+@pytest.fixture(
+ name="client",
+ params=[
+ "tutorial004",
+ pytest.param("tutorial004_py310", marks=needs_py310),
+ "tutorial004_an",
+ pytest.param("tutorial004_an_py39", marks=needs_py39),
+ pytest.param("tutorial004_an_py310", marks=needs_py310),
+ ],
+)
+def get_client(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
-client = TestClient(app)
+ client = TestClient(mod.app)
+ return client
@pytest.mark.parametrize(
),
],
)
-def test_get(path, expected_status, expected_response):
+def test_get(path, expected_status, expected_response, client: TestClient):
response = client.get(path)
assert response.status_code == expected_status
assert response.json() == expected_response
-def test_openapi_schema():
+def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from docs_src.dependencies.tutorial004_an import app
-
-client = TestClient(app)
-
-
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- (
- "/items",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ]
- },
- ),
- (
- "/items?q=foo",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ],
- "q": "foo",
- },
- ),
- (
- "/items?q=foo&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"},
- ),
- (
- "/items?q=bar&limit=2",
- 200,
- {"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?q=bar&skip=1&limit=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?limit=1&q=bar&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- ],
-)
-def test_get(path, expected_status, expected_response):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-def test_openapi_schema():
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- }
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial004_an_py310 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py310
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- (
- "/items",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ]
- },
- ),
- (
- "/items?q=foo",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ],
- "q": "foo",
- },
- ),
- (
- "/items?q=foo&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"},
- ),
- (
- "/items?q=bar&limit=2",
- 200,
- {"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?q=bar&skip=1&limit=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?limit=1&q=bar&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- ],
-)
-def test_get(path, expected_status, expected_response, client: TestClient):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-@needs_py310
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- }
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial004_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- (
- "/items",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ]
- },
- ),
- (
- "/items?q=foo",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ],
- "q": "foo",
- },
- ),
- (
- "/items?q=foo&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"},
- ),
- (
- "/items?q=bar&limit=2",
- 200,
- {"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?q=bar&skip=1&limit=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?limit=1&q=bar&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- ],
-)
-def test_get(path, expected_status, expected_response, client: TestClient):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-@needs_py39
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- }
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py310
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial004_py310 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py310
-@pytest.mark.parametrize(
- "path,expected_status,expected_response",
- [
- (
- "/items",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ]
- },
- ),
- (
- "/items?q=foo",
- 200,
- {
- "items": [
- {"item_name": "Foo"},
- {"item_name": "Bar"},
- {"item_name": "Baz"},
- ],
- "q": "foo",
- },
- ),
- (
- "/items?q=foo&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"},
- ),
- (
- "/items?q=bar&limit=2",
- 200,
- {"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?q=bar&skip=1&limit=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- (
- "/items?limit=1&q=bar&skip=1",
- 200,
- {"items": [{"item_name": "Bar"}], "q": "bar"},
- ),
- ],
-)
-def test_get(path, expected_status, expected_response, client: TestClient):
- response = client.get(path)
- assert response.status_code == expected_status
- assert response.json() == expected_response
-
-
-@needs_py310
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": False,
- "schema": IsDict(
- {
- "anyOf": [{"type": "string"}, {"type": "null"}],
- "title": "Q",
- }
- )
- | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {"title": "Q", "type": "string"}
- ),
- "name": "q",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Skip",
- "type": "integer",
- "default": 0,
- },
- "name": "skip",
- "in": "query",
- },
- {
- "required": False,
- "schema": {
- "title": "Limit",
- "type": "integer",
- "default": 100,
- },
- "name": "limit",
- "in": "query",
- },
- ],
- }
- }
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+import importlib
+
+import pytest
from dirty_equals import IsDict
from fastapi.testclient import TestClient
-from docs_src.dependencies.tutorial006 import app
+from ...utils import needs_py39
+
+
+@pytest.fixture(
+ name="client",
+ params=[
+ "tutorial006",
+ "tutorial006_an",
+ pytest.param("tutorial006_an_py39", marks=needs_py39),
+ ],
+)
+def get_client(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
-client = TestClient(app)
+ client = TestClient(mod.app)
+ return client
-def test_get_no_headers():
+def test_get_no_headers(client: TestClient):
response = client.get("/items/")
assert response.status_code == 422, response.text
assert response.json() == IsDict(
)
-def test_get_invalid_one_header():
+def test_get_invalid_one_header(client: TestClient):
response = client.get("/items/", headers={"X-Token": "invalid"})
assert response.status_code == 400, response.text
assert response.json() == {"detail": "X-Token header invalid"}
-def test_get_invalid_second_header():
+def test_get_invalid_second_header(client: TestClient):
response = client.get(
"/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
)
assert response.json() == {"detail": "X-Key header invalid"}
-def test_get_valid_headers():
+def test_get_valid_headers(client: TestClient):
response = client.get(
"/items/",
headers={
assert response.json() == [{"item": "Foo"}, {"item": "Bar"}]
-def test_openapi_schema():
+def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
+++ /dev/null
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from docs_src.dependencies.tutorial006_an import app
-
-client = TestClient(app)
-
-
-def test_get_no_headers():
- response = client.get("/items/")
- assert response.status_code == 422, response.text
- assert response.json() == IsDict(
- {
- "detail": [
- {
- "type": "missing",
- "loc": ["header", "x-token"],
- "msg": "Field required",
- "input": None,
- },
- {
- "type": "missing",
- "loc": ["header", "x-key"],
- "msg": "Field required",
- "input": None,
- },
- ]
- }
- ) | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {
- "detail": [
- {
- "loc": ["header", "x-token"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- {
- "loc": ["header", "x-key"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- ]
- }
- )
-
-
-def test_get_invalid_one_header():
- response = client.get("/items/", headers={"X-Token": "invalid"})
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Token header invalid"}
-
-
-def test_get_invalid_second_header():
- response = client.get(
- "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
- )
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Key header invalid"}
-
-
-def test_get_valid_headers():
- response = client.get(
- "/items/",
- headers={
- "X-Token": "fake-super-secret-token",
- "X-Key": "fake-super-secret-key",
- },
- )
- assert response.status_code == 200, response.text
- assert response.json() == [{"item": "Foo"}, {"item": "Bar"}]
-
-
-def test_openapi_schema():
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": True,
- "schema": {"title": "X-Token", "type": "string"},
- "name": "x-token",
- "in": "header",
- },
- {
- "required": True,
- "schema": {"title": "X-Key", "type": "string"},
- "name": "x-key",
- "in": "header",
- },
- ],
- }
- }
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial006_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-def test_get_no_headers(client: TestClient):
- response = client.get("/items/")
- assert response.status_code == 422, response.text
- assert response.json() == IsDict(
- {
- "detail": [
- {
- "type": "missing",
- "loc": ["header", "x-token"],
- "msg": "Field required",
- "input": None,
- },
- {
- "type": "missing",
- "loc": ["header", "x-key"],
- "msg": "Field required",
- "input": None,
- },
- ]
- }
- ) | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {
- "detail": [
- {
- "loc": ["header", "x-token"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- {
- "loc": ["header", "x-key"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- ]
- }
- )
-
-
-@needs_py39
-def test_get_invalid_one_header(client: TestClient):
- response = client.get("/items/", headers={"X-Token": "invalid"})
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Token header invalid"}
-
-
-@needs_py39
-def test_get_invalid_second_header(client: TestClient):
- response = client.get(
- "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
- )
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Key header invalid"}
-
-
-@needs_py39
-def test_get_valid_headers(client: TestClient):
- response = client.get(
- "/items/",
- headers={
- "X-Token": "fake-super-secret-token",
- "X-Key": "fake-super-secret-key",
- },
- )
- assert response.status_code == 200, response.text
- assert response.json() == [{"item": "Foo"}, {"item": "Bar"}]
-
-
-@needs_py39
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": True,
- "schema": {"title": "X-Token", "type": "string"},
- "name": "x-token",
- "in": "header",
- },
- {
- "required": True,
- "schema": {"title": "X-Key", "type": "string"},
- "name": "x-key",
- "in": "header",
- },
- ],
- }
- }
- },
- "components": {
- "schemas": {
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- }
- },
- }
+import importlib
+
+import pytest
from fastapi.testclient import TestClient
-from docs_src.dependencies.tutorial008b import app
+from ...utils import needs_py39
+
+
+@pytest.fixture(
+ name="client",
+ params=[
+ "tutorial008b",
+ "tutorial008b_an",
+ pytest.param("tutorial008b_an_py39", marks=needs_py39),
+ ],
+)
+def get_client(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
-client = TestClient(app)
+ client = TestClient(mod.app)
+ return client
-def test_get_no_item():
+def test_get_no_item(client: TestClient):
response = client.get("/items/foo")
assert response.status_code == 404, response.text
assert response.json() == {"detail": "Item not found"}
-def test_owner_error():
+def test_owner_error(client: TestClient):
response = client.get("/items/plumbus")
assert response.status_code == 400, response.text
assert response.json() == {"detail": "Owner error: Rick"}
-def test_get_item():
+def test_get_item(client: TestClient):
response = client.get("/items/portal-gun")
assert response.status_code == 200, response.text
assert response.json() == {"description": "Gun to create portals", "owner": "Rick"}
+++ /dev/null
-from fastapi.testclient import TestClient
-
-from docs_src.dependencies.tutorial008b_an import app
-
-client = TestClient(app)
-
-
-def test_get_no_item():
- response = client.get("/items/foo")
- assert response.status_code == 404, response.text
- assert response.json() == {"detail": "Item not found"}
-
-
-def test_owner_error():
- response = client.get("/items/plumbus")
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "Owner error: Rick"}
-
-
-def test_get_item():
- response = client.get("/items/portal-gun")
- assert response.status_code == 200, response.text
- assert response.json() == {"description": "Gun to create portals", "owner": "Rick"}
+++ /dev/null
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008b_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-def test_get_no_item(client: TestClient):
- response = client.get("/items/foo")
- assert response.status_code == 404, response.text
- assert response.json() == {"detail": "Item not found"}
-
-
-@needs_py39
-def test_owner_error(client: TestClient):
- response = client.get("/items/plumbus")
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "Owner error: Rick"}
-
-
-@needs_py39
-def test_get_item(client: TestClient):
- response = client.get("/items/portal-gun")
- assert response.status_code == 200, response.text
- assert response.json() == {"description": "Gun to create portals", "owner": "Rick"}
+import importlib
+from types import ModuleType
+
import pytest
from fastapi.exceptions import FastAPIError
from fastapi.testclient import TestClient
+from ...utils import needs_py39
+
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008c import app
+@pytest.fixture(
+ name="mod",
+ params=[
+ "tutorial008c",
+ "tutorial008c_an",
+ pytest.param("tutorial008c_an_py39", marks=needs_py39),
+ ],
+)
+def get_mod(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
- client = TestClient(app)
- return client
+ return mod
-def test_get_no_item(client: TestClient):
+def test_get_no_item(mod: ModuleType):
+ client = TestClient(mod.app)
response = client.get("/items/foo")
assert response.status_code == 404, response.text
assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
-def test_get(client: TestClient):
+def test_get(mod: ModuleType):
+ client = TestClient(mod.app)
response = client.get("/items/plumbus")
assert response.status_code == 200, response.text
assert response.json() == "plumbus"
-def test_fastapi_error(client: TestClient):
+def test_fastapi_error(mod: ModuleType):
+ client = TestClient(mod.app)
with pytest.raises(FastAPIError) as exc_info:
client.get("/items/portal-gun")
assert "No response object was returned" in exc_info.value.args[0]
-def test_internal_server_error():
- from docs_src.dependencies.tutorial008c import app
-
- client = TestClient(app, raise_server_exceptions=False)
+def test_internal_server_error(mod: ModuleType):
+ client = TestClient(mod.app, raise_server_exceptions=False)
response = client.get("/items/portal-gun")
assert response.status_code == 500, response.text
assert response.text == "Internal Server Error"
+++ /dev/null
-import pytest
-from fastapi.exceptions import FastAPIError
-from fastapi.testclient import TestClient
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008c_an import app
-
- client = TestClient(app)
- return client
-
-
-def test_get_no_item(client: TestClient):
- response = client.get("/items/foo")
- assert response.status_code == 404, response.text
- assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
-
-
-def test_get(client: TestClient):
- response = client.get("/items/plumbus")
- assert response.status_code == 200, response.text
- assert response.json() == "plumbus"
-
-
-def test_fastapi_error(client: TestClient):
- with pytest.raises(FastAPIError) as exc_info:
- client.get("/items/portal-gun")
- assert "No response object was returned" in exc_info.value.args[0]
-
-
-def test_internal_server_error():
- from docs_src.dependencies.tutorial008c_an import app
-
- client = TestClient(app, raise_server_exceptions=False)
- response = client.get("/items/portal-gun")
- assert response.status_code == 500, response.text
- assert response.text == "Internal Server Error"
+++ /dev/null
-import pytest
-from fastapi.exceptions import FastAPIError
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008c_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-def test_get_no_item(client: TestClient):
- response = client.get("/items/foo")
- assert response.status_code == 404, response.text
- assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
-
-
-@needs_py39
-def test_get(client: TestClient):
- response = client.get("/items/plumbus")
- assert response.status_code == 200, response.text
- assert response.json() == "plumbus"
-
-
-@needs_py39
-def test_fastapi_error(client: TestClient):
- with pytest.raises(FastAPIError) as exc_info:
- client.get("/items/portal-gun")
- assert "No response object was returned" in exc_info.value.args[0]
-
-
-@needs_py39
-def test_internal_server_error():
- from docs_src.dependencies.tutorial008c_an_py39 import app
-
- client = TestClient(app, raise_server_exceptions=False)
- response = client.get("/items/portal-gun")
- assert response.status_code == 500, response.text
- assert response.text == "Internal Server Error"
+import importlib
+from types import ModuleType
+
import pytest
from fastapi.testclient import TestClient
+from ...utils import needs_py39
+
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008d import app
+@pytest.fixture(
+ name="mod",
+ params=[
+ "tutorial008d",
+ "tutorial008d_an",
+ pytest.param("tutorial008d_an_py39", marks=needs_py39),
+ ],
+)
+def get_mod(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
- client = TestClient(app)
- return client
+ return mod
-def test_get_no_item(client: TestClient):
+def test_get_no_item(mod: ModuleType):
+ client = TestClient(mod.app)
response = client.get("/items/foo")
assert response.status_code == 404, response.text
assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
-def test_get(client: TestClient):
+def test_get(mod: ModuleType):
+ client = TestClient(mod.app)
response = client.get("/items/plumbus")
assert response.status_code == 200, response.text
assert response.json() == "plumbus"
-def test_internal_error(client: TestClient):
- from docs_src.dependencies.tutorial008d import InternalError
-
- with pytest.raises(InternalError) as exc_info:
+def test_internal_error(mod: ModuleType):
+ client = TestClient(mod.app)
+ with pytest.raises(mod.InternalError) as exc_info:
client.get("/items/portal-gun")
assert (
exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick"
)
-def test_internal_server_error():
- from docs_src.dependencies.tutorial008d import app
-
- client = TestClient(app, raise_server_exceptions=False)
+def test_internal_server_error(mod: ModuleType):
+ client = TestClient(mod.app, raise_server_exceptions=False)
response = client.get("/items/portal-gun")
assert response.status_code == 500, response.text
assert response.text == "Internal Server Error"
+++ /dev/null
-import pytest
-from fastapi.testclient import TestClient
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008d_an import app
-
- client = TestClient(app)
- return client
-
-
-def test_get_no_item(client: TestClient):
- response = client.get("/items/foo")
- assert response.status_code == 404, response.text
- assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
-
-
-def test_get(client: TestClient):
- response = client.get("/items/plumbus")
- assert response.status_code == 200, response.text
- assert response.json() == "plumbus"
-
-
-def test_internal_error(client: TestClient):
- from docs_src.dependencies.tutorial008d_an import InternalError
-
- with pytest.raises(InternalError) as exc_info:
- client.get("/items/portal-gun")
- assert (
- exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick"
- )
-
-
-def test_internal_server_error():
- from docs_src.dependencies.tutorial008d_an import app
-
- client = TestClient(app, raise_server_exceptions=False)
- response = client.get("/items/portal-gun")
- assert response.status_code == 500, response.text
- assert response.text == "Internal Server Error"
+++ /dev/null
-import pytest
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial008d_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-def test_get_no_item(client: TestClient):
- response = client.get("/items/foo")
- assert response.status_code == 404, response.text
- assert response.json() == {"detail": "Item not found, there's only a plumbus here"}
-
-
-@needs_py39
-def test_get(client: TestClient):
- response = client.get("/items/plumbus")
- assert response.status_code == 200, response.text
- assert response.json() == "plumbus"
-
-
-@needs_py39
-def test_internal_error(client: TestClient):
- from docs_src.dependencies.tutorial008d_an_py39 import InternalError
-
- with pytest.raises(InternalError) as exc_info:
- client.get("/items/portal-gun")
- assert (
- exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick"
- )
-
-
-@needs_py39
-def test_internal_server_error():
- from docs_src.dependencies.tutorial008d_an_py39 import app
-
- client = TestClient(app, raise_server_exceptions=False)
- response = client.get("/items/portal-gun")
- assert response.status_code == 500, response.text
- assert response.text == "Internal Server Error"
+import importlib
+
+import pytest
from dirty_equals import IsDict
from fastapi.testclient import TestClient
-from docs_src.dependencies.tutorial012 import app
+from ...utils import needs_py39
+
+
+@pytest.fixture(
+ name="client",
+ params=[
+ "tutorial012",
+ "tutorial012_an",
+ pytest.param("tutorial012_an_py39", marks=needs_py39),
+ ],
+)
+def get_client(request: pytest.FixtureRequest):
+ mod = importlib.import_module(f"docs_src.dependencies.{request.param}")
-client = TestClient(app)
+ client = TestClient(mod.app)
+ return client
-def test_get_no_headers_items():
+def test_get_no_headers_items(client: TestClient):
response = client.get("/items/")
assert response.status_code == 422, response.text
assert response.json() == IsDict(
)
-def test_get_no_headers_users():
+def test_get_no_headers_users(client: TestClient):
response = client.get("/users/")
assert response.status_code == 422, response.text
assert response.json() == IsDict(
)
-def test_get_invalid_one_header_items():
+def test_get_invalid_one_header_items(client: TestClient):
response = client.get("/items/", headers={"X-Token": "invalid"})
assert response.status_code == 400, response.text
assert response.json() == {"detail": "X-Token header invalid"}
-def test_get_invalid_one_users():
+def test_get_invalid_one_users(client: TestClient):
response = client.get("/users/", headers={"X-Token": "invalid"})
assert response.status_code == 400, response.text
assert response.json() == {"detail": "X-Token header invalid"}
-def test_get_invalid_second_header_items():
+def test_get_invalid_second_header_items(client: TestClient):
response = client.get(
"/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
)
assert response.json() == {"detail": "X-Key header invalid"}
-def test_get_invalid_second_header_users():
+def test_get_invalid_second_header_users(client: TestClient):
response = client.get(
"/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
)
assert response.json() == {"detail": "X-Key header invalid"}
-def test_get_valid_headers_items():
+def test_get_valid_headers_items(client: TestClient):
response = client.get(
"/items/",
headers={
assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}]
-def test_get_valid_headers_users():
+def test_get_valid_headers_users(client: TestClient):
response = client.get(
"/users/",
headers={
assert response.json() == [{"username": "Rick"}, {"username": "Morty"}]
-def test_openapi_schema():
+def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
+++ /dev/null
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from docs_src.dependencies.tutorial012_an import app
-
-client = TestClient(app)
-
-
-def test_get_no_headers_items():
- response = client.get("/items/")
- assert response.status_code == 422, response.text
- assert response.json() == IsDict(
- {
- "detail": [
- {
- "type": "missing",
- "loc": ["header", "x-token"],
- "msg": "Field required",
- "input": None,
- },
- {
- "type": "missing",
- "loc": ["header", "x-key"],
- "msg": "Field required",
- "input": None,
- },
- ]
- }
- ) | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {
- "detail": [
- {
- "loc": ["header", "x-token"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- {
- "loc": ["header", "x-key"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- ]
- }
- )
-
-
-def test_get_no_headers_users():
- response = client.get("/users/")
- assert response.status_code == 422, response.text
- assert response.json() == IsDict(
- {
- "detail": [
- {
- "type": "missing",
- "loc": ["header", "x-token"],
- "msg": "Field required",
- "input": None,
- },
- {
- "type": "missing",
- "loc": ["header", "x-key"],
- "msg": "Field required",
- "input": None,
- },
- ]
- }
- ) | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {
- "detail": [
- {
- "loc": ["header", "x-token"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- {
- "loc": ["header", "x-key"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- ]
- }
- )
-
-
-def test_get_invalid_one_header_items():
- response = client.get("/items/", headers={"X-Token": "invalid"})
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Token header invalid"}
-
-
-def test_get_invalid_one_users():
- response = client.get("/users/", headers={"X-Token": "invalid"})
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Token header invalid"}
-
-
-def test_get_invalid_second_header_items():
- response = client.get(
- "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
- )
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Key header invalid"}
-
-
-def test_get_invalid_second_header_users():
- response = client.get(
- "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
- )
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Key header invalid"}
-
-
-def test_get_valid_headers_items():
- response = client.get(
- "/items/",
- headers={
- "X-Token": "fake-super-secret-token",
- "X-Key": "fake-super-secret-key",
- },
- )
- assert response.status_code == 200, response.text
- assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}]
-
-
-def test_get_valid_headers_users():
- response = client.get(
- "/users/",
- headers={
- "X-Token": "fake-super-secret-token",
- "X-Key": "fake-super-secret-key",
- },
- )
- assert response.status_code == 200, response.text
- assert response.json() == [{"username": "Rick"}, {"username": "Morty"}]
-
-
-def test_openapi_schema():
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": True,
- "schema": {"title": "X-Token", "type": "string"},
- "name": "x-token",
- "in": "header",
- },
- {
- "required": True,
- "schema": {"title": "X-Key", "type": "string"},
- "name": "x-key",
- "in": "header",
- },
- ],
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- }
- },
- "/users/": {
- "get": {
- "summary": "Read Users",
- "operationId": "read_users_users__get",
- "parameters": [
- {
- "required": True,
- "schema": {"title": "X-Token", "type": "string"},
- "name": "x-token",
- "in": "header",
- },
- {
- "required": True,
- "schema": {"title": "X-Key", "type": "string"},
- "name": "x-key",
- "in": "header",
- },
- ],
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- }
- },
- },
- "components": {
- "schemas": {
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- }
- },
- }
+++ /dev/null
-import pytest
-from dirty_equals import IsDict
-from fastapi.testclient import TestClient
-
-from ...utils import needs_py39
-
-
-@pytest.fixture(name="client")
-def get_client():
- from docs_src.dependencies.tutorial012_an_py39 import app
-
- client = TestClient(app)
- return client
-
-
-@needs_py39
-def test_get_no_headers_items(client: TestClient):
- response = client.get("/items/")
- assert response.status_code == 422, response.text
- assert response.json() == IsDict(
- {
- "detail": [
- {
- "type": "missing",
- "loc": ["header", "x-token"],
- "msg": "Field required",
- "input": None,
- },
- {
- "type": "missing",
- "loc": ["header", "x-key"],
- "msg": "Field required",
- "input": None,
- },
- ]
- }
- ) | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {
- "detail": [
- {
- "loc": ["header", "x-token"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- {
- "loc": ["header", "x-key"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- ]
- }
- )
-
-
-@needs_py39
-def test_get_no_headers_users(client: TestClient):
- response = client.get("/users/")
- assert response.status_code == 422, response.text
- assert response.json() == IsDict(
- {
- "detail": [
- {
- "type": "missing",
- "loc": ["header", "x-token"],
- "msg": "Field required",
- "input": None,
- },
- {
- "type": "missing",
- "loc": ["header", "x-key"],
- "msg": "Field required",
- "input": None,
- },
- ]
- }
- ) | IsDict(
- # TODO: remove when deprecating Pydantic v1
- {
- "detail": [
- {
- "loc": ["header", "x-token"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- {
- "loc": ["header", "x-key"],
- "msg": "field required",
- "type": "value_error.missing",
- },
- ]
- }
- )
-
-
-@needs_py39
-def test_get_invalid_one_header_items(client: TestClient):
- response = client.get("/items/", headers={"X-Token": "invalid"})
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Token header invalid"}
-
-
-@needs_py39
-def test_get_invalid_one_users(client: TestClient):
- response = client.get("/users/", headers={"X-Token": "invalid"})
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Token header invalid"}
-
-
-@needs_py39
-def test_get_invalid_second_header_items(client: TestClient):
- response = client.get(
- "/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
- )
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Key header invalid"}
-
-
-@needs_py39
-def test_get_invalid_second_header_users(client: TestClient):
- response = client.get(
- "/users/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"}
- )
- assert response.status_code == 400, response.text
- assert response.json() == {"detail": "X-Key header invalid"}
-
-
-@needs_py39
-def test_get_valid_headers_items(client: TestClient):
- response = client.get(
- "/items/",
- headers={
- "X-Token": "fake-super-secret-token",
- "X-Key": "fake-super-secret-key",
- },
- )
- assert response.status_code == 200, response.text
- assert response.json() == [{"item": "Portal Gun"}, {"item": "Plumbus"}]
-
-
-@needs_py39
-def test_get_valid_headers_users(client: TestClient):
- response = client.get(
- "/users/",
- headers={
- "X-Token": "fake-super-secret-token",
- "X-Key": "fake-super-secret-key",
- },
- )
- assert response.status_code == 200, response.text
- assert response.json() == [{"username": "Rick"}, {"username": "Morty"}]
-
-
-@needs_py39
-def test_openapi_schema(client: TestClient):
- response = client.get("/openapi.json")
- assert response.status_code == 200, response.text
- assert response.json() == {
- "openapi": "3.1.0",
- "info": {"title": "FastAPI", "version": "0.1.0"},
- "paths": {
- "/items/": {
- "get": {
- "summary": "Read Items",
- "operationId": "read_items_items__get",
- "parameters": [
- {
- "required": True,
- "schema": {"title": "X-Token", "type": "string"},
- "name": "x-token",
- "in": "header",
- },
- {
- "required": True,
- "schema": {"title": "X-Key", "type": "string"},
- "name": "x-key",
- "in": "header",
- },
- ],
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- }
- },
- "/users/": {
- "get": {
- "summary": "Read Users",
- "operationId": "read_users_users__get",
- "parameters": [
- {
- "required": True,
- "schema": {"title": "X-Token", "type": "string"},
- "name": "x-token",
- "in": "header",
- },
- {
- "required": True,
- "schema": {"title": "X-Key", "type": "string"},
- "name": "x-key",
- "in": "header",
- },
- ],
- "responses": {
- "200": {
- "description": "Successful Response",
- "content": {"application/json": {"schema": {}}},
- },
- "422": {
- "description": "Validation Error",
- "content": {
- "application/json": {
- "schema": {
- "$ref": "#/components/schemas/HTTPValidationError"
- }
- }
- },
- },
- },
- }
- },
- },
- "components": {
- "schemas": {
- "HTTPValidationError": {
- "title": "HTTPValidationError",
- "type": "object",
- "properties": {
- "detail": {
- "title": "Detail",
- "type": "array",
- "items": {"$ref": "#/components/schemas/ValidationError"},
- }
- },
- },
- "ValidationError": {
- "title": "ValidationError",
- "required": ["loc", "msg", "type"],
- "type": "object",
- "properties": {
- "loc": {
- "title": "Location",
- "type": "array",
- "items": {
- "anyOf": [{"type": "string"}, {"type": "integer"}]
- },
- },
- "msg": {"title": "Message", "type": "string"},
- "type": {"title": "Error Type", "type": "string"},
- },
- },
- }
- },
- }