Skip to content

Commit 690a2e5

Browse files
author
Matias Melograno
committed
fixes for tests and code improvement on input_validator
1 parent 1d3a5aa commit 690a2e5

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

splitio/clients.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ def destroy(self):
4848
self._destroyed = True
4949
self._broker.destroy()
5050

51-
def _handle_custom_impression(self, impression, attributes):
51+
def _send_impression_to_listener(self, impression, attributes):
5252
'''
53-
Handles custom impression if is present. Basically, sends the data
54-
to client if some logic is wanted to do.
53+
Sends impression result to custom listener.
54+
55+
:param impression: Generated impression
56+
:type impression: Impression
57+
58+
:param attributes: An optional dictionary of attributes
59+
:type attributes: dict
5560
'''
5661
if self._impression_listener is not None:
5762
try:
@@ -124,7 +129,7 @@ def get_treatment(self, key, feature, attributes=None):
124129
_change_number, bucketing_key, start)
125130
self._record_stats(impression, start, SDK_GET_TREATMENT)
126131

127-
self._handle_custom_impression(impression, attributes)
132+
self._send_impression_to_listener(impression, attributes)
128133

129134
return _treatment
130135
except Exception: # pylint: disable=broad-except
@@ -140,7 +145,7 @@ def get_treatment(self, key, feature, attributes=None):
140145
)
141146
self._record_stats(impression, start, SDK_GET_TREATMENT)
142147

