From 489f38e131fdcf554b92d1b2f11867fec23f39b7 Mon Sep 17 00:00:00 2001 From: jiansong Date: Tue, 13 Sep 2016 01:51:45 -0700 Subject: [PATCH] Concentrated test_validate_xx_configuration to a new method It seems,Test what name such as test_validate_xx_configuration, only have different parameters, so we can take them to focus on a new method, to reduce repetitive code. Closes-Bug: #1622939 Change-Id: I80ae25a37a862ca3315291a88e3dbc39c1a94fc8 --- .../test_configuration_controller.py | 97 ++++++++++--------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/trove/tests/unittests/configuration/test_configuration_controller.py b/trove/tests/unittests/configuration/test_configuration_controller.py index f4b76cb3f2..9d1de61eae 100644 --- a/trove/tests/unittests/configuration/test_configuration_controller.py +++ b/trove/tests/unittests/configuration/test_configuration_controller.py @@ -53,6 +53,19 @@ class TestConfigurationController(trove_testtools.TestCase): super(TestConfigurationController, self).setUp() self.controller = ConfigurationsController() + def _test_validate_configuration_with_action(self, body, action, + is_valid=True): + schema = self.controller.get_schema(action, body) + self.assertIsNotNone(schema) + validator = jsonschema.Draft4Validator(schema) + if is_valid: + self.assertTrue(validator.is_valid(body)) + else: + self.assertFalse(validator.is_valid(body)) + errors = sorted(validator.iter_errors(body), key=lambda e: e.path) + error_messages = [error.message for error in errors] + return error_messages + def test_validate_create_configuration(self): body = { "configuration": { @@ -64,10 +77,7 @@ class TestConfigurationController(trove_testtools.TestCase): } } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertTrue(validator.is_valid(body)) + self._test_validate_configuration_with_action(body, action='create') def test_validate_create_configuration_no_datastore(self): body = { @@ -76,10 +86,7 @@ class TestConfigurationController(trove_testtools.TestCase): "name": "test" } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertTrue(validator.is_valid(body)) + self._test_validate_configuration_with_action(body, action='create') def test_validate_create_invalid_values_param(self): body = { @@ -92,12 +99,10 @@ class TestConfigurationController(trove_testtools.TestCase): } } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertFalse(validator.is_valid(body)) - errors = sorted(validator.iter_errors(body), key=lambda e: e.path) - error_messages = [error.message for error in errors] + error_messages = ( + self._test_validate_configuration_with_action(body, + action='create', + is_valid=False)) self.assertIn("'' is not of type 'object'", error_messages) def test_validate_create_invalid_name_param(self): @@ -111,12 +116,10 @@ class TestConfigurationController(trove_testtools.TestCase): } } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertFalse(validator.is_valid(body)) - errors = sorted(validator.iter_errors(body), key=lambda e: e.path) - error_messages = [error.message for error in errors] + error_messages = ( + self._test_validate_configuration_with_action(body, + action='create', + is_valid=False)) self.assertIn("'' is too short", error_messages) def test_validate_edit_configuration(self): @@ -125,10 +128,7 @@ class TestConfigurationController(trove_testtools.TestCase): "values": {} } } - schema = self.controller.get_schema('edit', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertTrue(validator.is_valid(body)) + self._test_validate_configuration_with_action(body, action="edit") def _test_validate_configuration(self, input_values, config_rules=None): if config_rules is None: @@ -186,6 +186,19 @@ class TestConfigurationsParameterController(trove_testtools.TestCase): super(TestConfigurationsParameterController, self).setUp() self.controller = service.ConfigurationsParameterController() + def _test_validate_configuration_with_action(self, body, action, + is_valid=True): + schema = self.controller.get_schema(action, body) + self.assertIsNotNone(schema) + validator = jsonschema.Draft4Validator(schema) + if is_valid: + self.assertTrue(validator.is_valid(body)) + else: + self.assertFalse(validator.is_valid(body)) + errors = sorted(validator.iter_errors(body), key=lambda e: e.path) + error_messages = [error.message for error in errors] + return error_messages + def test_validate_create_configuration_param(self): body = { 'configuration-parameter': { @@ -196,10 +209,8 @@ class TestConfigurationsParameterController(trove_testtools.TestCase): 'max': '255' } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertTrue(validator.is_valid(body)) + + self._test_validate_configuration_with_action(body, action='create') def test_validate_create_invalid_restart_required(self): body = { @@ -211,12 +222,10 @@ class TestConfigurationsParameterController(trove_testtools.TestCase): 'max': 255 } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertFalse(validator.is_valid(body)) - errors = sorted(validator.iter_errors(body), key=lambda e: e.path) - error_messages = [error.message for error in errors] + error_messages = ( + self._test_validate_configuration_with_action(body, + action='create', + is_valid=False)) self.assertIn("5 is greater than the maximum of 1", error_messages) self.assertIn("0 is not of type 'string'", error_messages) self.assertIn("255 is not of type 'string'", error_messages) @@ -231,12 +240,10 @@ class TestConfigurationsParameterController(trove_testtools.TestCase): 'max': '255' } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertFalse(validator.is_valid(body)) - errors = sorted(validator.iter_errors(body), key=lambda e: e.path) - error_messages = [error.message for error in errors] + error_messages = ( + self._test_validate_configuration_with_action(body, + action='create', + is_valid=False)) self.assertIn("-1 is less than the minimum of 0", error_messages) def test_validate_create_invalid_restart_required_3(self): @@ -249,10 +256,8 @@ class TestConfigurationsParameterController(trove_testtools.TestCase): 'max': '255' } } - schema = self.controller.get_schema('create', body) - self.assertIsNotNone(schema) - validator = jsonschema.Draft4Validator(schema) - self.assertFalse(validator.is_valid(body)) - errors = sorted(validator.iter_errors(body), key=lambda e: e.path) - error_messages = [error.message for error in errors] + error_messages = ( + self._test_validate_configuration_with_action(body, + action='create', + is_valid=False)) self.assertIn("'yes' is not of type 'integer'", error_messages)