def test_schema_check_relations(self): """test behaviour with some incorrect relations""" for rel in BAD_RELS: _from, _type, _to = rel.split() self.assertRaises(BadSchemaDefinition, schema.add_relation_def, RelationDefinition(_from, _type, _to)) # check we can't extend a final relation self.assertRaises(BadSchemaDefinition, schema.add_relation_def, RelationDefinition('Person', 'nom', 'affaire'))
def test_inheritance_rdefs(self): class Plan(EntityType): pass rdef = RelationDefinition('Plan', 'custom_workflow', 'Workflow') _add_relation(Plan.__relations__, rdef) class TE(Plan): pass self.assertListEqual(['custom_workflow'], [rel.name for rel in TE.__relations__])
def test_yams_serialize(self): s = schema.Schema('Unknown') register_base_types(s) e2 = EntityType('Ent2') s.add_entity_type(e2) e1 = EntityType('Ent1') e1.specialized_type = 'Ent2' s.add_entity_type(e1) s.add_relation_type(RelationType('attr1')) s.add_relation_def(RelationDefinition('Ent1', 'attr1', 'String')) out = serialize.serialize_to_python(s) self.assertMultiLineEqual( out, '\n'.join([ 'from yams.buildobjs import *', '', 'class Ent2(EntityType):', ' pass', '', 'class Ent1(Ent2):', ' attr1 = String()', '\n' ]))
def test_association_types(self): schema.add_relation_def(RelationDefinition('Bug', 'see_also', 'Bug')) schema.add_relation_def(RelationDefinition('Bug', 'see_also', 'Story')) schema.add_relation_def( RelationDefinition('Bug', 'see_also', 'Project')) schema.add_relation_def( RelationDefinition('Story', 'see_also', 'Story')) schema.add_relation_def( RelationDefinition('Story', 'see_also', 'Project')) schema.add_relation_def( RelationDefinition('Project', 'see_also', 'Project')) rsee_also = schema.rschema('see_also') subj_types = rsee_also.associations() subj_types.sort() self.assertEqual(subj_types, [('Bug', ['Bug', 'Story', 'Project']), ('Project', ['Bug', 'Story', 'Project']), ('Story', ['Bug', 'Story', 'Project'])])
('Personne evaluee Note'), ('Societe evaluee Note'), ('Personne concerne Affaire'), ('Personne concerne Societe'), ('Affaire concerne Societe'), ) done = {} for rel in RELS: _from, _type, _to = rel.split() if not _type.lower() in done: schema.add_relation_type(RelationType(_type)) done[_type.lower()] = True if _type == 'concerne': schema.add_relation_def( RelationDefinition(_from, _type, _to, __permissions__=CONCERNE_PERMISSIONS)) else: schema.add_relation_def(RelationDefinition(_from, _type, _to)) class CubicWebSchemaTC(TestCase): def test_rql_constraints_inheritance(self): # isinstance(cstr, RQLVocabularyConstraint) # -> expected to return RQLVocabularyConstraint and RQLConstraint # instances but not RQLUniqueConstraint # # isinstance(cstr, RQLConstraint) # -> expected to return RQLConstraint instances but not # RQLVocabularyConstraint and RQLUniqueConstraint self.assertFalse(
def define_container(self, schema): """ Produce the necessary schema relations of a container: * (<container_rtype>, etype, cetype) for each container etype * (container_etype, etype, 'CWEType') for each container etype * an optional (if needed) (container_parent, etype, parent etype) """ assert utils.fsschema(schema), \ 'this method is expected to be called only on filesystem schema (in schema.py)' assert not getattr(schema, '_%s_define_container' % self.cetype, False), \ 'this method is expected to be called only once on a schema (in schema.py)' setattr(schema, '_%s_define_container' % self.cetype, True) self.bind(schema) if self.crtype not in schema: # ease pluggability of container in existing applications schema.add_relation_type(RelationType(self.crtype, inlined=True)) cw_schema.META_RTYPES.add(self.crtype) else: logger.warning( '%r is already defined in the schema - you probably want ' 'to let it to the container cube', self.crtype) crschema = schema[self.crtype] cetype_rschema = schema['container_etype'] for etype in self.etypes: # populate <container_rtype> if (etype, self.cetype) not in crschema.rdefs: # checking this will help adding containers to existing applications # and reusing the container rtype schema.add_relation_def( RelationDefinition(etype, self.crtype, self.cetype, cardinality='?*')) else: logger.warning( '%r - %r - %r rdef is already defined in the schema ' '- you probably want to let it to the container cube', etype, self.crtype, self.cetype) # populate container_etype if (etype, 'CWEType') not in cetype_rschema.rdefs: schema.add_relation_def( RelationDefinition(etype, 'container_etype', 'CWEType', cardinality='?*')) # populate container_parent eschema = schema[etype] if utils.needs_container_parent(eschema): cparent_rschema = schema['container_parent'] # we take a list because iterating over the schema while # we're mutating parts of it won't work peschemas = list(utils.parent_eschemas(eschema)) for peschema in peschemas: petype = peschema.type if (etype, petype) in cparent_rschema.rdefs: continue schema.add_relation_def( RelationDefinition(etype, 'container_parent', petype, cardinality='?*'))
def test_raise_relation_def(self): self.assertRaisesMsg(BadSchemaDefinition, "using unknown type 'Afire' in relation evaluee", schema.add_relation_def, RelationDefinition('Afire', 'evaluee', 'Note'))
def setUp(self): global schema, enote, eaffaire, eperson, esociete, estring, eint global rconcerne, rnom schema = Schema('Test Schema') register_base_types(schema) enote = schema.add_entity_type(EntityType('Note')) eaffaire = schema.add_entity_type(EntityType('Affaire')) eperson = schema.add_entity_type(EntityType('Person')) esociete = schema.add_entity_type(EntityType('Societe')) RELS = ( # attribute relations ('Note date Datetime'), ('Note type String'), ('Affaire sujet String'), ('Affaire ref String'), ('Affaire starton Time'), ('Person nom String'), ('Person prenom String'), ('Person sexe Float'), ('Person tel Int'), ('Person fax Int'), ('Person datenaiss Date'), ('Person TEST Boolean'), ('Person promo String'), ('Person promo_enlarged String'), ('Person promo_encoding String'), ('Person promo_format String'), # real relations ('Person travaille Societe'), ('Person evaluee Note'), ('Societe evaluee Note'), ('Person concerne Affaire'), ('Person concerne Societe'), ('Affaire concerne Societe'), ) for i, rel in enumerate(RELS): _from, _type, _to = rel.split() try: schema.rschema(_type) except KeyError: schema.add_relation_type(RelationType(_type)) schema.add_relation_def( RelationDefinition(_from, _type, _to, order=i)) schema.rschema('nom').rdef('Person', 'String').cardinality = '11' # not null enote.rdef('type').constraints = [ StaticVocabularyConstraint( (u'bon', u'pasbon', u'bof', u'peux mieux faire')) ] enote.rdef('date').cardinality = '11' eaffaire.rdef('sujet').constraints = [SizeConstraint(128)] eaffaire.rdef('ref').constraints = [ SizeConstraint(12), RegexpConstraint(r'[A-Z]+\d+') ] eperson.rdef('nom').constraints = [SizeConstraint(20, 10)] eperson.rdef('prenom').constraints = [SizeConstraint(64)] eperson.rdef('tel').constraints = [ IntervalBoundConstraint(maxvalue=999999) ] eperson.rdef('fax').constraints = [ IntervalBoundConstraint(minvalue=12, maxvalue=999999) ] eperson.rdef('promo').constraints = [ StaticVocabularyConstraint((u'bon', u'pasbon')) ] eperson.rdef('promo_format').constraints = [FormatConstraint()] estring = schema.eschema('String') eint = schema.eschema('Int') rconcerne = schema.rschema('concerne') rnom = schema.rschema('nom')