diff options
author | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-06-06 16:12:36 +0800 |
---|---|---|
committer | Serena Feng <feng.xiaowei@zte.com.cn> | 2016-06-06 08:17:41 +0000 |
commit | 34db68599c76b124c57d5ebd4d749e4829a57bea (patch) | |
tree | 3c0afadff523dab4e47f3a4fc5ba20eda32928fd /utils | |
parent | cdf0e83b6697d2ff75ba30b567ead5bcfea0fa25 (diff) |
add unittest for 'last' query of results in testAPI
add 'last' related unittest in test_result.py
implement sort()&limit() in fake_pymongo.py
refactor 'sort' model in result_handlers.py
JIRA: FUNCTEST-296
Change-Id: Ib5371f1df67f170e3ba26c6da27a651e34f2b0d6
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils')
3 files changed, 47 insertions, 17 deletions
diff --git a/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py b/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py index 44834fd63..fe2d71e5e 100644 --- a/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/result_collection_api/opnfv_testapi/resources/result_handlers.py @@ -29,7 +29,7 @@ class GenericResultHandler(GenericApiHandler): try: value = int(value) except: - raise HTTPError(HTTP_BAD_REQUEST, '{} must be int', key) + raise HTTPError(HTTP_BAD_REQUEST, '{} must be int'.format(key)) return value def set_query(self): @@ -125,7 +125,7 @@ class ResultsCLHandler(GenericResultHandler): if last is not None: last = self.get_int('last', last) - self._list(self.set_query(), sort=[{'start_date', -1}], last=last) + self._list(self.set_query(), sort=[('start_date', -1)], last=last) @swagger.operation(nickname="create") def post(self): diff --git a/utils/test/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py b/utils/test/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py index 9b4d1208c..ef9c719be 100644 --- a/utils/test/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py +++ b/utils/test/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py @@ -8,6 +8,7 @@ ############################################################################## from bson.objectid import ObjectId from concurrent.futures import ThreadPoolExecutor +from operator import itemgetter def thread_execute(method, *args, **kwargs): @@ -20,6 +21,7 @@ class MemCursor(object): def __init__(self, collection): self.collection = collection self.count = len(self.collection) + self.sorted = [] def _is_next_exist(self): return self.count != 0 @@ -32,10 +34,22 @@ class MemCursor(object): self.count -= 1 return self.collection.pop() - def sort(self, key_or_list, direction=None): + def sort(self, key_or_list): + key = key_or_list[0][0] + if key_or_list[0][1] == -1: + reverse = True + else: + reverse = False + + if key_or_list is not None: + self.collection = sorted(self.collection, + key=itemgetter(key), reverse=reverse) return self def limit(self, limit): + if limit != 0 and limit < len(self.collection): + self.collection = self.collection[0:limit] + self.count = limit return self diff --git a/utils/test/result_collection_api/opnfv_testapi/tests/unit/test_result.py b/utils/test/result_collection_api/opnfv_testapi/tests/unit/test_result.py index 5a5dd3852..dbc4431ce 100644 --- a/utils/test/result_collection_api/opnfv_testapi/tests/unit/test_result.py +++ b/utils/test/result_collection_api/opnfv_testapi/tests/unit/test_result.py @@ -196,16 +196,30 @@ class TestResultGet(TestResultBase): def test_queryCriteria(self): self._query_and_assert(self._set_query('criteria')) + def test_queryPeriodNotInt(self): + code, body = self.query(self._set_query('period=a')) + self.assertEqual(code, HTTP_BAD_REQUEST) + self.assertIn('period must be int', body) + def test_queryPeriodFail(self): self._query_and_assert(self._set_query('period=1'), - aheadof=True, - found=False) + found=False, days=-10) def test_queryPeriodSuccess(self): self._query_and_assert(self._set_query('period=1'), - aheadof=False, found=True) + def test_queryLastNotInt(self): + code, body = self.query(self._set_query('last=a')) + self.assertEqual(code, HTTP_BAD_REQUEST) + self.assertIn('last must be int', body) + + def test_queryLast(self): + self._create_changed_date() + req = self._create_changed_date(minutes=20) + self._create_changed_date(minutes=-20) + self._query_and_assert(self._set_query('last=1'), req=req) + def test_combination(self): self._query_and_assert(self._set_query('pod', 'project', @@ -231,17 +245,9 @@ class TestResultGet(TestResultBase): 'period=1'), found=False) - def _query_and_assert(self, query, aheadof=False, found=True): - import copy - from datetime import datetime, timedelta - req = copy.deepcopy(self.req_d) - if aheadof: - req.start_date = datetime.now() - timedelta(days=10) - else: - req.start_date = datetime.now() - req.stop_date = str(req.start_date + timedelta(minutes=10)) - req.start_date = str(req.start_date) - _, res = self.create(req) + def _query_and_assert(self, query, found=True, req=None, **kwargs): + if req is None: + req = self._create_changed_date(**kwargs) code, body = self.query(query) if not found: self.assertEqual(code, HTTP_OK) @@ -251,6 +257,16 @@ class TestResultGet(TestResultBase): for result in body.results: self.assert_res(code, result, req) + def _create_changed_date(self, **kwargs): + import copy + from datetime import datetime, timedelta + req = copy.deepcopy(self.req_d) + req.start_date = datetime.now() + timedelta(**kwargs) + req.stop_date = str(req.start_date + timedelta(minutes=10)) + req.start_date = str(req.start_date) + self.create(req) + return req + def _set_query(self, *args): uri = '' for arg in args: |