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