-
-
Notifications
You must be signed in to change notification settings - Fork 953
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
tests: Add skipped coverages for websockets.py
and templating.py
using branch=true
#2793
Conversation
It's all good. :) Thanks for the help. |
tests/test_websockets.py
Outdated
def test_websocket_accept_with_mocked_connected_state(test_client_factory: TestClientFactory) -> None: | ||
async def app(scope: Scope, receive: Receive, send: Send) -> None: | ||
websocket = WebSocket(scope, receive=receive, send=send) | ||
websocket.client_state = WebSocketState.CONNECTED | ||
await websocket.accept() | ||
await websocket.send_json({"url": str(websocket.url)}) | ||
await websocket.close() | ||
|
||
client = test_client_factory(app) | ||
with client.websocket_connect("/123?a=abc") as websocket: | ||
data = websocket.receive_json() | ||
assert data == {"url": "ws://testserver/123?a=abc"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to have tests that the only way to reproduce the path is with mocks.
The right way I believe would be to remove the conditional in this case, but I actually think it helps on the understanding of the states there. Can we just have a pragma: no branch
there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no branch
, not no cover
👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that
starlette/templating.py
Outdated
@@ -99,7 +99,7 @@ def __init__( | |||
if directory is not None: | |||
self.env = self._create_env(directory, **env_options) | |||
elif env is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have this instead?
elif env is not None: | |
elif env is not None: # pragma: no branch |
Vlw. :) |
Summary
Regarding the issue #2452
Changes to
starlette/websockets.py
This test is designed to skip the
if
statement at line 106. Specifically, theclient_state
is set toCONNECTED
before callingwebsocket.accept()
, which ensures that the code bypasses the conditional logic meant for theCONNECTING
state.I replicated the first test and mocked the value.
Changes to
starlette/templating.py
At line 97, an assertion ensures that either
directory
orenv
must be provided, but not bothNone
. Since it's not possible to skip both conditionals (if
orelif
), I added a# pragma: no cover
comment to prevent code coverage from flagging this as untested.EDIT: Another option could be to separate the conditional blocks as shown below, ensuring that all branches are covered and that
directory
takes precedence overenv
if both are provided, as the current code behaves.Regarding the precedence of the
directory
overenv
, would it be worth adding this to the documentation?Checklist