test/unittest_schema.py
changeset 9952 0f3f965b6365
parent 9711 59616edc20d7
child 9956 19a683a0047c
equal deleted inserted replaced
9951:8cdcbf3f4fd0 9952:0f3f965b6365
    24 
    24 
    25 from rql import RQLSyntaxError
    25 from rql import RQLSyntaxError
    26 
    26 
    27 from yams import ValidationError, BadSchemaDefinition
    27 from yams import ValidationError, BadSchemaDefinition
    28 from yams.constraints import SizeConstraint, StaticVocabularyConstraint
    28 from yams.constraints import SizeConstraint, StaticVocabularyConstraint
    29 from yams.buildobjs import RelationDefinition, EntityType, RelationType
    29 from yams.buildobjs import (RelationDefinition, EntityType, RelationType,
       
    30                             String, SubjectRelation, ComputedRelation)
    30 from yams.reader import fill_schema
    31 from yams.reader import fill_schema
    31 
    32 
    32 from cubicweb.schema import (
    33 from cubicweb.schema import (
    33     CubicWebSchema, CubicWebEntitySchema, CubicWebSchemaLoader,
    34     CubicWebSchema, CubicWebEntitySchema, CubicWebSchemaLoader,
    34     RQLConstraint, RQLUniqueConstraint, RQLVocabularyConstraint,
    35     RQLConstraint, RQLUniqueConstraint, RQLVocabularyConstraint,
    35     RQLExpression, ERQLExpression, RRQLExpression,
    36     RQLExpression, ERQLExpression, RRQLExpression,
    36     normalize_expression, order_eschemas, guess_rrqlexpr_mainvars)
    37     normalize_expression, order_eschemas, guess_rrqlexpr_mainvars,
       
    38     build_schema_from_namespace)
    37 from cubicweb.devtools import TestServerConfiguration as TestConfiguration
    39 from cubicweb.devtools import TestServerConfiguration as TestConfiguration
    38 from cubicweb.devtools.testlib import CubicWebTC
    40 from cubicweb.devtools.testlib import CubicWebTC
    39 
    41 
    40 DATADIR = join(dirname(__file__), 'data')
    42 DATADIR = join(dirname(__file__), 'data')
    41 
    43 
   278                           'delete': ('managers',)})
   280                           'delete': ('managers',)})
   279         self.assertEqual(schema['cw_for_source'].rdefs.values()[0].permissions,
   281         self.assertEqual(schema['cw_for_source'].rdefs.values()[0].permissions,
   280                          {'read': ('managers', 'users'),
   282                          {'read': ('managers', 'users'),
   281                           'add': ('managers',),
   283                           'add': ('managers',),
   282                           'delete': ('managers',)})
   284                           'delete': ('managers',)})
       
   285 
       
   286 
       
   287 class SchemaReaderComputedRelationAndAttributesTest(TestCase):
       
   288 
       
   289     def test_infer_computed_relation(self):
       
   290         class Person(EntityType):
       
   291             name = String()
       
   292 
       
   293         class Company(EntityType):
       
   294             name  = String()
       
   295 
       
   296         class Service(EntityType):
       
   297             name = String()
       
   298 
       
   299         class works_for(RelationDefinition):
       
   300             subject = 'Person'
       
   301             object = 'Company'
       
   302 
       
   303         class produce(RelationDefinition):
       
   304             subject = ('Person', 'Company')
       
   305             object = 'Service'
       
   306 
       
   307         class achete(RelationDefinition):
       
   308             subject = 'Person'
       
   309             object = 'Service'
       
   310 
       
   311         class produces_and_buys(ComputedRelation):
       
   312             rule = 'S produce O, S achete O'
       
   313 
       
   314         class produces_and_buys2(ComputedRelation):
       
   315             rule = 'S works_for SO, SO produce O'
       
   316 
       
   317         class reproduce(ComputedRelation):
       
   318             rule = 'S produce O'
       
   319 
       
   320         schema = build_schema_from_namespace(vars().items())
       
   321 
       
   322         # check object/subject type
       
   323         self.assertEqual([('Person','Service')],
       
   324                          schema['produces_and_buys'].rdefs.keys())
       
   325         self.assertEqual([('Person','Service')],
       
   326                          schema['produces_and_buys2'].rdefs.keys())
       
   327         self.assertEqual([('Company', 'Service'), ('Person', 'Service')],
       
   328                          schema['reproduce'].rdefs.keys())
       
   329         # check relations as marked infered
       
   330         self.assertTrue(
       
   331             schema['produces_and_buys'].rdefs[('Person','Service')].infered)
       
   332 
       
   333         del schema
       
   334         class autoname(ComputedRelation):
       
   335             rule = 'S produce X, X name O'
       
   336 
       
   337         with self.assertRaises(BadSchemaDefinition) as cm:
       
   338             build_schema_from_namespace(vars().items())
       
   339         self.assertEqual(str(cm.exception), 'computed relations cannot be final')
   283 
   340 
   284 
   341 
   285 class BadSchemaTC(TestCase):
   342 class BadSchemaTC(TestCase):
   286     def setUp(self):
   343     def setUp(self):
   287         self.loader = CubicWebSchemaLoader()
   344         self.loader = CubicWebSchemaLoader()