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
22 changes: 17 additions & 5 deletions splitio/models/splits.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

SplitView = namedtuple(
'SplitView',
['name', 'traffic_type', 'killed', 'treatments', 'change_number', 'configs']
['name', 'traffic_type', 'killed', 'treatments', 'change_number', 'configs', 'sets']
)


Expand Down Expand Up @@ -41,7 +41,8 @@ def __init__( # pylint: disable=too-many-arguments
algo=None,
traffic_allocation=None,
traffic_allocation_seed=None,
configurations=None
configurations=None,
sets=[]
):
"""
Class constructor.
Expand All @@ -62,6 +63,8 @@ def __init__( # pylint: disable=too-many-arguments
:type traffic_allocation: int
:pram traffic_allocation_seed: Seed used to hash traffic allocation.
:type traffic_allocation_seed: int
:pram sets: list of flag sets
:type sets: list
"""
self._name = name
self._seed = seed
Expand Down Expand Up @@ -90,6 +93,7 @@ def __init__( # pylint: disable=too-many-arguments
self._algo = HashAlgorithm.LEGACY

self._configurations = configurations
self._sets = sets

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

@property
def sets(self):
"""Return the flag sets of the split."""
return self._sets

def get_configurations_for(self, treatment):
"""Return the mapping of treatments to configurations."""
return self._configurations.get(treatment) if self._configurations else None
Expand Down Expand Up @@ -173,7 +182,8 @@ def to_json(self):
'defaultTreatment': self.default_treatment,
'algo': self.algo.value,
'conditions': [c.to_json() for c in self.conditions],
'configurations': self._configurations
'configurations': self._configurations,
'sets': self._sets
}

def to_split_view(self):
Expand All @@ -189,7 +199,8 @@ def to_split_view(self):
self.killed,
list(set(part.treatment for cond in self.conditions for part in cond.partitions)),
self.change_number,
self._configurations if self._configurations is not None else {}
self._configurations if self._configurations is not None else {},
self._sets
)

def local_kill(self, default_treatment, change_number):
Expand Down Expand Up @@ -238,5 +249,6 @@ def from_raw(raw_split):
raw_split.get('algo'),
traffic_allocation=raw_split.get('trafficAllocation'),
traffic_allocation_seed=raw_split.get('trafficAllocationSeed'),
configurations=raw_split.get('configurations')
configurations=raw_split.get('configurations'),
sets=raw_split.get('sets')
)
5 changes: 4 additions & 1 deletion tests/models/test_splits.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SplitTests(object):
'configurations': {
'on': '{"color": "blue", "size": 13}'
},
'sets': ['set1', 'set2']
}

def test_from_raw(self):
Expand All @@ -79,6 +80,7 @@ def test_from_raw(self):
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']

def test_get_segment_names(self, mocker):
"""Test fetching segment names."""
Expand All @@ -89,7 +91,6 @@ def test_get_segment_names(self, mocker):
split1 = splits.Split( 'some_split', 123, False, 'off', 'user', 'ACTIVE', 123, [cond1, cond2])
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()
Expand All @@ -105,6 +106,7 @@ def test_to_json(self):
assert as_json['defaultTreatment'] == 'off'
assert as_json['algo'] == 2
assert len(as_json['conditions']) == 2
assert as_json['sets'] == ['set1', 'set2']

def test_to_split_view(self):
"""Test SplitView creation."""
Expand All @@ -115,3 +117,4 @@ def test_to_split_view(self):
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 as_split_view.sets == self.raw['sets']