Skip to content

Commit 1e9ba83

Browse files
committed
Added tests to show flags are not working.
1 parent 18a1104 commit 1e9ba83

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
@@ -714,3 +716,32 @@ async def subapp_route():
714716
assert event["transaction"] == "/subapp"
715717
else:
716718
assert event["transaction"].endswith("subapp_route")
719+
720+
721+
@pytest.mark.asyncio
722+
async def test_feature_flags(sentry_init, capture_events):
723+
sentry_init(integrations=[StarletteIntegration(), FastApiIntegration()])
724+
725+
events = capture_events()
726+
727+
app = FastAPI()
728+
729+
@app.get("/error")
730+
async def _error():
731+
add_feature_flag("hello", False)
732+
733+
with sentry_sdk.start_span(name="test-span"):
734+
with sentry_sdk.start_span(name="test-span-2"):
735+
raise ValueError("something is wrong!")
736+
737+
try:
738+
client = TestClient(app)
739+
client.get("/error")
740+
except ValueError:
741+
pass
742+
743+
assert events[0]["contexts"]["flags"] == {
744+
"values": [
745+
{"flag": "hello", "result": False},
746+
]
747+
}

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)