aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Casagrande <milo.casagrande@linaro.org>2014-10-16 16:13:20 +0200
committerMilo Casagrande <milo.casagrande@linaro.org>2014-10-16 16:13:20 +0200
commit8c83a50a7d228fef93e4e47f5ab60bde892dc3d5 (patch)
tree17d2237333a2d3a3413952f0580deb69920d772f
parentd282c05e6552ed53c290646d91e1a49fbdea0093 (diff)
downloadkernel-ci-backend-batch-api.tar.gz
common: Treat case where date_range is not a number.batch-api
* Corner case where the query might be kind of valid, but the value passed is missing the '&' between a value. Handle this case for the date_range value. * Decrease default date range to 5 days. * Add test. Change-Id: Ie62d456ac1edf0648ca8f056385636f17301f69c
-rw-r--r--app/handlers/common.py8
-rw-r--r--app/handlers/tests/test_handlers_common.py11
2 files changed, 16 insertions, 3 deletions
diff --git a/app/handlers/common.py b/app/handlers/common.py
index 290e7a5..b99c0b2 100644
--- a/app/handlers/common.py
+++ b/app/handlers/common.py
@@ -74,7 +74,7 @@ from utils import get_log
# Default value to calculate a date range in case the provided value is
# out of range.
-DEFAULT_DATE_RANGE = 15
+DEFAULT_DATE_RANGE = 5
LOG = get_log()
@@ -297,7 +297,11 @@ def calculate_date_range(date_range):
date_range = date_range[-1]
if isinstance(date_range, types.StringTypes):
- date_range = int(date_range)
+ try:
+ date_range = int(date_range)
+ except ValueError:
+ LOG.error("Wrong value passed to date_range: %s", date_range)
+ date_range = DEFAULT_DATE_RANGE
date_range = abs(date_range)
if date_range > timedelta.max.days:
diff --git a/app/handlers/tests/test_handlers_common.py b/app/handlers/tests/test_handlers_common.py
index ba3ab04..b1b0d56 100644
--- a/app/handlers/tests/test_handlers_common.py
+++ b/app/handlers/tests/test_handlers_common.py
@@ -104,12 +104,21 @@ class TestHandlersCommon(unittest.TestCase):
def test_calculate_date_range_out_of_range(self):
self.mock_date.today.return_value = date(2014, 1, 1)
- expected = datetime.combine(date(2013, 12, 17), self.min_time)
+ expected = datetime.combine(date(2013, 12, 27), self.min_time)
self.assertEqual(
expected,
calculate_date_range(timedelta.max.days + 10)
)
+ def test_calculate_date_range_wrong_type(self):
+ self.mock_date.today.return_value = date(2014, 1, 1)
+
+ expected = datetime.combine(date(2013, 12, 27), self.min_time)
+ self.assertEqual(
+ expected,
+ calculate_date_range("15foo$%^%&^%&")
+ )
+
def test_get_aggregate_value_empty(self):
def query_args_func(key):
return []