]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:white_check_mark: Update query param tests and docs
authorSebastián Ramírez <tiangolo@gmail.com>
Tue, 18 Dec 2018 19:38:38 +0000 (23:38 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Tue, 18 Dec 2018 19:38:38 +0000 (23:38 +0400)
docs/tutorial/query-params.md
docs/tutorial/src/query_params/tutorial005.py
docs/tutorial/src/query_params/tutorial006.py [new file with mode: 0644]
tests/test_tutorial/test_cookie_params/test_tutorial001.py
tests/test_tutorial/test_query_params/__init__.py [new file with mode: 0644]
tests/test_tutorial/test_query_params/test_tutorial005.py [new file with mode: 0644]
tests/test_tutorial/test_query_params/test_tutorial006.py [new file with mode: 0644]

index dd75e00a5dbd2e361cf22a01d3eab09d8e1af698..90e9a85951bf1f5033f4614b1cc71af87f79191e 100644 (file)
@@ -140,7 +140,7 @@ Here the query parameter `needy` is a required query parameter of type `str`.
 If you open in your browser a URL like:
 
 ```
-http://127.0.0.1:8000/users/2/items/foo-item
+http://127.0.0.1:8000/items/foo-item
 ```
 
 ...without adding the required parameter `needy`, you will see an error like:
@@ -163,7 +163,7 @@ http://127.0.0.1:8000/users/2/items/foo-item
 As `needy` is a required parameter, you would need to set it in the URL:
 
 ```
-http://127.0.0.1:8000/users/2/items/foo-item?needy=sooooneedy
+http://127.0.0.1:8000/items/foo-item?needy=sooooneedy
 ```
 
 ...this would work:
@@ -171,7 +171,18 @@ http://127.0.0.1:8000/users/2/items/foo-item?needy=sooooneedy
 ```JSON
 {
     "item_id": "foo-item",
-    "owner_id": 2,
     "needy": "sooooneedy"
 }
-```
\ No newline at end of file
+```
+
+And of course, you can define some parameters as required, some as having a default value, and some entirely optional:
+
+```Python hl_lines="7"
+{!./tutorial/src/query_params/tutorial006.py!}
+```
+
+In this case, there are 3 query parameters:
+
+* `needy`, a required `str`.
+* `skip`, an `int` with a default value of `0`.
+* `limit`, an optional `int`.
index 0979ad390dd0e7a57ca8ddb399be3c65825f61d1..e16a40574dce705d121bc64585a21ffa537a185b 100644 (file)
@@ -3,7 +3,7 @@ from fastapi import FastAPI
 app = FastAPI()
 
 
-@app.get("/users/{user_id}/items/{item_id}")
-async def read_user_item(user_id: int, item_id: str, needy: str):
-    item = {"item_id": item_id, "owner_id": user_id, "needy": needy}
+@app.get("/items/{item_id}")
+async def read_user_item(item_id: str, needy: str):
+    item = {"item_id": item_id, "needy": needy}
     return item
diff --git a/docs/tutorial/src/query_params/tutorial006.py b/docs/tutorial/src/query_params/tutorial006.py
new file mode 100644 (file)
index 0000000..d764c2a
--- /dev/null
@@ -0,0 +1,9 @@
+from fastapi import FastAPI
+
+app = FastAPI()
+
+
+@app.get("/items/{item_id}")
+async def read_user_item(item_id: str, needy: str, skip: int = 0, limit: int = None):
+    item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
+    return item
index 90b828ba477d29d259aa99231c658bf91232a86b..088ad9bef53854dd355df08840807304cdf24d2e 100644 (file)
@@ -1,5 +1,3 @@
-import sys
-
 import pytest
 from starlette.testclient import TestClient
 
@@ -7,9 +5,6 @@ from cookie_params.tutorial001 import app
 
 client = TestClient(app)
 
-
-print(sys.path)
-
 openapi_schema = {
     "openapi": "3.0.2",
     "info": {"title": "Fast API", "version": "0.1.0"},
diff --git a/tests/test_tutorial/test_query_params/__init__.py b/tests/test_tutorial/test_query_params/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_tutorial/test_query_params/test_tutorial005.py b/tests/test_tutorial/test_query_params/test_tutorial005.py
new file mode 100644 (file)
index 0000000..b209219
--- /dev/null
@@ -0,0 +1,104 @@
+import pytest
+from starlette.testclient import TestClient
+
+from query_params.tutorial005 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "Fast API", "version": "0.1.0"},
+    "paths": {
+        "/items/{item_id}": {
+            "get": {
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+                "summary": "Read User Item Get",
+                "operationId": "read_user_item_items__item_id__get",
+                "parameters": [
+                    {
+                        "required": True,
+                        "schema": {"title": "Item_Id", "type": "string"},
+                        "name": "item_id",
+                        "in": "path",
+                    },
+                    {
+                        "required": True,
+                        "schema": {"title": "Needy", "type": "string"},
+                        "name": "needy",
+                        "in": "query",
+                    },
+                ],
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "ValidationError": {
+                "title": "ValidationError",
+                "required": ["loc", "msg", "type"],
+                "type": "object",
+                "properties": {
+                    "loc": {
+                        "title": "Location",
+                        "type": "array",
+                        "items": {"type": "string"},
+                    },
+                    "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"},
+                    }
+                },
+            },
+        }
+    },
+}
+
+
+query_required = {
+    "detail": [
+        {
+            "loc": ["query", "needy"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+
+
+@pytest.mark.parametrize(
+    "path,expected_status,expected_response",
+    [
+        ("/openapi.json", 200, openapi_schema),
+        ("/items/foo?needy=very", 200, {"item_id": "foo", "needy": "very"}),
+        ("/items/foo", 422, query_required),
+        ("/items/foo", 422, query_required),
+    ],
+)
+def test(path, expected_status, expected_response):
+    response = client.get(path)
+    assert response.status_code == expected_status
+    assert response.json() == expected_response
diff --git a/tests/test_tutorial/test_query_params/test_tutorial006.py b/tests/test_tutorial/test_query_params/test_tutorial006.py
new file mode 100644 (file)
index 0000000..b29295e
--- /dev/null
@@ -0,0 +1,141 @@
+import pytest
+from starlette.testclient import TestClient
+
+from query_params.tutorial006 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "Fast API", "version": "0.1.0"},
+    "paths": {
+        "/items/{item_id}": {
+            "get": {
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+                "summary": "Read User Item Get",
+                "operationId": "read_user_item_items__item_id__get",
+                "parameters": [
+                    {
+                        "required": True,
+                        "schema": {"title": "Item_Id", "type": "string"},
+                        "name": "item_id",
+                        "in": "path",
+                    },
+                    {
+                        "required": True,
+                        "schema": {"title": "Needy", "type": "string"},
+                        "name": "needy",
+                        "in": "query",
+                    },
+                    {
+                        "required": False,
+                        "schema": {"title": "Skip", "type": "integer", "default": 0},
+                        "name": "skip",
+                        "in": "query",
+                    },
+                    {
+                        "required": False,
+                        "schema": {"title": "Limit", "type": "integer"},
+                        "name": "limit",
+                        "in": "query",
+                    },
+                ],
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "ValidationError": {
+                "title": "ValidationError",
+                "required": ["loc", "msg", "type"],
+                "type": "object",
+                "properties": {
+                    "loc": {
+                        "title": "Location",
+                        "type": "array",
+                        "items": {"type": "string"},
+                    },
+                    "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"},
+                    }
+                },
+            },
+        }
+    },
+}
+
+
+query_required = {
+    "detail": [
+        {
+            "loc": ["query", "needy"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+
+
+@pytest.mark.parametrize(
+    "path,expected_status,expected_response",
+    [
+        ("/openapi.json", 200, openapi_schema),
+        (
+            "/items/foo?needy=very",
+            200,
+            {"item_id": "foo", "needy": "very", "skip": 0, "limit": None},
+        ),
+        (
+            "/items/foo?skip=a&limit=b",
+            422,
+            {
+                "detail": [
+                    {
+                        "loc": ["query", "needy"],
+                        "msg": "field required",
+                        "type": "value_error.missing",
+                    },
+                    {
+                        "loc": ["query", "skip"],
+                        "msg": "value is not a valid integer",
+                        "type": "type_error.integer",
+                    },
+                    {
+                        "loc": ["query", "limit"],
+                        "msg": "value is not a valid integer",
+                        "type": "type_error.integer",
+                    },
+                ]
+            },
+        ),
+    ],
+)
+def test(path, expected_status, expected_response):
+    response = client.get(path)
+    assert response.status_code == expected_status
+    assert response.json() == expected_response