Skip to content

Database transactions with asyncio #580

@dgilge

Description

@dgilge

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions