def test_webapi_request_fields_state(self): """Testing @webapi_request_fields state""" def orig_func(): """Function 1""" required = { 'required_param': { 'type': bool, 'description': 'Required param' }, } optional = { 'optional_param': { 'type': bool, 'description': 'Optional param' }, } func = webapi_request_fields(required, optional)(orig_func) self.assertFalse(hasattr(orig_func, 'required_fields')) self.assertFalse(hasattr(orig_func, 'optional_fields')) self.assertFalse(hasattr(orig_func, 'response_errors')) self.assertEqual(func.__name__, 'orig_func') self.assertEqual(func.__doc__, 'Function 1') self.assertTrue(hasattr(func, 'response_errors')) self.assertTrue(hasattr(func, 'required_fields')) self.assertTrue(hasattr(func, 'optional_fields')) self.assertEqual(func.required_fields, required) self.assertEqual(func.optional_fields, optional) self.assertEqual(func.response_errors, set([INVALID_FORM_DATA]))
def test_webapi_request_fields_preserves_state(self): """Testing @webapi_request_fields preserves decorator state""" required1 = { 'required1': { 'type': bool, 'description': 'Required param' }, } optional1 = { 'optional1': { 'type': bool, 'description': 'Optional param' }, } @webapi_request_fields(required1, optional1) @webapi_response_errors(DOES_NOT_EXIST) def orig_func(): """Function 1""" required2 = { 'required2': { 'type': bool, 'description': 'Required param' }, } optional2 = { 'optional2': { 'type': bool, 'description': 'Optional param' }, } func = webapi_request_fields(required2, optional2)(orig_func) expected_required = required1.copy() expected_required.update(required2) expected_optional = optional1.copy() expected_optional.update(optional2) self.assertTrue(hasattr(orig_func, 'required_fields')) self.assertTrue(hasattr(orig_func, 'optional_fields')) self.assertTrue(hasattr(orig_func, 'response_errors')) self.assertEqual(func.__name__, 'orig_func') self.assertEqual(func.__doc__, 'Function 1') self.assertTrue(hasattr(func, 'response_errors')) self.assertTrue(hasattr(func, 'required_fields')) self.assertTrue(hasattr(func, 'optional_fields')) self.assertEqual(func.required_fields, expected_required) self.assertEqual(func.optional_fields, expected_optional) self.assertEqual(func.response_errors, set([DOES_NOT_EXIST, INVALID_FORM_DATA]))