def test_schema_property_instantiation(self): """PropertySchema instantiation testing to confirm dict behavior.""" schema_property = PropertySchema({'type': 'int', 'required': True}) self.assertIsNotNone(schema_property) schema_property = PropertySchema({'type': 'int', 'required': False}) self.assertIsNotNone(schema_property)
def test_validate_schema_exception_handling(self): """Ensure validate_schema covers basic exception reporting.""" property_schema = PropertySchema() property_schema.required = 'UNDEFINED' schema_instance = SchemaType() schema_instance.some_attr = property_schema self.assertRaisesRegexp( ValidationException, r"""The value for "required" is not """ r"""of type "<type 'bool'>": UNDEFINED""", schema_type.validate_schema, schema_instance) expected_errors_list = [ """The value for "required" is not of type "<type 'bool'>": UNDEFINED""" ] try: schema_type.validate_schema(schema_instance) self.fail('A ValidationException should have been thrown.') except ValidationException as ve: self.assertListEqual(expected_errors_list, ve.validation_errors) errors = schema_type.validate_schema(schema_instance, raise_validation_exception=False) self.assertListEqual(expected_errors_list, errors)
def __init__(self, *args, **kwargs): r"""Initializes in accordance with dict specification. Dict Style Initialization *SchemaType* supports dict style initialization. SchemaType() -> new empty SchemaType SchemaType(mapping) -> new SchemaType initialized from a mapping object's (key, value) pairs SchemaType(iterable) -> new SchemaType initialized as if via:: d = SchemaType() for k, v in iterable: d[k] = v SchemaType(\*\*kwargs) -> new SchemaType initialized with the name=value pairs in the keyword argument list. For example:: SchemaType(one={. . .}, two={. . .}) """ super(SchemaType, self).__init__(*args, **kwargs) for key, value in self.iteritems(): if not isinstance(value, PropertySchema): self[key] = PropertySchema(value)
def test_property_schema_instantiation(self): """PropertySchema instantiation testing to confirm dict behavior.""" property_schema = PropertySchema() self.assertIsNotNone(property_schema) expected_schema = { 'default': None, 'enum': None, 'max': 7, 'member_max': None, 'member_min': None, 'member_type': None, 'min': 3, 'regex': None, 'required': True, 'type': int, } property_schema = PropertySchema({ 'type': 'int', 'required': True, 'min': 3, 'max': 7, }) self.assertIsNotNone(property_schema) self.assertDictEqual(expected_schema, property_schema) property_schema = PropertySchema(type='int', required=True, min=3, max=7) self.assertIsNotNone(property_schema) self.assertDictEqual(expected_schema, property_schema) property_schema = PropertySchema([['type', 'int'], ['required', True], ['min', 3], ['max', 7]]) self.assertIsNotNone(property_schema) self.assertDictEqual(expected_schema, property_schema)
def test_validate_schema_property_exception(self): """Test validate_schema validation exception handling.""" invalid_property_schema = PropertySchema() invalid_property_schema.type = 'UNKNOWN' self.maxDiff = None self.assertRaisesRegexp( ValidationException, r"""The value "UNKNOWN" for "type" not in enumeration \[.*\].""", meta_type.validate_property_schema, invalid_property_schema) value_errors = meta_type.validate_property_schema( invalid_property_schema, raise_validation_exception=False) self.assertEqual(1, len(value_errors)) self.assertTrue(value_errors[0].startswith( 'The value "UNKNOWN" for "type" not in enumeration'))