|
1 |
| -from unittest.mock import PropertyMock, patch |
| 1 | +from unittest.mock import Mock, PropertyMock, patch |
2 | 2 |
|
3 | 3 | from django.test import TestCase
|
4 | 4 |
|
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 | +) |
6 | 10 | from shared.rollouts import Feature
|
7 | 11 |
|
8 | 12 |
|
@@ -133,3 +137,49 @@ def test_return_values_for_each_bucket(self):
|
133 | 137 | with patch("mmh3.hash128", side_effect=[33, 66]):
|
134 | 138 | assert feature.check_value(owner_id=123, default="c") == "first bucket"
|
135 | 139 | 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