From db986d8524d8befa075b213c81168f7102c78b56 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Wed, 19 Jul 2017 13:29:53 +0800 Subject: bugfix: query doesn't work well with period=1 when querying by date, if $lt is not provided, the empty/None/null/'' results will also be returned, the patch aims to fix this issue by adding $lt = datetime.now() if not provided JIRA: RELENG-212 Change-Id: Ida1e7d386a88d4ab640441df161c1fe134593f82 Signed-off-by: SerenaFeng --- .../opnfv_testapi/resources/result_handlers.py | 6 +++++ .../opnfv_testapi/tests/unit/fake_pymongo.py | 10 ++++++--- .../tests/unit/resources/test_result.py | 26 ++++++++++++++++++---- 3 files changed, 35 insertions(+), 7 deletions(-) (limited to 'utils') diff --git a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py index 1773216c0..57ad27e6e 100644 --- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py @@ -60,6 +60,12 @@ class GenericResultHandler(handlers.GenericApiHandler): query[k] = v if date_range: query['start_date'] = date_range + + # if $lt is not provided, + # empty/None/null/'' start_date will also be returned + if 'start_date' in query and '$lt' not in query['start_date']: + query['start_date'].update({'$lt': str(datetime.now())}) + return query diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py index adaf6f7c3..04785d2b8 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/fake_pymongo.py @@ -119,10 +119,14 @@ class MemDb(object): @staticmethod def _compare_date(spec, value): + gte = True + lt = False for k, v in spec.iteritems(): - if k == '$gte' and value >= v: - return True - return False + if k == '$gte' and value < v: + gte = False + elif k == '$lt' and value < v: + lt = True + return gte and lt def _in(self, content, *args): if self.name == 'scenarios': diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_result.py b/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_result.py index c8463cb02..2eb66cd1e 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_result.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/resources/test_result.py @@ -256,9 +256,9 @@ class TestResultGet(TestResultBase): def test_queryPeriodNotInt(self): return self._set_query('period=a') - @executor.query(httplib.OK, '_query_last_one', 1) + @executor.query(httplib.OK, '_query_period_one', 1) def test_queryPeriodSuccess(self): - return self._set_query('period=1') + return self._set_query('period=11') @executor.query(httplib.BAD_REQUEST, message.must_int('last')) def test_queryLastNotInt(self): @@ -268,7 +268,7 @@ class TestResultGet(TestResultBase): def test_queryLast(self): return self._set_query('last=1') - @executor.query(httplib.OK, '_query_last_one', 1) + @executor.query(httplib.OK, '_query_period_one', 1) def test_combination(self): return self._set_query('pod', 'project', @@ -279,7 +279,7 @@ class TestResultGet(TestResultBase): 'scenario', 'trust_indicator', 'criteria', - 'period=1') + 'period=11') @executor.query(httplib.OK, '_query_success', 0) def test_notFound(self): @@ -294,6 +294,14 @@ class TestResultGet(TestResultBase): 'criteria', 'period=1') + @executor.query(httplib.OK, '_query_success', 1) + def test_filterErrorStartdate(self): + self._create_error_start_date(None) + # self._create_error_start_date('None') + self._create_error_start_date('null') + self._create_error_start_date('') + return self._set_query('period=11') + def _query_success(self, body, number): self.assertEqual(number, len(body.results)) @@ -301,6 +309,16 @@ class TestResultGet(TestResultBase): self.assertEqual(number, len(body.results)) self.assert_res(body.results[0], self.req_10d_later) + def _query_period_one(self, body, number): + self.assertEqual(number, len(body.results)) + self.assert_res(body.results[0], self.req_10d_before) + + def _create_error_start_date(self, start_date): + req = copy.deepcopy(self.req_d) + req.start_date = start_date + self.create(req) + return req + def _create_changed_date(self, **kwargs): req = copy.deepcopy(self.req_d) req.start_date = datetime.now() + timedelta(**kwargs) -- cgit 1.2.3-korg