summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tests
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-20 18:00:52 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2016-05-20 18:11:04 +0800
commiteb168d7608278b670b3178099766ef0c78d3d85a (patch)
tree39c8eb251bf39b9dc3a5d480dfd3f7c6b23d734b /utils/test/result_collection_api/tests
parent0fb2530a0e88a08e9d77a23073b3cd576ce6e47f (diff)
add unittests for testcase and refactor testcase related implementation
modification: add unittests for testcase refactor testcase table name in db to testcases refactor response body fix some bugs JIRA:FUNCTEST-254 Change-Id: Ia7c7101fa742af82bfc8c4a3d398a7dad601bca1 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/result_collection_api/tests')
-rw-r--r--utils/test/result_collection_api/tests/unit/fake_pymongo.py2
-rw-r--r--utils/test/result_collection_api/tests/unit/test_base.py73
-rw-r--r--utils/test/result_collection_api/tests/unit/test_project.py18
-rw-r--r--utils/test/result_collection_api/tests/unit/test_testcase.py163
4 files changed, 221 insertions, 35 deletions
diff --git a/utils/test/result_collection_api/tests/unit/fake_pymongo.py b/utils/test/result_collection_api/tests/unit/fake_pymongo.py
index a6d91aee7..1521bfa35 100644
--- a/utils/test/result_collection_api/tests/unit/fake_pymongo.py
+++ b/utils/test/result_collection_api/tests/unit/fake_pymongo.py
@@ -128,5 +128,5 @@ class MemDb(object):
pods = MemDb()
projects = MemDb()
-test_cases = MemDb()
+testcases = MemDb()
test_results = MemDb()
diff --git a/utils/test/result_collection_api/tests/unit/test_base.py b/utils/test/result_collection_api/tests/unit/test_base.py
index 04caa3992..57d863ca4 100644
--- a/utils/test/result_collection_api/tests/unit/test_base.py
+++ b/utils/test/result_collection_api/tests/unit/test_base.py
@@ -3,7 +3,7 @@ from tornado.web import Application
from tornado.testing import AsyncHTTPTestCase
from resources.handlers import VersionHandler, PodHandler, \
- TestProjectHandler, TestCasesHandler, TestResultsHandler, DashboardHandler
+ ProjectHandler, TestcaseHandler, TestResultsHandler, DashboardHandler
from resources.models import CreateResponse
import fake_pymongo
@@ -28,10 +28,10 @@ class TestBase(AsyncHTTPTestCase):
(r"/version", VersionHandler),
(r"/pods", PodHandler),
(r"/pods/([^/]+)", PodHandler),
- (r"/projects", TestProjectHandler),
- (r"/projects/([^/]+)", TestProjectHandler),
- (r"/projects/([^/]+)/cases", TestCasesHandler),
- (r"/projects/([^/]+)/cases/([^/]+)", TestCasesHandler),
+ (r"/projects", ProjectHandler),
+ (r"/projects/([^/]+)", ProjectHandler),
+ (r"/projects/([^/]+)/cases", TestcaseHandler),
+ (r"/projects/([^/]+)/cases/([^/]+)", TestcaseHandler),
(r"/results", TestResultsHandler),
(r"/results([^/]*)", TestResultsHandler),
(r"/results/([^/]*)", TestResultsHandler),
@@ -43,52 +43,71 @@ class TestBase(AsyncHTTPTestCase):
debug=True,
)
- def create_d(self):
- return self.create(self.req_d)
+ def create_d(self, *args):
+ return self.create(self.req_d, *args)
- def create_e(self):
- return self.create(self.req_e)
+ def create_e(self, *args):
+ return self.create(self.req_e, *args)
- def create(self, req=None):
+ def create(self, req=None, *args):
if req:
req = req.format()
- res = self.fetch(self.basePath,
+ res = self.fetch(self._get_uri(*args),
method='POST',
body=json.dumps(req),
headers=self.headers)
return self._get_return(res, self.create_res)
- def get(self, item=None):
- res = self.fetch(self._get_uri(item),
+ def create_help(self, uri, req, cls):
+ res = self.fetch(uri,
+ method='POST',
+ body=json.dumps(req.format()),
+ headers=self.headers)
+
+ return self._get_return(res, cls)
+
+ def get(self, *args):
+ res = self.fetch(self._get_uri(*args),
method='GET',
headers=self.headers)
def inner():
- return self.get_res if item else self.list_res
+ new_args, num = self._get_valid_args(*args)
+ return self.get_res if num != self._need_arg_num() else self.list_res
return self._get_return(res, inner())
- def update(self, item, new=None):
+ def update(self, new=None, *args):
if new:
new = new.format()
- res = self.fetch(self._get_uri(item),
+ res = self.fetch(self._get_uri(*args),
method='PUT',
body=json.dumps(new),
headers=self.headers)
return self._get_return(res, self.update_res)
- def delete(self, item):
- res = self.fetch(self._get_uri(item),
+ def delete(self, *args):
+ res = self.fetch(self._get_uri(*args),
method='DELETE',
headers=self.headers)
- return res.code
+ return res.code, res.body
- def _get_uri(self, item=None):
+ @staticmethod
+ def _get_valid_args(*args):
+ new_args = tuple(['%s' % arg for arg in args if arg is not None])
+ return new_args, len(new_args)
+
+ def _need_arg_num(self):
+ return self.basePath.count('%s')
+
+ def _get_uri(self, *args):
+ new_args, num = self._get_valid_args(*args)
uri = self.basePath
- if item:
- uri += '/{}'.format(item)
- return uri
+ if num != self._need_arg_num():
+ uri += '/%s'
+
+ return uri % tuple(['%s' % arg for arg in new_args])
def _get_return(self, res, cls):
code = res.code
@@ -99,15 +118,15 @@ class TestBase(AsyncHTTPTestCase):
def _get_return_body(code, body, cls):
return cls.from_dict(json.loads(body)) if code < 300 else body
- def assert_create_body(self, body, req=None):
- print(body.href)
+ def assert_create_body(self, body, req=None, *args):
if not req:
req = self.req_d
- self.assertIn(self._get_uri(req.name), body.href)
+ new_args = args + tuple([req.name])
+ self.assertIn(self._get_uri(*new_args), body.href)
@staticmethod
def _clear():
fake_pymongo.pods.clear()
fake_pymongo.projects.clear()
- fake_pymongo.test_cases.clear()
+ fake_pymongo.testcases.clear()
fake_pymongo.test_results.clear()
diff --git a/utils/test/result_collection_api/tests/unit/test_project.py b/utils/test/result_collection_api/tests/unit/test_project.py
index e79311197..6ce21db24 100644
--- a/utils/test/result_collection_api/tests/unit/test_project.py
+++ b/utils/test/result_collection_api/tests/unit/test_project.py
@@ -66,23 +66,23 @@ class TestProjectGet(TestProjectBase):
class TestProjectUpdate(TestProjectBase):
def test_withoutBody(self):
- code, _ = self.update('noBody')
+ code, _ = self.update(None, 'noBody')
self.assertEqual(code, HTTP_BAD_REQUEST)
def test_notFound(self):
- code, _ = self.update('notFound', self.req_e)
+ code, _ = self.update(self.req_e, 'notFound')
self.assertEqual(code, HTTP_NOT_FOUND)
def test_newNameExist(self):
self.create_d()
self.create_e()
- code, body = self.update(self.req_d.name, self.req_e)
+ code, body = self.update(self.req_e, self.req_d.name)
self.assertEqual(code, HTTP_FORBIDDEN)
self.assertIn("already exists", body)
def test_noUpdate(self):
self.create_d()
- code, body = self.update(self.req_d.name, self.req_d)
+ code, body = self.update(self.req_d, self.req_d.name)
self.assertEqual(code, HTTP_FORBIDDEN)
self.assertIn("Nothing to update", body)
@@ -92,7 +92,7 @@ class TestProjectUpdate(TestProjectBase):
_id = body._id
req = ProjectCreateRequest('newName', 'new description')
- code, body = self.update(self.req_d.name, req)
+ code, body = self.update(req, self.req_d.name)
self.assertEqual(code, HTTP_OK)
self.assertEqual(_id, body._id)
self.assert_body(body, req)
@@ -104,13 +104,17 @@ class TestProjectUpdate(TestProjectBase):
class TestProjectDelete(TestProjectBase):
def test_notFound(self):
- code = self.delete('notFound')
+ code, body = self.delete('notFound')
self.assertEqual(code, HTTP_NOT_FOUND)
def test_success(self):
self.create_d()
- code = self.delete(self.req_d.name)
+ code, body = self.delete(self.req_d.name)
self.assertEqual(code, HTTP_OK)
+ self.assertEqual(body, '')
+
+ code, body = self.get(self.req_d.name)
+ self.assertEqual(code, HTTP_NOT_FOUND)
if __name__ == '__main__':
unittest.main()
diff --git a/utils/test/result_collection_api/tests/unit/test_testcase.py b/utils/test/result_collection_api/tests/unit/test_testcase.py
new file mode 100644
index 000000000..4b998378a
--- /dev/null
+++ b/utils/test/result_collection_api/tests/unit/test_testcase.py
@@ -0,0 +1,163 @@
+import unittest
+
+from test_base import TestBase
+from resources.testcase_models import TestcaseCreateRequest, \
+ Testcase, Testcases, TestcaseUpdateRequest
+from resources.project_models import ProjectCreateRequest, Project
+from common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
+ HTTP_FORBIDDEN, HTTP_NOT_FOUND
+
+
+__author__ = '__serena__'
+
+
+class TestCaseBase(TestBase):
+ def setUp(self):
+ super(TestCaseBase, self).setUp()
+ self.req_d = TestcaseCreateRequest('/cases/vping_1', 'vping_1', 'vping-ssh test')
+ self.req_e = TestcaseCreateRequest('/cases/doctor_1', 'doctor_1', 'create doctor')
+ self.update_d = TestcaseUpdateRequest('vping_1', 'vping-ssh test', 'functest')
+ self.update_e = TestcaseUpdateRequest('doctor_1', 'create doctor', 'functest')
+ self.get_res = Testcase
+ self.list_res = Testcases
+ self.update_res = Testcase
+ self.basePath = '/projects/%s/cases'
+ self.create_project()
+
+ def assert_body(self, case, req=None):
+ if not req:
+ req = self.req_d
+ self.assertEqual(case.name, req.name)
+ self.assertEqual(case.description, req.description)
+ self.assertEqual(case.url, req.url)
+ self.assertIsNotNone(case._id)
+ self.assertIsNotNone(case.creation_date)
+
+ def assert_update_body(self, old, new, req=None):
+ if not req:
+ req = self.req_d
+ self.assertEqual(new.name, req.name)
+ self.assertEqual(new.description, req.description)
+ self.assertEqual(new.url, old.url)
+ self.assertIsNotNone(new._id)
+ self.assertIsNotNone(new.creation_date)
+
+ def create_project(self):
+ req_p = ProjectCreateRequest('functest', 'vping-ssh test')
+ self.create_help('/projects', req_p, Project)
+ self.project = req_p.name
+
+ def create_d(self):
+ return super(TestCaseBase, self).create_d(self.project)
+
+ def create_e(self):
+ return super(TestCaseBase, self).create_e(self.project)
+
+ def get(self, case=None):
+ return super(TestCaseBase, self).get(self.project, case)
+
+ def update(self, new=None, case=None):
+ return super(TestCaseBase, self).update(new, self.project, case)
+
+ def delete(self, case):
+ return super(TestCaseBase, self).delete(self.project, case)
+
+
+class TestCaseCreate(TestCaseBase):
+ def test_noBody(self):
+ (code, body) = self.create(None, 'vping')
+ self.assertEqual(code, HTTP_BAD_REQUEST)
+
+ def test_noProject(self):
+ code, body = self.create(self.req_d, 'noProject')
+ self.assertEqual(code, HTTP_FORBIDDEN)
+ self.assertIn('Could not find project', body)
+
+ def test_success(self):
+ code, body = self.create_d()
+ self.assertEqual(code, HTTP_OK)
+ self.assert_create_body(body, None, self.project)
+
+ def test_alreadyExist(self):
+ self.create_d()
+ code, body = self.create_d()
+ self.assertEqual(code, HTTP_FORBIDDEN)
+ self.assertIn('already exists', body)
+
+
+class TestCaseGet(TestCaseBase):
+ def test_notExist(self):
+ code, body = self.get('notExist')
+ self.assertEqual(code, HTTP_NOT_FOUND)
+
+ def test_getOne(self):
+ self.create_d()
+ code, body = self.get(self.req_d.name)
+ self.assertEqual(code, HTTP_OK)
+ self.assert_body(body)
+
+ def test_list(self):
+ self.create_d()
+ self.create_e()
+ code, body = self.get()
+ for case in body.testcases:
+ if self.req_d.name == case.name:
+ self.assert_body(case)
+ else:
+ self.assert_body(case, self.req_e)
+
+
+class TestCaseUpdate(TestCaseBase):
+ def test_noBody(self):
+ code, _ = self.update(case='noBody')
+ self.assertEqual(code, HTTP_BAD_REQUEST)
+
+ def test_notFound(self):
+ code, _ = self.update(self.update_e, 'notFound')
+ self.assertEqual(code, HTTP_NOT_FOUND)
+
+ def test_newNameExist(self):
+ self.create_d()
+ self.create_e()
+ code, body = self.update(self.update_e, self.req_d.name)
+ self.assertEqual(code, HTTP_FORBIDDEN)
+ self.assertIn("already exists", body)
+
+ def test_noUpdate(self):
+ self.create_d()
+ code, body = self.update(self.update_d, self.req_d.name)
+ self.assertEqual(code, HTTP_FORBIDDEN)
+ self.assertIn("Nothing to update", body)
+
+ def test_success(self):
+ self.create_d()
+ code, body = self.get(self.req_d.name)
+ _id = body._id
+
+ code, body = self.update(self.update_e, self.req_d.name)
+ self.assertEqual(code, HTTP_OK)
+ self.assertEqual(_id, body._id)
+ self.assert_update_body(self.req_d, body, self.update_e)
+
+ _, new_body = self.get(self.req_e.name)
+ self.assertEqual(_id, new_body._id)
+ self.assert_update_body(self.req_d, new_body, self.update_e)
+
+
+class TestCaseDelete(TestCaseBase):
+ def test_notFound(self):
+ code, body = self.delete('notFound')
+ self.assertEqual(code, HTTP_NOT_FOUND)
+
+ def test_success(self):
+ self.create_d()
+ code, body = self.delete(self.req_d.name)
+ self.assertEqual(code, HTTP_OK)
+ self.assertEqual(body, '')
+
+ code, body = self.get(self.req_d.name)
+ self.assertEqual(code, HTTP_NOT_FOUND)
+
+
+if __name__ == '__main__':
+ unittest.main()