def test_bad_int_size_constraint(self): class Entity(EntityType): attr = Int(maxsize=40) with self.assertRaises(BadSchemaDefinition) as cm: build_schema_from_namespace(locals().items()) self.assertEqual("size constraint doesn't apply to Int entity type", str(cm.exception))
def test_bad_rtype_inlined_conflict2(self): class Anentity(EntityType): rel = SubjectRelation('Anentity', inlined=True) class rel(RelationType): inlined = False with self.assertRaises(BadSchemaDefinition) as cm: build_schema_from_namespace(locals().items()) self.assertBadInlinedMessage(cm.exception)
def test_bad_rtype_rdef_conflict(self): class foo(RelationType): __permissions__={'read': ()} class Entity(EntityType): foo = String(__permissions__={'add': ()}) with self.assertRaises(BadSchemaDefinition) as cm: build_schema_from_namespace(locals().items()) self.assertEqual("conflicting values {'add': ()}/{'read': ()} for property __permissions__ of relation 'foo'", str(cm.exception))
def test_override_read_perms_on_computed_attribute(self): class Entity(EntityType): oattr = String() subjrel = SubjectRelation('String') class attr(RelationDefinition): __permissions__ = {'read': ('clows', ), 'add': (), 'update': ()} subject = 'Entity' object = 'Int' formula = 'Any Z WHERE X oattr Z' class foo(RelationDefinition): subject = 'Entity' object = 'Boolean' schema = build_schema_from_namespace(vars().items()) self.assertEqual({'read': ('clows', ), 'update': (), 'add': ()}, schema['attr'].rdef('Entity', 'Int').permissions) self.assertEqual( DEFAULT_ATTRPERMS, schema['foo'].rdef('Entity', 'Boolean').permissions)
def test_computed_attribute_subjrel(self): class Entity(EntityType): oattr = String() attr = SubjectRelation('Int', formula='Any Z WHERE X oattr Z') schema = build_schema_from_namespace(vars().items()) self.assertEqual('Any Z WHERE X oattr Z', schema['attr'].rdef('Entity', 'Int').formula)
def create_schema_1(): class Affaire(EntityType): nom = String() associate_affaire = SubjectRelation('Affaire') return build_schema_from_namespace([('PersonBase', PersonBase), ('Affaire', Affaire), ('PersonAttrMod', PersonAttrMod)])
def test_computed_attribute_type(self): class Entity(EntityType): attr = Int(formula='Any Z WHERE X oattr Z') oattr = String() schema = build_schema_from_namespace(vars().items()) self.assertEqual('Any Z WHERE X oattr Z', schema['Entity'].rdef('attr').formula) self.assertIsNone(schema['Entity'].rdef('oattr').formula)
def create_schema_2(): class Affaire(EntityType): nom = String(maxsize=150) numero = Int() associate_affaire = SubjectRelation('Affaire', cardinality='**') associate_person = SubjectRelation('PersonBase') return build_schema_from_namespace([('PersonBase', PersonBase), ('Affaire', Affaire)])
def test_bad_vocab_and_size(self): class Entity(EntityType): attr = String(constraints=[StaticVocabularyConstraint(['ab', 'abc']), SizeConstraint(2)]) # "auto-fixed" when using: #vocabulary=['ab', 'abc'], maxsize=1) with self.assertRaises(BadSchemaDefinition) as cm: schema = build_schema_from_namespace(locals().items()) self.assertEqual("size constraint set to 2 but vocabulary contains string of greater size", str(cm.exception))
def test_computed_attribute_rdef(self): class Entity(EntityType): oattr = String() class attr(RelationDefinition): subject = 'Entity' object = 'Int' formula='Any Z WHERE X oattr Z' schema = build_schema_from_namespace(vars().items()) self.assertEqual('Any Z WHERE X oattr Z', schema['Entity'].rdef('attr').formula) self.assertIsNone(schema['Entity'].rdef('oattr').formula)
def test_computed_attribute_perms(self): class Entity(EntityType): oattr = String() class attr(RelationDefinition): subject = 'Entity' object = 'Int' formula = 'Any Z WHERE X oattr Z' schema = build_schema_from_namespace(vars().items()) self.assertEqual({'read': ('managers', 'users', 'guests'), 'update': (), 'add': ()}, schema['attr'].rdef('Entity', 'Int').permissions)
def test_cannot_set_addupdate_perms_on_computed_attribute(self): class Entity(EntityType): oattr = String() class attr(RelationDefinition): __permissions__ = {'read': ('managers', 'users', 'guests'), 'add': ('hacker inside!', ), 'update': ()} subject = 'Entity' object = 'Int' formula = 'Any Z WHERE X oattr Z' with self.assertRaises(BadSchemaDefinition) as cm: schema = build_schema_from_namespace(vars().items()) self.assertEqual( 'Cannot set add/update permissions on computed attribute Entity.attr[Int]', str(cm.exception))
def test_build_schema(self): class Question(EntityType): number = Int() text = String() class Form(EntityType): title = String() class in_form(RelationDefinition): subject = 'Question' object = 'Form' cardinality = '*1' schema = build_schema_from_namespace(vars().items()) entities = [x for x in schema.entities() if not x.final] self.assertCountEqual(['Form', 'Question'], entities) relations = [x for x in schema.relations() if not x.final] self.assertCountEqual(['in_form'], relations)
def test_invalid_attributes_in_computedrelation(self): class Societe(EntityType): name = String() class Employe(EntityType): name = String() class travaille(RelationDefinition): subject = 'Employe' object = 'Societe' class est_paye_par(ComputedRelation): rule = ('S travaille O') inlined = True with self.assertRaises(BadSchemaDefinition) as cm: schema = build_schema_from_namespace(vars().items()) self.assertEqual("Computed relation has no inlined attribute", str(cm.exception))
def test_computed_schema(self): class Societe(EntityType): name = String() class Employe(EntityType): name = String() class travaille(RelationDefinition): subject = 'Employe' object = 'Societe' class est_paye_par(ComputedRelation): __permissions__ = {'read': ('managers', 'users')} rule = ('S travaille O') schema = build_schema_from_namespace(vars().items()) self.assertEqual('S travaille O', schema['est_paye_par'].rule) self.assertEqual({'read': ('managers', 'users')}, schema['est_paye_par'].permissions)
def test_no_rdef_from_computedrelation(self): class Personne(EntityType): name = String() class Mafieu(EntityType): nickname = String() class est_paye_par(ComputedRelation): rule = ('S travaille O') class est_soudoye_par(RelationDefinition): name = 'est_paye_par' subject = 'Personne' object = 'Mafieu' with self.assertRaises(BadSchemaDefinition) as cm: schema = build_schema_from_namespace(vars().items()) self.assertEqual('Cannot add relation definition "est_paye_par" ' 'because an homonymous computed relation already ' 'exists with rule "S travaille O"', str(cm.exception))
def test_invalid_permissions_in_computedrelation(self): class Societe(EntityType): name = String() class Employe(EntityType): name = String() class travaille(RelationDefinition): subject = 'Employe' object = 'Societe' class est_paye_par(ComputedRelation): __permissions__ = {'read': ('managers', 'users', 'guests'), 'add': ('hacker inside!', ), 'delete': ()} rule = ('S travaille O') with self.assertRaises(BadSchemaDefinition) as cm: schema = build_schema_from_namespace(vars().items()) self.assertEqual( 'Cannot set add/delete permissions on computed relation est_paye_par', str(cm.exception))
def test_unfinalized_manipulation(self): class MyEntity(EntityType): base_arg_b = String() base_arg_a = Boolean() base_sub = SubjectRelation('MyOtherEntity') class base_obj(RelationDefinition): subject = 'MyOtherEntity' object = 'MyEntity' class MyOtherEntity(EntityType): base_o_obj = SubjectRelation('MyEntity') class base_o_sub(RelationDefinition): subject = 'MyEntity' object = 'MyOtherEntity' MyEntity.add_relation(Date(), name='new_arg_a') MyEntity.add_relation(Int(), name='new_arg_b') MyEntity.add_relation(SubjectRelation('MyOtherEntity'), name="new_sub") MyOtherEntity.add_relation(SubjectRelation('MyEntity'), name="new_o_obj") class new_obj(RelationDefinition): subject = 'MyOtherEntity' object = 'MyEntity' class new_o_sub(RelationDefinition): subject = 'MyEntity' object = 'MyOtherEntity' schema = build_schema_from_namespace(locals().items()) self.assertIn('MyEntity', schema.entities()) my_entity = schema['MyEntity'] attributes_def = my_entity.attribute_definitions() attributes = sorted(attr[0].type for attr in attributes_def) self.assertEqual(['base_arg_a', 'base_arg_b', 'new_arg_a', 'new_arg_b'], attributes) relations_def = my_entity.relation_definitions() relations = sorted( rel[0].type for rel in relations_def) self.assertEqual(['base_o_obj', 'base_o_sub', 'base_obj', 'base_sub', 'new_o_obj', 'new_o_sub', 'new_obj', 'new_sub'], relations)
def test_schema(self): class Anentity(EntityType): rel = SubjectRelation('Anentity', inlined=True) class Anotherentity(EntityType): rel = SubjectRelation('Anentity') class rel(RelationType): composite = 'subject' cardinality = '1*' symmetric = True schema = build_schema_from_namespace(locals().items()) self.assertEqual('<builtin>', schema['Anentity'].package) rel = schema['rel'] self.assertEqual(True, rel.symmetric) self.assertEqual(True, rel.inlined) self.assertEqual('<builtin>', rel.package) rdef1 = rel.rdef('Anentity', 'Anentity') self.assertEqual('subject', rdef1.composite) self.assertEqual('1*', rdef1.cardinality) self.assertEqual('<builtin>', rdef1.package) rdef2 = rel.rdef('Anotherentity', 'Anentity') self.assertEqual('subject', rdef2.composite) self.assertEqual('1*', rdef2.cardinality) self.assertEqual('<builtin>', rdef2.package)
def build_rdef(self, props_ref): class AType(EntityType): attr = String(**props_ref) schema = build_schema_from_namespace(locals().items()) return schema['AType'].rdef('attr')