|
1 | | -from unittest import mock |
2 | | - |
3 | 1 | import pytest |
4 | 2 | from pydantic import ValidationError |
5 | | -from pytest_mock import MockerFixture |
6 | 3 |
|
7 | | -from flag_engine.features.constants import STANDARD |
8 | 4 | from flag_engine.features.models import ( |
9 | 5 | FeatureModel, |
10 | 6 | FeatureStateModel, |
@@ -135,108 +131,3 @@ def test_feature_state_get_value_no_mv_values(feature_1: FeatureModel) -> None: |
135 | 131 | # Then |
136 | 132 | # the default value is always returned, even if an identity id is provided |
137 | 133 | assert feature_state.get_value() == feature_state.get_value(1) == value |
138 | | - |
139 | | - |
140 | | -mv_feature_control_value = "control" |
141 | | -mv_feature_value_1 = "foo" |
142 | | -mv_feature_value_2 = "bar" |
143 | | - |
144 | | - |
145 | | -@pytest.mark.parametrize( |
146 | | - "percentage_value, expected_value", |
147 | | - ( |
148 | | - (10, mv_feature_value_1), |
149 | | - (40, mv_feature_value_2), |
150 | | - (70, mv_feature_control_value), |
151 | | - ), |
152 | | -) |
153 | | -@mock.patch("flag_engine.features.models.get_hashed_percentage_for_object_ids") |
154 | | -def test_feature_state_get_value_mv_values( |
155 | | - mock_get_hashed_percentage: mock.Mock, |
156 | | - percentage_value: int, |
157 | | - expected_value: str, |
158 | | -) -> None: |
159 | | - # Given |
160 | | - # a feature |
161 | | - my_feature = FeatureModel(id=1, name="mv_feature", type=STANDARD) |
162 | | - |
163 | | - # with some multivariate feature options |
164 | | - mv_feature_option_1 = MultivariateFeatureOptionModel(id=1, value=mv_feature_value_1) |
165 | | - mv_feature_option_2 = MultivariateFeatureOptionModel(id=2, value=mv_feature_value_2) |
166 | | - |
167 | | - # and associated values |
168 | | - mv_feature_state_value_1 = MultivariateFeatureStateValueModel( |
169 | | - id=1, multivariate_feature_option=mv_feature_option_1, percentage_allocation=30 |
170 | | - ) |
171 | | - mv_feature_state_value_2 = MultivariateFeatureStateValueModel( |
172 | | - id=2, multivariate_feature_option=mv_feature_option_2, percentage_allocation=30 |
173 | | - ) |
174 | | - |
175 | | - # and we assign the above to a feature state |
176 | | - mv_feature_state = FeatureStateModel( |
177 | | - django_id=1, |
178 | | - feature=my_feature, |
179 | | - enabled=True, |
180 | | - multivariate_feature_state_values=[ |
181 | | - mv_feature_state_value_1, |
182 | | - mv_feature_state_value_2, |
183 | | - ], |
184 | | - ) |
185 | | - mv_feature_state.set_value(mv_feature_control_value) |
186 | | - |
187 | | - # and we mock the function which gets the percentage value for an identity to |
188 | | - # return a deterministic value so we know which value to expect |
189 | | - mock_get_hashed_percentage.return_value = percentage_value |
190 | | - |
191 | | - # Then |
192 | | - # the value of the feature state is correct based on the percentage value returned |
193 | | - assert mv_feature_state.get_value(identity_id=1) == expected_value |
194 | | - |
195 | | - |
196 | | -def test_get_value_uses_django_id_for_multivariate_value_calculation_if_not_none( |
197 | | - feature_1: FeatureModel, |
198 | | - mv_feature_state_value: MultivariateFeatureStateValueModel, |
199 | | - mocker: MockerFixture, |
200 | | -) -> None: |
201 | | - # Given |
202 | | - mocked_get_hashed_percentage = mocker.patch( |
203 | | - "flag_engine.features.models.get_hashed_percentage_for_object_ids", |
204 | | - return_value=10, |
205 | | - ) |
206 | | - identity_id = 1 |
207 | | - feature_state = FeatureStateModel( |
208 | | - django_id=1, |
209 | | - feature=feature_1, |
210 | | - enabled=True, |
211 | | - multivariate_feature_state_values=[mv_feature_state_value], |
212 | | - ) |
213 | | - # When |
214 | | - feature_state.get_value(identity_id=identity_id) |
215 | | - # Then |
216 | | - mocked_get_hashed_percentage.assert_called_with( |
217 | | - [feature_state.django_id, identity_id] |
218 | | - ) |
219 | | - |
220 | | - |
221 | | -def test_get_value_uses_featuestate_uuid_for_multivariate_value_calculation_if_django_id_is_not_present( |
222 | | - feature_1: FeatureModel, |
223 | | - mv_feature_state_value: MultivariateFeatureStateValueModel, |
224 | | - mocker: MockerFixture, |
225 | | -) -> None: |
226 | | - # Given |
227 | | - mocked_get_hashed_percentage = mocker.patch( |
228 | | - "flag_engine.features.models.get_hashed_percentage_for_object_ids", |
229 | | - return_value=10, |
230 | | - ) |
231 | | - identity_id = 1 |
232 | | - feature_state = FeatureStateModel( |
233 | | - feature=feature_1, |
234 | | - enabled=True, |
235 | | - multivariate_feature_state_values=[mv_feature_state_value], |
236 | | - ) |
237 | | - # When |
238 | | - feature_state.get_value(identity_id=identity_id) |
239 | | - # Then |
240 | | - mocked_get_hashed_percentage.assert_called_with( |
241 | | - [feature_state.featurestate_uuid, identity_id] |
242 | | - ) |
0 commit comments