📝 Use in memory database for testing SQL in docs (#1223)

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>
This commit is contained in:
Harsha Laxman 2023-06-22 04:20:12 -07:00 committed by GitHub
parent dd1c2018dc
commit 2cef119cd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -44,7 +44,7 @@ So the new file structure looks like:
First, we create a new database session with the new database. 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. But the rest of the session code is more or less the same, we just copy it.

View File

@ -1,14 +1,17 @@
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from ..database import Base from ..database import Base
from ..main import app, get_db from ..main import app, get_db
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine( 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) TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)