diff options
author | 2016-05-19 18:13:27 +0800 | |
---|---|---|
committer | 2016-05-19 18:18:07 +0800 | |
commit | f25ad4e8a7e4bd6505b2144494da81fbcad40b7d (patch) | |
tree | c811d7b15048680d7d6717beb02bfbfa75aea717 /utils/test/result_collection_api/tests | |
parent | 598be795dec8fa11bee7b6e140b33947ab35b8f0 (diff) |
add unittests of project & refactor response
post body: {'href':'<http://ip:8000/pods/pod_name >'}
get body: Pod/Project
list body: list of Pod/Project
update body: Pod/Project
delete: no body
JIRA: FUNCTEST-253
Change-Id: I28c6f5b35d8909d827f2f803197d95be4abd44bb
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/result_collection_api/tests')
5 files changed, 248 insertions, 87 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 e5ded376e..a6d91aee7 100644 --- a/utils/test/result_collection_api/tests/unit/fake_pymongo.py +++ b/utils/test/result_collection_api/tests/unit/fake_pymongo.py @@ -126,7 +126,7 @@ class MemDb(object): def clear(self): self._remove() -pod = MemDb() -test_projects = MemDb() +pods = MemDb() +projects = MemDb() test_cases = 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 98190fb94..04caa3992 100644 --- a/utils/test/result_collection_api/tests/unit/test_base.py +++ b/utils/test/result_collection_api/tests/unit/test_base.py @@ -4,6 +4,7 @@ from tornado.testing import AsyncHTTPTestCase from resources.handlers import VersionHandler, PodHandler, \ TestProjectHandler, TestCasesHandler, TestResultsHandler, DashboardHandler +from resources.models import CreateResponse import fake_pymongo @@ -11,6 +12,13 @@ class TestBase(AsyncHTTPTestCase): headers = {'Content-Type': 'application/json; charset=UTF-8'} def setUp(self): + self.basePath = '' + self.create_res = CreateResponse + self.get_res = None + self.list_res = None + self.update_res = None + self.req_d = None + self.req_e = None self.addCleanup(self._clear) super(TestBase, self).setUp() @@ -20,10 +28,10 @@ class TestBase(AsyncHTTPTestCase): (r"/version", VersionHandler), (r"/pods", PodHandler), (r"/pods/([^/]+)", PodHandler), - (r"/test_projects", TestProjectHandler), - (r"/test_projects/([^/]+)", TestProjectHandler), - (r"/test_projects/([^/]+)/cases", TestCasesHandler), - (r"/test_projects/([^/]+)/cases/([^/]+)", TestCasesHandler), + (r"/projects", TestProjectHandler), + (r"/projects/([^/]+)", TestProjectHandler), + (r"/projects/([^/]+)/cases", TestCasesHandler), + (r"/projects/([^/]+)/cases/([^/]+)", TestCasesHandler), (r"/results", TestResultsHandler), (r"/results([^/]*)", TestResultsHandler), (r"/results/([^/]*)", TestResultsHandler), @@ -35,20 +43,71 @@ class TestBase(AsyncHTTPTestCase): debug=True, ) - def create(self, uri, body=None): - return self.fetch(uri, - method='POST', - body=json.dumps(body), - headers=self.headers) + def create_d(self): + return self.create(self.req_d) - def get(self, uri): - return self.fetch(uri, - method='GET', - headers=self.headers) + def create_e(self): + return self.create(self.req_e) + + def create(self, req=None): + if req: + req = req.format() + + res = self.fetch(self.basePath, + 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), + method='GET', + headers=self.headers) + + def inner(): + return self.get_res if item else self.list_res + return self._get_return(res, inner()) + + def update(self, item, new=None): + if new: + new = new.format() + res = self.fetch(self._get_uri(item), + 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), + method='DELETE', + headers=self.headers) + return res.code + + def _get_uri(self, item=None): + uri = self.basePath + if item: + uri += '/{}'.format(item) + return uri + + def _get_return(self, res, cls): + code = res.code + body = res.body + return code, self._get_return_body(code, body, cls) + + @staticmethod + 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) + if not req: + req = self.req_d + self.assertIn(self._get_uri(req.name), body.href) @staticmethod def _clear(): - fake_pymongo.pod.clear() - fake_pymongo.test_projects.clear() + fake_pymongo.pods.clear() + fake_pymongo.projects.clear() fake_pymongo.test_cases.clear() fake_pymongo.test_results.clear() diff --git a/utils/test/result_collection_api/tests/unit/test_fake_pymongo.py b/utils/test/result_collection_api/tests/unit/test_fake_pymongo.py index 5ddbf28d9..228fed72c 100644 --- a/utils/test/result_collection_api/tests/unit/test_fake_pymongo.py +++ b/utils/test/result_collection_api/tests/unit/test_fake_pymongo.py @@ -19,17 +19,17 @@ class MyTest(AsyncHTTPTestCase): def fixture_setup(self): self.test1 = {'_id': '1', 'name': 'test1'} self.test2 = {'name': 'test2'} - yield self.db.pod.insert({'_id': '1', 'name': 'test1'}) - yield self.db.pod.insert({'name': 'test2'}) + yield self.db.pods.insert({'_id': '1', 'name': 'test1'}) + yield self.db.pods.insert({'name': 'test2'}) @gen_test def test_find_one(self): - user = yield self.db.pod.find_one({'name': 'test1'}) + user = yield self.db.pods.find_one({'name': 'test1'}) self.assertEqual(user, self.test1) @gen_test def test_find(self): - cursor = self.db.pod.find() + cursor = self.db.pods.find() names = [] while (yield cursor.fetch_next): ob = cursor.next_object() @@ -38,14 +38,14 @@ class MyTest(AsyncHTTPTestCase): @gen_test def test_update(self): - yield self.db.pod.update({'_id': '1'}, {'name': 'new_test1'}) - user = yield self.db.pod.find_one({'_id': '1'}) + yield self.db.pods.update({'_id': '1'}, {'name': 'new_test1'}) + user = yield self.db.pods.find_one({'_id': '1'}) self.assertEqual(user.get('name', None), 'new_test1') @gen_test def test_remove(self): - yield self.db.pod.remove({'_id': '1'}) - user = yield self.db.pod.find_one({'_id': '1'}) + yield self.db.pods.remove({'_id': '1'}) + user = yield self.db.pods.find_one({'_id': '1'}) self.assertIsNone(user) if __name__ == '__main__': diff --git a/utils/test/result_collection_api/tests/unit/test_pod.py b/utils/test/result_collection_api/tests/unit/test_pod.py index 5a3d485ab..07f55db8d 100644 --- a/utils/test/result_collection_api/tests/unit/test_pod.py +++ b/utils/test/result_collection_api/tests/unit/test_pod.py @@ -1,86 +1,72 @@ import unittest -import json from test_base import TestBase -from resources.pod_models import PodCreateRequest, \ - PodCreateResponse, PodsGetResponse -from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_FORBIDDEN - - -class TestPodCreate(TestBase): - req = PodCreateRequest(name='zte-1', mode='alive', details='zte pod 1') - +from resources.pod_models import PodCreateRequest, Pod, Pods +from common.constants import HTTP_OK, HTTP_BAD_REQUEST, \ + HTTP_FORBIDDEN, HTTP_NOT_FOUND + + +class TestPodBase(TestBase): + def setUp(self): + super(TestPodBase, self).setUp() + self.req_d = PodCreateRequest('zte-1', 'fuel', 'zte pod 1') + self.req_e = PodCreateRequest('zte-2', 'apex', 'zte pod 2') + self.get_res = Pod + self.list_res = Pods + self.basePath = '/pods' + + def assert_get_body(self, pod, req=None): + if not req: + req = self.req_d + self.assertEqual(pod.name, req.name) + self.assertEqual(pod.mode, req.mode) + self.assertEqual(pod.details, req.details) + self.assertIsNotNone(pod.creation_date) + self.assertIsNotNone(pod._id) + + +class TestPodCreate(TestPodBase): def test_withoutBody(self): - res = self.create('/pods', body=None) - self.assertEqual(res.code, HTTP_BAD_REQUEST) + (code, body) = self.create() + self.assertEqual(code, HTTP_BAD_REQUEST) def test_success(self): - res = self.create('/pods', body=self.req.format()) - self.assertEqual(res.code, HTTP_OK) - res_body = PodCreateResponse.from_dict(json.loads(res.body)) - self._assertMeta(res_body.meta, True) - self._assertBody(res_body.pod) + code, body = self.create_d() + self.assertEqual(code, HTTP_OK) + self.assert_create_body(body) def test_alreadyExist(self): - self.create('/pods', body=self.req.format()) - res = self.create('/pods', body=self.req.format()) - self.assertEqual(res.code, HTTP_FORBIDDEN) - self.assertIn('already exists', res.body) + self.create_d() + code, body = self.create_d() + self.assertEqual(code, HTTP_FORBIDDEN) + self.assertIn('already exists', body) def _assertMeta(self, meta, success): self.assertEqual(meta.success, success) if success: - self.assertEqual(meta.uri, '/pods/{}'.format(self.req.name)) - - def _assertBody(self, res): - self.assertEqual(res.name, self.req.name) - self.assertEqual(res.mode, self.req.mode) - self.assertEqual(res.details, self.req.details) - self.assertIsNotNone(res.creation_date) - self.assertIsNotNone(res._id) + self.assertEqual(meta.uri, '/pods/{}'.format(self.req_d.name)) -class TestPodGet(TestBase): +class TestPodGet(TestPodBase): def test_notExist(self): - res = self.get('/pods/notExist') - body = PodsGetResponse.from_dict(json.loads(res.body)) - self._assertMeta(body.meta, 0) + code, body = self.get('notExist') + self.assertEqual(code, HTTP_NOT_FOUND) def test_getOne(self): - self.create('/pods', body=TestPodCreate.req.format()) - res = self.get('/pods/{}'.format(TestPodCreate.req.name)) - body = PodsGetResponse.from_dict(json.loads(res.body)) - self._assertMeta(body.meta, 1) - self._assertBody(TestPodCreate.req, body.pods[0]) + self.create_d() + code, body = self.get(self.req_d.name) + self.assert_get_body(body) def test_list(self): - req = PodCreateRequest(name='zte-2', mode='alive', details='zte pod 2') - self.create('/pods', body=TestPodCreate.req.format()) - self.create('/pods', body=req.format()) - res = self.get('/pods') - body = PodsGetResponse.from_dict(json.loads(res.body)) - self._assertMeta(body.meta, 2) + self.create_d() + self.create_e() + code, body = self.get() + self.assertEqual(len(body.pods), 2) for pod in body.pods: - if req.name == pod.name: - self._assertBody(req, pod) + if self.req_d.name == pod.name: + self.assert_get_body(pod) else: - self._assertBody(TestPodCreate.req, pod) - - def _assertMeta(self, meta, total): - def check_success(): - if total is 0: - return False - else: - return True - self.assertEqual(meta.total, total) - self.assertEqual(meta.success, check_success()) - - def _assertBody(self, req, res): - self.assertEqual(res.name, req.name) - self.assertEqual(res.mode, req.mode) - self.assertEqual(res.details, req.details) - self.assertIsNotNone(res.creation_date) - + self.assert_get_body(pod, self.req_e) if __name__ == '__main__': unittest.main() diff --git a/utils/test/result_collection_api/tests/unit/test_project.py b/utils/test/result_collection_api/tests/unit/test_project.py new file mode 100644 index 000000000..e79311197 --- /dev/null +++ b/utils/test/result_collection_api/tests/unit/test_project.py @@ -0,0 +1,116 @@ +import unittest + +from test_base import TestBase +from resources.project_models import ProjectCreateRequest, Project, Projects +from common.constants import HTTP_OK, HTTP_BAD_REQUEST, \ + HTTP_FORBIDDEN, HTTP_NOT_FOUND + + +class TestProjectBase(TestBase): + def setUp(self): + super(TestProjectBase, self).setUp() + self.req_d = ProjectCreateRequest('vping', 'vping-ssh test') + self.req_e = ProjectCreateRequest('doctor', 'doctor test') + self.get_res = Project + self.list_res = Projects + self.update_res = Project + self.basePath = '/projects' + + def assert_body(self, project, req=None): + if not req: + req = self.req_d + self.assertEqual(project.name, req.name) + self.assertEqual(project.description, req.description) + self.assertIsNotNone(project._id) + self.assertIsNotNone(project.creation_date) + + +class TestProjectCreate(TestProjectBase): + def test_withoutBody(self): + (code, body) = self.create() + self.assertEqual(code, HTTP_BAD_REQUEST) + + def test_success(self): + (code, body) = self.create_d() + self.assertEqual(code, HTTP_OK) + self.assert_create_body(body) + + def test_alreadyExist(self): + self.create_d() + (code, body) = self.create_d() + self.assertEqual(code, HTTP_FORBIDDEN) + self.assertIn('already exists', body) + + +class TestProjectGet(TestProjectBase): + 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 project in body.projects: + if self.req_d.name == project.name: + self.assert_body(project) + else: + self.assert_body(project, self.req_e) + + +class TestProjectUpdate(TestProjectBase): + def test_withoutBody(self): + code, _ = self.update('noBody') + self.assertEqual(code, HTTP_BAD_REQUEST) + + def test_notFound(self): + code, _ = self.update('notFound', self.req_e) + 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) + 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) + 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 + + req = ProjectCreateRequest('newName', 'new description') + code, body = self.update(self.req_d.name, req) + self.assertEqual(code, HTTP_OK) + self.assertEqual(_id, body._id) + self.assert_body(body, req) + + _, new_body = self.get(req.name) + self.assertEqual(_id, new_body._id) + self.assert_body(new_body, req) + + +class TestProjectDelete(TestProjectBase): + def test_notFound(self): + code = self.delete('notFound') + self.assertEqual(code, HTTP_NOT_FOUND) + + def test_success(self): + self.create_d() + code = self.delete(self.req_d.name) + self.assertEqual(code, HTTP_OK) + +if __name__ == '__main__': + unittest.main() |