add unittest for 'last' query of results in testAPI
[releng.git] / utils / test / result_collection_api / opnfv_testapi / tests / unit / test_result.py
index 23a745f..dbc4431 100644 (file)
@@ -1,16 +1,21 @@
+##############################################################################
+# Copyright (c) 2016 ZTE Corporation
+# feng.xiaowei@zte.com.cn
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
 import unittest
-import copy
 
-from test_base import TestBase
+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.testcase_models import TestcaseCreateRequest
 from opnfv_testapi.resources.result_models import ResultCreateRequest, \
     TestResult, TestResults
-from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
-    HTTP_NOT_FOUND
-
-__author__ = '__serena__'
+from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest
+from test_base import TestBase
 
 
 class Details(object):
@@ -156,15 +161,6 @@ class TestResultCreate(TestResultBase):
         self.assertEqual(code, HTTP_OK)
         self.assert_href(body)
 
-    def test_createSameResults(self):
-        req_again = copy.deepcopy(self.req_d)
-        req_again.start_date = "2016-05-23 08:16:09.477097"
-        req_again.stop_date = "2016-05-23 08:16:19.477097"
-
-        (code, body) = self.create(req_again)
-        self.assertEqual(code, HTTP_OK)
-        self.assert_href(body)
-
 
 class TestResultGet(TestResultBase):
     def test_getOne(self):
@@ -200,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',
@@ -235,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)
@@ -255,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: