Browse Source

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

Co-authored-by: Harsha Laxman <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Sebastián Ramírez <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
pull/9720/head
Harsha Laxman 2 years ago
committed by GitHub
parent
commit
2cef119cd7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/en/docs/advanced/testing-database.md
  2. 7
      docs_src/sql_databases/sql_app/tests/test_sql_app.py

2
docs/en/docs/advanced/testing-database.md

@ -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.

7
docs_src/sql_databases/sql_app/tests/test_sql_app.py

@ -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)

Loading…
Cancel
Save