-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_evaluator_target.py
80 lines (67 loc) · 3.5 KB
/
test_evaluator_target.py
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
from ldclient.client import Context
from testing.builders import *
from testing.impl.evaluator_util import *
FALLTHROUGH_VAR = 0
MATCH_VAR_1 = 1
MATCH_VAR_2 = 2
VARIATIONS = ['fallthrough', 'match1', 'match2']
def assert_match_clause(clause: dict, context: Context, should_match: bool):
assert_match(basic_evaluator, make_boolean_flag_with_clauses(clause), context, should_match)
def base_flag_builder() -> FlagBuilder:
return FlagBuilder('feature').on(True).variations(*VARIATIONS) \
.fallthrough_variation(FALLTHROUGH_VAR).off_variation(FALLTHROUGH_VAR)
def expect_match(flag: dict, context: Context, variation: int):
result = basic_evaluator.evaluate(flag, context, event_factory)
assert result.detail.variation_index == variation
assert result.detail.value == VARIATIONS[variation]
assert result.detail.reason == {'kind': 'TARGET_MATCH'}
def expect_fallthrough(flag: dict, context: Context):
result = basic_evaluator.evaluate(flag, context, event_factory)
assert result.detail.variation_index == FALLTHROUGH_VAR
assert result.detail.value == VARIATIONS[FALLTHROUGH_VAR]
assert result.detail.reason == {'kind': 'FALLTHROUGH'}
class TestEvaluatorTarget:
def test_user_targets_only(self):
flag = base_flag_builder() \
.target(MATCH_VAR_1, 'c') \
.target(MATCH_VAR_2, 'b', 'a') \
.build()
expect_match(flag, Context.create('a'), MATCH_VAR_2)
expect_match(flag, Context.create('b'), MATCH_VAR_2)
expect_match(flag, Context.create('c'), MATCH_VAR_1)
expect_fallthrough(flag, Context.create('z'))
# in a multi-kind context, these targets match only the key for the user kind
expect_match(flag,
Context.create_multi(Context.create('b', 'dog'), Context.create('a')),
MATCH_VAR_2)
expect_match(flag,
Context.create_multi(Context.create('a', 'dog'), Context.create('c')),
MATCH_VAR_1)
expect_fallthrough(flag,
Context.create_multi(Context.create('b', 'dog'), Context.create('z')))
expect_fallthrough(flag,
Context.create_multi(Context.create('a', 'dog'), Context.create('b', 'cat')))
def test_user_targets_and_context_targets(self):
flag = base_flag_builder() \
.target(MATCH_VAR_1, 'c') \
.target(MATCH_VAR_2, 'b', 'a') \
.context_target('dog', MATCH_VAR_1, 'a', 'b') \
.context_target('dog', MATCH_VAR_2, 'c') \
.context_target(Context.DEFAULT_KIND, MATCH_VAR_1) \
.context_target(Context.DEFAULT_KIND, MATCH_VAR_2) \
.build()
expect_match(flag, Context.create('a'), MATCH_VAR_2)
expect_match(flag, Context.create('b'), MATCH_VAR_2)
expect_match(flag, Context.create('c'), MATCH_VAR_1)
expect_fallthrough(flag, Context.create('z'))
expect_match(flag,
Context.create_multi(Context.create('b', 'dog'), Context.create('a')),
MATCH_VAR_1) # the "dog" target takes precedence due to ordering
expect_match(flag,
Context.create_multi(Context.create('z', 'dog'), Context.create('a')),
MATCH_VAR_2) # "dog" targets don't match, continue to "user" targets
expect_fallthrough(flag,
Context.create_multi(Context.create('x', 'dog'), Context.create('z'))) # nothing matches
expect_match(flag,
Context.create_multi(Context.create('a', 'dog'), Context.create('b', 'cat')),
MATCH_VAR_1)