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

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

Open
lsabi opened this issue Jun 12, 2020 · 0 comments
Open

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

lsabi opened this issue Jun 12, 2020 · 0 comments

Comments

@lsabi
Copy link

@lsabi lsabi commented Jun 12, 2020

  • 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.