Skip to content

Commit 2c4e880

Browse files
committed
test: improve coverage by removing unnecessary pragma no cover annotations
1 parent f260dcf commit 2c4e880

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/functions_framework/execution_id.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _get_current_context():
5454
context = execution_context_var.get()
5555
if context is not None:
5656
return context
57-
return ( # pragma: no cover
57+
return (
5858
flask.g.execution_id_context
5959
if flask.has_request_context() and "execution_id_context" in flask.g
6060
else None
@@ -188,7 +188,7 @@ async def async_wrapper(request, *args, **kwargs):
188188
if inspect.iscoroutinefunction(view_function):
189189
result = await view_function(request, *args, **kwargs)
190190
else:
191-
result = view_function(request, *args, **kwargs) # pragma: no cover
191+
result = view_function(request, *args, **kwargs)
192192

193193
# Only reset context on successful completion
194194
# On exception, leave context available for exception handlers

tests/test_execution_id_async.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,22 @@ def sync_func(request):
284284
result = wrapped(request)
285285

286286
assert result == {"status": "ok"}
287+
288+
289+
def test_sync_function_called_from_async_context():
290+
"""Test that a sync function works correctly when called from async ASGI app."""
291+
source = TEST_FUNCTIONS_DIR / "execution_id" / "async_main.py"
292+
target = "sync_function_in_async_context"
293+
app = create_asgi_app(target, source)
294+
client = TestClient(app)
295+
resp = client.post(
296+
"/",
297+
headers={
298+
"Function-Execution-Id": TEST_EXECUTION_ID,
299+
"Content-Type": "application/json",
300+
},
301+
)
302+
303+
result = resp.json()
304+
assert result["execution_id"] == TEST_EXECUTION_ID
305+
assert result["type"] == "sync"

tests/test_functions/execution_id/async_main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ async def async_trace_test(request):
4242
"execution_id": context.execution_id if context else None,
4343
"span_id": context.span_id if context else None,
4444
}
45+
46+
47+
# Sync function to test the sync branch in async decorator
48+
def sync_function_in_async_context(request):
49+
"""A sync function that can be called from async context."""
50+
return {"execution_id": request.headers.get("Function-Execution-Id"), "type": "sync"}

0 commit comments

Comments
 (0)