summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tests/unit/test_base.py
diff options
context:
space:
mode:
authorMorgan Richomme <morgan.richomme@orange.com>2016-05-19 12:29:55 +0000
committerGerrit Code Review <gerrit@172.30.200.206>2016-05-19 12:29:55 +0000
commit80014d7bb5932998e95ca37729b68938fb28fe9f (patch)
tree90bc44912ccbe41c8d2859cfb4728be5f601e43a /utils/test/result_collection_api/tests/unit/test_base.py
parent06c23d763454fc5d2c0626a88c550331a3249ef4 (diff)
parentf25ad4e8a7e4bd6505b2144494da81fbcad40b7d (diff)
Merge "add unittests of project & refactor response"
Diffstat (limited to 'utils/test/result_collection_api/tests/unit/test_base.py')
-rw-r--r--utils/test/result_collection_api/tests/unit/test_base.py89
1 files changed, 74 insertions, 15 deletions
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()