Skip to content
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

When using ContextVars for db dependency injection all related tests are failing. #95

Open
madeinoz67 opened this issue Jun 6, 2021 · 2 comments
Labels
status: blocked Something is blocking this type: bug Something isn't working

Comments

@madeinoz67
Copy link
Owner

Describe the bug
When using dependency injection instead of passing the db dependency down through all the layers, a ContextVars is being used. Passing the db sessions via ContextVar over function parameters is working with no problems with the application and results in a cleaner solution.

However with testing, a context is setup in a fixture and is not being propagated to the underlying test, resulting in the underlying function not finding the required context and failing.

This issue is documented pytest-dev/pytest-asyncio#127 and a PR pytest-dev/pytest-asyncio#161

Expected behaviour
the test fixture should be propagating the ContextVar down to the test function so it available to the function under test

Screenshots

LookupError: <ContextVar name='db_session_context' at 0x1120f29a0>
tests/test_services/test_part_service.py:23: in test_part_get
    item: PartModel = await part_service.create_part(obj_in)  # noqa:
app/services/part_service.py:103: in create_part
    db_session: AsyncSession = db_session_context.get()
E   LookupError: <ContextVar name='db_session_context' at 0x1120f29a0>

--------------------------------- Captured Log ---------------------------------

--------------------------------- Captured Out ---------------------------------


--------------------------------- Captured Err ---------------------------------
INFO  [alembic.env] DETECTED aiosqlite driver, stripping from DATABASE_URL for migration
INFO  [alembic.env] Database URL: sqlite:///./tests/files/test.db
INFO  [alembic.env] Running migrations online
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 9905971ce75e, Adds Parts Table
INFO  [alembic.env] DETECTED aiosqlite driver, stripping from DATABASE_URL for migration
INFO  [alembic.env] Database URL: sqlite:///./tests/files/test.db
INFO  [alembic.env] Running migrations online
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running downgrade 9905971ce75e -> , Adds Parts Table

Additional context
Should be resolved by this PR pytest-dev/pytest-asyncio#161

@madeinoz67 madeinoz67 added type: bug Something isn't working status: blocked Something is blocking this labels Jun 6, 2021
@madeinoz67
Copy link
Owner Author

More information here by the author of the PR mentioned above https://linw1995.com/en/blog/How-To-Write-Asynchronous-Code-With-Contextvars-Properly/

@madeinoz67
Copy link
Owner Author

madeinoz67 commented Jun 6, 2021

To Move forward I'll move to db sessions passed as function parameters until I can get this working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: blocked Something is blocking this type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant