-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_segments_evaluator.py
More file actions
386 lines (356 loc) · 13.2 KB
/
test_segments_evaluator.py
File metadata and controls
386 lines (356 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import typing
import pytest
from pytest_lazyfixture import lazy_fixture
from pytest_mock import MockerFixture
from flag_engine.identities.models import IdentityModel
from flag_engine.identities.traits.models import TraitModel
from flag_engine.segments import constants
from flag_engine.segments.evaluator import (
_condition_matches_value,
evaluate_identity_in_segment,
)
from flag_engine.segments.models import (
SegmentConditionModel,
SegmentModel,
SegmentRuleModel,
)
from flag_engine.segments.types import ConditionOperator
from tests.unit.segments.fixtures import (
empty_segment,
identifier,
segment_conditions_and_nested_rules,
segment_identity_override,
segment_multiple_conditions_all,
segment_multiple_conditions_any,
segment_nested_rules,
segment_single_condition,
trait_key_1,
trait_key_2,
trait_key_3,
trait_value_1,
trait_value_2,
trait_value_3,
)
@pytest.mark.parametrize(
"segment, identity_traits, expected_result",
(
(empty_segment, [], False),
(segment_single_condition, [], False),
(
segment_single_condition,
[TraitModel(trait_key=trait_key_1, trait_value=trait_value_1)],
True,
),
(segment_multiple_conditions_all, [], False),
(
segment_multiple_conditions_all,
[TraitModel(trait_key=trait_key_1, trait_value=trait_value_1)],
False,
),
(
segment_multiple_conditions_all,
[
TraitModel(trait_key=trait_key_1, trait_value=trait_value_1),
TraitModel(trait_key=trait_key_2, trait_value=trait_value_2),
],
True,
),
(segment_multiple_conditions_any, [], False),
(
segment_multiple_conditions_any,
[TraitModel(trait_key=trait_key_1, trait_value=trait_value_1)],
True,
),
(
segment_multiple_conditions_any,
[TraitModel(trait_key=trait_key_2, trait_value=trait_value_2)],
True,
),
(
segment_multiple_conditions_any,
[
TraitModel(trait_key=trait_key_1, trait_value=trait_value_1),
TraitModel(trait_key=trait_key_2, trait_value=trait_value_2),
],
True,
),
(segment_nested_rules, [], False),
(
segment_nested_rules,
[TraitModel(trait_key=trait_key_1, trait_value=trait_value_1)],
False,
),
(
segment_nested_rules,
[
TraitModel(trait_key=trait_key_1, trait_value=trait_value_1),
TraitModel(trait_key=trait_key_2, trait_value=trait_value_2),
TraitModel(trait_key=trait_key_3, trait_value=trait_value_3),
],
True,
),
(segment_conditions_and_nested_rules, [], False),
(
segment_conditions_and_nested_rules,
[TraitModel(trait_key=trait_key_1, trait_value=trait_value_1)],
False,
),
(
segment_conditions_and_nested_rules,
[
TraitModel(trait_key=trait_key_1, trait_value=trait_value_1),
TraitModel(trait_key=trait_key_2, trait_value=trait_value_2),
TraitModel(trait_key=trait_key_3, trait_value=trait_value_3),
],
True,
),
(segment_identity_override, [], True),
),
)
def test_identity_in_segment(
segment: SegmentModel,
identity_traits: typing.List[TraitModel],
expected_result: bool,
) -> None:
identity = IdentityModel(
identifier=identifier,
identity_traits=identity_traits,
environment_api_key="api-key",
)
assert evaluate_identity_in_segment(identity, segment) == expected_result
@pytest.mark.parametrize(
"segment_split_value, identity_hashed_percentage, expected_result",
((10, 1, True), (100, 50, True), (0, 1, False), (10, 20, False)),
)
def test_identity_in_segment_percentage_split(
mocker: MockerFixture,
identity: IdentityModel,
segment_split_value: int,
identity_hashed_percentage: int,
expected_result: bool,
) -> None:
# Given
percentage_split_condition = SegmentConditionModel(
operator=constants.PERCENTAGE_SPLIT, value=str(segment_split_value)
)
rule = SegmentRuleModel(
type=constants.ALL_RULE, conditions=[percentage_split_condition]
)
segment = SegmentModel(id=1, name="% split", rules=[rule])
mock_get_hashed_percentage = mocker.patch(
"flag_engine.segments.evaluator.get_hashed_percentage_for_object_ids"
)
mock_get_hashed_percentage.return_value = identity_hashed_percentage
# When
result = evaluate_identity_in_segment(identity=identity, segment=segment)
# Then
assert result == expected_result
@pytest.mark.parametrize(
"operator, property_, expected_result",
(
(constants.IS_SET, lazy_fixture("segment_condition_property"), True),
(constants.IS_NOT_SET, lazy_fixture("segment_condition_property"), False),
(constants.IS_SET, "random_property", False),
(constants.IS_NOT_SET, "random_property", True),
),
)
def test_identity_in_segment_is_set_and_is_not_set(
identity_in_segment: IdentityModel,
operator: ConditionOperator,
property_: str,
expected_result: bool,
) -> None:
# Given
segment_condition_model = SegmentConditionModel(
operator=operator,
property_=property_,
)
rule = SegmentRuleModel(
type=constants.ALL_RULE,
conditions=[segment_condition_model],
)
segment = SegmentModel(id=1, name="segment model", rules=[rule])
# When
result = evaluate_identity_in_segment(identity=identity_in_segment, segment=segment)
# Then
assert result is expected_result
@pytest.mark.parametrize(
"operator, trait_value, condition_value, expected_result",
(
(constants.EQUAL, "bar", "bar", True),
(constants.EQUAL, "bar", "baz", False),
(constants.EQUAL, 1, "1", True),
(constants.EQUAL, 1, "not_an_int", False),
(constants.EQUAL, 1, "2", False),
(constants.EQUAL, True, "True", True),
(constants.EQUAL, False, "False", True),
(constants.EQUAL, False, "True", False),
(constants.EQUAL, True, "False", False),
(constants.EQUAL, 1.23, "1.23", True),
(constants.EQUAL, 1.23, "not_a_float", False),
(constants.EQUAL, 1.23, "4.56", False),
(constants.EQUAL, 2, "not_an_number", False),
(constants.GREATER_THAN, 2, "1", True),
(constants.GREATER_THAN, 1, "1", False),
(constants.GREATER_THAN, 0, "1", False),
(constants.GREATER_THAN, 2.1, "2.0", True),
(constants.GREATER_THAN, 2.1, "2.1", False),
(constants.GREATER_THAN, 2.0, "2.1", False),
(constants.GREATER_THAN, 2, "not_an_number", False),
(constants.GREATER_THAN_INCLUSIVE, 2, "1", True),
(constants.GREATER_THAN_INCLUSIVE, 1, "1", True),
(constants.GREATER_THAN_INCLUSIVE, 0, "1", False),
(constants.GREATER_THAN_INCLUSIVE, 2.1, "2.0", True),
(constants.GREATER_THAN_INCLUSIVE, 2.1, "2.1", True),
(constants.GREATER_THAN_INCLUSIVE, 2.0, "2.1", False),
(constants.GREATER_THAN_INCLUSIVE, 2, "not_an_number", False),
(constants.LESS_THAN, 1, "2", True),
(constants.LESS_THAN, 1, "1", False),
(constants.LESS_THAN, 1, "0", False),
(constants.LESS_THAN, 2.0, "2.1", True),
(constants.LESS_THAN, 2.1, "2.1", False),
(constants.LESS_THAN, 2.1, "2.0", False),
(constants.LESS_THAN, 2, "not_an_number", False),
(constants.LESS_THAN_INCLUSIVE, 1, "2", True),
(constants.LESS_THAN_INCLUSIVE, 1, "1", True),
(constants.LESS_THAN_INCLUSIVE, 1, "0", False),
(constants.LESS_THAN_INCLUSIVE, 2.0, "2.1", True),
(constants.LESS_THAN_INCLUSIVE, 2.1, "2.1", True),
(constants.LESS_THAN_INCLUSIVE, 2.1, "2.0", False),
(constants.LESS_THAN_INCLUSIVE, 2, "not_a_number", False),
(constants.NOT_EQUAL, "bar", "baz", True),
(constants.NOT_EQUAL, "bar", "bar", False),
(constants.NOT_EQUAL, 1, "2", True),
(constants.NOT_EQUAL, 1, "1", False),
(constants.NOT_EQUAL, True, "False", True),
(constants.NOT_EQUAL, False, "True", True),
(constants.NOT_EQUAL, False, "False", False),
(constants.NOT_EQUAL, True, "True", False),
(constants.CONTAINS, "bar", "b", True),
(constants.CONTAINS, "bar", "bar", True),
(constants.CONTAINS, "bar", "baz", False),
(constants.CONTAINS, "bar", 1, False),
(constants.CONTAINS, 1, "1", False),
(constants.NOT_CONTAINS, "bar", "b", False),
(constants.NOT_CONTAINS, "bar", "bar", False),
(constants.NOT_CONTAINS, "bar", "baz", True),
(constants.REGEX, "foo", r"[a-z]+", True),
(constants.REGEX, "FOO", r"[a-z]+", False),
(constants.REGEX, "1.2.3", r"\d", True),
(constants.REGEX, 1, r"\d", True),
(constants.REGEX, None, r"[a-z]", False),
(constants.REGEX, "foo", 12, False),
(constants.REGEX, 1, "1", True),
(constants.IN, "foo", "", False),
(constants.IN, "foo", "foo,bar", True),
(constants.IN, "bar", "foo,bar", True),
(constants.IN, "foo", "foo", True),
(constants.IN, 1, "1,2,3,4", True),
(constants.IN, 1, "", False),
(constants.IN, 1, "1", True),
(constants.IN, 1, None, False),
(constants.IN, 1, None, False),
),
)
def test_segment_condition_matches_value(
operator: ConditionOperator,
trait_value: typing.Union[None, int, str, float],
condition_value: object,
expected_result: bool,
) -> None:
# Given
segment_condition = SegmentConditionModel(
operator=operator,
property_="foo",
value=condition_value,
)
# When
result = _condition_matches_value(segment_condition, trait_value)
# Then
assert result == expected_result
def test_segment_condition__unsupported_operator__return_false(
mocker: MockerFixture,
) -> None:
# Given
mocker.patch("flag_engine.segments.evaluator.MATCH_FUNCS_BY_OPERATOR", new={})
segment_condition = SegmentConditionModel(
operator=constants.EQUAL,
property_="x",
value="foo",
)
trait_value = "foo"
# When
result = _condition_matches_value(segment_condition, trait_value)
# Then
assert result is False
@pytest.mark.parametrize(
"operator, trait_value, condition_value, expected_result",
[
(constants.EQUAL, "1.0.0", "1.0.0:semver", True),
(constants.EQUAL, "not_a_semver", "1.0.0:semver", False),
(constants.EQUAL, "1.0.0", "1.0.1:semver", False),
(constants.NOT_EQUAL, "1.0.0", "1.0.0:semver", False),
(constants.NOT_EQUAL, "1.0.0", "1.0.1:semver", True),
(constants.GREATER_THAN, "1.0.1", "1.0.0:semver", True),
(constants.GREATER_THAN, "1.0.0", "1.0.0-beta:semver", True),
(constants.GREATER_THAN, "1.0.1", "1.2.0:semver", False),
(constants.GREATER_THAN, "1.0.1", "1.0.1:semver", False),
(constants.GREATER_THAN, "1.2.4", "1.2.3-pre.2+build.4:semver", True),
(constants.LESS_THAN, "1.0.0", "1.0.1:semver", True),
(constants.LESS_THAN, "1.0.0", "1.0.0:semver", False),
(constants.LESS_THAN, "1.0.1", "1.0.0:semver", False),
(constants.LESS_THAN, "1.0.0-rc.2", "1.0.0-rc.3:semver", True),
(constants.GREATER_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", True),
(constants.GREATER_THAN_INCLUSIVE, "1.0.1", "1.2.0:semver", False),
(constants.GREATER_THAN_INCLUSIVE, "1.0.1", "1.0.1:semver", True),
(constants.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.1:semver", True),
(constants.LESS_THAN_INCLUSIVE, "1.0.0", "1.0.0:semver", True),
(constants.LESS_THAN_INCLUSIVE, "1.0.1", "1.0.0:semver", False),
],
)
def test_segment_condition_matches_value_for_semver(
operator: ConditionOperator,
trait_value: str,
condition_value: str,
expected_result: bool,
) -> None:
# Given
segment_condition = SegmentConditionModel(
operator=operator,
property_="version",
value=condition_value,
)
# When
result = _condition_matches_value(segment_condition, trait_value)
# Then
assert result == expected_result
@pytest.mark.parametrize(
"trait_value, condition_value, expected_result",
[
(1, "2|0", False),
(2, "2|0", True),
(3, "2|0", False),
(34.2, "4|3", False),
(35.0, "4|3", True),
("dummy", "3|0", False),
("1.0.0", "3|0", False),
(False, "1|3", False),
(1, "invalid|value", False),
(1, None, False),
],
)
def test_segment_condition_matches_value_for_modulo(
trait_value: typing.Union[int, float, str, bool],
condition_value: typing.Optional[str],
expected_result: bool,
) -> None:
# Given
segment_condition = SegmentConditionModel(
operator=constants.MODULO,
property_="version",
value=condition_value,
)
# When
result = _condition_matches_value(segment_condition, trait_value)
# Then
assert result == expected_result