diff options
author | Morgan Richomme <morgan.richomme@orange.com> | 2016-07-06 08:49:08 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@172.30.200.206> | 2016-07-06 08:49:08 +0000 |
commit | 049e6265ac30ddf7392cdcf1cf4b3399364a2ef1 (patch) | |
tree | 3141a6c930cd5d2e78f7939bd5f01498eb8bcefe /result_collection_api/opnfv_testapi | |
parent | b7d3e63e77b1de41e9d9fb14fa32456096460022 (diff) | |
parent | b3eccb10c0fa8812413288771a74c9c10556eb93 (diff) |
Merge "Add a put result method to modify trust_indicator"
Diffstat (limited to 'result_collection_api/opnfv_testapi')
6 files changed, 149 insertions, 37 deletions
diff --git a/result_collection_api/opnfv_testapi/resources/handlers.py b/result_collection_api/opnfv_testapi/resources/handlers.py index 8737011..f98c35e 100644 --- a/result_collection_api/opnfv_testapi/resources/handlers.py +++ b/result_collection_api/opnfv_testapi/resources/handlers.py @@ -198,9 +198,8 @@ class GenericApiHandler(RequestHandler): comparing values """ if not (new_value is None): - if len(new_value) > 0: - if new_value != old_value: - edit_request[key] = new_value + if new_value != old_value: + edit_request[key] = new_value return edit_request diff --git a/result_collection_api/opnfv_testapi/resources/result_handlers.py b/result_collection_api/opnfv_testapi/resources/result_handlers.py index 5198ba3..400b84a 100644 --- a/result_collection_api/opnfv_testapi/resources/result_handlers.py +++ b/result_collection_api/opnfv_testapi/resources/result_handlers.py @@ -45,7 +45,7 @@ class GenericResultHandler(GenericApiHandler): obj = {"$gte": str(period)} query['start_date'] = obj elif k == 'trust_indicator': - query[k] = float(v) + query[k + '.current'] = float(v) elif k != 'last': query[k] = v return query @@ -116,8 +116,8 @@ class ResultsCLHandler(GenericResultHandler): @type last: L{string} @in last: query @required last: False - @param trust_indicator: must be int/long/float - @type trust_indicator: L{string} + @param trust_indicator: must be float + @type trust_indicator: L{float} @in trust_indicator: query @required trust_indicator: False """ @@ -180,3 +180,19 @@ class ResultsGURHandler(GenericResultHandler): query = dict() query["_id"] = ObjectId(result_id) self._get_one(query) + + @swagger.operation(nickname="update") + def put(self, result_id): + """ + @description: update a single result by _id + @param body: fields to be updated + @type body: L{ResultUpdateRequest} + @in body: body + @rtype: L{Result} + @return 200: update success + @raise 404: result not exist + @raise 403: nothing to update + """ + query = {'_id': ObjectId(result_id)} + db_keys = [] + self._update(query, db_keys) diff --git a/result_collection_api/opnfv_testapi/resources/result_models.py b/result_collection_api/opnfv_testapi/resources/result_models.py index fdd8059..dd1e3dc 100644 --- a/result_collection_api/opnfv_testapi/resources/result_models.py +++ b/result_collection_api/opnfv_testapi/resources/result_models.py @@ -10,7 +10,69 @@ from opnfv_testapi.tornado_swagger import swagger @swagger.model() +class TIHistory(object): + """ + @ptype step: L{float} + """ + def __init__(self, date=None, step=0): + self.date = date + self.step = step + + def format(self): + return { + "date": self.date, + "step": self.step + } + + @staticmethod + def from_dict(a_dict): + if a_dict is None: + return None + + return TIHistory(a_dict.get('date'), a_dict.get('step')) + + +@swagger.model() +class TI(object): + """ + @property histories: trust_indicator update histories + @ptype histories: C{list} of L{TIHistory} + @ptype current: L{float} + """ + def __init__(self, current=0): + self.current = current + self.histories = list() + + def format(self): + hs = [] + for h in self.histories: + hs.append(h.format()) + + return { + "current": self.current, + "histories": hs + } + + @staticmethod + def from_dict(a_dict): + if a_dict is None: + return None + t = TI() + t.current = a_dict.get('current') + if 'histories' in a_dict.keys(): + for history in a_dict.get('histories', None): + t.histories.append(TIHistory.from_dict(history)) + else: + t.histories = [] + return t + + +@swagger.model() class ResultCreateRequest(object): + """ + @property trust_indicator: + @ptype trust_indicator: L{TI} + """ def __init__(self, pod_name=None, project_name=None, @@ -50,15 +112,30 @@ class ResultCreateRequest(object): "build_tag": self.build_tag, "scenario": self.scenario, "criteria": self.criteria, - "trust_indicator": self.trust_indicator + "trust_indicator": self.trust_indicator.format() + } + + +@swagger.model() +class ResultUpdateRequest(object): + """ + @property trust_indicator: + @ptype trust_indicator: L{TI} + """ + def __init__(self, trust_indicator=None): + self.trust_indicator = trust_indicator + + def format(self): + return { + "trust_indicator": self.trust_indicator.format(), } @swagger.model() class TestResult(object): """ - @property trust_indicator: must be int/long/float - @ptype trust_indicator: L{float} + @property trust_indicator: used for long duration test case + @ptype trust_indicator: L{TI} """ def __init__(self, _id=None, case_name=None, project_name=None, pod_name=None, installer=None, version=None, @@ -98,19 +175,7 @@ class TestResult(object): t.build_tag = a_dict.get('build_tag') t.scenario = a_dict.get('scenario') t.criteria = a_dict.get('criteria') - # 0 < trust indicator < 1 - # if bad value => set this indicator to 0 - t.trust_indicator = a_dict.get('trust_indicator') - if t.trust_indicator is not None: - if isinstance(t.trust_indicator, (int, long, float)): - if t.trust_indicator < 0: - t.trust_indicator = 0 - elif t.trust_indicator > 1: - t.trust_indicator = 1 - else: - t.trust_indicator = 0 - else: - t.trust_indicator = 0 + t.trust_indicator = TI.from_dict(a_dict.get('trust_indicator')) return t def format(self): @@ -126,7 +191,7 @@ class TestResult(object): "build_tag": self.build_tag, "scenario": self.scenario, "criteria": self.criteria, - "trust_indicator": self.trust_indicator + "trust_indicator": self.trust_indicator.format() } def format_http(self): @@ -143,7 +208,7 @@ class TestResult(object): "build_tag": self.build_tag, "scenario": self.scenario, "criteria": self.criteria, - "trust_indicator": self.trust_indicator + "trust_indicator": self.trust_indicator.format() } diff --git a/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py b/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py index 6ab98c7..4509692 100644 --- a/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py +++ b/result_collection_api/opnfv_testapi/tests/unit/fake_pymongo.py @@ -116,8 +116,8 @@ class MemDb(object): if k == 'start_date': if not MemDb._compare_date(v, content.get(k)): return False - elif k == 'trust_indicator': - if float(content.get(k)) != float(v): + elif k == 'trust_indicator.current': + if content.get('trust_indicator').get('current') != v: return False elif content.get(k, None) != v: return False @@ -173,7 +173,6 @@ class MemDb(object): def _check_keys(self, doc): for key in doc.keys(): - print('key', key, 'value', doc.get(key)) if '.' in key: raise NameError('key {} must not contain .'.format(key)) if key.startswith('$'): diff --git a/result_collection_api/opnfv_testapi/tests/unit/test_fake_pymongo.py b/result_collection_api/opnfv_testapi/tests/unit/test_fake_pymongo.py index 27382f0..9a1253e 100644 --- a/result_collection_api/opnfv_testapi/tests/unit/test_fake_pymongo.py +++ b/result_collection_api/opnfv_testapi/tests/unit/test_fake_pymongo.py @@ -8,9 +8,9 @@ ############################################################################## import unittest -from tornado.web import Application from tornado import gen from tornado.testing import AsyncHTTPTestCase, gen_test +from tornado.web import Application import fake_pymongo diff --git a/result_collection_api/opnfv_testapi/tests/unit/test_result.py b/result_collection_api/opnfv_testapi/tests/unit/test_result.py index bba3b22..98ef7c0 100644 --- a/result_collection_api/opnfv_testapi/tests/unit/test_result.py +++ b/result_collection_api/opnfv_testapi/tests/unit/test_result.py @@ -6,15 +6,16 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import unittest import copy +import unittest +from datetime import datetime, timedelta from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \ HTTP_NOT_FOUND from opnfv_testapi.resources.pod_models import PodCreateRequest from opnfv_testapi.resources.project_models import ProjectCreateRequest from opnfv_testapi.resources.result_models import ResultCreateRequest, \ - TestResult, TestResults + TestResult, TestResults, ResultUpdateRequest, TI, TIHistory from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest from test_base import TestBase @@ -55,9 +56,11 @@ class TestResultBase(TestBase): self.build_tag = 'v3.0' self.scenario = 'odl-l2' self.criteria = 'passed' - self.trust_indicator = 0.7 + self.trust_indicator = TI(0.7) self.start_date = "2016-05-23 07:16:09.477097" self.stop_date = "2016-05-23 07:16:19.477097" + self.update_date = "2016-05-24 07:16:19.477097" + self.update_step = -0.05 super(TestResultBase, self).setUp() self.details = Details(timestart='0', duration='9s', status='OK') self.req_d = ResultCreateRequest(pod_name=self.pod, @@ -74,6 +77,7 @@ class TestResultBase(TestBase): trust_indicator=self.trust_indicator) self.get_res = TestResult self.list_res = TestResults + self.update_res = TestResult self.basePath = '/api/v1/results' self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1') self.req_project = ProjectCreateRequest(self.project, 'vping test') @@ -103,10 +107,19 @@ class TestResultBase(TestBase): self.assertEqual(result.build_tag, req.build_tag) self.assertEqual(result.scenario, req.scenario) self.assertEqual(result.criteria, req.criteria) - self.assertEqual(result.trust_indicator, req.trust_indicator) self.assertEqual(result.start_date, req.start_date) self.assertEqual(result.stop_date, req.stop_date) self.assertIsNotNone(result._id) + ti = result.trust_indicator + self.assertEqual(ti.current, req.trust_indicator.current) + if ti.histories: + history = ti.histories[0] + self.assertEqual(history.date, self.update_date) + self.assertEqual(history.step, self.update_step) + + def _create_d(self): + _, res = self.create_d() + return res.href.split('/')[-1] class TestResultCreate(TestResultBase): @@ -172,8 +185,7 @@ class TestResultCreate(TestResultBase): class TestResultGet(TestResultBase): def test_getOne(self): - _, res = self.create_d() - _id = res.href.split('/')[-1] + _id = self._create_d() code, body = self.get(_id) self.assert_res(code, body) @@ -266,8 +278,6 @@ class TestResultGet(TestResultBase): 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)) @@ -276,13 +286,36 @@ class TestResultGet(TestResultBase): return req def _set_query(self, *args): + def get_value(arg): + return eval('self.' + arg) \ + if arg != 'trust_indicator' else self.trust_indicator.current uri = '' for arg in args: if '=' in arg: uri += arg + '&' else: - uri += '{}={}&'.format(arg, eval('self.' + arg)) + uri += '{}={}&'.format(arg, get_value(arg)) return uri[0: -1] + +class TestResultUpdate(TestResultBase): + def test_success(self): + _id = self._create_d() + + new_ti = copy.deepcopy(self.trust_indicator) + new_ti.current += self.update_step + new_ti.histories.append(TIHistory(self.update_date, self.update_step)) + new_data = copy.deepcopy(self.req_d) + new_data.trust_indicator = new_ti + update = ResultUpdateRequest(trust_indicator=new_ti) + code, body = self.update(update, _id) + self.assertEqual(_id, body._id) + self.assert_res(code, body, new_data) + + code, new_body = self.get(_id) + self.assertEqual(_id, new_body._id) + self.assert_res(code, new_body, new_data) + + if __name__ == '__main__': unittest.main() |