-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_splits.py
More file actions
154 lines (145 loc) · 6.13 KB
/
test_splits.py
File metadata and controls
154 lines (145 loc) · 6.13 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
"""Split model tests module."""
import copy
from splitio.models import splits
from splitio.models.grammar.condition import Condition
class SplitTests(object):
"""Split model tests."""
raw = {
'changeNumber': 123,
'trafficTypeName': 'user',
'name': 'some_name',
'prerequisites': [
{ 'n': 'flag1', 'ts': ['on','v1'] },
{ 'n': 'flag2', 'ts': ['off'] }
],
'trafficAllocation': 100,
'trafficAllocationSeed': 123456,
'seed': 321654,
'status': 'ACTIVE',
'killed': False,
'defaultTreatment': 'off',
'algo': 2,
'conditions': [
{
'partitions': [
{'treatment': 'on', 'size': 50},
{'treatment': 'off', 'size': 50}
],
'contitionType': 'WHITELIST',
'label': 'some_label',
'matcherGroup': {
'matchers': [
{
'matcherType': 'WHITELIST',
'whitelistMatcherData': {
'whitelist': ['k1', 'k2', 'k3']
},
'negate': False,
}
],
'combiner': 'AND'
}
},
{
'partitions': [
{'treatment': 'on', 'size': 25},
{'treatment': 'off', 'size': 75}
],
'contitionType': 'WHITELIST',
'label': 'some_other_label',
'matcherGroup': {
'matchers': [
{
'matcherType': 'ALL_KEYS',
'negate': False,
}
],
'combiner': 'AND'
}
}
],
'configurations': {
'on': '{"color": "blue", "size": 13}'
},
'sets': ['set1', 'set2'],
'impressionsDisabled': False
}
def test_from_raw(self):
"""Test split model parsing."""
parsed = splits.from_raw(self.raw)
assert isinstance(parsed, splits.Split)
assert parsed.change_number == 123
assert parsed.traffic_type_name == 'user'
assert parsed.name == 'some_name'
assert parsed.traffic_allocation == 100
assert parsed.traffic_allocation_seed == 123456
assert parsed.seed == 321654
assert parsed.status == splits.Status.ACTIVE
assert parsed.killed is False
assert parsed.default_treatment == 'off'
assert parsed.algo == splits.HashAlgorithm.MURMUR
assert len(parsed.conditions) == 2
assert parsed.get_configurations_for('on') == '{"color": "blue", "size": 13}'
assert parsed._configurations == {'on': '{"color": "blue", "size": 13}'}
assert parsed.sets == {'set1', 'set2'}
assert parsed.impressions_disabled == False
assert len(parsed.prerequisites) == 2
flag1 = False
flag2 = False
for prerequisite in parsed.prerequisites:
if prerequisite.feature_flag_name == 'flag1':
flag1 = True
assert prerequisite.treatments == ['on','v1']
if prerequisite.feature_flag_name == 'flag2':
flag2 = True
assert prerequisite.treatments == ['off']
assert flag1
assert flag2
def test_get_segment_names(self, mocker):
"""Test fetching segment names."""
cond1 = mocker.Mock(spec=Condition)
cond2 = mocker.Mock(spec=Condition)
cond1.get_segment_names.return_value = ['segment1', 'segment2']
cond2.get_segment_names.return_value = ['segment3', 'segment4']
split1 = splits.Split( 'some_split', 123, False, 'off', 'user', 'ACTIVE', 123, [cond1, cond2], None)
assert split1.get_segment_names() == ['segment%d' % i for i in range(1, 5)]
def test_to_json(self):
"""Test json serialization."""
as_json = splits.from_raw(self.raw).to_json()
assert isinstance(as_json, dict)
assert as_json['changeNumber'] == 123
assert as_json['trafficTypeName'] == 'user'
assert as_json['name'] == 'some_name'
assert as_json['trafficAllocation'] == 100
assert as_json['trafficAllocationSeed'] == 123456
assert as_json['seed'] == 321654
assert as_json['status'] == 'ACTIVE'
assert as_json['killed'] is False
assert as_json['defaultTreatment'] == 'off'
assert as_json['algo'] == 2
assert len(as_json['conditions']) == 2
assert sorted(as_json['sets']) == ['set1', 'set2']
assert as_json['impressionsDisabled'] is False
def test_to_split_view(self):
"""Test SplitView creation."""
as_split_view = splits.from_raw(self.raw).to_split_view()
assert isinstance(as_split_view, splits.SplitView)
assert as_split_view.name == self.raw['name']
assert as_split_view.change_number == self.raw['changeNumber']
assert as_split_view.killed == self.raw['killed']
assert as_split_view.traffic_type == self.raw['trafficTypeName']
assert set(as_split_view.treatments) == set(['on', 'off'])
assert sorted(as_split_view.sets) == sorted(list(self.raw['sets']))
assert as_split_view.impressions_disabled == self.raw['impressionsDisabled']
def test_incorrect_matcher(self):
"""Test incorrect matcher in split model parsing."""
split = copy.deepcopy(self.raw)
split['conditions'][0]['matcherGroup']['matchers'][0]['matcherType'] = 'INVALID_MATCHER'
parsed = splits.from_raw(split)
assert parsed.conditions[0].to_json() == splits._DEFAULT_CONDITIONS_TEMPLATE
# using multiple conditions
split = copy.deepcopy(self.raw)
split['conditions'].append(split['conditions'][0])
split['conditions'][0]['matcherGroup']['matchers'][0]['matcherType'] = 'INVALID_MATCHER'
parsed = splits.from_raw(split)
assert parsed.conditions[0].to_json() == splits._DEFAULT_CONDITIONS_TEMPLATE