You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to create tests for a fast_api websocket endpoint as shown with below snippet.
importasyncioimportpytestfromsqlalchemyimport (
Column,
Text
)
fromgino.ext.starletteimportGinofromstarlette.websocketsimportWebSocketDisconnectfromfastapiimportDepends, FastAPI, WebSocketfromfastapi.testclientimportTestClientfromfastapiimportstatusdb=Gino(
dsn='postgresql://postgres:@0.0.0.0:5432/internal',
pool_min_size=1,
pool_max_size=16,
echo=False,
ssl=None,
use_connection_for_request=True,
retry_limit=1,
retry_interval=1,
)
app=FastAPI()
db.init_app(app)
classUser(db.Model):
__tablename__="client_user"__table_args__= {"schema": 'internal'}
alias="client_user"username=Column(Text, nullable=False)
password=Column(Text)
asyncdefget_current_user(websocket: WebSocket):
# for simplicity sake username is passed to sec-websocket-protocol instead of JWTusername=websocket.headers.get('sec-websocket-protocol')
# database content is# admin/passuser=awaitUser.query.where(User.username==username).gino.first()
returnuser@app.websocket("/websocket")asyncdefwebsocket_endpoint(
websocket: WebSocket,
user: User=Depends(get_current_user),
):
ifuserisNone:
awaitwebsocket.close(code=status.WS_1008_POLICY_VIOLATION)
returnawaitwebsocket.accept()
print(f'User: {user.username} connected')
@pytest.fixture(scope="session")defevent_loop():
""" This is to make the asyncio event loop shared for the whole test session, otherwise it will be recreated for each test which will prevent using the test_db fixture. https://github.com/FactoryBoy/factory_boy/issues/679 https://stackoverflow.com/a/56238383 """loop=asyncio.get_event_loop()
yieldlooploop.close()
@pytest.fixturedefclient():
"""Get client for test app"""withTestClient(app) asclient:
yieldclientdeftest_invalid_user(client):
withpytest.raises(WebSocketDisconnect):
withclient.websocket_connect("/websocket", ['fake_user']):
assertTruedeftest_valid_user(client):
withclient.websocket_connect("/websocket", ['admin']):
assertTrue
Description
I am trying to create tests for a fast_api websocket endpoint as shown with below snippet.
What I Did
I run test with below
here's the traceback
The text was updated successfully, but these errors were encountered: