I am trying to execute this unit test. I would like to asynchronously await the GET as it takes a while to retrieve data. However, I am getting: TypeError: object Response can't be used in 'await' expression
.
@pytest.mark.asyncio
async def test_get_report(report_service, client, headers):
"""Test Report GET Response"""
report_id = "sample-report-id"
report_service.query_db.query_fetchone = mock.AsyncMock(
return_value={
"id": "sample-id",
"reportId": "sample-report-id",
"reportName": "sample-report-name",
"report": [],
}
)
response = await client.get(f"/v2/report/{report_id }", headers=headers)
assert response.status_code == 200
If I remove the await
, I will get this response {'detail': 'Not Found'}
.
client.get is not an asynchronous function, that is why you are getting the error. I guess you are using FastAPI, that ‘.get’ function is synchronous. In your case i guess the following might work:
@pytest.mark.asyncio
async def test_get_report(report_service, client, headers):
"""Test Report GET Response"""
report_id = "sample-report-id"
report_service.query_db.query_fetchone = mock.AsyncMock(
return_value={
"id": "sample-id",
"reportId": "sample-report-id",
"reportName": "sample-report-name",
"report": [],
}
)
async with client.get(f"/v2/report/{report_id}", headers=headers) as response:
assert response.status == 200
Hope this works.