143-
self._handle_custom_impression(impression, attributes)
148+
self._send_impression_to_listener(impression, attributes)
144149
except Exception: # pylint: disable=broad-except
145150
self._logger.exception(
146151
'Exception reporting impression into get_treatment exception block'
@@ -153,8 +158,6 @@ def _build_impression(
153158
):
154159
"""
155160
Build an impression.
156-
157-
TODO: REFACTOR THIS!
158161
"""
159162
if not self._labels_enabled:
160163
label = None
@@ -176,7 +179,7 @@ def _record_stats(self, impression, start, operation):
176179
:type start: int
177180
178181
:param operation: operation performed.
179-
:type operation: string
182+
:type operation: str
180183
"""
181184
try:
182185
end = int(round(time.time() * 1000))
@@ -243,13 +246,13 @@ def track(self, key, traffic_type, event_type, value=None):
243246
Track an event.
244247
245248
:param key: user key associated to the event
246-
:type key: string
249+
:type key: str
247250
248251
:param traffic_type: traffic type name
249-
:type traffic_type: string
252+
:type traffic_type: str
250253
251254
:param event_type: event type name
252-
:type event_type: string
255+
:type event_type: str
253256
254257
:param value: (Optional) value associated to the event
255258
:type value: Number

splitio/input_validator.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _check_not_empty(value, name, operation):
6262
:return: The result of validation
6363
:rtype: True|False
6464
"""
65-
if not value:
65+
if value.strip() == "":
6666
_LOGGER.error('{}: {} must not be empty.'.format(operation, name))
6767
return False
6868
return True
@@ -108,7 +108,7 @@ def _check_can_convert(value, name, operation, message):
108108
if isinstance(value, six.string_types):
109109
return True
110110
else:
111-
if isinstance(value, bool) or isinstance(value, Number) is False:
111+
if isinstance(value, bool) or (not isinstance(value, Number)):
112112
_LOGGER.error('{}: {} {} {}'.format(operation, name, value, message))
113113
return False
114114
_LOGGER.warning('{}: {} {} is not of type string, converting.'
@@ -131,11 +131,11 @@ def _check_valid_matching_key(matching_key):
131131
'matchingKey with valid string properties.')
132132
return False
133133
if isinstance(matching_key, six.string_types):
134-
if _check_not_empty(matching_key, 'matching_key', 'get_treatment') is False:
134+
if not _check_not_empty(matching_key, 'matching_key', 'get_treatment'):
135135
return False
136136
else:
137-
if _check_can_convert(matching_key, 'matching_key', 'get_treatment',
138-
'has to be of type string.') is False:
137+
if not _check_can_convert(matching_key, 'matching_key', 'get_treatment',
138+
'has to be of type string.'):
139139
return False
140140
return True
141141

@@ -153,8 +153,8 @@ def _check_valid_bucketing_key(bucketing_key):
153153
if bucketing_key is None:
154154
_LOGGER.warning('get_treatment: Key object should have bucketingKey set.')
155155
return None
156-
if _check_can_convert(bucketing_key, 'bucketing_key', 'get_treatment',
157-
'has to be of type string.') is False:
156+
if not _check_can_convert(bucketing_key, 'bucketing_key', 'get_treatment',
157+
'has to be of type string.'):
158158
return False
159159
return str(bucketing_key)
160160

@@ -171,7 +171,7 @@ def validate_key(key):
171171
"""
172172
matching_key_result = None
173173
bucketing_key_result = None
174-
if _check_not_null(key, 'key', 'get_treatment') is False:
174+
if not _check_not_null(key, 'key', 'get_treatment'):
175175
return None, None
176176
if isinstance(key, Key):
177177
if _check_valid_matching_key(key.matching_key):
@@ -198,8 +198,8 @@ def validate_feature_name(feature_name):
198198
:return: feature_name
199199
:rtype: str|None
200200
"""
201-
if _check_not_null(feature_name, 'feature_name', 'get_treatment') is False or \
202-
_check_is_string(feature_name, 'feature_name', 'get_treatment') is False:
201+
if (not _check_not_null(feature_name, 'feature_name', 'get_treatment')) or \
202+
(not _check_is_string(feature_name, 'feature_name', 'get_treatment')):
203203
return None
204204
return feature_name
205205

@@ -213,8 +213,8 @@ def validate_track_key(key):
213213
:return: key
214214
:rtype: str|None
215215
"""
216-
if _check_not_null(key, 'key', 'track') is False or \
217-
_check_can_convert(key, 'key', 'track', 'has to be of type string.') is False:
216+
if (not _check_not_null(key, 'key', 'track')) or \
217+
(not _check_can_convert(key, 'key', 'track', 'has to be of type string.')):
218218
return None
219219
return str(key)
220220

@@ -228,9 +228,9 @@ def validate_traffic_type(traffic_type):
228228
:return: traffic_type
229229
:rtype: str|None
230230
"""
231-
if _check_not_null(traffic_type, 'traffic_type', 'track') is False or \
232-
_check_is_string(traffic_type, 'traffic_type', 'track') is False or \
233-
_check_not_empty(traffic_type, 'traffic_type', 'track') is False:
231+
if (not _check_not_null(traffic_type, 'traffic_type', 'track')) or \
232+
(not _check_is_string(traffic_type, 'traffic_type', 'track')) or \
233+
(not _check_not_empty(traffic_type, 'traffic_type', 'track')):
234234
return None
235235
return traffic_type
236236

@@ -244,10 +244,10 @@ def validate_event_type(event_type):
244244
:return: event_type
245245
:rtype: str|None
246246
"""
247-
if _check_not_null(event_type, 'event_type', 'track') is False or \
248-
_check_is_string(event_type, 'event_type', 'track') is False or \
249-
_check_pattern_match(event_type, 'event_type', 'track',
250-
r'[a-zA-Z0-9][-_\.a-zA-Z0-9]{0,62}') is False:
247+
if (not _check_not_null(event_type, 'event_type', 'track')) or \
248+
(not _check_is_string(event_type, 'event_type', 'track')) or \
249+
(not _check_pattern_match(event_type, 'event_type', 'track',
250+
r'[a-zA-Z0-9][-_\.a-zA-Z0-9]{0,62}')):
251251
return None
252252
return event_type
253253

@@ -263,7 +263,7 @@ def validate_value(value):
263263
"""
264264
if value is None:
265265
return None
266-
if not isinstance(value, Number) or isinstance(value, bool):
266+
if (not isinstance(value, Number)) or isinstance(value, bool):
267267
_LOGGER.error('track: value must be a number.')
268268
return False
269269
return value
@@ -278,7 +278,7 @@ def validate_manager_feature_name(feature_name):
278278
:return: feature_name
279279
:rtype: str|None
280280
"""
281-
if _check_not_null(feature_name, 'feature_name', 'split') is False or \
282-
_check_is_string(feature_name, 'feature_name', 'split') is False:
281+
if (not _check_not_null(feature_name, 'feature_name', 'split')) or \
282+
(not _check_is_string(feature_name, 'feature_name', 'split')):
283283
return None
284284
return feature_name

splitio/tests/test_clients.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
# Python 2
99
import mock
1010

11+
import tempfile
1112
import arrow
1213
import os.path
1314

1415
from unittest import TestCase
16+
from time import sleep
1517

18+
from splitio import get_factory
1619
from splitio.clients import Client
17-
from splitio.brokers import JSONFileBroker, RedisBroker, \
20+
from splitio.brokers import JSONFileBroker, RedisBroker, LocalhostBroker, \
1821
UWSGIBroker, randomize_interval, SelfRefreshingBroker
1922
from splitio.exceptions import TimeoutException
2023
from splitio.config import DEFAULT_CONFIG, MAX_INTERVAL, SDK_API_BASE_URL, \
@@ -1149,7 +1152,6 @@ def test_test_killed_not_in_segment_key(self):
11491152
self.fake_id_not_in_segment, 'test_killed'))
11501153

11511154

1152-
'''
11531155
class LocalhostEnvironmentClientParseSplitFileTests(TestCase, MockUtilsMixin):
11541156
def setUp(self):
11551157
self.some_file_name = mock.MagicMock()
@@ -1161,9 +1163,8 @@ def setUp(self):
11611163

11621164
self.open_mock = self.patch_builtin('open')
11631165
self.some_config = mock.MagicMock()
1164-
self.broker = LocalhostBroker(self.some_config)
11651166
self.threading_mock = self.patch('threading.Thread')
1166-
self.broker = LocalhostBroker()
1167+
self.broker = LocalhostBroker(self.some_config)
11671168

11681169
def test_skips_comment_lines(self):
11691170
"""Test that _parse_split_file skips comment lines"""
@@ -1202,6 +1203,7 @@ def test_raises_value_error_if_ioerror_is_raised(self):
12021203
with self.assertRaises(ValueError):
12031204
self.broker._parse_split_file(self.some_file_name)
12041205

1206+
12051207
class LocalhostBrokerOffTheGrid(TestCase):
12061208
"""
12071209
Tests for LocalhostEnvironmentClient. Auto update config behaviour
@@ -1226,7 +1228,6 @@ def test_auto_update_splits(self):
12261228

12271229
self.assertEqual(client.get_treatment('x', 'a_test_split'), 'on')
12281230
client.destroy()
1229-
'''
12301231

12311232

12321233
class TestClientDestroy(TestCase):

0 commit comments

Comments
 (0)