Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions splitio/engine/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def evaluate_treatment(self, feature, matching_key,

return {
'treatment': _treatment,
'configurations': split.get_configurations_for(_treatment) if split else None,
'impression': {
'label': label,
'change_number': _change_number
Expand Down
15 changes: 12 additions & 3 deletions splitio/models/splits.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def __init__( #pylint: disable=too-many-arguments
conditions=None,
algo=None,
traffic_allocation=None,
traffic_allocation_seed=None
traffic_allocation_seed=None,
configurations=None
):
"""
Class constructor.
Expand Down Expand Up @@ -91,6 +92,8 @@ def __init__( #pylint: disable=too-many-arguments
except ValueError:
self._algo = HashAlgorithm.LEGACY

self._configurations = configurations

@property
def name(self):
"""Return name."""
Expand Down Expand Up @@ -146,6 +149,10 @@ def traffic_allocation_seed(self):
"""Return the traffic allocation seed of the split."""
return self._traffic_allocation_seed

def get_configurations_for(self, treatment):
"""Return the mapping of treatments to configurations."""
return self._configurations.get(treatment) if self._configurations else None

def get_segment_names(self):
"""
Return a list of segment names referenced in all matchers from this split.
Expand All @@ -168,7 +175,8 @@ def to_json(self):
'killed': self.killed,
'defaultTreatment': self.default_treatment,
'algo': self.algo.value,
'conditions': [c.to_json() for c in self.conditions]
'conditions': [c.to_json() for c in self.conditions],
'configurations': self._configurations
}

def to_split_view(self):
Expand Down Expand Up @@ -219,5 +227,6 @@ def from_raw(raw_split):
[condition.from_raw(c) for c in raw_split['conditions']],
raw_split.get('algo'),
traffic_allocation=raw_split.get('trafficAllocation'),
traffic_allocation_seed=raw_split.get('trafficAllocationSeed')
traffic_allocation_seed=raw_split.get('trafficAllocationSeed'),
configurations=raw_split.get('configurations')
)
14 changes: 12 additions & 2 deletions tests/engine/test_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_evaluate_treatment_missing_split(self, mocker):
e = self._build_evaluator_with_mocks(mocker)
e._split_storage.get.return_value = None
result = e.evaluate_treatment('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
assert result['configurations'] == None
assert result['treatment'] == evaluator.CONTROL
assert result['impression']['change_number'] == -1
assert result['impression']['label'] == Label.SPLIT_NOT_FOUND
Expand All @@ -36,11 +37,14 @@ def test_evaluate_treatment_killed_split(self, mocker):
mocked_split.default_treatment = 'off'
mocked_split.killed = True
mocked_split.change_number = 123
mocked_split.get_configurations_for.return_value = '{"some_property": 123}'
e._split_storage.get.return_value = mocked_split
result = e.evaluate_treatment('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
assert result['treatment'] == 'off'
assert result['configurations'] == '{"some_property": 123}'
assert result['impression']['change_number'] == 123
assert result['impression']['label'] == Label.KILLED
assert mocked_split.get_configurations_for.mock_calls == [mocker.call('off')]

def test_evaluate_treatment_ok(self, mocker):
"""Test that a non-killed split returns the appropriate treatment."""
Expand All @@ -51,13 +55,16 @@ def test_evaluate_treatment_ok(self, mocker):
mocked_split.default_treatment = 'off'
mocked_split.killed = False
mocked_split.change_number = 123
mocked_split.get_configurations_for.return_value = '{"some_property": 123}'
e._split_storage.get.return_value = mocked_split
result = e.evaluate_treatment('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
assert result['treatment'] == 'on'
assert result['configurations'] == '{"some_property": 123}'
assert result['impression']['change_number'] == 123
assert result['impression']['label'] == 'some_label'
assert mocked_split.get_configurations_for.mock_calls == [mocker.call('on')]

def test_evaluate_treatment_ok(self, mocker):
def test_evaluate_treatment_ok_no_config(self, mocker):
"""Test that a killed split returns the default treatment."""
e = self._build_evaluator_with_mocks(mocker)
e._get_treatment_for_split = mocker.Mock()
Expand All @@ -66,11 +73,14 @@ def test_evaluate_treatment_ok(self, mocker):
mocked_split.default_treatment = 'off'
mocked_split.killed = False
mocked_split.change_number = 123
mocked_split.get_configurations_for.return_value = None
e._split_storage.get.return_value = mocked_split
result = e.evaluate_treatment('feature1', 'some_key', 'some_bucketing_key', {'attr1': 1})
assert result['treatment'] == 'on'
assert result['configurations'] == None
assert result['impression']['change_number'] == 123
assert result['impression']['label'] == 'some_label'
assert mocked_split.get_configurations_for.mock_calls == [mocker.call('on')]

def test_get_gtreatment_for_split_no_condition_matches(self, mocker):
"""Test no condition matches."""
Expand Down Expand Up @@ -102,7 +112,7 @@ def test_get_gtreatment_for_split_non_rollout(self, mocker):
assert treatment == 'on'
assert label == 'some_label'

def test_get_gtreatment_for_split_rollout(self, mocker):
def test_get_treatment_for_split_rollout(self, mocker):
"""Test rollout condition returns default treatment."""
e = self._build_evaluator_with_mocks(mocker)
e._splitter.get_bucket.return_value = 60
Expand Down
7 changes: 6 additions & 1 deletion tests/models/test_splits.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ class SplitTests(object):
'combiner': 'AND'
}
}
]
],
'configurations': {
'on': '{"color": "blue", "size": 13}'
},
}

def test_from_raw(self):
Expand All @@ -74,6 +77,8 @@ def test_from_raw(self):
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}'}

def test_get_segment_names(self, mocker):
"""Test fetching segment names."""
Expand Down