|
16 | 16 |
|
17 | 17 | import pytest |
18 | 18 | import starlette |
19 | | - |
20 | | -from newrelic.common.object_names import callable_name |
21 | 19 | from testing_support.fixtures import ( |
22 | 20 | override_ignore_status_codes, |
23 | 21 | validate_transaction_errors, |
24 | 22 | validate_transaction_metrics, |
25 | 23 | ) |
26 | 24 |
|
| 25 | +from newrelic.common.object_names import callable_name |
| 26 | + |
27 | 27 |
|
28 | 28 | @pytest.fixture(scope="session") |
29 | 29 | def target_application(): |
@@ -96,27 +96,28 @@ def test_exception_in_middleware(target_application, app_name): |
96 | 96 | app = target_application[app_name] |
97 | 97 |
|
98 | 98 | from starlette import __version__ as version |
| 99 | + |
99 | 100 | starlette_version = tuple(int(v) for v in version.split(".")) |
100 | 101 |
|
101 | | - # Starlette >=0.15 raises an exception group instead of reraising the ValueError |
| 102 | + # Starlette >=0.15 and <0.17 raises an exception group instead of reraising the ValueError |
102 | 103 | # This only occurs on Python versions >=3.8 |
103 | | - if sys.version_info[0:2] > (3, 7) and starlette_version >= (0, 15, 0): |
| 104 | + if sys.version_info[0:2] > (3, 7) and starlette_version >= (0, 15, 0) and starlette_version < (0, 17, 0): |
104 | 105 | from anyio._backends._asyncio import ExceptionGroup |
| 106 | + |
105 | 107 | exc_type = ExceptionGroup |
106 | 108 | else: |
107 | 109 | exc_type = ValueError |
108 | 110 |
|
109 | 111 | @validate_transaction_metrics( |
110 | 112 | "_test_application:middleware_factory.<locals>.middleware", |
111 | | - scoped_metrics=[ |
112 | | - ("Function/_test_application:middleware_factory.<locals>.middleware", 1) |
113 | | - ], |
| 113 | + scoped_metrics=[("Function/_test_application:middleware_factory.<locals>.middleware", 1)], |
114 | 114 | rollup_metrics=[FRAMEWORK_METRIC], |
115 | 115 | ) |
116 | 116 | @validate_transaction_errors(errors=[callable_name(exc_type)]) |
117 | 117 | def _test(): |
118 | 118 | with pytest.raises(exc_type): # Later versions of starlette |
119 | 119 | app.get("/crash_me_now") |
| 120 | + |
120 | 121 | _test() |
121 | 122 |
|
122 | 123 |
|
@@ -161,14 +162,10 @@ def _test(): |
161 | 162 | ), |
162 | 163 | ) |
163 | 164 | @validate_transaction_errors(errors=["builtins:RuntimeError"]) |
164 | | -def test_server_error_middleware( |
165 | | - target_application, app_name, transaction_name, path, scoped_metrics |
166 | | -): |
| 165 | +def test_server_error_middleware(target_application, app_name, transaction_name, path, scoped_metrics): |
167 | 166 | @validate_transaction_metrics( |
168 | 167 | transaction_name, |
169 | | - scoped_metrics=scoped_metrics |
170 | | - + [("Function/_test_application:runtime_error", 1)] |
171 | | - + DEFAULT_MIDDLEWARE_METRICS, |
| 168 | + scoped_metrics=scoped_metrics + [("Function/_test_application:runtime_error", 1)] + DEFAULT_MIDDLEWARE_METRICS, |
172 | 169 | rollup_metrics=[FRAMEWORK_METRIC], |
173 | 170 | ) |
174 | 171 | def _test(): |
@@ -196,9 +193,7 @@ def _test(): |
196 | 193 | ), |
197 | 194 | ), |
198 | 195 | ) |
199 | | -def test_application_handled_error( |
200 | | - target_application, app_name, transaction_name, path, error |
201 | | -): |
| 196 | +def test_application_handled_error(target_application, app_name, transaction_name, path, error): |
202 | 197 | @validate_transaction_errors(errors=[error]) |
203 | 198 | @validate_transaction_metrics( |
204 | 199 | transaction_name, |
@@ -229,9 +224,7 @@ def _test(): |
229 | 224 | ), |
230 | 225 | ) |
231 | 226 | @override_ignore_status_codes(set((500,))) |
232 | | -def test_application_ignored_error( |
233 | | - target_application, app_name, transaction_name, path |
234 | | -): |
| 227 | +def test_application_ignored_error(target_application, app_name, transaction_name, path): |
235 | 228 | @validate_transaction_errors(errors=[]) |
236 | 229 | @validate_transaction_metrics( |
237 | 230 | transaction_name, |
@@ -276,9 +269,7 @@ def _test(): |
276 | 269 |
|
277 | 270 | @pytest.mark.parametrize("app_name", ("no_middleware",)) |
278 | 271 | @validate_transaction_errors(errors=["builtins:RuntimeError"]) |
279 | | -@validate_transaction_metrics( |
280 | | - "_test_application:CustomRoute", rollup_metrics=[FRAMEWORK_METRIC] |
281 | | -) |
| 272 | +@validate_transaction_metrics("_test_application:CustomRoute", rollup_metrics=[FRAMEWORK_METRIC]) |
282 | 273 | def test_starlette_http_exception_after_response_start(target_application, app_name): |
283 | 274 | app = target_application[app_name] |
284 | 275 | with pytest.raises(RuntimeError): |
|
0 commit comments