3 from tornado.web import Application
4 from tornado.testing import AsyncHTTPTestCase
6 from opnfv_testapi.router import url_mappings
7 from opnfv_testapi.resources.models import CreateResponse
11 class TestBase(AsyncHTTPTestCase):
12 headers = {'Content-Type': 'application/json; charset=UTF-8'}
16 self.create_res = CreateResponse
19 self.update_res = None
22 self.addCleanup(self._clear)
23 super(TestBase, self).setUp()
27 url_mappings.mappings,
32 def create_d(self, *args):
33 return self.create(self.req_d, *args)
35 def create_e(self, *args):
36 return self.create(self.req_e, *args)
38 def create(self, req=None, *args):
39 return self.create_help(self.basePath, req, *args)
41 def create_help(self, uri, req, *args):
44 res = self.fetch(self._update_uri(uri, *args),
49 return self._get_return(res, self.create_res)
52 res = self.fetch(self._get_uri(*args),
57 new_args, num = self._get_valid_args(*args)
59 if num != self._need_arg_num(self.basePath) else self.list_res
60 return self._get_return(res, inner())
62 def query(self, query):
63 res = self.fetch(self._get_query_uri(query),
66 return self._get_return(res, self.list_res)
68 def update(self, new=None, *args):
71 res = self.fetch(self._get_uri(*args),
75 return self._get_return(res, self.update_res)
77 def delete(self, *args):
78 res = self.fetch(self._get_uri(*args),
81 return res.code, res.body
84 def _get_valid_args(*args):
85 new_args = tuple(['%s' % arg for arg in args if arg is not None])
86 return new_args, len(new_args)
88 def _need_arg_num(self, uri):
89 return uri.count('%s')
91 def _get_query_uri(self, query):
92 return self.basePath + '?' + query
94 def _get_uri(self, *args):
95 return self._update_uri(self.basePath, *args)
97 def _update_uri(self, uri, *args):
99 new_args, num = self._get_valid_args(*args)
100 if num != self._need_arg_num(uri):
103 return r_uri % tuple(['%s' % arg for arg in new_args])
105 def _get_return(self, res, cls):
108 return code, self._get_return_body(code, body, cls)
111 def _get_return_body(code, body, cls):
112 return cls.from_dict(json.loads(body)) if code < 300 and cls else body
114 def assert_href(self, body):
115 self.assertIn(self.basePath, body.href)
117 def assert_create_body(self, body, req=None, *args):
120 new_args = args + tuple([req.name])
121 self.assertIn(self._get_uri(*new_args), body.href)
125 fake_pymongo.pods.clear()
126 fake_pymongo.projects.clear()
127 fake_pymongo.testcases.clear()
128 fake_pymongo.results.clear()