test/unittest_schema.py
branchstable
changeset 6340 470d8e828fda
parent 6306 e6557be1127e
child 6415 b0b0f097a72d
equal deleted inserted replaced
6339:bdc3dc94d744 6340:470d8e828fda
   140                          ['Any S,U WHERE U has_update_permission S, S eid %(s)s, U eid %(u)s'])
   140                          ['Any S,U WHERE U has_update_permission S, S eid %(s)s, U eid %(u)s'])
   141 
   141 
   142     def test_erqlexpression(self):
   142     def test_erqlexpression(self):
   143         self.assertRaises(RQLSyntaxError, ERQLExpression, '1')
   143         self.assertRaises(RQLSyntaxError, ERQLExpression, '1')
   144         expr = ERQLExpression('X travaille S, S owned_by U')
   144         expr = ERQLExpression('X travaille S, S owned_by U')
   145         self.assertEquals(str(expr), 'Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s')
   145         self.assertEqual(str(expr), 'Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s')
   146 
   146 
   147     def test_rrqlexpression(self):
   147     def test_rrqlexpression(self):
   148         self.assertRaises(Exception, RRQLExpression, '1')
   148         self.assertRaises(Exception, RRQLExpression, '1')
   149         self.assertRaises(RQLSyntaxError, RRQLExpression, 'O X Y')
   149         self.assertRaises(RQLSyntaxError, RRQLExpression, 'O X Y')
   150         expr = RRQLExpression('U has_update_permission O')
   150         expr = RRQLExpression('U has_update_permission O')
   151         self.assertEquals(str(expr), 'Any O,U WHERE U has_update_permission O, O eid %(o)s, U eid %(u)s')
   151         self.assertEqual(str(expr), 'Any O,U WHERE U has_update_permission O, O eid %(o)s, U eid %(u)s')
   152 
   152 
   153 loader = CubicWebSchemaLoader()
   153 loader = CubicWebSchemaLoader()
   154 config = TestConfiguration('data')
   154 config = TestConfiguration('data')
   155 config.bootstrap_cubes()
   155 config.bootstrap_cubes()
   156 
   156 
   157 class SchemaReaderClassTest(TestCase):
   157 class SchemaReaderClassTest(TestCase):
   158 
   158 
   159     def test_order_eschemas(self):
   159     def test_order_eschemas(self):
   160         schema = loader.load(config)
   160         schema = loader.load(config)
   161         self.assertEquals(order_eschemas([schema['Note'], schema['SubNote']]),
   161         self.assertEqual(order_eschemas([schema['Note'], schema['SubNote']]),
   162                                          [schema['Note'], schema['SubNote']])
   162                                          [schema['Note'], schema['SubNote']])
   163         self.assertEquals(order_eschemas([schema['SubNote'], schema['Note']]),
   163         self.assertEqual(order_eschemas([schema['SubNote'], schema['Note']]),
   164                                          [schema['Note'], schema['SubNote']])
   164                                          [schema['Note'], schema['SubNote']])
   165 
   165 
   166     def test_knownValues_load_schema(self):
   166     def test_knownValues_load_schema(self):
   167         schema = loader.load(config)
   167         schema = loader.load(config)
   168         self.assert_(isinstance(schema, CubicWebSchema))
   168         self.assert_(isinstance(schema, CubicWebSchema))
   169         self.assertEquals(schema.name, 'data')
   169         self.assertEqual(schema.name, 'data')
   170         entities = [str(e) for e in schema.entities()]
   170         entities = [str(e) for e in schema.entities()]
   171         entities.sort()
   171         entities.sort()
   172         expected_entities = ['BaseTransition', 'Bookmark', 'Boolean', 'Bytes', 'Card',
   172         expected_entities = ['BaseTransition', 'Bookmark', 'Boolean', 'Bytes', 'Card',
   173                              'Date', 'Datetime', 'Decimal',
   173                              'Date', 'Datetime', 'Decimal',
   174                              'CWCache', 'CWConstraint', 'CWConstraintType', 'CWEType',
   174                              'CWCache', 'CWConstraint', 'CWConstraintType', 'CWEType',
   179                              'Password', 'Personne',
   179                              'Password', 'Personne',
   180                              'RQLExpression',
   180                              'RQLExpression',
   181                              'Societe', 'State', 'String', 'SubNote', 'SubWorkflowExitPoint',
   181                              'Societe', 'State', 'String', 'SubNote', 'SubWorkflowExitPoint',
   182                              'Tag', 'Time', 'Transition', 'TrInfo',
   182                              'Tag', 'Time', 'Transition', 'TrInfo',
   183                              'Workflow', 'WorkflowTransition']
   183                              'Workflow', 'WorkflowTransition']
   184         self.assertListEquals(entities, sorted(expected_entities))
   184         self.assertListEqual(entities, sorted(expected_entities))
   185         relations = [str(r) for r in schema.relations()]
   185         relations = [str(r) for r in schema.relations()]
   186         relations.sort()
   186         relations.sort()
   187         expected_relations = ['add_permission', 'address', 'alias', 'allowed_transition',
   187         expected_relations = ['add_permission', 'address', 'alias', 'allowed_transition',
   188                               'bookmarked_by', 'by_transition',
   188                               'bookmarked_by', 'by_transition',
   189 
   189 
   225 
   225 
   226                               'value',
   226                               'value',
   227 
   227 
   228                               'wf_info_for', 'wikiid', 'workflow_of']
   228                               'wf_info_for', 'wikiid', 'workflow_of']
   229 
   229 
   230         self.assertListEquals(relations, expected_relations)
   230         self.assertListEqual(relations, expected_relations)
   231 
   231 
   232         eschema = schema.eschema('CWUser')
   232         eschema = schema.eschema('CWUser')
   233         rels = sorted(str(r) for r in eschema.subject_relations())
   233         rels = sorted(str(r) for r in eschema.subject_relations())
   234         self.assertListEquals(rels, ['created_by', 'creation_date', 'custom_workflow', 'cwuri', 'eid',
   234         self.assertListEqual(rels, ['created_by', 'creation_date', 'custom_workflow', 'cwuri', 'eid',
   235                                      'evaluee', 'firstname', 'has_text', 'identity',
   235                                      'evaluee', 'firstname', 'has_text', 'identity',
   236                                      'in_group', 'in_state', 'is',
   236                                      'in_group', 'in_state', 'is',
   237                                      'is_instance_of', 'last_login_time',
   237                                      'is_instance_of', 'last_login_time',
   238                                      'login', 'modification_date', 'owned_by',
   238                                      'login', 'modification_date', 'owned_by',
   239                                      'primary_email', 'surname', 'upassword',
   239                                      'primary_email', 'surname', 'upassword',
   240                                      'use_email'])
   240                                      'use_email'])
   241         rels = sorted(r.type for r in eschema.object_relations())
   241         rels = sorted(r.type for r in eschema.object_relations())
   242         self.assertListEquals(rels, ['bookmarked_by', 'created_by', 'for_user',
   242         self.assertListEqual(rels, ['bookmarked_by', 'created_by', 'for_user',
   243                                      'identity', 'owned_by', 'wf_info_for'])
   243                                      'identity', 'owned_by', 'wf_info_for'])
   244         rschema = schema.rschema('relation_type')
   244         rschema = schema.rschema('relation_type')
   245         properties = rschema.rdef('CWAttribute', 'CWRType')
   245         properties = rschema.rdef('CWAttribute', 'CWRType')
   246         self.assertEquals(properties.cardinality, '1*')
   246         self.assertEqual(properties.cardinality, '1*')
   247         constraints = properties.constraints
   247         constraints = properties.constraints
   248         self.failUnlessEqual(len(constraints), 1, constraints)
   248         self.failUnlessEqual(len(constraints), 1, constraints)
   249         constraint = constraints[0]
   249         constraint = constraints[0]
   250         self.failUnless(isinstance(constraint, RQLConstraint))
   250         self.failUnless(isinstance(constraint, RQLConstraint))
   251         self.failUnlessEqual(constraint.restriction, 'O final TRUE')
   251         self.failUnlessEqual(constraint.restriction, 'O final TRUE')
   256         self.failIf('has_text' in schema['EmailAddress'].subject_relations())
   256         self.failIf('has_text' in schema['EmailAddress'].subject_relations())
   257 
   257 
   258     def test_permission_settings(self):
   258     def test_permission_settings(self):
   259         schema = loader.load(config)
   259         schema = loader.load(config)
   260         aschema = schema['TrInfo'].rdef('comment')
   260         aschema = schema['TrInfo'].rdef('comment')
   261         self.assertEquals(aschema.get_groups('read'),
   261         self.assertEqual(aschema.get_groups('read'),
   262                           set(('managers', 'users', 'guests')))
   262                           set(('managers', 'users', 'guests')))
   263         self.assertEquals(aschema.get_rqlexprs('read'),
   263         self.assertEqual(aschema.get_rqlexprs('read'),
   264                           ())
   264                           ())
   265         self.assertEquals(aschema.get_groups('update'),
   265         self.assertEqual(aschema.get_groups('update'),
   266                           set(('managers',)))
   266                           set(('managers',)))
   267         self.assertEquals([x.expression for x in aschema.get_rqlexprs('update')],
   267         self.assertEqual([x.expression for x in aschema.get_rqlexprs('update')],
   268                           ['U has_update_permission X'])
   268                           ['U has_update_permission X'])
   269 
   269 
   270 class BadSchemaRQLExprTC(TestCase):
   270 class BadSchemaRQLExprTC(TestCase):
   271     def setUp(self):
   271     def setUp(self):
   272         self.loader = CubicWebSchemaLoader()
   272         self.loader = CubicWebSchemaLoader()
   277 
   277 
   278     def _test(self, schemafile, msg):
   278     def _test(self, schemafile, msg):
   279         self.loader.handle_file(join(DATADIR, schemafile))
   279         self.loader.handle_file(join(DATADIR, schemafile))
   280         ex = self.assertRaises(BadSchemaDefinition,
   280         ex = self.assertRaises(BadSchemaDefinition,
   281                                self.loader._build_schema, 'toto', False)
   281                                self.loader._build_schema, 'toto', False)
   282         self.assertEquals(str(ex), msg)
   282         self.assertEqual(str(ex), msg)
   283 
   283 
   284     def test_rrqlexpr_on_etype(self):
   284     def test_rrqlexpr_on_etype(self):
   285         self._test('rrqlexpr_on_eetype.py',
   285         self._test('rrqlexpr_on_eetype.py',
   286                    "can't use RRQLExpression on ToTo, use an ERQLExpression")
   286                    "can't use RRQLExpression on ToTo, use an ERQLExpression")
   287 
   287 
   299 
   299 
   300 
   300 
   301 class NormalizeExpressionTC(TestCase):
   301 class NormalizeExpressionTC(TestCase):
   302 
   302 
   303     def test(self):
   303     def test(self):
   304         self.assertEquals(normalize_expression('X  bla Y,Y blur Z  ,  Z zigoulou   X '),
   304         self.assertEqual(normalize_expression('X  bla Y,Y blur Z  ,  Z zigoulou   X '),
   305                                                'X bla Y, Y blur Z, Z zigoulou X')
   305                                                'X bla Y, Y blur Z, Z zigoulou X')
   306 
   306 
   307 class RQLExpressionTC(TestCase):
   307 class RQLExpressionTC(TestCase):
   308     def test_comparison(self):
   308     def test_comparison(self):
   309         self.assertEquals(ERQLExpression('X is CWUser', 'X', 0),
   309         self.assertEqual(ERQLExpression('X is CWUser', 'X', 0),
   310                           ERQLExpression('X is CWUser', 'X', 0))
   310                           ERQLExpression('X is CWUser', 'X', 0))
   311         self.assertNotEquals(ERQLExpression('X is CWUser', 'X', 0),
   311         self.assertNotEquals(ERQLExpression('X is CWUser', 'X', 0),
   312                              ERQLExpression('X is CWGroup', 'X', 0))
   312                              ERQLExpression('X is CWGroup', 'X', 0))
   313 
   313 
   314 class GuessRrqlExprMainVarsTC(TestCase):
   314 class GuessRrqlExprMainVarsTC(TestCase):
   315     def test_exists(self):
   315     def test_exists(self):
   316         mainvars = guess_rrqlexpr_mainvars(normalize_expression('NOT EXISTS(O team_competition C, C level < 3)'))
   316         mainvars = guess_rrqlexpr_mainvars(normalize_expression('NOT EXISTS(O team_competition C, C level < 3)'))
   317         self.assertEquals(mainvars, 'O')
   317         self.assertEqual(mainvars, 'O')
   318 
   318 
   319 
   319 
   320 if __name__ == '__main__':
   320 if __name__ == '__main__':
   321     unittest_main()
   321     unittest_main()