Skip to content

Commit 7481dd9

Browse files
committed
Improve code according to review
1 parent ea46f30 commit 7481dd9

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

pyslicer/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SlicingDice(SlicingDiceAPI):
8282
"""
8383
def __init__(
8484
self, write_key=None, read_key=None, master_key=None,
85-
custom_key=None, use_ssl=True, timeout=60, test=False):
85+
custom_key=None, use_ssl=True, timeout=60, uses_test_endpoint=False):
8686
"""Instantiate a new SlicingDice object.
8787
8888
Keyword arguments:
@@ -95,7 +95,7 @@ def __init__(
9595
"""
9696
super(SlicingDice, self).__init__(
9797
master_key, write_key, read_key, custom_key, use_ssl, timeout)
98-
self.test = test
98+
self.uses_test_endpoint = uses_test_endpoint
9999

100100
def _count_query_wrapper(self, url, query):
101101
"""Validate count query and make request.
@@ -129,7 +129,7 @@ def _data_extraction_wrapper(self, url, query):
129129

130130
def _wrapper_test(self):
131131
base_url = SlicingDice.BASE_URL
132-
if self.test:
132+
if self.uses_test_endpoint:
133133
base_url += "/test"
134134
return base_url
135135

pyslicer/utils/validators.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,42 @@
77

88
from pyslicer.utils.data_utils import is_str_empty
99

10+
MAX_QUERY_SIZE = 10
11+
12+
MAX_INDEXATION_SIZE = 1000
13+
1014

1115
class SDBaseValidator(object):
1216
"""Base field, query and index validator."""
1317
__metaclass__ = abc.ABCMeta
1418

19+
def check_dictionary_value(self, dictionary_value):
20+
print dictionary_value
21+
if isinstance(dictionary_value, dict):
22+
self.check_dictionary(dictionary_value)
23+
elif isinstance(dictionary_value, list):
24+
self.check_list(dictionary_value)
25+
else:
26+
if dictionary_value is None or dictionary_value == "":
27+
raise exceptions.InvalidQueryException(
28+
"This query has invalid keys or values.")
29+
30+
def check_dictionary(self, dictionary):
31+
for key in dictionary:
32+
dictionary_value = dictionary[key]
33+
self.check_dictionary_value(dictionary_value)
34+
35+
def check_list(self, dictionary):
36+
for dictionary_value in dictionary:
37+
self.check_dictionary_value(dictionary_value)
38+
1539
def __init__(self, dictionary):
1640
if not dictionary:
17-
for key in dictionary:
18-
if not dictionary[key]:
19-
raise exceptions.InvalidQueryException(
20-
"This query has invalid keys or values.")
41+
raise exceptions.InvalidQueryException(
42+
"This query has invalid keys or values.")
43+
44+
self.check_dictionary(dictionary)
45+
2146
self.data = dictionary
2247

2348
@abc.abstractmethod
@@ -76,7 +101,7 @@ def validator(self):
76101
if "bypass-cache" in self.data:
77102
query_size -= 1
78103

79-
if query_size > 10:
104+
if query_size > MAX_QUERY_SIZE:
80105
raise exceptions.MaxLimitException(
81106
"The query count entity has a limit of 10 queries by request.")
82107
return True
@@ -193,11 +218,6 @@ def _has_empty_field(self):
193218
Returns:
194219
false if dictionary don't have empty fields
195220
"""
196-
if not self.data:
197-
for key in self.data:
198-
if not self.data[key]:
199-
raise exceptions.InvalidIndexException(
200-
"This index has invalid keys or values.")
201221
for value in self.data.values():
202222
# Value is a dictionary when it is an entity being indexed:
203223
# "my-entity": {"year": 2016}
@@ -217,7 +237,7 @@ def check_indexation_size(self):
217237
if "auto-create-fields" in self.data:
218238
indexation_size -= 1
219239

220-
if indexation_size > 1000:
240+
if indexation_size > MAX_INDEXATION_SIZE:
221241
raise exceptions.InvalidIndexException(
222242
"Your index command shouldn't have more than 1000 values.")
223243

tests_and_examples/run_query_tests.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333

3434
class SlicingDiceTester(object):
3535
"""Test orchestration class."""
36-
def __init__(self, api_key, verbose=False, endpoint_test=True):
36+
def __init__(self, api_key, verbose=False, uses_test_endpoint=True):
3737
# The Slicing Dice API client
38-
self.client = SlicingDice(master_key=api_key, test=endpoint_test)
38+
self.client = SlicingDice(master_key=api_key,
39+
uses_test_endpoint=uses_test_endpoint)
3940

4041
# Translation table for fields with timestamp
4142
self.field_translation = {}
@@ -300,11 +301,11 @@ def main():
300301

301302
# MODE_TEST give us if you want to use endpoint Test or Prod
302303
mode_test = os.environ.get("MODE_TEST", "test")
303-
endpoint_test = False if mode_test.lower() == 'prod' else True
304+
uses_test_endpoint = False if mode_test.lower() == 'prod' else True
304305
sd_tester = SlicingDiceTester(
305306
api_key=api_key,
306307
verbose=False,
307-
endpoint_test=endpoint_test)
308+
uses_test_endpoint=uses_test_endpoint)
308309

309310
try:
310311
for query_type in query_types:

0 commit comments

Comments
 (0)