diff options
author | 2016-05-19 18:13:27 +0800 | |
---|---|---|
committer | 2016-05-19 18:18:07 +0800 | |
commit | 09f38fb67b1caeb4ccd33b83120842fbad306f78 (patch) | |
tree | 3331bf5af2add086548f8d4cec12f9f154fb0df1 /result_collection_api/tests/unit/test_pod.py | |
parent | 40c340fadbbcb1be66f011759f7daa3bb0e3927e (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 'result_collection_api/tests/unit/test_pod.py')
-rw-r--r-- | result_collection_api/tests/unit/test_pod.py | 110 |
1 files changed, 48 insertions, 62 deletions
diff --git a/result_collection_api/tests/unit/test_pod.py b/result_collection_api/tests/unit/test_pod.py index 5a3d485..07f55db 100644 --- a/result_collection_api/tests/unit/test_pod.py +++ b/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() |