Skip to content

Commit d40ff60

Browse files
authored
Fix starlette tests for new release (#256)
* Fix starlette tests for new release * Fix python version specific failures
1 parent 208344e commit d40ff60

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

tests/framework_starlette/test_application.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import sys
16+
1517
import pytest
1618
import starlette
19+
20+
from newrelic.common.object_names import callable_name
1721
from testing_support.fixtures import (
1822
override_ignore_status_codes,
1923
validate_transaction_errors,
@@ -88,17 +92,32 @@ def _test():
8892

8993

9094
@pytest.mark.parametrize("app_name", ("no_error_handler",))
91-
@validate_transaction_metrics(
92-
"_test_application:middleware_factory.<locals>.middleware",
93-
scoped_metrics=[
94-
("Function/_test_application:middleware_factory.<locals>.middleware", 1)
95-
],
96-
rollup_metrics=[FRAMEWORK_METRIC],
97-
)
9895
def test_exception_in_middleware(target_application, app_name):
9996
app = target_application[app_name]
100-
with pytest.raises(ValueError):
101-
app.get("/crash_me_now")
97+
98+
from starlette import __version__ as version
99+
starlette_version = tuple(int(v) for v in version.split("."))
100+
101+
# Starlette >=0.15 raises an exception group instead of reraising the ValueError
102+
# This only occurs on Python versions >=3.8
103+
if sys.version_info[0:2] > (3, 7) and starlette_version >= (0, 15, 0):
104+
from anyio._backends._asyncio import ExceptionGroup
105+
exc_type = ExceptionGroup
106+
else:
107+
exc_type = ValueError
108+
109+
@validate_transaction_metrics(
110+
"_test_application:middleware_factory.<locals>.middleware",
111+
scoped_metrics=[
112+
("Function/_test_application:middleware_factory.<locals>.middleware", 1)
113+
],
114+
rollup_metrics=[FRAMEWORK_METRIC],
115+
)
116+
@validate_transaction_errors(errors=[callable_name(exc_type)])
117+
def _test():
118+
with pytest.raises(exc_type): # Later versions of starlette
119+
app.get("/crash_me_now")
120+
_test()
102121

103122

104123
@pytest.mark.parametrize(

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ envlist =
120120
python-framework_pyramid-{pypy3,py36,py37,py38,py39}-Pyramidmaster,
121121
python-framework_sanic-{py38,pypy3}-sanic{190301,1906,1812,1912,200904,210300}
122122
python-framework_sanic-{py36,py37,py38,pypy3}-saniclatest
123-
python-framework_starlette-{py36,py37,py38,py39,pypy3},
123+
python-framework_starlette-{py36,py37,py38,py39,pypy3}-starlette{0014,latest},
124124
libcurl-framework_tornado-{py36,py37,py38,py39,pypy3}-tornado0600,
125125
libcurl-framework_tornado-{py36,py37,py38,py39,pypy3}-tornadomaster,
126126
rabbitmq-messagebroker_pika-{py27,py36,py37,py38,py39,pypy,pypy3}-pika{0.13,latest},
@@ -254,7 +254,8 @@ deps =
254254
framework_sanic-sanic210300: sanic<21.3.1
255255
framework_sanic-saniclatest: sanic
256256
framework_sanic-sanic{1812,190301,1906}: aiohttp
257-
framework_starlette: starlette
257+
framework_starlette-starlette0014: starlette<0.15
258+
framework_starlette-starlettelatest: starlette
258259
framework_tornado: pycurl
259260
framework_tornado-tornado0600: tornado<6.1
260261
framework_tornado-tornadomaster: https://github.com/tornadoweb/tornado/archive/master.zip

0 commit comments

Comments
 (0)