]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Use in memory database for testing SQL in docs (#1223)
authorHarsha Laxman <HarshaLaxman@users.noreply.github.com>
Thu, 22 Jun 2023 11:20:12 +0000 (04:20 -0700)
committerGitHub <noreply@github.com>
Thu, 22 Jun 2023 11:20:12 +0000 (11:20 +0000)
Co-authored-by: Harsha Laxman <harsh@Harshas-MacBook-Pro.local>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
docs/en/docs/advanced/testing-database.md
docs_src/sql_databases/sql_app/tests/test_sql_app.py

index 16484b09af9b1945071a58cdaa3d0670f2b5ef90..13a6959b6fca8d7d87812066513f6dbbcdd9d037 100644 (file)
@@ -44,7 +44,7 @@ So the new file structure looks like:
 
 First, we create a new database session with the new database.
 
-For the tests we'll use a file `test.db` instead of `sql_app.db`.
+We'll use an in-memory database that persists during the tests instead of the local file `sql_app.db`.
 
 But the rest of the session code is more or less the same, we just copy it.
 
index c60c3356f85edc3a339a095b366a5cc63cfeec61..5f55add0a9b4989fcef19ba9002594ed282d8e10 100644 (file)
@@ -1,14 +1,17 @@
 from fastapi.testclient import TestClient
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
+from sqlalchemy.pool import StaticPool
 
 from ..database import Base
 from ..main import app, get_db
 
-SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
+SQLALCHEMY_DATABASE_URL = "sqlite://"
 
 engine = create_engine(
-    SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
+    SQLALCHEMY_DATABASE_URL,
+    connect_args={"check_same_thread": False},
+    poolclass=StaticPool,
 )
 TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)