Skip to content

Commit

Permalink
Test additional variant payload types and split up to 1 file per method
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Dec 22, 2024
1 parent e974188 commit 3b78bd6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import pytest
from typing import Any
from unittest.mock import Mock

import sentry_sdk
from sentry_sdk.integrations.unleash import UnleashIntegration

import pytest


@pytest.fixture
def mock_unleash_client():
features = {
"hello": True,
"world": False,
}

def is_enabled(feature: str, *a, **kw) -> bool:
return features.get(feature, False)

feature_to_variant = {
"str_feature": {
"string_feature": {
"name": "variant1",
"enabled": True,
"payload": {"type": "string", "value": "val1"},
},
"json_feature": {
"name": "variant1",
"enabled": True,
"payload": {"type": "json", "value": {"key1": 0.53}},
"payload": {"type": "json", "value": '{"key1": 0.53}'},
},
"number_feature": {
"name": "variant1",
"enabled": True,
"payload": {"type": "number", "value": "134.5"},
},
"csv_feature": {
"name": "variant1",
"enabled": True,
"payload": {"type": "csv", "value": "abc 123\ncsbq 94"},
},
"toggle_feature": {"name": "variant1", "enabled": True},
}
Expand All @@ -37,43 +38,21 @@ def get_variant(feature: str, *a, **kw) -> dict[str, Any]:
return feature_to_variant.get(feature, disabled_variant)

client = Mock()
client.is_enabled = is_enabled
client.get_variant = get_variant
return client


def test_is_enabled(
sentry_init, capture_events, uninstall_integration, mock_unleash_client
):
uninstall_integration(UnleashIntegration.identifier)
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])

mock_unleash_client.is_enabled("hello")
mock_unleash_client.is_enabled("world")
mock_unleash_client.is_enabled("other")

events = capture_events()
sentry_sdk.capture_exception(Exception("something wrong!"))

assert len(events) == 1
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "world", "result": False},
{"flag": "other", "result": False},
]
}


def test_get_variant(
sentry_init, capture_events, uninstall_integration, mock_unleash_client
):
uninstall_integration(UnleashIntegration.identifier)
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])

mock_unleash_client.get_variant("toggle_feature")
mock_unleash_client.get_variant("str_feature")
mock_unleash_client.get_variant("string_feature")
mock_unleash_client.get_variant("json_feature")
mock_unleash_client.get_variant("csv_feature")
mock_unleash_client.get_variant("number_feature")
mock_unleash_client.get_variant("unknown_feature")

events = capture_events()
Expand Down
43 changes: 43 additions & 0 deletions tests/integrations/unleash/test_unleash_is_enabled_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
from unittest.mock import Mock

import sentry_sdk
from sentry_sdk.integrations.unleash import UnleashIntegration


@pytest.fixture
def mock_unleash_client():
features = {
"hello": True,
"world": False,
}

def is_enabled(feature: str, *a, **kw) -> bool:
return features.get(feature, False)

client = Mock()
client.is_enabled = is_enabled
return client


def test_is_enabled(
sentry_init, capture_events, uninstall_integration, mock_unleash_client
):
uninstall_integration(UnleashIntegration.identifier)
sentry_init(integrations=[UnleashIntegration(mock_unleash_client)])

mock_unleash_client.is_enabled("hello")
mock_unleash_client.is_enabled("world")
mock_unleash_client.is_enabled("other")

events = capture_events()
sentry_sdk.capture_exception(Exception("something wrong!"))

assert len(events) == 1
assert events[0]["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": True},
{"flag": "world", "result": False},
{"flag": "other", "result": False},
]
}

0 comments on commit 3b78bd6

Please sign in to comment.