From c12d3a2c298724bf5186c627967430f32a7e33ba Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 10 Apr 2017 10:27:37 +0800 Subject: import from absolute path in TestAPI unit Change-Id: Ic42db22d825229713b38c75c141af7742ca65dd3 Signed-off-by: SerenaFeng --- utils/test/testapi/opnfv_testapi/tests/unit/test_base.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_fake_pymongo.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_project.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_result.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_testcase.py | 2 +- utils/test/testapi/opnfv_testapi/tests/unit/test_token.py | 4 ++-- utils/test/testapi/opnfv_testapi/tests/unit/test_version.py | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) 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 b955f4a5a..a6e733914 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_base.py @@ -12,9 +12,9 @@ from os import path import mock from tornado import testing -import fake_pymongo from opnfv_testapi.cmd import server from opnfv_testapi.resources import models +from opnfv_testapi.tests.unit import fake_pymongo class TestBase(testing.AsyncHTTPTestCase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_fake_pymongo.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_fake_pymongo.py index 7c43fca62..1ebc96f3b 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_fake_pymongo.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_fake_pymongo.py @@ -12,7 +12,7 @@ from tornado import gen from tornado import testing from tornado import web -import fake_pymongo +from opnfv_testapi.tests.unit import fake_pymongo class MyTest(testing.AsyncHTTPTestCase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py index cae86e8bb..c2c14149b 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py @@ -11,7 +11,7 @@ import unittest from opnfv_testapi.common import message from opnfv_testapi.resources import pod_models -import test_base as base +from opnfv_testapi.tests.unit import test_base as base class TestPodBase(base.TestBase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_project.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_project.py index 74cefd711..9143f8a8a 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_project.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_project.py @@ -11,7 +11,7 @@ import unittest from opnfv_testapi.common import message from opnfv_testapi.resources import project_models -import test_base as base +from opnfv_testapi.tests.unit import test_base as base class TestProjectBase(base.TestBase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_result.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_result.py index 2e0aa3685..940279cd4 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_result.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_result.py @@ -16,7 +16,7 @@ from opnfv_testapi.resources import pod_models from opnfv_testapi.resources import project_models from opnfv_testapi.resources import result_models from opnfv_testapi.resources import testcase_models -import test_base as base +from opnfv_testapi.tests.unit import test_base as base class Details(object): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py index f2291a566..b232bc168 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_scenario.py @@ -7,7 +7,7 @@ import os from opnfv_testapi.common import message import opnfv_testapi.resources.scenario_models as models -import test_base as base +from opnfv_testapi.tests.unit import test_base as base class TestScenarioBase(base.TestBase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_testcase.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_testcase.py index 62d0fa043..73c481986 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_testcase.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_testcase.py @@ -13,7 +13,7 @@ import unittest from opnfv_testapi.common import message from opnfv_testapi.resources import project_models from opnfv_testapi.resources import testcase_models -import test_base as base +from opnfv_testapi.tests.unit import test_base as base class TestCaseBase(base.TestBase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_token.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_token.py index ed3eda0f7..6e3d4b3ea 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_token.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_token.py @@ -8,11 +8,11 @@ import unittest from tornado import web -import fake_pymongo from opnfv_testapi.common import message from opnfv_testapi.resources import project_models from opnfv_testapi.router import url_mappings -import test_base as base +from opnfv_testapi.tests.unit import fake_pymongo +from opnfv_testapi.tests.unit import test_base as base class TestToken(base.TestBase): diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_version.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_version.py index c8f3f5062..5cd83fda5 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_version.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_version.py @@ -9,7 +9,7 @@ import unittest from opnfv_testapi.resources import models -import test_base as base +from opnfv_testapi.tests.unit import test_base as base class TestVersionBase(base.TestBase): -- cgit 1.2.3-korg From ac305c04f90715fe92894817966e3afcdb1a54e6 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 10 Apr 2017 11:03:29 +0800 Subject: impl executor and leverage to test_pod.py in TestAPI implement executor.py leverage executor to test_pod.py Change-Id: Ief70a28a935c86430e26f90f35112a7bab9fa81b Signed-off-by: SerenaFeng --- .../testapi/opnfv_testapi/tests/unit/executor.py | 83 ++++++++++++++++++++++ .../testapi/opnfv_testapi/tests/unit/test_pod.py | 48 ++++++------- 2 files changed, 107 insertions(+), 24 deletions(-) create mode 100644 utils/test/testapi/opnfv_testapi/tests/unit/executor.py diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/executor.py b/utils/test/testapi/opnfv_testapi/tests/unit/executor.py new file mode 100644 index 000000000..b30c3258b --- /dev/null +++ b/utils/test/testapi/opnfv_testapi/tests/unit/executor.py @@ -0,0 +1,83 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp +# feng.xiaowei@zte.com.cn +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import functools +import httplib + + +def create(excepted_status, excepted_response): + def _create(create_request): + @functools.wraps(create_request) + def wrap(self): + request = create_request(self) + status, body = self.create(request) + if excepted_status == httplib.OK: + getattr(self, excepted_response)(body) + else: + self.assertIn(excepted_response, body) + return wrap + return _create + + +def get(excepted_status, excepted_response): + def _get(get_request): + @functools.wraps(get_request) + def wrap(self): + request = get_request(self) + status, body = self.get(request) + if excepted_status == httplib.OK: + getattr(self, excepted_response)(body) + else: + self.assertIn(excepted_response, body) + return wrap + return _get + + +def update(excepted_status, excepted_response): + def _update(update_request): + @functools.wraps(update_request) + def wrap(self): + request, resource = update_request(self) + status, body = self.update(request, resource) + if excepted_status == httplib.OK: + getattr(self, excepted_response)(request, body) + else: + self.assertIn(excepted_response, body) + return wrap + return _update + + +def delete(excepted_status, excepted_response): + def _delete(delete_request): + @functools.wraps(delete_request) + def wrap(self): + request = delete_request(self) + if isinstance(request, tuple): + status, body = self.delete(request[0], *(request[1])) + else: + status, body = self.delete(request) + if excepted_status == httplib.OK: + getattr(self, excepted_response)(body) + else: + self.assertIn(excepted_response, body) + return wrap + return _delete + + +def query(excepted_status, excepted_response, number=0): + def _query(get_request): + @functools.wraps(get_request) + def wrap(self): + request = get_request(self) + status, body = self.query(request) + if excepted_status == httplib.OK: + getattr(self, excepted_response)(body, number) + else: + self.assertIn(excepted_response, body) + return wrap + return _query diff --git a/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py b/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py index c2c14149b..0ed348df9 100644 --- a/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py +++ b/utils/test/testapi/opnfv_testapi/tests/unit/test_pod.py @@ -11,6 +11,7 @@ import unittest from opnfv_testapi.common import message from opnfv_testapi.resources import pod_models +from opnfv_testapi.tests.unit import executor from opnfv_testapi.tests.unit import test_base as base @@ -36,48 +37,47 @@ class TestPodBase(base.TestBase): class TestPodCreate(TestPodBase): + @executor.create(httplib.BAD_REQUEST, message.no_body()) def test_withoutBody(self): - (code, body) = self.create() - self.assertEqual(code, httplib.BAD_REQUEST) + return None + @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_emptyName(self): - req_empty = pod_models.PodCreateRequest('') - (code, body) = self.create(req_empty) - self.assertEqual(code, httplib.BAD_REQUEST) - self.assertIn(message.missing('name'), body) + return pod_models.PodCreateRequest('') + @executor.create(httplib.BAD_REQUEST, message.missing('name')) def test_noneName(self): - req_none = pod_models.PodCreateRequest(None) - (code, body) = self.create(req_none) - self.assertEqual(code, httplib.BAD_REQUEST) - self.assertIn(message.missing('name'), body) + return pod_models.PodCreateRequest(None) + @executor.create(httplib.OK, 'assert_create_body') def test_success(self): - code, body = self.create_d() - self.assertEqual(code, httplib.OK) - self.assert_create_body(body) + return self.req_d + @executor.create(httplib.FORBIDDEN, message.exist_base) def test_alreadyExist(self): self.create_d() - code, body = self.create_d() - self.assertEqual(code, httplib.FORBIDDEN) - self.assertIn(message.exist_base, body) + return self.req_d class TestPodGet(TestPodBase): + def setUp(self): + super(TestPodGet, self).setUp() + self.create_d() + self.create_e() + + @executor.get(httplib.NOT_FOUND, message.not_found_base) def test_notExist(self): - code, body = self.get('notExist') - self.assertEqual(code, httplib.NOT_FOUND) + return 'notExist' + @executor.get(httplib.OK, 'assert_get_body') def test_getOne(self): - self.create_d() - code, body = self.get(self.req_d.name) - self.assert_get_body(body) + return self.req_d.name + @executor.get(httplib.OK, '_assert_list') def test_list(self): - self.create_d() - self.create_e() - code, body = self.get() + return None + + def _assert_list(self, body): self.assertEqual(len(body.pods), 2) for pod in body.pods: if self.req_d.name == pod.name: -- cgit 1.2.3-korg