]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:white_check_mark: Update database setup for tests (#1226)
authorSebastián Ramírez <tiangolo@gmail.com>
Wed, 8 Apr 2020 05:41:53 +0000 (07:41 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Apr 2020 05:41:53 +0000 (07:41 +0200)
* :card_file_box: Update database setup for tests

* :white_check_mark: Add pragmas and update db handling for tests

tests/test_tutorial/test_sql_databases/test_sql_databases.py
tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py
tests/test_tutorial/test_sql_databases/test_testing_databases.py

index d7b6e1b3afcb32479af1f13aa32b6bfa45268243..69092889ca32f444c7b223200ea922bd9c192a84 100644 (file)
@@ -1,3 +1,4 @@
+import importlib
 from pathlib import Path
 
 import pytest
@@ -283,13 +284,18 @@ openapi_schema = {
 
 @pytest.fixture(scope="module")
 def client():
+    test_db = Path("./sql_app.db")
+    if test_db.is_file():  # pragma: nocover
+        test_db.unlink()
     # Import while creating the client to create the DB after starting the test session
-    from sql_databases.sql_app.main import app
+    from sql_databases.sql_app import main
 
-    test_db = Path("./sql_app.db")
-    with TestClient(app) as c:
+    # Ensure import side effects are re-executed
+    importlib.reload(main)
+    with TestClient(main.app) as c:
         yield c
-    test_db.unlink()
+    if test_db.is_file():  # pragma: nocover
+        test_db.unlink()
 
 
 def test_openapi_schema(client):
index d26a442b707b3d44225670a53cd2790bec680fa4..6b4ed567ed35376b6e0cf5c0e84c44b849e4f3b4 100644 (file)
@@ -1,3 +1,4 @@
+import importlib
 from pathlib import Path
 
 import pytest
@@ -283,13 +284,19 @@ openapi_schema = {
 
 @pytest.fixture(scope="module")
 def client():
+    test_db = Path("./sql_app.db")
+    if test_db.is_file():  # pragma: nocover
+        test_db.unlink()
     # Import while creating the client to create the DB after starting the test session
-    from sql_databases.sql_app.alt_main import app
+    from sql_databases.sql_app import alt_main
 
-    test_db = Path("./sql_app.db")
-    with TestClient(app) as c:
+    # Ensure import side effects are re-executed
+    importlib.reload(alt_main)
+
+    with TestClient(alt_main.app) as c:
         yield c
-    test_db.unlink()
+    if test_db.is_file():  # pragma: nocover
+        test_db.unlink()
 
 
 def test_openapi_schema(client):
index fae66d3b87c1e1744c9ee3d283f5860ce02259d0..83a156b8509bde0e6fb6f556407a04c5a9d5ee25 100644 (file)
@@ -1,13 +1,16 @@
+import importlib
 from pathlib import Path
 
 
 def test_testing_dbs():
+    test_db = Path("./test.db")
+    if test_db.is_file():  # pragma: nocover
+        test_db.unlink()
     # Import while creating the client to create the DB after starting the test session
-    from sql_databases.sql_app.tests.test_sql_app import test_create_user
+    from sql_databases.sql_app.tests import test_sql_app
 
-    test_db = Path("./test.db")
-    app_db = Path("./sql_app.db")
-    test_create_user()
-    test_db.unlink()
-    if app_db.is_file():  # pragma: nocover
-        app_db.unlink()
+    # Ensure import side effects are re-executed
+    importlib.reload(test_sql_app)
+    test_sql_app.test_create_user()
+    if test_db.is_file():  # pragma: nocover
+        test_db.unlink()