AttributeError: 'NoneType' object has no attribute 'send' Fastapi Pytest
import pytest
from app import main
from httpx import AsyncClient, ASGITransport
BASE_URL = "http://localhost:8000/users"
@pytest.fixture
def anyio_backend():
return "asyncio"
@pytest.mark.anyio
async def test_read_users():
async with AsyncClient(
transport=ASGITransport(app=main.app), base_url=BASE_URL
) as ac:
response = await ac.get("/")
assert response.status_code == 200
assert "password" not in response.json()
@pytest.mark.anyio
async def test_create_new_user():
test_user = {
"name": "test_name",
"phone_number": "+380950000000",
"email": "testemail@gmail.com",
"password": "testpassword",
"avatar_url": "static/avatars/d-avatar.jpg",
}
async with AsyncClient(
transport=ASGITransport(app=main.app), base_url=BASE_URL
) as ac:
create_response = await ac.post("/create/", json=test_user)
assert create_response.status_code == 201
created_user = create_response.json()
assert created_user["name"] == test_user["name"]
user_id = created_user["id"]
delete_response = await ac.delete(f"/delete/{user_id}")
assert delete_response.status_code in (200, 204)
tests\routers\users\users_test.py .F [100%] FAILURES test_create_new_user
self = , callback = <bound method BaseProtocol._on_waiter_completed of <asyncpg.protocol.protocol.Protocol object at 0x000001EF36FF78B0>> context = <_contextvars.Context object at 0x000001EF3725A0C0>, args = (<Future finished exception=AttributeError("'NoneType' object has no attribute 'send'")>,)
def call_soon(self, callback, *args, context=None):
"""Arrange for a callback to be called as soon as possible.
This operates as a FIFO queue: callbacks are called in the
order in which they are registered. Each callback will be
called exactly once.
Any positional arguments after the callback will be passed to
the callback when it is called.
"""
self._check_closed()
C:\Users\denis\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py:833:
self =
def _check_closed(self):
if self._closed:
raise RuntimeError('Event loop is closed')
E RuntimeError: Event loop is closed
C:\Users\denis\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py:556: RuntimeError During handling of the above exception, another exception occurred:
@pytest.mark.anyio
async def test_create_new_user():
test_user = {
"name": "test_name",
"phone_number": "+380950000000",
"email": "testemail@gmail.com",
"password": "testpassword",
"avatar_url": "static/avatars/d-avatar.jpg",
}
async with AsyncClient(
transport=ASGITransport(app=main.app), base_url=BASE_URL
) as ac:
create_response = await ac.post("/create/", json=test_user)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tests\routers\users\users_test.py:34:
self = <_ProactorSocketTransport fd=856 read=<_OverlappedFuture cancelled>>, f = None, data = b'Q\x00\x00\x00\x0bBEGIN;\x00'
def _loop_writing(self, f=None, data=None):
try:
if f is not None and self._write_fut is None and self._closing:
# XXX most likely self._force_close() has been called, and
# it has set self._write_fut to None.
return
assert f is self._write_fut
self._write_fut = None
self._pending_write = 0
if f:
f.result()
if data is None:
data = self._buffer
self._buffer = None
if not data:
if self._closing:
self._loop.call_soon(self._call_connection_lost, None)
if self._eof_written:
self._sock.shutdown(socket.SHUT_WR)
# Now that we've reduced the buffer size, tell the
# protocol to resume writing if it was paused. Note that
# we do this last since the callback is called immediately
# and it may add more data to the buffer (even causing the
# protocol to be paused again).
self._maybe_resume_protocol()
else:
self._write_fut = self._loop._proactor.send(self._sock, data)
^^^^^^^^^^^^^^^^^^^^^^^^^
E AttributeError: 'NoneType' object has no attribute 'send'
C:\Users\denis\AppData\Local\Programs\Python\Python313\Lib\asyncio\proactor_events.py:402: AttributeError ========================================================================================== short test summary info ========================================================================================== FAILED tests/routers/users/users_test.py::test_create_new_user - AttributeError: 'NoneType' object has no attribute 'send' ======================================================================================== 1 failed, 1 passed in 1.94s ========