def test_schema_validate(self): s = Schema(**SCHEMA_1) # Duplicate unique attribute 'name' m = [{ 'type': 'listener', 'name': 'x' }, { 'type': 'listener', 'name': 'x' }] self.assertRaises(ValidationError, s.validate_all, m) # Duplicate singleton entity 'container' m = [{ 'type': 'container', 'name': 'x' }, { 'type': 'container', 'name': 'y' }] self.assertRaises(ValidationError, s.validate_all, m) # Valid model m = [{ 'type': 'container', 'name': 'x' }, { 'type': 'listener', 'name': 'y' }] s.validate_all(m)
def test_schema_entity(self): s = Schema(**SCHEMA_1) self.assertRaises(ValidationError, s.entity, {'type': 'nosuch'}) self.assertRaises(ValidationError, s.entity, { 'type': 'listener', 'nosuch': 'x' }) e = s.entity({'host': 'foo', 'type': 'listener', 'name': 'listener-1'}) self.assertEqual(e.attributes, { 'host': 'foo', 'name': 'listener-1', 'type': 'org.example.listener' }) self.assertEqual(e['host'], 'foo') self.assertRaises(ValidationError, e.__setitem__, 'nosuch', 'x') # The container entity itself has been deprecated self.assertTrue(s.entity_types['org.example.container'].deprecated) # This will make sure that deprecated flag defaults to false for entities self.assertFalse(s.entity_types['org.example.connector'].deprecated) # This will make sure that deprecated flag defaults to false for attributes of entities self.assertFalse(s.entity_types['org.example.listener']. attributes['host'].deprecated) try: e.nosuch = 'x' self.fail("Expected exception") except: pass
def test_entity_type(self): s = Schema() e = EntityType('MyEntity', s, attributes={ 'foo': {'type': 'string', 'default': 'FOO'}, 'req': {'type': 'integer', 'required': True}, 'e': {'type': ['x', 'y']}}) e.init() self.assertRaises(ValidationError, e.validate, {}) # Missing required 'req' self.assertEqual(e.validate({'req': 42}), {'foo': 'FOO', 'req': 42})
def test_entity_type(self): s = Schema( annotations={ 'i1': { 'attributes': { 'foo1': { 'type': 'string', 'default': 'FOO1' } } }, 'i2': { 'attributes': { 'foo2': { 'type': 'string', 'default': 'FOO2' } } } }) e = EntityType('MyEntity', s, attributes={ 'foo': { 'type': 'string', 'default': 'FOO' }, 'req': { 'type': 'integer', 'required': True }, 'e': { 'type': ['x', 'y'] } }) e.init() self.assertRaises(ValidationError, e.validate, {}) # Missing required 'req' self.assertEqual(e.validate({'req': 42}), {'foo': 'FOO', 'req': 42}) # Try with an annotation e = EntityType('e2', s, attributes={'x': { 'type': 'integer' }}, annotations=['i1', 'i2']) e.init() self.assertEqual(e.validate({'x': 1}), { 'x': 1, 'foo1': 'FOO1', 'foo2': 'FOO2' })
def test_schema_entity(self): s = Schema(**SCHEMA_1) self.assertRaises(ValidationError, s.entity, {'type': 'nosuch'}) self.assertRaises(ValidationError, s.entity, { 'type': 'listener', 'nosuch': 'x' }) e = s.entity({'type': 'listener', 'name': 'x', 'addr': 'foo'}) self.assertEqual(e.attributes, { 'type': 'org.example.listener', 'name': 'x', 'addr': 'foo' }) self.assertEqual(e['addr'], 'foo') self.assertRaises(ValidationError, e.__setitem__, 'nosuch', 'x') try: e.nosuch = 'x' self.fail("Expected exception") except: pass
def test_entity_refs(self): e = EntityType('MyEntity', Schema(), attributes={ 'type': { 'type': 'string', 'required': True, 'value': '$$entityType' }, 'name': { 'type': 'string', 'default': '$identity' }, 'identity': { 'type': 'string', 'default': '$name', "required": True } }) self.assertEqual({ 'type': 'MyEntity', 'identity': 'x', 'name': 'x' }, e.validate({'identity': 'x'})) self.assertEqual({ 'type': 'MyEntity', 'identity': 'x', 'name': 'x' }, e.validate({'name': 'x'})) self.assertEqual({ 'type': 'MyEntity', 'identity': 'x', 'name': 'y' }, e.validate({ 'identity': 'x', 'name': 'y' })) self.assertRaises(ValidationError, e.validate, {}) # Circular reference.
def test_entity_annotation_refs(self): s = Schema( annotations={ 'i1': { 'attributes': { 'name': { 'type': 'string', 'default': '$identity' }, 'identity': { 'type': 'string', 'default': '$name', "required": True } } } }) e = EntityType('MyEntity', s, attributes={}, annotations=['i1']) e.init() self.assertEqual({ 'identity': 'x', 'name': 'x' }, e.validate({'identity': 'x'})) self.assertEqual({ 'identity': 'x', 'name': 'x' }, e.validate({'name': 'x'})) self.assertEqual({ 'identity': 'x', 'name': 'y' }, e.validate({ 'identity': 'x', 'name': 'y' })) self.assertRaises(ValidationError, e.validate, {})