cubicweb/server/test/unittest_schemaserial.py
changeset 11417 5e5e224239c3
parent 11358 179b5ff3f428
child 11767 432f87a63057
equal deleted inserted replaced
11416:9c2fbb872e91 11417:5e5e224239c3
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    56                'SizeConstraint': 'SizeConstraint_eid',
    56                'SizeConstraint': 'SizeConstraint_eid',
    57                'StaticVocabularyConstraint': 'StaticVocabularyConstraint_eid',
    57                'StaticVocabularyConstraint': 'StaticVocabularyConstraint_eid',
    58                'FormatConstraint': 'FormatConstraint_eid',
    58                'FormatConstraint': 'FormatConstraint_eid',
    59                }
    59                }
    60 
    60 
       
    61 
    61 class Schema2RQLTC(TestCase):
    62 class Schema2RQLTC(TestCase):
    62 
    63 
    63     def test_eschema2rql1(self):
    64     def test_eschema2rql1(self):
    64         self.assertListEqual([
    65         self.assertListEqual([
    65             ('INSERT CWEType X: X description %(description)s,X final %(final)s,X name %(name)s',
    66             ('INSERT CWEType X: X description %(description)s,X final %(final)s,X name %(name)s',
    69               'name': u'CWAttribute', 'final': False})],
    70               'name': u'CWAttribute', 'final': False})],
    70                              list(eschema2rql(schema.eschema('CWAttribute'))))
    71                              list(eschema2rql(schema.eschema('CWAttribute'))))
    71 
    72 
    72     def test_eschema2rql2(self):
    73     def test_eschema2rql2(self):
    73         self.assertListEqual([
    74         self.assertListEqual([
    74                 ('INSERT CWEType X: X description %(description)s,X final %(final)s,X name %(name)s',
    75             ('INSERT CWEType X: X description %(description)s,X final %(final)s,X name %(name)s',
    75                  {'description': u'', 'final': True, 'name': u'String'})],
    76              {'description': u'', 'final': True, 'name': u'String'})],
    76                              list(eschema2rql(schema.eschema('String'))))
    77                              list(eschema2rql(schema.eschema('String'))))
    77 
    78 
    78     def test_eschema2rql_specialization(self):
    79     def test_eschema2rql_specialization(self):
    79         # x: None since eschema.eid are None
    80         # x: None since eschema.eid are None
    80         self.assertListEqual([('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
    81         self.assertListEqual([('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
    85 
    86 
    86     def test_esche2rql_custom_type(self):
    87     def test_esche2rql_custom_type(self):
    87         expected = [('INSERT CWEType X: X description %(description)s,X final %(final)s,'
    88         expected = [('INSERT CWEType X: X description %(description)s,X final %(final)s,'
    88                      'X name %(name)s',
    89                      'X name %(name)s',
    89                      {'description': u'',
    90                      {'description': u'',
    90                      'name': u'BabarTestType', 'final': True},)]
    91                       'name': u'BabarTestType', 'final': True},)]
    91         got = list(eschema2rql(schema.eschema('BabarTestType')))
    92         got = list(eschema2rql(schema.eschema('BabarTestType')))
    92         self.assertListEqual(expected, got)
    93         self.assertListEqual(expected, got)
    93 
    94 
    94     def test_rschema2rql1(self):
    95     def test_rschema2rql1(self):
    95         self.assertListEqual([
    96         self.assertListEqual([
    97              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
    98              'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
    98              'X name %(name)s,X symmetric %(symmetric)s',
    99              'X name %(name)s,X symmetric %(symmetric)s',
    99              {'description': u'link a relation definition to its relation type',
   100              {'description': u'link a relation definition to its relation type',
   100               'symmetric': False,
   101               'symmetric': False,
   101               'name': u'relation_type',
   102               'name': u'relation_type',
   102               'final' : False,
   103               'final': False,
   103               'fulltext_container': None,
   104               'fulltext_container': None,
   104               'inlined': True}),
   105               'inlined': True}),
   105 
   106 
   106             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
   107             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
   107              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
   108              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
   122              {'se': None, 'rt': None, 'oe': None,
   123              {'se': None, 'rt': None, 'oe': None,
   123               'description': u'', 'composite': u'object',
   124               'description': u'', 'composite': u'object',
   124               'ordernum': 1, 'cardinality': u'1*'}),
   125               'ordernum': 1, 'cardinality': u'1*'}),
   125             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
   126             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X '
   126              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
   127              'WHERE CT eid %(ct)s, EDEF eid %(x)s',
   127              {'x': None, 'ct': u'RQLConstraint_eid', 'value': u'{"expression": "O final FALSE", "mainvars": ["O"], "msg": null}'}),
   128              {'x': None, 'ct': u'RQLConstraint_eid',
       
   129               'value': u'{"expression": "O final FALSE", "mainvars": ["O"], "msg": null}'}),
   128         ],
   130         ],
   129                              list(rschema2rql(schema.rschema('relation_type'), cstrtypemap)))
   131                              list(rschema2rql(schema.rschema('relation_type'), cstrtypemap)))
   130 
   132 
   131     def test_rschema2rql2(self):
   133     def test_rschema2rql2(self):
   132         self.assertListEqual([
   134         self.assertListEqual([
   182               'ordernum': 9999,
   184               'ordernum': 9999,
   183               'cardinality': u'*?'}),
   185               'cardinality': u'*?'}),
   184             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
   186             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
   185              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
   187              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
   186              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
   188              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
   187             {'cardinality': u'**',
   189              {'cardinality': u'**',
   188              'composite': None,
   190               'composite': None,
   189              'description': u'groups allowed to add entities/relations of this type',
   191               'description': u'groups allowed to add entities/relations of this type',
   190              'oe': None,
   192               'oe': None,
   191              'ordernum': 9999,
   193               'ordernum': 9999,
   192              'rt': None,
   194               'rt': None,
   193              'se': None}),
   195               'se': None}),
   194             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
   196             ('INSERT CWRelation X: X cardinality %(cardinality)s,X composite %(composite)s,'
   195              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
   197              'X description %(description)s,X ordernum %(ordernum)s,X relation_type ER,'
   196              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
   198              'X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
   197              {'cardinality': u'*?',
   199              {'cardinality': u'*?',
   198               'composite': u'subject',
   200               'composite': u'subject',
   385 
   387 
   386 
   388 
   387 class Perms2RQLTC(TestCase):
   389 class Perms2RQLTC(TestCase):
   388     GROUP_MAPPING = {
   390     GROUP_MAPPING = {
   389         'managers': 0,
   391         'managers': 0,
   390         'users':  1,
   392         'users': 1,
   391         'guests': 2,
   393         'guests': 2,
   392         'owners': 3,
   394         'owners': 3,
   393         }
   395     }
   394 
   396 
   395     def test_eperms2rql1(self):
   397     def test_eperms2rql1(self):
   396         self.assertListEqual([('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0}),
   398         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}),
   399                               ('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}),
   400                               ('SET X read_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 2}),
   420                               ('SET X update_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0})],
   422                               ('SET X update_permission Y WHERE Y eid %(g)s, X eid %(x)s', {'g': 0})],
   421                              [(rql, kwargs)
   423                              [(rql, kwargs)
   422                               for rql, kwargs in erperms2rql(schema.rschema('name').rdef('CWEType', 'String'),
   424                               for rql, kwargs in erperms2rql(schema.rschema('name').rdef('CWEType', 'String'),
   423                                                              self.GROUP_MAPPING)])
   425                                                              self.GROUP_MAPPING)])
   424 
   426 
   425     #def test_perms2rql(self):
       
   426     #    self.assertListEqual(perms2rql(schema, self.GROUP_MAPPING),
       
   427     #                         ['INSERT CWEType X: X name 'Societe', X final FALSE'])
       
   428 
   427 
   429 class ComputedAttributeAndRelationTC(CubicWebTC):
   428 class ComputedAttributeAndRelationTC(CubicWebTC):
   430     appid = 'data-cwep002'
   429     appid = 'data-cwep002'
   431 
   430 
   432     def test(self):
   431     def test(self):
   440                          schema['has_employee'].rule)
   439                          schema['has_employee'].rule)
   441         self.assertEqual([('Company', 'Int')], list(schema['total_salary'].rdefs))
   440         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',
   441         self.assertEqual('Any SUM(SA) GROUPBY X WHERE P works_for X, P salary SA',
   443                          schema['total_salary'].rdefs['Company', 'Int'].formula)
   442                          schema['total_salary'].rdefs['Company', 'Int'].formula)
   444 
   443 
       
   444 
   445 if __name__ == '__main__':
   445 if __name__ == '__main__':
   446     from unittest import main
   446     from unittest import main
   447     main()
   447     main()