pytest-dev / pytest-dev/pytest-django
Database transactions with asyncio
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
The newly released Channels 2.0 Django package "relies on the asyncio library and native Python async support". They recommend to use py.test tests with the asyncio plugin.
Now, you can use the db and django_db fixtures with asyncio (when you use Channels' database_sync_to_async wrapper):
def create_my_model():
return MyModel.objects.create(name='foo')
@pytest.fixture
async def add_my_model_to_db(db):
return await database_sync_to_async(create_my_model)()
@pytest.mark.asyncio
async def test_my_db_modification(add_my_model_to_db):
assert add_my_model_to_db.name == 'foo'
# ...
However, transactions don't seem to work. As a result all changes to the test database will be kept.
"Note that you can’t mix this with unittest.TestCase subclasses" (which pytest-django does internally – but I don't know if this is of importance concerning this issue, because another asyncio issue seems to be related).
Is there a way to use transactions in an async setting with the current packages? Or are there plans to support transactions in async tests in the near future? Or should this be done in another package?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the shown async fixture with the db or django_db fixture, database_sync_to_async, and the asyncio marker. Inspect how pytest-django cleans up transactions around these tests; done means database changes made by an async test are rolled back rather than retained.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- databases, testing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100