mirror of
https://github.com/fastapi/fastapi.git
synced 2026-02-24 02:38:13 -05:00
✅ Update database setup for tests (#1226)
* 🗃️ Update database setup for tests * ✅ Add pragmas and update db handling for tests
This commit is contained in:
committed by
GitHub
parent
a4405bbed2
commit
a46bbc54cd
@@ -1,3 +1,4 @@
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -283,13 +284,18 @@ openapi_schema = {
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from sql_databases.sql_app.main import app
|
||||
|
||||
test_db = Path("./sql_app.db")
|
||||
with TestClient(app) as c:
|
||||
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 import main
|
||||
|
||||
# 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):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -283,13 +284,19 @@ openapi_schema = {
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from sql_databases.sql_app.alt_main import app
|
||||
|
||||
test_db = Path("./sql_app.db")
|
||||
with TestClient(app) as c:
|
||||
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 import alt_main
|
||||
|
||||
# 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):
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def test_testing_dbs():
|
||||
# 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
|
||||
|
||||
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()
|
||||
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 import test_sql_app
|
||||
|
||||
# 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()
|
||||
|
||||
Reference in New Issue
Block a user