Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 31e4818

Browse files
add tests
1 parent 6b6e887 commit 31e4818

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

tests/unit/test_rollouts.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
from unittest.mock import PropertyMock, patch
1+
from unittest.mock import Mock, PropertyMock, patch
22

33
from django.test import TestCase
44

5-
from shared.django_apps.rollouts.models import FeatureFlag, FeatureFlagVariant
5+
from shared.django_apps.rollouts.models import (
6+
FeatureExposure,
7+
FeatureFlag,
8+
FeatureFlagVariant,
9+
)
610
from shared.rollouts import Feature
711

812

@@ -133,3 +137,49 @@ def test_return_values_for_each_bucket(self):
133137
with patch("mmh3.hash128", side_effect=[33, 66]):
134138
assert feature.check_value(owner_id=123, default="c") == "first bucket"
135139
assert feature.check_value(owner_id=123, default="c") == "second bucket"
140+
141+
142+
class TestFeatureExposures(TestCase):
143+
def test_exposure_created(self):
144+
complex = FeatureFlag.objects.create(
145+
name="my_feature", proportion=1.0, salt="random_salt"
146+
)
147+
enabled = FeatureFlagVariant.objects.create(
148+
name="enabled", feature_flag=complex, proportion=1.0, value=True
149+
)
150+
FeatureFlagVariant.objects.create(
151+
name="disabled", feature_flag=complex, proportion=0, value=False
152+
)
153+
154+
owner_id = 123123123
155+
156+
MY_FEATURE = Feature("my_feature")
157+
MY_FEATURE.check_value(owner_id=owner_id)
158+
159+
exposure = FeatureExposure.objects.all().first()
160+
161+
assert exposure is not None
162+
assert exposure.owner == owner_id
163+
assert exposure.feature_flag == complex
164+
assert exposure.feature_flag_variant == enabled
165+
166+
def test_exposure_not_created(self):
167+
complex = FeatureFlag.objects.create(
168+
name="my_feature", proportion=1.0, salt="random_salt"
169+
)
170+
FeatureFlagVariant.objects.create(
171+
name="enabled", feature_flag=complex, proportion=0, value=True
172+
)
173+
174+
with patch.object(Feature, "create_exposure") as create_exposure:
175+
owner_id = 123123123
176+
177+
MY_FEATURE = Feature("my_feature")
178+
MY_FEATURE.check_value(owner_id=owner_id)
179+
180+
exposure = FeatureExposure.objects.first()
181+
182+
# Should not create an exposure because the owner was not exposed to any
183+
# explicit variant, it was assigned the default behaviour
184+
assert exposure is None
185+
create_exposure.assert_not_called()

0 commit comments

Comments
 (0)