diff options
Diffstat (limited to 'utils/test/result_collection_api/tests')
3 files changed, 113 insertions, 6 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 e2db46038..e5ded376e 100644 --- a/utils/test/result_collection_api/tests/unit/fake_pymongo.py +++ b/utils/test/result_collection_api/tests/unit/fake_pymongo.py @@ -123,6 +123,9 @@ class MemDb(object): result = executor.submit(self._remove, spec_or_id) return result + def clear(self): + self._remove() + pod = MemDb() test_projects = MemDb() test_cases = 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 b72436eb0..98190fb94 100644 --- a/utils/test/result_collection_api/tests/unit/test_base.py +++ b/utils/test/result_collection_api/tests/unit/test_base.py @@ -1,3 +1,4 @@ +import json from tornado.web import Application from tornado.testing import AsyncHTTPTestCase @@ -7,6 +8,12 @@ import fake_pymongo class TestBase(AsyncHTTPTestCase): + headers = {'Content-Type': 'application/json; charset=UTF-8'} + + def setUp(self): + self.addCleanup(self._clear) + super(TestBase, self).setUp() + def get_app(self): return Application( [ @@ -28,9 +35,20 @@ class TestBase(AsyncHTTPTestCase): debug=True, ) - def tearDown(self): - yield fake_pymongo.pod.remove() - yield fake_pymongo.test_projects.remove() - yield fake_pymongo.test_cases.remove() - yield fake_pymongo.test_results.remove() - super(TestBase, self).tearDown() + def create(self, uri, body=None): + return self.fetch(uri, + method='POST', + body=json.dumps(body), + headers=self.headers) + + def get(self, uri): + return self.fetch(uri, + method='GET', + headers=self.headers) + + @staticmethod + def _clear(): + fake_pymongo.pod.clear() + fake_pymongo.test_projects.clear() + fake_pymongo.test_cases.clear() + fake_pymongo.test_results.clear() diff --git a/utils/test/result_collection_api/tests/unit/test_pod.py b/utils/test/result_collection_api/tests/unit/test_pod.py new file mode 100644 index 000000000..5a3d485ab --- /dev/null +++ b/utils/test/result_collection_api/tests/unit/test_pod.py @@ -0,0 +1,86 @@ +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') + + def test_withoutBody(self): + res = self.create('/pods', body=None) + self.assertEqual(res.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) + + 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) + + 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) + + +class TestPodGet(TestBase): + def test_notExist(self): + res = self.get('/pods/notExist') + body = PodsGetResponse.from_dict(json.loads(res.body)) + self._assertMeta(body.meta, 0) + + 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]) + + 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) + for pod in body.pods: + if req.name == pod.name: + self._assertBody(req, 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) + + +if __name__ == '__main__': + unittest.main() |