server/test/unittest_schemaserial.py
changeset 8945 ba9e3fbfa5a5
parent 8223 b1a685038c3b
child 9128 d988eec2d5d3
equal deleted inserted replaced
8944:b167f039b6cb 8945:ba9e3fbfa5a5
    23 from logilab.common.testlib import TestCase, unittest_main
    23 from logilab.common.testlib import TestCase, unittest_main
    24 
    24 
    25 from cubicweb.schema import CubicWebSchemaLoader
    25 from cubicweb.schema import CubicWebSchemaLoader
    26 from cubicweb.devtools import TestServerConfiguration
    26 from cubicweb.devtools import TestServerConfiguration
    27 
    27 
       
    28 from logilab.database import get_db_helper
       
    29 from yams import register_base_type, unregister_base_type
       
    30 
    28 def setUpModule(*args):
    31 def setUpModule(*args):
       
    32     register_base_type('BabarTestType', ('jungle_speed',))
       
    33     helper = get_db_helper('sqlite')
       
    34     helper.TYPE_MAPPING['BabarTestType'] = 'TEXT'
       
    35     helper.TYPE_CONVERTERS['BabarTestType'] = lambda x: '"%s"' % x
       
    36 
    29     global schema, config
    37     global schema, config
    30     loader = CubicWebSchemaLoader()
    38     loader = CubicWebSchemaLoader()
    31     config = TestServerConfiguration('data', apphome=Schema2RQLTC.datadir)
    39     apphome = Schema2RQLTC.datadir + '-schemaserial'
       
    40     config = TestServerConfiguration('data', apphome=apphome)
    32     config.bootstrap_cubes()
    41     config.bootstrap_cubes()
    33     schema = loader.load(config)
    42     schema = loader.load(config)
       
    43 
    34 
    44 
    35 def tearDownModule(*args):
    45 def tearDownModule(*args):
    36     global schema, config
    46     global schema, config
    37     del schema, config
    47     del schema, config
       
    48 
       
    49     unregister_base_type('BabarTestType')
       
    50     helper = get_db_helper('sqlite')
       
    51     helper.TYPE_MAPPING.pop('BabarTestType', None)
       
    52     helper.TYPE_CONVERTERS.pop('BabarTestType', None)
    38 
    53 
    39 from cubicweb.server.schemaserial import *
    54 from cubicweb.server.schemaserial import *
    40 from cubicweb.server.schemaserial import _erperms2rql as erperms2rql
    55 from cubicweb.server.schemaserial import _erperms2rql as erperms2rql
    41 
    56 
    42 cstrtypemap = {'RQLConstraint': 'RQLConstraint_eid',
    57 cstrtypemap = {'RQLConstraint': 'RQLConstraint_eid',
    70                                ('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
    85                                ('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
    71                                 {'et': None, 'x': None}),
    86                                 {'et': None, 'x': None}),
    72                                ('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
    87                                ('SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s',
    73                                 {'et': None, 'x': None})])
    88                                 {'et': None, 'x': None})])
    74 
    89 
       
    90     def test_esche2rql_custom_type(self):
       
    91         expected = [('INSERT CWEType X: X description %(description)s,X final %(final)s,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 
    75     def test_rschema2rql1(self):
    97     def test_rschema2rql1(self):
    76         self.assertListEqual(list(rschema2rql(schema.rschema('relation_type'), cstrtypemap)),
    98         self.assertListEqual(list(rschema2rql(schema.rschema('relation_type'), cstrtypemap)),
    77                              [
    99                              [
    78             ('INSERT CWRType X: X description %(description)s,X final %(final)s,X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,X name %(name)s,X symmetric %(symmetric)s',
   100             ('INSERT CWRType X: X description %(description)s,X final %(final)s,X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,X name %(name)s,X symmetric %(symmetric)s',
    79              {'description': u'link a relation definition to its relation type', 'symmetric': False, 'name': u'relation_type', 'final' : False, 'fulltext_container': None, 'inlined': True}),
   101              {'description': u'link a relation definition to its relation type', 'symmetric': False, 'name': u'relation_type', 'final' : False, 'fulltext_container': None, 'inlined': True}),
   133             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE CT eid %(ct)s, EDEF eid %(x)s',
   155             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE CT eid %(ct)s, EDEF eid %(x)s',
   134              {'x': None, 'ct': u'SizeConstraint_eid', 'value': u'max=2'}),
   156              {'x': None, 'ct': u'SizeConstraint_eid', 'value': u'max=2'}),
   135             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE CT eid %(ct)s, EDEF eid %(x)s',
   157             ('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE CT eid %(ct)s, EDEF eid %(x)s',
   136              {'x': None, 'ct': u'StaticVocabularyConstraint_eid', 'value': u"u'?*', u'1*', u'+*', u'**', u'?+', u'1+', u'++', u'*+', u'?1', u'11', u'+1', u'*1', u'??', u'1?', u'+?', u'*?'"}),
   158              {'x': None, 'ct': u'StaticVocabularyConstraint_eid', 'value': u"u'?*', u'1*', u'+*', u'**', u'?+', u'1+', u'++', u'*+', u'?1', u'11', u'+1', u'*1', u'??', u'1?', u'+?', u'*?'"}),
   137             ])
   159             ])
       
   160 
       
   161     def test_rschema2rql_custom_type(self):
       
   162         expected = [('INSERT CWRType X: X description %(description)s,X final %(final)s,'
       
   163                      'X fulltext_container %(fulltext_container)s,X inlined %(inlined)s,'
       
   164                      'X name %(name)s,X symmetric %(symmetric)s',
       
   165                      {'description': u'',
       
   166                       'final': True,
       
   167                       'fulltext_container': None,
       
   168                       'inlined': False,
       
   169                       'name': u'custom_field_of_jungle',
       
   170                       'symmetric': False}),
       
   171                      ('INSERT CWAttribute X: X cardinality %(cardinality)s,'
       
   172                       'X defaultval %(defaultval)s,X description %(description)s,'
       
   173                       'X extra_props %(extra_props)s,X indexed %(indexed)s,'
       
   174                       'X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,'
       
   175                       'X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
       
   176                       {'cardinality': u'?1',
       
   177                        'defaultval': None,
       
   178                        'description': u'',
       
   179                        'extra_props': '{"jungle_speed": 42}',
       
   180                        'indexed': False,
       
   181                        'oe': None,
       
   182                        'ordernum': 19,
       
   183                        'rt': None,
       
   184                        'se': None})]
       
   185 
       
   186         got = list(rschema2rql(schema.rschema('custom_field_of_jungle'), cstrtypemap))
       
   187         self.assertEqual(2, len(got))
       
   188         # this is a custom type attribute with an extra parameter
       
   189         self.assertIn('extra_props', got[1][1])
       
   190         # this extr
       
   191         extra_props = got[1][1]['extra_props']
       
   192         from cubicweb import Binary
       
   193         self.assertIsInstance(extra_props, Binary)
       
   194         got[1][1]['extra_props'] = got[1][1]['extra_props'].getvalue()
       
   195         self.assertListEqual(expected, got)
   138 
   196 
   139     def test_rdef2rql(self):
   197     def test_rdef2rql(self):
   140         self.assertListEqual(list(rdef2rql(schema['description_format'].rdefs[('CWRType', 'String')], cstrtypemap)),
   198         self.assertListEqual(list(rdef2rql(schema['description_format'].rdefs[('CWRType', 'String')], cstrtypemap)),
   141                               [
   199                               [
   142             ('INSERT CWAttribute X: X cardinality %(cardinality)s,X defaultval %(defaultval)s,X description %(description)s,X fulltextindexed %(fulltextindexed)s,X indexed %(indexed)s,X internationalizable %(internationalizable)s,X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',
   200             ('INSERT CWAttribute X: X cardinality %(cardinality)s,X defaultval %(defaultval)s,X description %(description)s,X fulltextindexed %(fulltextindexed)s,X indexed %(indexed)s,X internationalizable %(internationalizable)s,X ordernum %(ordernum)s,X relation_type ER,X from_entity SE,X to_entity OE WHERE SE eid %(se)s,ER eid %(rt)s,OE eid %(oe)s',