goa/test/unittest_schema.py
changeset 6366 1806148d6ce8
parent 6333 e3994fcc21c3
parent 6365 a15cc5e16178
child 6367 d4c485ec1ca1
equal deleted inserted replaced
6333:e3994fcc21c3 6366:1806148d6ce8
     1 # copyright 2003-2010 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 """
       
    19 
       
    20 """
       
    21 from cubicweb.goa.testlib import *
       
    22 
       
    23 class Article(db.Model):
       
    24     content = db.TextProperty()
       
    25     synopsis = db.StringProperty(default='hello')
       
    26 
       
    27 class Blog(db.Model):
       
    28     diem = db.DateProperty(required=True, auto_now_add=True)
       
    29     title = db.StringProperty(required=True)
       
    30     content = db.TextProperty()
       
    31     talks_about = db.ReferenceProperty(Article)
       
    32     cites = db.SelfReferenceProperty()
       
    33 
       
    34 
       
    35 class SomeViewsTC(GAEBasedTC):
       
    36     MODEL_CLASSES = (Article, Blog)
       
    37 
       
    38     def test_entities_and_relation(self):
       
    39         schema = self.schema
       
    40         self.assertSetEquals(set(str(e) for e in schema.entities()),
       
    41                              set(('Boolean', 'Bytes', 'Date', 'Datetime', 'Float',
       
    42                               'Decimal',
       
    43                               'Int', 'Interval', 'Password', 'String', 'Time',
       
    44                               'CWEType', 'CWGroup', 'CWPermission', 'CWProperty', 'CWRType',
       
    45                               'CWUser', 'EmailAddress',
       
    46                               'RQLExpression', 'State', 'Transition', 'TrInfo',
       
    47                               'Article', 'Blog', 'YamsEntity')))
       
    48         self.assertSetEquals(set(str(e) for e in schema.relations()),
       
    49                              set(('add_permission', 'address', 'alias', 'allowed_transition',
       
    50                                   'ambiguous_relation', 'canonical', 'cites',
       
    51                                   'comment', 'comment_format', 'condition', 'content',
       
    52                                   'created_by', 'creation_date', 'delete_permission',
       
    53                                   'description', 'description_format', 'destination_state',
       
    54                                   'diem', 'eid', 'expression', 'exprtype', 'final', 'firstname',
       
    55                                   'for_user', 'from_state', 'fulltext_container', 'has_text',
       
    56                                   'identical_to', 'identity', 'in_group', 'initial_state',
       
    57                                   'inlined', 'inlined_relation', 'is', 'is_instance_of',
       
    58                                   'label', 'last_login_time', 'login',
       
    59                                   'mainvars', 'meta', 'modification_date', 'name', 'owned_by', 'pkey', 'primary_email',
       
    60                                   'read_permission', 'require_group', 'state_of', 'surname', 'symmetric',
       
    61                                   'synopsis', 'talks_about', 'title', 'to_state', 'transition_of',
       
    62                                   'update_permission', 'use_email', 'value')))
       
    63 
       
    64     def test_dbmodel_imported(self):
       
    65         eschema = self.schema['Blog']
       
    66         orels = [str(e) for e in eschema.ordered_relations()]
       
    67         # only relations defined in the class are actually ordered
       
    68         orels, others = orels[:5], orels[5:]
       
    69         self.assertEquals(orels,
       
    70                           ['diem', 'title', 'content', 'talks_about', 'cites'])
       
    71         self.assertUnorderedIterableEquals(others,
       
    72                              ['eid', 'identity', 'owned_by', 'modification_date',
       
    73                               'created_by', 'creation_date', 'is', 'is_instance_of'])
       
    74         self.assertUnorderedIterableEquals((str(e) for e in eschema.object_relations()),
       
    75                              ('ambiguous_relation', 'cites', 'identity', 'inlined_relation'))
       
    76         eschema = self.schema['Article']
       
    77         orels = [str(e) for e in eschema.ordered_relations()]
       
    78         # only relations defined in the class are actually ordered
       
    79         orels, others = orels[:2], orels[2:]
       
    80         self.assertEquals(orels,
       
    81                           ['content', 'synopsis'])
       
    82         self.assertUnorderedIterableEquals(others,
       
    83                              ['eid', 'identity', 'owned_by', 'modification_date',
       
    84                               'created_by', 'creation_date', 'is', 'is_instance_of'])
       
    85         self.assertUnorderedIterableEquals((str(e) for e in eschema.object_relations()),
       
    86                              ('ambiguous_relation', 'talks_about', 'identity'))
       
    87 
       
    88     def test_yams_imported(self):
       
    89         eschema = self.schema['CWProperty']
       
    90         # only relations defined in the class are actually ordered
       
    91         orels = [str(e) for e in eschema.ordered_relations()]
       
    92         orels, others = orels[:3], orels[3:]
       
    93         self.assertEquals(orels,
       
    94                           ['pkey', 'value', 'for_user'])
       
    95         self.assertEquals(others,
       
    96                           ['created_by', 'creation_date', 'eid', 'identity',
       
    97                            'is', 'is_instance_of', 'modification_date', 'owned_by'])
       
    98         self.assertUnorderedIterableEquals((str(e) for e in eschema.object_relations()),
       
    99                              ('identity',))
       
   100 
       
   101     def test_yams_ambiguous_relation(self):
       
   102         rschema = self.schema['ambiguous_relation']
       
   103         # only relations defined in the class are actually ordered
       
   104         self.assertUnorderedIterableEquals((str(e) for e in rschema.subjects()),
       
   105                              ('YamsEntity',))
       
   106         self.assertUnorderedIterableEquals((str(e) for e in rschema.objects()),
       
   107                              ('Blog', 'Article'))
       
   108 
       
   109     def test_euser(self):
       
   110         eschema = self.schema['CWUser']
       
   111         # XXX pretend to have some relations it has not
       
   112         self.assertEquals([str(e) for e in eschema.ordered_relations()],
       
   113                           ['login', 'firstname', 'surname', 'last_login_time',
       
   114                            'primary_email', 'use_email', 'in_group', 'created_by',
       
   115                            'creation_date', 'eid', 'has_text', 'identity',
       
   116                            'is', 'is_instance_of', 'modification_date',
       
   117                            'owned_by'])
       
   118         self.assertUnorderedIterableEquals((str(e) for e in eschema.object_relations()),
       
   119                              ('owned_by', 'created_by', 'identity', 'for_user'))
       
   120 
       
   121     def test_eid(self):
       
   122         rschema = self.schema['eid']
       
   123         self.assertEquals(rschema.objects(), ('Bytes',))
       
   124         self.assertEquals(rschema.rproperty('Blog', 'Bytes', 'cardinality'), '?1')
       
   125 
       
   126 
       
   127 if __name__ == '__main__':
       
   128     from logilab.common.testlib import unittest_main
       
   129     unittest_main()