-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documented testing approach does not work for SQLite in-memory instances #3906
Comments
After reading up on this, I think I figured out a work-around: # …
def override_get_db():
try:
db = TestingSessionLocal()
Base.metadata.create_all(bind=db.connection().engine) # re-initialize the DB every time
yield db
finally:
db.close()
# … That allows the above tests to pass, but that only seems appropriate for this particular case. (More experimentation might be needed.) UPDATE: I don't think this works. Subsequent requests using |
I got this to work following #1223 with: from sqlalchemy.pool import StaticPool
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool) |
Just a heads up that SQLAlchemy offers a |
This will effectively mean you end up with From the SQLAlchemy docs:
Using |
@posita Did you find a solution to this? I'm currently running up against the same issue. One thing I did find is that if I change |
I did not find a solution. We actually moved away from SQLAlchemy since we were fighting too much friction. |
Using this solution works for me: from sqlalchemy.pool import StaticPool
...
engine = create_engine(
"sqlite:///:memory:", connect_args={"check_same_thread": False},
poolclass=StaticPool
) |
Сделать так как написано тут: https://sqlmodel.tiangolo.com/tutorial/fastapi/tests/ только не для SQLModel, а для sqlalchemy, отличия только в импортах. Так же заменить названия переменных 'session' на 'db', так как у меня в app 'db' везде. Похожая ситуация описано так же тут: tiangolo/fastapi#3906 Теперь при запуске каждого теста, создается бд в памяти, и при завершении теста от бд ничего не остается. P.S.: uvicorn не подключал, как я понял все что нужно делает под капотом TestClient. И uvicorn нашего app может быть выключен (выключен сервер). fix test_create_week_bad_unique/test_create_cycle_bad_unique: Поправил assert, чтобы если срабатывает unique на другое поле (напр. 'title'), так же все работало. В test_create_week_bad_unique при случае когда title=None & sunday_title=None нужная ошибка IntegrityError не срабатывает и тест не выполнятся. Поэтому написал: week_in.title = 'foo' для того чтобы нужная ошибка, повторного значения поля в бд (unique=True), всегда срабатывала (не знаю насколько это верно).
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
The above is modeled after these instructions, but those do not appear to work for SQLite's in-memory databases. Running the following tests results in three errors similar to:
Changing
TEST_DB_URL
to, e.g.,sqlite:///./test.db
allows those tests to pass.Operating System
Linux, macOS
Operating System Details
No response
FastAPI Version
0.68.1
Python Version
Python 3.9.6
Additional Context
No response
The text was updated successfully, but these errors were encountered: