Skip to content

Commit 0cd97d1

Browse files
committed
Added tests to show flags are not working.
1 parent 23eb439 commit 0cd97d1

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

tests/integrations/fastapi/test_fastapi.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from fastapi.testclient import TestClient
1111
from fastapi.middleware.trustedhost import TrustedHostMiddleware
1212

13+
import sentry_sdk
1314
from sentry_sdk import capture_message
15+
from sentry_sdk.feature_flags import add_feature_flag
1416
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1517
from sentry_sdk.integrations.fastapi import FastApiIntegration
1618
from sentry_sdk.integrations.starlette import StarletteIntegration
@@ -671,3 +673,32 @@ async def subapp_route():
671673
assert event["transaction"] == "/subapp"
672674
else:
673675
assert event["transaction"].endswith("subapp_route")
676+
677+
678+
@pytest.mark.asyncio
679+
async def test_feature_flags(sentry_init, capture_events):
680+
sentry_init(integrations=[StarletteIntegration(), FastApiIntegration()])
681+
682+
events = capture_events()
683+
684+
app = FastAPI()
685+
686+
@app.get("/error")
687+
async def _error():
688+
add_feature_flag("hello", False)
689+
690+
with sentry_sdk.start_span(name="test-span"):
691+
with sentry_sdk.start_span(name="test-span-2"):
692+
raise ValueError("something is wrong!")
693+
694+
try:
695+
client = TestClient(app)
696+
client.get("/error")
697+
except ValueError:
698+
pass
699+
700+
assert events[0]["contexts"]["flags"] == {
701+
"values": [
702+
{"flag": "hello", "result": False},
703+
]
704+
}

tests/test_feature_flags.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,51 @@ def test_featureflags_integration(sentry_init, capture_events, uninstall_integra
3131
}
3232

3333

34+
@pytest.mark.asyncio
35+
async def test_featureflags_integration_spans_async(sentry_init, capture_events):
36+
sentry_init(
37+
traces_sample_rate=1.0,
38+
)
39+
events = capture_events()
40+
41+
add_feature_flag("hello", False)
42+
43+
try:
44+
with sentry_sdk.start_span(name="test-span"):
45+
with sentry_sdk.start_span(name="test-span-2"):
46+
raise ValueError("something wrong!")
47+
except ValueError as e:
48+
sentry_sdk.capture_exception(e)
49+
50+
assert events[0]["contexts"]["flags"] == {
51+
"values": [
52+
{"flag": "hello", "result": False},
53+
]
54+
}
55+
56+
57+
def test_featureflags_integration_spans_sync(sentry_init, capture_events):
58+
sentry_init(
59+
traces_sample_rate=1.0,
60+
)
61+
events = capture_events()
62+
63+
add_feature_flag("hello", False)
64+
65+
try:
66+
with sentry_sdk.start_span(name="test-span"):
67+
with sentry_sdk.start_span(name="test-span-2"):
68+
raise ValueError("something wrong!")
69+
except ValueError as e:
70+
sentry_sdk.capture_exception(e)
71+
72+
assert events[0]["contexts"]["flags"] == {
73+
"values": [
74+
{"flag": "hello", "result": False},
75+
]
76+
}
77+
78+
3479
def test_featureflags_integration_threaded(
3580
sentry_init, capture_events, uninstall_integration
3681
):

0 commit comments

Comments
 (0)