X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=utils%2Ftest%2Ftestapi%2Fopnfv_testapi%2Ftests%2Funit%2Ftest_base.py;h=4d3445659a6765740b648d5b6c49b7986e81aed6;hb=1dea2cad906da7da95ccd84f96ed49429e2b9c3d;hp=ff1a1932c1ca065bd983c55180b14f49f2d2f74f;hpb=5559f4d344bfe26b39a01f76954ce864c589eae3;p=releng.git diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py index ff1a1932c..4d3445659 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py @@ -7,21 +7,26 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import json +from os import path -from tornado.web import Application -from tornado.testing import AsyncHTTPTestCase +import mock +from tornado import testing -from opnfv_testapi.router import url_mappings -from opnfv_testapi.resources.models import CreateResponse -import fake_pymongo +from opnfv_testapi.common import config +from opnfv_testapi.resources import models +from opnfv_testapi.tests.unit import fake_pymongo +config.Config.CONFIG = path.join(path.dirname(__file__), + '../../../etc/config.ini') -class TestBase(AsyncHTTPTestCase): + +class TestBase(testing.AsyncHTTPTestCase): headers = {'Content-Type': 'application/json; charset=UTF-8'} def setUp(self): + self._patch_server() self.basePath = '' - self.create_res = CreateResponse + self.create_res = models.CreateResponse self.get_res = None self.list_res = None self.update_res = None @@ -30,12 +35,26 @@ class TestBase(AsyncHTTPTestCase): self.addCleanup(self._clear) super(TestBase, self).setUp() + def tearDown(self): + self.db_patcher.stop() + + def _patch_server(self): + from opnfv_testapi.cmd import server + server.parse_config([ + '--config-file', + path.join(path.dirname(__file__), 'common/normal.ini') + ]) + self.db_patcher = mock.patch('opnfv_testapi.cmd.server.get_db', + self._fake_pymongo) + self.db_patcher.start() + + @staticmethod + def _fake_pymongo(): + return fake_pymongo + def get_app(self): - return Application( - url_mappings.mappings, - db=fake_pymongo, - debug=True, - ) + from opnfv_testapi.cmd import server + return server.make_app() def create_d(self, *args): return self.create(self.req_d, *args) @@ -47,7 +66,7 @@ class TestBase(AsyncHTTPTestCase): return self.create_help(self.basePath, req, *args) def create_help(self, uri, req, *args): - if req: + if req and not isinstance(req, str) and hasattr(req, 'format'): req = req.format() res = self.fetch(self._update_uri(uri, *args), method='POST', @@ -97,7 +116,7 @@ class TestBase(AsyncHTTPTestCase): return uri.count('%s') def _get_query_uri(self, query): - return self.basePath + '?' + query + return self.basePath + '?' + query if query else self.basePath def _get_uri(self, *args): return self._update_uri(self.basePath, *args) @@ -123,9 +142,17 @@ class TestBase(AsyncHTTPTestCase): self.assertIn(self.basePath, body.href) def assert_create_body(self, body, req=None, *args): + import inspect if not req: req = self.req_d - new_args = args + tuple([req.name]) + resource_name = '' + if inspect.isclass(req): + resource_name = req.name + elif isinstance(req, dict): + resource_name = req['name'] + elif isinstance(req, str): + resource_name = json.loads(req)['name'] + new_args = args + tuple([resource_name]) self.assertIn(self._get_uri(*new_args), body.href) @staticmethod @@ -134,3 +161,4 @@ class TestBase(AsyncHTTPTestCase): fake_pymongo.projects.clear() fake_pymongo.testcases.clear() fake_pymongo.results.clear() + fake_pymongo.scenarios.clear()