test/unittest_schema.py
changeset 9966 6c2d57d1b6de
parent 9965 a8769b752299
child 9967 e65873ad0371
equal deleted inserted replaced
9965:a8769b752299 9966:6c2d57d1b6de
    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                             Int, String, SubjectRelation, ComputedRelation)
    31 from yams.reader import fill_schema
    31 from yams.reader import fill_schema
    32 
    32 
    33 from cubicweb.schema import (
    33 from cubicweb.schema import (
    34     CubicWebSchema, CubicWebEntitySchema, CubicWebSchemaLoader,
    34     CubicWebSchema, CubicWebEntitySchema, CubicWebSchemaLoader,
    35     RQLConstraint, RQLUniqueConstraint, RQLVocabularyConstraint,
    35     RQLConstraint, RQLUniqueConstraint, RQLVocabularyConstraint,
   281                           'delete': ('managers',)})
   281                           'delete': ('managers',)})
   282         self.assertEqual(schema['cw_for_source'].rdefs.values()[0].permissions,
   282         self.assertEqual(schema['cw_for_source'].rdefs.values()[0].permissions,
   283                          {'read': ('managers', 'users'),
   283                          {'read': ('managers', 'users'),
   284                           'add': ('managers',),
   284                           'add': ('managers',),
   285                           'delete': ('managers',)})
   285                           'delete': ('managers',)})
       
   286 
       
   287     def test_computed_attribute(self):
       
   288         """Check schema finalization for computed attributes."""
       
   289         class Person(EntityType):
       
   290             salary = Int()
       
   291 
       
   292         class works_for(RelationDefinition):
       
   293             subject = 'Person'
       
   294             object  = 'Company'
       
   295             cardinality = '?*'
       
   296 
       
   297         class Company(EntityType):
       
   298             total_salary = Int(formula='Any SUM(SA) GROUPBY X WHERE '
       
   299                                        'P works_for X, P salary SA')
       
   300         good_schema = build_schema_from_namespace(vars().items())
       
   301 
       
   302         class Company(EntityType):
       
   303             total_salary = String(formula='Any SUM(SA) GROUPBY X WHERE '
       
   304                                           'P works_for X, P salary SA')
       
   305 
       
   306         with self.assertRaises(BadSchemaDefinition) as exc:
       
   307             bad_schema = build_schema_from_namespace(vars().items())
       
   308 
       
   309         self.assertEqual(str(exc.exception),
       
   310                          'computed attribute total_salary on Company: '
       
   311                          'computed attribute type (Int) mismatch with '
       
   312                          'specified type (String)')
   286 
   313 
   287 
   314 
   288 class SchemaReaderComputedRelationAndAttributesTest(TestCase):
   315 class SchemaReaderComputedRelationAndAttributesTest(TestCase):
   289 
   316 
   290     def test_infer_computed_relation(self):
   317     def test_infer_computed_relation(self):