Skip to content

[QUESTION] Testing pool __aexit__ error on context manager #589

@lsabi

Description

@lsabi
  • asyncpg version: 0.20.1
  • PostgreSQL version: 12
  • Python version: 3.8.2
  • Platform: Ubuntu 20.04
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes

I'm using pytest-asyncio for testing some functions that I have. Below an example

# import the pool of connections (already initialized), called db_pool
async def my_fun():
    try:
        async with db_pool.acquire() as conn:
            async with conn.transaction():
                res = await conn.fetchval(query)
    except Exception as e:
        print(e)
        raise e

    return res

I've tested it manually and it works perfectly, but now I'm trying to automate the testing process via pytest and pytest-asyncio.

# import all the stuff that I need
@patch("app.api.db_pool")
@pytest.mark.asyncio
async def test_my_fun(db_pool):
    db_pool.acquire.return_value.__aenter__.transaction.return_value.__aenter__.fetch.return_value = 1

    res = await my_fun()

    assert res == 1

Then I run my tests with
pytest

and I get the following error

>           async with db_pool.acquire() as conn:
E           AttributeError: __aexit__

app/api.py:57: AttributeError
------------------------------------ Captured stdout call ------------------------------------
__aexit__
====================================== warnings summary ======================================
RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
    async with db_pool.acquire() as conn:

================================== short test summary info ===================================
FAILED tests/unit/api.py::test_my_fun - AttributeError: __aexit__

Am I doing tests wrongly or is it a missing feature?

Thanks.

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