Database testing fixtures using the SQLAlchemy asyncio API
Project description
pytest-async-sqlalchemy
Database testing fixtures using the SQLAlchemy asyncio API
You can install "pytest-async-sqlalchemy" via pip from PyPI
$ pip install pytest-async-sqlalchemy
Setup
Providing a Session Scoped Event Loop
The first thing to do is to declare an even_loop
fixture with the scope set as "session".
You can copy & paste the code below to your conftest.py
:
@pytest.fixture(scope="session")
def event_loop():
"""
Creates an instance of the default event loop for the test session.
"""
if sys.platform.startswith("win") and sys.version_info[:2] >= (3, 8):
# Avoid "RuntimeError: Event loop is closed" on Windows when tearing down tests
# https://github.com/encode/httpx/issues/914
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
loop = asyncio.new_event_loop()
yield loop
loop.close()
This is required since pytest-async-sqlalchemy shares the database connection between tests
for performance reasons, but the default even_loop
fixture defined by
pytest-asyncio is function scoped1 (i.e., it
kills the database connection after each test).
Providing Database URL and Initialization
pytest-async-sqlalchemy provides placeholders to configure and initialize
the testing database: database_url
and init_database
. These two must be
defined in your project conftest.py
like below:
@pytest.fixture(scope="session")
def _database_url():
return "postgresql+asyncpg://postgres:masterkey@localhost/dbtest"
@pytest.fixture(scope="session")
def init_database():
from myprorject.db import metadata
return metadata.create_all
The _database_url
must be a session-scoped fixture that returns the database URL in
SQLAlchemy standard. init_database
must also be a session-scoped fixture that returns
the callable used to initialize the database (in most cases, this would return the
metadata.create_all
function).
Usage
This plugin provides the following fixtures:
db_session
: AnAsyncSession
object bounded to the test session database. Database transactions are discarded after each test function, so the same database is used for the entire test suite (avoiding the overhead of initializing a database on every test).database
: An URL to the initialized test session database.scoped_database
:scoped_database
provides a new database within the scope of the test function. The value of the fixture is a string URL pointing to the database.
Contributing
Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.
License
Distributed under the terms of the MIT license, "pytest-async-sqlalchemy" is free and open source software
1. pytest-async-sqlalchemy can't provide its own even_loop
since pytest plugins are not
able to override fixtures from one another. So the only solution we have now is to aks the user to
declare its own event_loop
fixture. Suggestions on how to overcome that in a better way are
welcome, hit us on the Issues section.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-async-sqlalchemy-0.2.0.tar.gz
.
File metadata
- Download URL: pytest-async-sqlalchemy-0.2.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0dcf80fdff1ea0046834cff2bc100c82d159e45a7ae21545a6ba9119a962b9d7 |
|
MD5 | 174925cd3e6cbdc16386e37f286254fd |
|
BLAKE2b-256 | 88aa264687cab98d67b73e680fe9299daf81edfe9b6bac016c4978f2380321cc |
File details
Details for the file pytest_async_sqlalchemy-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_async_sqlalchemy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60d7159f43d21e79d7051841fd2d6e094b7267ddc8d7192daea597afca938b12 |
|
MD5 | b101c817be92901e108ac640946bb79c |
|
BLAKE2b-256 | 6514d1abbf8dcb11097a7108be8eb4b598c958fc49eb91c5f62449ca9dda489a |