cubicweb/server/test/unittest_schemaserial.py
changeset 11057 0b59724cb3f2
parent 10811 5a1b4361a797
child 11077 09be48c01fa4
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """unit tests for schema rql (de)serialization"""
       
    19 
       
    20 from logilab.common.testlib import TestCase, unittest_main
       
    21 
       
    22 from cubicweb import Binary
       
    23 from cubicweb.schema import CubicWebSchemaLoader
       
    24 from cubicweb.devtools import TestServerConfiguration
       
    25 from cubicweb.devtools.testlib import CubicWebTC
       
    26 
       
    27 from cubicweb.server.schemaserial import (updateeschema2rql, updaterschema2rql, rschema2rql,
       
    28                                           eschema2rql, rdef2rql, specialize2rql,
       
    29                                           _erperms2rql as erperms2rql)
       
    30 
       
    31 from logilab.database import get_db_helper
       
    32 from yams import register_base_type, unregister_base_type
       
    33 
       
    34 schema = config = None
       
    35 def setUpModule(*args):
       
    36     register_base_type('BabarTestType', ('jungle_speed',))
       
    37     helper = get_db_helper('sqlite')
       
    38     helper.TYPE_MAPPING['BabarTestType'] = 'TEXT'
       
    39     helper.TYPE_CONVERTERS['BabarTestType'] = lambda x: '"%s"' % x
       
    40 
       
    41     global schema, config
       
    42     loader = CubicWebSchemaLoader()
       
    43     apphome = Schema2RQLTC.datadir + '-schemaserial'
       
    44     config = TestServerConfiguration('data', apphome=apphome)
       
    45     config.bootstrap_cubes()
       
    46     schema = loader.load(config)
       
    47 
       
    48 
       
    49 def tearDownModule(*args):
       
    50     global schema, config
       
    51     schema = config = None
       
    52 
       
    53     unregister_base_type('BabarTestType')
       
    54     helper = get_db_helper('sqlite')
       
    55     helper.TYPE_MAPPING.pop('BabarTestType', None)
       
    56     helper.TYPE_CONVERTERS.pop('BabarTestType', None)
       
    57 
       
    58 cstrtypemap = {'RQLConstraint': 'RQLConstraint_eid',
       
    59                'SizeConstraint': 'SizeConstraint_eid',
       
    60                'StaticVocabularyConstraint': 'StaticVocabularyConstraint_eid',
       
    61                'FormatConstraint': 'FormatConstraint_eid',
       
    62                }
       
    63 
       
    64 class Schema2RQLTC(TestCase):
       
    65 
       
    66     def test_eschema2rql1(self):
       
    67         self.assertListEqual([
       
    68             ('INSERT CWEType X: X description %(description)s,X final %(final)s,X name %(name)s',
       
    69              {'description': u'define a final relation: '
       
    70               'link a final relation type from a non final entity '
       
    71               'to a final entity type. used to build the instance schema',
       
    72               'name': u'CWAttribute', 'final': False})],
       
    73                              list(eschema2rql(schema.eschema('CWAttribute'))))
       
    74 
       
    75     def test_eschema2rql2(self):
       
    76         self.assertListEqual([
       
    77                 ('INSERT CWEType X: X description %(description)s,X final %(final)s,X name %(name)s',
       
    78                  {'description': u'', 'final': True, 'name': u'String'})],
       
    79                              list(eschema2rql(schema.eschema('String'))))
       
    80 
       
    81     def test_eschema2rql_specialization(self):
       
    82         # x: None since eschema.eid are None
       
    83         self.assertListEqual([('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
       
    84                                {'et': None, 'x': None}),
       
    85                               ('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
       
    86                                {'et': None, 'x': None})],
       
    87                              sorted(specialize2rql(schema)))
       
    88 
       
    89     def test_esche2rql_custom_type(self):
       
    90         expected = [('INSERT CWEType X: X description %(description)s,X final %(final)s,'
       
    91                      'X name %(name)s',
       
    92                      {'description': u'',
       
    93                      'name': u'BabarTestType', 'final': True},)]
       
    94         got = list(eschema2rql(schema.eschema('BabarTestType')))
       
    95         self.assertListEqual(expected, got)
       
    96 
       
    97     def test_rschema2rql1(self):
       
    98         self.assertListEqual([
       
    99             ('INSERT CWRType X: X description %(description)s,X final %(final)s,'
       
   100              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   101              'X name %(name)s,X symmetric %(symmetric)s',
       
   102              {'description': u'link a relation definition to its relation type',
       
   103               'symmetric': False,
       
   104               'name': u'relation_type',
       
   105               'final' : False,
       
   106               'fulltext_container': None,
       
   107               'inlined': True}),
       
   108 
       
   109             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   110              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   111              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   112              {'se': None, 'rt': None, 'oe': None,
       
   113               'description': u'',
       
   114               'composite': u'object',
       
   115               'cardinality': u'1*',
       
   116               'ordernum': 1}),
       
   117             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   118              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   119              {'x': None, 'ct': u'RQLConstraint_eid',
       
   120               'value': u';O;O final TRUE\n'}),
       
   121 
       
   122             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   123              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   124              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   125              {'se': None, 'rt': None, 'oe': None,
       
   126               'description': u'', 'composite': u'object',
       
   127               'ordernum': 1, 'cardinality': u'1*'}),
       
   128             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   129              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   130              {'x': None, 'ct': u'RQLConstraint_eid', 'value': u';O;O final FALSE\n'}),
       
   131         ],
       
   132                              list(rschema2rql(schema.rschema('relation_type'), cstrtypemap)))
       
   133 
       
   134     def test_rschema2rql2(self):
       
   135         self.assertListEqual([
       
   136             ('INSERT CWRType X: X description %(description)s,X final %(final)s,'
       
   137              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   138              'X name %(name)s,X symmetric %(symmetric)s',
       
   139              {'description': u'',
       
   140               'symmetric': False,
       
   141               'name': u'add_permission',
       
   142               'final': False,
       
   143               'fulltext_container': None,
       
   144               'inlined': False}),
       
   145 
       
   146             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   147              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   148              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   149              {'se': None,
       
   150               'rt': None,
       
   151               'oe': None,
       
   152               'description': u'groups allowed to add entities/relations of this type',
       
   153               'composite': None,
       
   154               'ordernum': 9999,
       
   155               'cardinality': u'**'}),
       
   156             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   157              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   158              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   159              {'se': None,
       
   160               'rt': None,
       
   161               'oe': None,
       
   162               'description': u'rql expression allowing to add entities/relations of this type',
       
   163               'composite': 'subject',
       
   164               'ordernum': 9999,
       
   165               'cardinality': u'*?'}),
       
   166 
       
   167             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   168              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   169              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   170              {'se': None,
       
   171               'rt': None,
       
   172               'oe': None,
       
   173               'description': u'groups allowed to add entities/relations of this type',
       
   174               'composite': None,
       
   175               'ordernum': 9999,
       
   176               'cardinality': u'**'}),
       
   177             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   178              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   179              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   180              {'se': None,
       
   181               'rt': None,
       
   182               'oe': None,
       
   183               'description': u'rql expression allowing to add entities/relations of this type',
       
   184               'composite': 'subject',
       
   185               'ordernum': 9999,
       
   186               'cardinality': u'*?'}),
       
   187             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   188              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   189              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   190             {'cardinality': u'**',
       
   191              'composite': None,
       
   192              'description': u'groups allowed to add entities/relations of this type',
       
   193              'oe': None,
       
   194              'ordernum': 9999,
       
   195              'rt': None,
       
   196              'se': None}),
       
   197             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
       
   198              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
       
   199              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   200              {'cardinality': u'*?',
       
   201               'composite': u'subject',
       
   202               'description': u'rql expression allowing to add entities/relations of this type',
       
   203               'oe': None,
       
   204               'ordernum': 9999,
       
   205               'rt': None,
       
   206               'se': None})],
       
   207                              list(rschema2rql(schema.rschema('add_permission'), cstrtypemap)))
       
   208 
       
   209     def test_rschema2rql3(self):
       
   210         self.assertListEqual([
       
   211             ('INSERT CWRType X: X description %(description)s,X final %(final)s,'
       
   212              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   213              'X name %(name)s,X symmetric %(symmetric)s',
       
   214              {'description': u'',
       
   215               'symmetric': False,
       
   216               'name': u'cardinality',
       
   217               'final': True,
       
   218               'fulltext_container': None,
       
   219               'inlined': False}),
       
   220 
       
   221             ('INSERT CWAttribute X: X cardinality %(cardinality)s,X defaultval %(defaultval)s,'
       
   222              'X description %(description)s,X formula %(formula)s,X fulltextindexed %(fulltextindexed)s,'
       
   223              'X indexed %(indexed)s,X internationalizable %(internationalizable)s,'
       
   224              'X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,'
       
   225              'X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   226              {'se': None,
       
   227               'rt': None,
       
   228               'oe': None,
       
   229               'description': u'subject/object cardinality',
       
   230               'internationalizable': True,
       
   231               'fulltextindexed': False,
       
   232               'ordernum': 5,
       
   233               'defaultval': None,
       
   234               'indexed': False,
       
   235               'formula': None,
       
   236               'cardinality': u'?1'}),
       
   237             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   238              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   239              {'x': None,
       
   240               'ct': u'SizeConstraint_eid',
       
   241               'value': u'max=2'}),
       
   242             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   243              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   244              {'x': None,
       
   245               'ct': u'StaticVocabularyConstraint_eid',
       
   246               'value': u"u'?1', u'11'"}),
       
   247 
       
   248             ('INSERT CWAttribute X: X cardinality %(cardinality)s,X defaultval %(defaultval)s,'
       
   249              'X description %(description)s,X formula %(formula)s,X fulltextindexed %(fulltextindexed)s,'
       
   250              'X indexed %(indexed)s,X internationalizable %(internationalizable)s,'
       
   251              'X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,X to_entity OE '
       
   252              'WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   253              {'se': None,
       
   254               'rt': None,
       
   255               'oe': None,
       
   256               'description': u'subject/object cardinality',
       
   257               'internationalizable': True,
       
   258               'fulltextindexed': False,
       
   259               'ordernum': 5,
       
   260               'defaultval': None,
       
   261               'indexed': False,
       
   262               'formula': None,
       
   263               'cardinality': u'?1'}),
       
   264             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   265              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   266              {'x': None,
       
   267               'ct': u'SizeConstraint_eid',
       
   268               'value': u'max=2'}),
       
   269             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   270              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   271              {'x': None,
       
   272               'ct': u'StaticVocabularyConstraint_eid',
       
   273               'value': (u"u'?*', u'1*', u'+*', u'**', u'?+', u'1+', u'++', u'*+', u'?1', "
       
   274                         "u'11', u'+1', u'*1', u'??', u'1?', u'+?', u'*?'")})],
       
   275               list(rschema2rql(schema.rschema('cardinality'), cstrtypemap)))
       
   276 
       
   277     def test_rschema2rql_custom_type(self):
       
   278         expected = [('INSERT CWRType X: X description %(description)s,X final %(final)s,'
       
   279                      'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   280                      'X name %(name)s,X symmetric %(symmetric)s',
       
   281                      {'description': u'',
       
   282                       'final': True,
       
   283                       'fulltext_container': None,
       
   284                       'inlined': False,
       
   285                       'name': u'custom_field_of_jungle',
       
   286                       'symmetric': False}),
       
   287                      ('INSERT CWAttribute X: X cardinality %(cardinality)s,'
       
   288                       'X defaultval %(defaultval)s,X description %(description)s,'
       
   289                       'X extra_props %(extra_props)s,X formula %(formula)s,X indexed %(indexed)s,'
       
   290                       'X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,'
       
   291                       'X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   292                       {'cardinality': u'?1',
       
   293                        'defaultval': None,
       
   294                        'description': u'',
       
   295                        'extra_props': b'{"jungle_speed": 42}',
       
   296                        'formula': None,
       
   297                        'indexed': False,
       
   298                        'oe': None,
       
   299                        'ordernum': 4,
       
   300                        'rt': None,
       
   301                        'se': None})]
       
   302 
       
   303         got = list(rschema2rql(schema.rschema('custom_field_of_jungle'), cstrtypemap))
       
   304         self.assertEqual(2, len(got))
       
   305         # this is a custom type attribute with an extra parameter
       
   306         self.assertIn('extra_props', got[1][1])
       
   307         # this extr
       
   308         extra_props = got[1][1]['extra_props']
       
   309         self.assertIsInstance(extra_props, Binary)
       
   310         got[1][1]['extra_props'] = got[1][1]['extra_props'].getvalue()
       
   311         self.assertListEqual(expected, got)
       
   312 
       
   313     def test_rdef2rql(self):
       
   314         self.assertListEqual([
       
   315             ('INSERT CWAttribute X: X cardinality %(cardinality)s,X defaultval %(defaultval)s,'
       
   316              'X description %(description)s,X formula %(formula)s,X fulltextindexed %(fulltextindexed)s,'
       
   317              'X indexed %(indexed)s,X internationalizable %(internationalizable)s,'
       
   318              'X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,'
       
   319              'X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   320              {'se': None,
       
   321               'rt': None,
       
   322               'oe': None,
       
   323               'description': u'',
       
   324               'internationalizable': True,
       
   325               'fulltextindexed': False,
       
   326               'ordernum': 3,
       
   327               'defaultval': Binary.zpickle(u'text/plain'),
       
   328               'indexed': False,
       
   329               'formula': None,
       
   330               'cardinality': u'?1'}),
       
   331             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   332              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   333              {'x': None,
       
   334               'value': u'None',
       
   335               'ct': 'FormatConstraint_eid'}),
       
   336             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
       
   337              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
       
   338              {'x': None,
       
   339               'value': u'max=50',
       
   340               'ct': 'SizeConstraint_eid'})],
       
   341                              list(rdef2rql(schema['description_format'].rdefs[('CWRType', 'String')],
       
   342                                            cstrtypemap)))
       
   343 
       
   344     def test_updateeschema2rql1(self):
       
   345         self.assertListEqual([('SET X description %(description)s,X final %(final)s,'
       
   346                                'X name %(name)s WHERE X eid %(x)s',
       
   347                                {'description': u'define a final relation: link a final relation type from'
       
   348                                 ' a non final entity to a final entity type. used to build the instance schema',
       
   349                                 'x': 1, 'final': False, 'name': u'CWAttribute'})],
       
   350                              list(updateeschema2rql(schema.eschema('CWAttribute'), 1)))
       
   351 
       
   352     def test_updateeschema2rql2(self):
       
   353         self.assertListEqual([('SET X description %(description)s,X final %(final)s,'
       
   354                                'X name %(name)s WHERE X eid %(x)s',
       
   355                                {'description': u'', 'x': 1, 'final': True, 'name': u'String'})],
       
   356                              list(updateeschema2rql(schema.eschema('String'), 1)))
       
   357 
       
   358     def test_updaterschema2rql1(self):
       
   359         self.assertListEqual([
       
   360             ('SET X description %(description)s,X final %(final)s,'
       
   361              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   362              'X name %(name)s,X symmetric %(symmetric)s WHERE X eid %(x)s',
       
   363              {'x': 1,
       
   364               'symmetric': False,
       
   365               'description': u'link a relation definition to its relation type',
       
   366               'final': False, 'fulltext_container': None,
       
   367               'inlined': True,
       
   368               'name': u'relation_type'})],
       
   369                              list(updaterschema2rql(schema.rschema('relation_type'), 1)))
       
   370 
       
   371     def test_updaterschema2rql2(self):
       
   372         expected = [
       
   373             ('SET X description %(description)s,X final %(final)s,'
       
   374              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   375              'X name %(name)s,X symmetric %(symmetric)s WHERE X eid %(x)s',
       
   376              {'x': 1,
       
   377               'symmetric': False,
       
   378               'description': u'',
       
   379               'final': False,
       
   380               'fulltext_container': None,
       
   381               'inlined': False,
       
   382               'name': u'add_permission'})
       
   383             ]
       
   384         for i, (rql, args) in enumerate(updaterschema2rql(schema.rschema('add_permission'), 1)):
       
   385             yield self.assertEqual, expected[i], (rql, args)
       
   386 
       
   387 class Perms2RQLTC(TestCase):
       
   388     GROUP_MAPPING = {
       
   389         'managers': 0,
       
   390         'users':  1,
       
   391         'guests': 2,
       
   392         'owners': 3,
       
   393         }
       
   394 
       
   395     def test_eperms2rql1(self):
       
   396         self.assertListEqual([('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   397                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 1}),
       
   398                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 2}),
       
   399                               ('SET X add_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   400                               ('SET X update_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   401                               ('SET X delete_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0})],
       
   402                              [(rql, kwargs)
       
   403                               for rql, kwargs in erperms2rql(schema.eschema('CWEType'), self.GROUP_MAPPING)])
       
   404 
       
   405     def test_rperms2rql2(self):
       
   406         self.assertListEqual([('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   407                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 1}),
       
   408                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 2}),
       
   409                               ('SET X add_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   410                               ('SET X delete_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0})],
       
   411                              [(rql, kwargs)
       
   412                               for rql, kwargs in erperms2rql(schema.rschema('read_permission').rdef('CWEType', 'CWGroup'),
       
   413                                                              self.GROUP_MAPPING)])
       
   414 
       
   415     def test_rperms2rql3(self):
       
   416         self.assertListEqual([('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   417                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 1}),
       
   418                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 2}),
       
   419                               ('SET X add_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
       
   420                               ('SET X update_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0})],
       
   421                              [(rql, kwargs)
       
   422                               for rql, kwargs in erperms2rql(schema.rschema('name').rdef('CWEType', 'String'),
       
   423                                                              self.GROUP_MAPPING)])
       
   424 
       
   425     #def test_perms2rql(self):
       
   426     #    self.assertListEqual(perms2rql(schema, self.GROUP_MAPPING),
       
   427     #                         ['INSERT CWEType X: X name 'Societe', X final FALSE'])
       
   428 
       
   429 class ComputedAttributeAndRelationTC(CubicWebTC):
       
   430     appid = 'data-cwep002'
       
   431 
       
   432     def test(self):
       
   433         # force to read schema from the database
       
   434         self.repo.set_schema(self.repo.deserialize_schema(), resetvreg=False)
       
   435         schema = self.repo.schema
       
   436         self.assertEqual([('Company', 'Person')], list(schema['has_employee'].rdefs))
       
   437         self.assertEqual(schema['has_employee'].rdef('Company', 'Person').permissions['read'],
       
   438                          (u'managers',))
       
   439         self.assertEqual('O works_for S',
       
   440                          schema['has_employee'].rule)
       
   441         self.assertEqual([('Company', 'Int')], list(schema['total_salary'].rdefs))
       
   442         self.assertEqual('Any SUM(SA) GROUPBY X WHERE P works_for X, P salary SA',
       
   443                          schema['total_salary'].rdefs['Company', 'Int'].formula)
       
   444 
       
   445 if __name__ == '__main__':
       
   446     unittest_main()