From b3eccb10c0fa8812413288771a74c9c10556eb93 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Fri, 1 Jul 2016 22:56:30 +0800 Subject: Add a put result method to modify trust_indicator update trust_indicator date model add update trust_indicator method add unittest add swagger information JIRA: FUNCTEST-309 Change-Id: Ibb05efbe008aa7fd4dccb27452d8f5dbf541b8f2 Signed-off-by: SerenaFeng --- .../opnfv_testapi/resources/handlers.py | 5 +- .../opnfv_testapi/resources/result_handlers.py | 22 ++++- .../opnfv_testapi/resources/result_models.py | 101 +++++++++++++++++---- 3 files changed, 104 insertions(+), 24 deletions(-) (limited to 'result_collection_api/opnfv_testapi/resources') 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 56bed6c..148a803 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 @@ -9,8 +9,70 @@ 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() } -- cgit 1.2.3-korg