]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✅ Add the `docs_src` directory to test coverage and update tests (#1904)
authorMarcelo Trylesinski <marcelotryle@gmail.com>
Thu, 29 Jul 2021 09:26:07 +0000 (11:26 +0200)
committerGitHub <noreply@github.com>
Thu, 29 Jul 2021 09:26:07 +0000 (11:26 +0200)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
docs/en/docs/advanced/settings.md
docs_src/settings/app01/main.py
docs_src/settings/app02/main.py
docs_src/settings/app02/test_main.py
scripts/test.sh
tests/test_tutorial/test_bigger_applications/test_main.py
tests/test_tutorial/test_conditional_openapi/test_tutorial001.py
tests/test_tutorial/test_settings/test_app02.py
tests/test_tutorial/test_testing/test_main_b.py [new file with mode: 0644]
tests/test_tutorial/test_websockets/test_tutorial003.py

index 706a71173e76fe025032f75d2512d57cd159af1a..d5151b585c7337d0e153580583489e267a3be19c 100644 (file)
@@ -235,7 +235,7 @@ And then we can require it from the *path operation function* as a dependency an
 
 Then it would be very easy to provide a different settings object during testing by creating a dependency override for `get_settings`:
 
-```Python hl_lines="8-9  12  21"
+```Python hl_lines="9-10  13  21"
 {!../../../docs_src/settings/app02/test_main.py!}
 ```
 
@@ -288,7 +288,7 @@ Reading a file from disk is normally a costly (slow) operation, so you probably
 But every time we do:
 
 ```Python
-config.Settings()
+Settings()
 ```
 
 a new `Settings` object would be created, and at creation it would read the `.env` file again.
@@ -297,7 +297,7 @@ If the dependency function was just like:
 
 ```Python
 def get_settings():
-    return config.Settings()
+    return Settings()
 ```
 
 we would create that object for each request, and we would be reading the `.env` file for each request. ⚠️
index 53503797ecb315d7381d5c28aae0d9a74ee5b4ae..4a3a86ce2099cd251f83ea8f2c15ec56979ddc49 100644 (file)
@@ -1,6 +1,6 @@
 from fastapi import FastAPI
 
-from . import config
+from .config import settings
 
 app = FastAPI()
 
@@ -8,7 +8,7 @@ app = FastAPI()
 @app.get("/info")
 async def info():
     return {
-        "app_name": config.settings.app_name,
-        "admin_email": config.settings.admin_email,
-        "items_per_user": config.settings.items_per_user,
+        "app_name": settings.app_name,
+        "admin_email": settings.admin_email,
+        "items_per_user": settings.items_per_user,
     }
index 69bc8c6e0eb6b9d0643ddcc771633b77f73cd553..163aa261424645d20674bceed29318ec7a014d2e 100644 (file)
@@ -2,18 +2,18 @@ from functools import lru_cache
 
 from fastapi import Depends, FastAPI
 
-from . import config
+from .config import Settings
 
 app = FastAPI()
 
 
 @lru_cache()
 def get_settings():
-    return config.Settings()
+    return Settings()
 
 
 @app.get("/info")
-async def info(settings: config.Settings = Depends(get_settings)):
+async def info(settings: Settings = Depends(get_settings)):
     return {
         "app_name": settings.app_name,
         "admin_email": settings.admin_email,
index 44d9f837963d8612a482f4ebe775d54bfd91c4ca..7a04d7e8ee02af4f68b004384532941e281b6da8 100644 (file)
@@ -1,19 +1,19 @@
 from fastapi.testclient import TestClient
 
-from . import config, main
+from .config import Settings
+from .main import app, get_settings
 
-client = TestClient(main.app)
+client = TestClient(app)
 
 
 def get_settings_override():
-    return config.Settings(admin_email="testing_admin@example.com")
+    return Settings(admin_email="testing_admin@example.com")
 
 
-main.app.dependency_overrides[main.get_settings] = get_settings_override
+app.dependency_overrides[get_settings] = get_settings_override
 
 
 def test_app():
-
     response = client.get("/info")
     data = response.json()
     assert data == {
index b593133d8293f4f639e54d148e5e6f1bac3b7d4b..74ed7a1f19adc951e797558dbf3f5495d1be550f 100755 (executable)
@@ -7,4 +7,4 @@ bash ./scripts/lint.sh
 # Check README.md is up to date
 python ./scripts/docs.py verify-readme
 export PYTHONPATH=./docs_src
-pytest --cov=fastapi --cov=tests --cov=docs/src --cov-report=term-missing --cov-report=xml tests ${@}
+pytest --cov=fastapi --cov=tests --cov=docs_src --cov-report=term-missing:skip-covered --cov-report=xml tests ${@}
index 3670e25cfdc2aa84f16b95ef59e40c6992b8b54f..7eb675179eb009366b3ad6d7c8f906f88e36cfe1 100644 (file)
@@ -359,6 +359,12 @@ no_jessica = {
         ("/users/foo", 422, no_jessica, {}),
         ("/users/me?token=jessica", 200, {"username": "fakecurrentuser"}, {}),
         ("/users/me", 422, no_jessica, {}),
+        (
+            "/users?token=monica",
+            400,
+            {"detail": "No Jessica token provided"},
+            {},
+        ),
         (
             "/items?token=jessica",
             200,
@@ -372,6 +378,12 @@ no_jessica = {
             {"name": "Plumbus", "item_id": "plumbus"},
             {"X-Token": "fake-super-secret-token"},
         ),
+        (
+            "/items/bar?token=jessica",
+            404,
+            {"detail": "Item not found"},
+            {"X-Token": "fake-super-secret-token"},
+        ),
         ("/items/plumbus", 422, no_jessica, {"X-Token": "fake-super-secret-token"}),
         (
             "/items?token=jessica",
index 828cd62c1f2fd7c1b425330f00d26c28cbdea1b2..93c8775ce5f64034ffb86540daf8009a42b4dec1 100644 (file)
@@ -44,3 +44,10 @@ def test_disable_openapi(monkeypatch):
     assert response.status_code == 404, response.text
     response = client.get("/redoc")
     assert response.status_code == 404, response.text
+
+
+def test_root():
+    client = TestClient(tutorial001.app)
+    response = client.get("/")
+    assert response.status_code == 200
+    assert response.json() == {"message": "Hello World"}
index 2fb371202f6dcef60eb66870e37728ef1cc8ec95..fd32b8766f649d44ef73baf8ae73bdeb87fc1e9a 100644 (file)
@@ -1,9 +1,17 @@
 from fastapi.testclient import TestClient
+from pytest import MonkeyPatch
 
 from docs_src.settings.app02 import main, test_main
 
 client = TestClient(main.app)
 
 
-def test_setting_override():
+def test_settings(monkeypatch: MonkeyPatch):
+    monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com")
+    settings = main.get_settings()
+    assert settings.app_name == "Awesome API"
+    assert settings.items_per_user == 50
+
+
+def test_override_settings():
     test_main.test_app()
diff --git a/tests/test_tutorial/test_testing/test_main_b.py b/tests/test_tutorial/test_testing/test_main_b.py
new file mode 100644 (file)
index 0000000..820e485
--- /dev/null
@@ -0,0 +1,10 @@
+from docs_src.app_testing import test_main_b
+
+
+def test_app():
+    test_main_b.test_create_existing_item()
+    test_main_b.test_create_item()
+    test_main_b.test_create_item_bad_token()
+    test_main_b.test_read_inexistent_item()
+    test_main_b.test_read_item()
+    test_main_b.test_read_item_bad_token()
index adc2cdae7fce8e7ced40db55ec4fe34f35729cb9..dbcad3b02c94ce1a040df0b0beb729f16cf0c6fe 100644 (file)
@@ -1,10 +1,15 @@
 from fastapi.testclient import TestClient
 
-from docs_src.websockets.tutorial003 import app
+from docs_src.websockets.tutorial003 import app, html
 
 client = TestClient(app)
 
 
+def test_get():
+    response = client.get("/")
+    assert response.text == html
+
+
 def test_websocket_handle_disconnection():
     with client.websocket_connect("/ws/1234") as connection, client.websocket_connect(
         "/ws/5678"