]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:white_check_mark: Add tests for form and files
authorSebastián Ramírez <tiangolo@gmail.com>
Sat, 22 Dec 2018 04:39:26 +0000 (08:39 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Sat, 22 Dec 2018 04:39:26 +0000 (08:39 +0400)
tests/test_tutorial/test_body/test_tutorial001.py
tests/test_tutorial/test_request_files/test_tutorial001.py
tests/test_tutorial/test_request_forms/test_tutorial001.py
tests/test_tutorial/test_request_forms_and_files/__init__.py [new file with mode: 0644]
tests/test_tutorial/test_request_forms_and_files/test_tutorial001.py [new file with mode: 0644]

index 7a07c0367c943a18c30d276de0fc2678fc1c6c33..ee06de7cebe26af4cbd243a2b16dab74daa4a706 100644 (file)
@@ -172,3 +172,9 @@ def test_post_body(path, body, expected_status, expected_response):
     response = client.post(path, json=body)
     assert response.status_code == expected_status
     assert response.json() == expected_response
+
+
+def test_post_broken_body():
+    response = client.post("/items/", data={"name": "Foo", "price": 50.5})
+    assert response.status_code == 400
+    assert response.json() == {"detail": "There was an error parsing the body"}
index 1a59876b06a9844f3aeab65083335446fc03a591..6e20dd911c900deda3ead7c1e95c426aee94024b 100644 (file)
@@ -110,11 +110,12 @@ def test_post_body_json():
     assert response.json() == file_required
 
 
-def test_multipart_request_files(tmpdir):
+def test_post_file(tmpdir):
     path = os.path.join(tmpdir, "test.txt")
     with open(path, "wb") as file:
         file.write(b"<file content>")
 
     client = TestClient(app)
     response = client.post("/files/", files={"file": open(path, "rb")})
+    assert response.status_code == 200
     assert response.json() == {"file_size": 14}
index 4e6ae457bf6d07459d7ab72f8362cf1bc44006cd..f80490df5c33f173aca2b9234b9b5e3023d7c9d2 100644 (file)
@@ -87,16 +87,6 @@ def test_openapi_scheme():
     assert response.json() == openapi_schema
 
 
-item_id_not_int = {
-    "detail": [
-        {
-            "loc": ["path", "item_id"],
-            "msg": "value is not a valid integer",
-            "type": "type_error.integer",
-        }
-    ]
-}
-
 password_required = {
     "detail": [
         {
diff --git a/tests/test_tutorial/test_request_forms_and_files/__init__.py b/tests/test_tutorial/test_request_forms_and_files/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/test_tutorial/test_request_forms_and_files/test_tutorial001.py b/tests/test_tutorial/test_request_forms_and_files/test_tutorial001.py
new file mode 100644 (file)
index 0000000..968bbe5
--- /dev/null
@@ -0,0 +1,166 @@
+import os
+
+from starlette.testclient import TestClient
+
+from request_forms_and_files.tutorial001 import app
+
+client = TestClient(app)
+
+openapi_schema = {
+    "openapi": "3.0.2",
+    "info": {"title": "Fast API", "version": "0.1.0"},
+    "paths": {
+        "/files/": {
+            "post": {
+                "responses": {
+                    "200": {
+                        "description": "Successful Response",
+                        "content": {"application/json": {"schema": {}}},
+                    },
+                    "422": {
+                        "description": "Validation Error",
+                        "content": {
+                            "application/json": {
+                                "schema": {
+                                    "$ref": "#/components/schemas/HTTPValidationError"
+                                }
+                            }
+                        },
+                    },
+                },
+                "summary": "Create File Post",
+                "operationId": "create_file_files__post",
+                "requestBody": {
+                    "content": {
+                        "multipart/form-data": {
+                            "schema": {"$ref": "#/components/schemas/Body_create_file"}
+                        }
+                    },
+                    "required": True,
+                },
+            }
+        }
+    },
+    "components": {
+        "schemas": {
+            "Body_create_file": {
+                "title": "Body_create_file",
+                "required": ["file", "token"],
+                "type": "object",
+                "properties": {
+                    "file": {"title": "File", "type": "string", "format": "binary"},
+                    "token": {"title": "Token", "type": "string"},
+                },
+            },
+            "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"},
+                    }
+                },
+            },
+        }
+    },
+}
+
+
+def test_openapi_scheme():
+    response = client.get("/openapi.json")
+    assert response.status_code == 200
+    assert response.json() == openapi_schema
+
+
+file_required = {
+    "detail": [
+        {
+            "loc": ["body", "file"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+
+token_required = {
+    "detail": [
+        {
+            "loc": ["body", "token"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        }
+    ]
+}
+
+file_and_token_required = {
+    "detail": [
+        {
+            "loc": ["body", "file"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        },
+        {
+            "loc": ["body", "token"],
+            "msg": "field required",
+            "type": "value_error.missing",
+        },
+    ]
+}
+
+
+def test_post_form_no_body():
+    response = client.post("/files/")
+    assert response.status_code == 422
+    assert response.json() == file_and_token_required
+
+
+def test_post_form_no_file():
+    response = client.post("/files/", data={"token": "foo"})
+    assert response.status_code == 422
+    assert response.json() == file_required
+
+
+def test_post_body_json():
+    response = client.post("/files/", json={"file": "Foo", "token": "Bar"})
+    assert response.status_code == 422
+    assert response.json() == file_and_token_required
+
+
+def test_post_file_no_token(tmpdir):
+    path = os.path.join(tmpdir, "test.txt")
+    with open(path, "wb") as file:
+        file.write(b"<file content>")
+
+    client = TestClient(app)
+    response = client.post("/files/", files={"file": open(path, "rb")})
+    assert response.status_code == 422
+    assert response.json() == token_required
+
+
+def test_post_file_and_token(tmpdir):
+    path = os.path.join(tmpdir, "test.txt")
+    with open(path, "wb") as file:
+        file.write(b"<file content>")
+
+    client = TestClient(app)
+    response = client.post(
+        "/files/", data={"token": "foo"}, files={"file": open(path, "rb")}
+    )
+    assert response.status_code == 200
+    assert response.json() == {"file_size": 14, "token": "foo"}