[schema] ease understanding of schema naming error stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 Jan 2011 16:36:09 +0100
branchstable
changeset 6861 9d4e11d6e783
parent 6860 a0dbf6135c72
child 6862 8bee6dfdbe96
[schema] ease understanding of schema naming error
schema.py
test/unittest_schema.py
--- a/schema.py	Thu Jan 20 15:52:59 2011 +0100
+++ b/schema.py	Thu Jan 20 16:36:09 2011 +0100
@@ -550,7 +550,11 @@
     def add_entity_type(self, edef):
         edef.name = edef.name.encode()
         edef.name = bw_normalize_etype(edef.name)
-        assert re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name), repr(edef.name)
+        if not re.match(r'[A-Z][A-Za-z0-9]*[a-z]+[0-9]*$', edef.name):
+            raise BadSchemaDefinition(
+                '%r is not a valid name for an entity type. It should start '
+                'with an upper cased letter and be followed by at least a '
+                'lower cased letter' % edef.name)
         eschema = super(CubicWebSchema, self).add_entity_type(edef)
         if not eschema.final:
             # automatically add the eid relation to non final entity types
@@ -565,7 +569,11 @@
         return eschema
 
     def add_relation_type(self, rdef):
-        rdef.name = rdef.name.lower().encode()
+        if not rdef.name.islower():
+            raise BadSchemaDefinition(
+                '%r is not a valid name for a relation type. It should be '
+                'lower cased' % rdef.name)
+        rdef.name = rdef.name.encode()
         rschema = super(CubicWebSchema, self).add_relation_type(rdef)
         self._eid_index[rschema.eid] = rschema
         return rschema
--- a/test/unittest_schema.py	Thu Jan 20 15:52:59 2011 +0100
+++ b/test/unittest_schema.py	Thu Jan 20 16:36:09 2011 +0100
@@ -74,7 +74,6 @@
     ('Personne tel Int'),
     ('Personne fax Int'),
     ('Personne datenaiss Date'),
-    ('Personne TEST Boolean'),
     ('Personne promo String'),
     # real relations
     ('Personne  travaille Societe'),
@@ -82,7 +81,7 @@
     ('Societe evaluee   Note'),
     ('Personne  concerne  Affaire'),
     ('Personne  concerne  Societe'),
-    ('Affaire Concerne  Societe'),
+    ('Affaire concerne  Societe'),
     )
 done = {}
 for rel in RELS:
@@ -114,10 +113,8 @@
         """test that entities, relations and attributes name are normalized
         """
         self.assertEqual(esociete.type, 'Societe')
-        self.assertEqual(schema.has_relation('TEST'), 0)
         self.assertEqual(schema.has_relation('test'), 1)
         self.assertEqual(eperson.subjrels['test'].type, 'test')
-        self.assertEqual(schema.has_relation('Concerne'), 0)
         self.assertEqual(schema.has_relation('concerne'), 1)
         self.assertEqual(schema.rschema('concerne').type, 'concerne')
 
@@ -271,7 +268,7 @@
         self.assertEqual([x.expression for x in aschema.get_rqlexprs('update')],
                           ['U has_update_permission X'])
 
-class BadSchemaRQLExprTC(TestCase):
+class BadSchemaTC(TestCase):
     def setUp(self):
         self.loader = CubicWebSchemaLoader()
         self.loader.defined = {}
@@ -285,6 +282,16 @@
             self.loader._build_schema('toto', False)
         self.assertEqual(str(cm.exception), msg)
 
+    def test_lowered_etype(self):
+        self._test('lowered_etype.py',
+                   "'my_etype' is not a valid name for an entity type. It should "
+                   "start with an upper cased letter and be followed by at least "
+                   "a lower cased letter")
+
+    def test_uppered_rtype(self):
+        self._test('uppered_rtype.py',
+                   "'ARelation' is not a valid name for a relation type. It should be lower cased")
+
     def test_rrqlexpr_on_etype(self):
         self._test('rrqlexpr_on_eetype.py',
                    "can't use RRQLExpression on ToTo, use an ERQLExpression")