Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit 5203879

Browse files
matclaytonAdam Chainz
authored and
Adam Chainz
committed
Fix Range condition
Merges disqus#55, thanks @matclayton. Added tests for `Range` by constructing a `ConditionSet` and testing the range on that - the test fails before this patch.
1 parent 816ae55 commit 5203879

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Pending Release
88
---------------
99

1010
* New release notes here
11+
* Fixed the splitting of ``Range`` conditions, a merge of disqus/gargoyle#55,
12+
thanks @matclayton.
1113

1214
1.1.1 (2016-01-15)
1315
------------------

gargoyle/conditions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def clean(self, value):
8585

8686
class Range(Field):
8787
def is_active(self, condition, value):
88+
if not isinstance(value, six.integer_types):
89+
return False
90+
condition = list(map(int, condition.split('-')))
8891
return value >= condition[0] and value <= condition[1]
8992

9093
def validate(self, data):

tests/testapp/test_conditions.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from django.test import TestCase
2+
3+
from gargoyle.conditions import ConditionSet, Range
4+
from gargoyle.manager import SwitchManager
5+
from gargoyle.models import SELECTIVE, Switch
6+
7+
8+
class NumberConditionSet(ConditionSet):
9+
in_range = Range()
10+
11+
def get_field_value(self, instance, field_name):
12+
if field_name == 'in_range':
13+
return instance
14+
15+
16+
class NumberConditionSetTests(TestCase):
17+
18+
condition_set = __name__ + '.' + NumberConditionSet.__name__
19+
20+
def setUp(self):
21+
super(NumberConditionSetTests, self).setUp()
22+
self.gargoyle = SwitchManager(Switch, key='key', value='value', instances=True, auto_create=True)
23+
self.gargoyle.register(NumberConditionSet())
24+
25+
Switch.objects.create(key='test', status=SELECTIVE)
26+
self.switch = self.gargoyle['test']
27+
28+
def test_range(self):
29+
self.switch.add_condition(
30+
condition_set=self.condition_set,
31+
field_name='in_range',
32+
condition='1-3',
33+
)
34+
35+
assert not self.gargoyle.is_active('test', 0)
36+
assert self.gargoyle.is_active('test', 1)
37+
assert self.gargoyle.is_active('test', 2)
38+
assert self.gargoyle.is_active('test', 3)
39+
assert not self.gargoyle.is_active('test', 4)

0 commit comments

Comments
 (0)