#๐Ÿ”’ FAILED test_project.py::test_get_workout_plans - TypeError: 'async_generator' object does not supp

53 messages ยท Page 1 of 1 (latest)

lethal vigilBOT
#

Hey @wheat sandal!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
#

@wheat sandal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wheat sandal
#

FAILED test_project.py::test_get_workout_plans - AttributeError: 'async_generator' object has no att

empty compass
#

@wheat sandal can you paste your code in the pastebin service?

#

!paste

lethal vigilBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

wheat sandal
#
import pytest
from httpx import AsyncClient
from project import app as fastapi_app
from fastapi import Request
from fastapi_azure_auth.user import User
from project import azure_scheme

@pytest.fixture
async def normal_user_client():
    async def mock_normal_user(request: Request):
        user = User(
            claims={},
            preferred_username="NormalUser",
            roles=[],
            aud="aud",
            tid="tid",
            access_token="123",
            is_guest=False,
            iat=1537231048,
            nbf=1537231048,
            exp=1537234948,
            iss="iss",
            aio="aio",
            sub="sub",
            oid="oid",
            uti="uti",
            rh="rh",
            ver="2.0",
        )
        request.state.user = user
        return user

    fastapi_app.dependency_overrides[azure_scheme] = mock_normal_user
    async with AsyncClient(app=fastapi_app, base_url='http://test') as ac:
        yield ac

@pytest.mark.asyncio
async def test_get_workout_plans(normal_user_client):
    response = await normal_user_client.get("/workout-plans")
    assert response.status_coode == 200
    assert type(response.json()) == list

Help i wanan do some cllient testing. BTW, I'm using FastAPI-Azure-Auth. That's why this is the state of my client. I just copied this from the documentation, and I'm quite not sure of its usage.I already googled and chatgpt this error.ugm no solution thereof

empty compass
#

And also the exception you get

wheat sandal
#

copy, just accidentally deleted the message

#

FAILED test_project.py::test_get_workout_plans - AttributeError: 'async_generator' object has no attribute 'get'

This is the route that im trying to test

@app.get(
    "/workout-plans",
    response_model=WorkoutPlan_Pydantic_List,
    dependencies=[Security(azure_scheme)],
)
async def get_workout_plans(request: Request) -> list:
    """
    Retrieve workout plans for the authenticated user.

    Args:
        request (Request): The incoming request object.

    Returns:
        list: A list of workout plans in the response model format.

    Raises:
        HTTPException: If the user is unauthorized or if there is an error retrieving the workout plans.
    """

    user = await get_authenticated_user(request)
    return await get_user_workout_plans(user)
empty compass
#

Can you show the traceback that you get?

empty compass
wheat sandal
empty compass
wheat sandal
#

for those using FastAPI-Azure-Auth or thee Azure AD B2C

how would you do the testing..

empty compass
#

You should add:

async with asgi_lifespan.LifespanManager(fastapi_app) as manager:
    async with AsyncClient(app=manager.app, base_url="http://test") as ac:
        yield ac
wheat sandal
empty compass
#

FastAPI already depends on anyio which comes with an async testing pytest plug-in, so you don't need pytest-asyncio

wheat sandal
#

https://paste.pythondiscord.com/7UJQ

I've already tried it but the same error im getting

FAILED test_project.py::test_get_workout_plans - AttributeError: 'async_generator' object has no attribute 'get'

empty compass
#

You didn't try using @pytest.mark.anyio on your test or @pytest_asyncio.fixture for your fixture

#

@wheat sandal ^

#

You need one or the other, not both though

wheat sandal
#

FAILED test_project.py::test_get_workout_plans[asyncio] - RuntimeError: Cannot open a client instance more than once.
ERROR test_project.py::test_get_workout_plans[trio] - ExceptionGroup: Exceptions from Trio nursery (1 sub-exception)

empty compass
#

You need to add:

@pytest.fixture
def anyio_backend():
    return "asyncio"
wheat sandal
#
@pytest.fixture
def anyio_backend():
    return "asyncio"

ill just paste this into my

test_project.py?

empty compass
#

You can put it there or in a conftest.py

wheat sandal
empty compass
#

The lifespan one is probably easiest

wheat sandal
empty compass
#

Show that code?

wheat sandal
main fossil
#

its on tortoise

empty compass
#

You're supposed to use RegisterTortoise class instead

wheat sandal
#
RegisterTortoise(
    app,
    config=TORTOISE_ORM,
    generate_schemas=True,
    add_exception_handlers=True,
)

i did try this before but my routes are going bananas and also the pytest throwing a None Type

https://paste.pythondiscord.com/NVOA

empty compass
#

You need to use async with RegisterTortoise(...): in your lifespan context manager

#

You need to pass it to FastAPI

wheat sandal
lethal vigilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ FAILED test_project.py::test_get_workout_plans - AttributeError: 'async_generator' object has no a

lethal vigilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.