17 from yams.constraints import SizeConstraint, StaticVocabularyConstraint |
17 from yams.constraints import SizeConstraint, StaticVocabularyConstraint |
18 from yams.buildobjs import RelationDefinition, EntityType, RelationType |
18 from yams.buildobjs import RelationDefinition, EntityType, RelationType |
19 from yams.reader import PyFileReader |
19 from yams.reader import PyFileReader |
20 |
20 |
21 from cubicweb.schema import CubicWebSchema, CubicWebEntitySchema, \ |
21 from cubicweb.schema import CubicWebSchema, CubicWebEntitySchema, \ |
22 RQLConstraint, CubicWebSchemaLoader, ERQLExpression, RRQLExpression, \ |
22 RQLConstraint, CubicWebSchemaLoader, RQLExpression, ERQLExpression, RRQLExpression, \ |
23 normalize_expression, order_eschemas |
23 normalize_expression, order_eschemas |
24 from cubicweb.devtools import TestServerConfiguration as TestConfiguration |
24 from cubicweb.devtools import TestServerConfiguration as TestConfiguration |
25 |
25 |
26 DATADIR = join(dirname(__file__), 'data') |
26 DATADIR = join(dirname(__file__), 'data') |
27 |
27 |
71 ) |
71 ) |
72 done = {} |
72 done = {} |
73 for rel in RELS: |
73 for rel in RELS: |
74 _from, _type, _to = rel.split() |
74 _from, _type, _to = rel.split() |
75 if not _type.lower() in done: |
75 if not _type.lower() in done: |
76 if _type == 'concerne': |
76 schema.add_relation_type(RelationType(_type)) |
77 schema.add_relation_type(RelationType(_type, permissions=CONCERNE_PERMISSIONS)) |
|
78 else: |
|
79 schema.add_relation_type(RelationType(_type)) |
|
80 done[_type.lower()] = True |
77 done[_type.lower()] = True |
81 schema.add_relation_def(RelationDefinition(_from, _type, _to)) |
78 if _type == 'concerne': |
|
79 schema.add_relation_def(RelationDefinition(_from, _type, _to, |
|
80 __permissions__=CONCERNE_PERMISSIONS)) |
|
81 else: |
|
82 schema.add_relation_def(RelationDefinition(_from, _type, _to)) |
82 |
83 |
83 class CubicWebSchemaTC(TestCase): |
84 class CubicWebSchemaTC(TestCase): |
84 |
85 |
85 def test_normalize(self): |
86 def test_normalize(self): |
86 """test that entities, relations and attributes name are normalized |
87 """test that entities, relations and attributes name are normalized |
92 self.assertEqual(schema.has_relation('Concerne'), 0) |
93 self.assertEqual(schema.has_relation('Concerne'), 0) |
93 self.assertEqual(schema.has_relation('concerne'), 1) |
94 self.assertEqual(schema.has_relation('concerne'), 1) |
94 self.assertEqual(schema.rschema('concerne').type, 'concerne') |
95 self.assertEqual(schema.rschema('concerne').type, 'concerne') |
95 |
96 |
96 def test_entity_perms(self): |
97 def test_entity_perms(self): |
97 eperson.set_default_groups() |
|
98 self.assertEqual(eperson.get_groups('read'), set(('managers', 'users', 'guests'))) |
98 self.assertEqual(eperson.get_groups('read'), set(('managers', 'users', 'guests'))) |
99 self.assertEqual(eperson.get_groups('update'), set(('managers', 'owners',))) |
99 self.assertEqual(eperson.get_groups('update'), set(('managers', 'owners',))) |
100 self.assertEqual(eperson.get_groups('delete'), set(('managers', 'owners'))) |
100 self.assertEqual(eperson.get_groups('delete'), set(('managers', 'owners'))) |
101 self.assertEqual(eperson.get_groups('add'), set(('managers',))) |
101 self.assertEqual(eperson.get_groups('add'), set(('managers',))) |
102 self.assertEqual([str(e) for e in eperson.get_rqlexprs('add')], |
102 self.assertEqual([str(e) for e in eperson.get_rqlexprs('add')], |
103 ['Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s']) |
103 ['Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s']) |
104 eperson.set_groups('read', ('managers',)) |
104 eperson.set_action_permissions('read', ('managers',)) |
105 self.assertEqual(eperson.get_groups('read'), set(('managers',))) |
105 self.assertEqual(eperson.get_groups('read'), set(('managers',))) |
106 |
106 |
107 def test_relation_perms(self): |
107 def test_relation_perms(self): |
108 rconcerne = schema.rschema('concerne') |
108 rconcerne = schema.rschema('concerne').rdef('Personne', 'Societe') |
109 rconcerne.set_default_groups() |
|
110 self.assertEqual(rconcerne.get_groups('read'), set(('managers', 'users', 'guests'))) |
109 self.assertEqual(rconcerne.get_groups('read'), set(('managers', 'users', 'guests'))) |
111 self.assertEqual(rconcerne.get_groups('delete'), set(('managers',))) |
110 self.assertEqual(rconcerne.get_groups('delete'), set(('managers',))) |
112 self.assertEqual(rconcerne.get_groups('add'), set(('managers', ))) |
111 self.assertEqual(rconcerne.get_groups('add'), set(('managers', ))) |
113 rconcerne.set_groups('read', ('managers',)) |
112 rconcerne.set_action_permissions('read', ('managers',)) |
114 self.assertEqual(rconcerne.get_groups('read'), set(('managers',))) |
113 self.assertEqual(rconcerne.get_groups('read'), set(('managers',))) |
115 self.assertEqual([str(e) for e in rconcerne.get_rqlexprs('add')], |
114 self.assertEqual([str(e) for e in rconcerne.get_rqlexprs('add')], |
116 ['Any S WHERE U has_update_permission S, S eid %(s)s, U eid %(u)s']) |
115 ['Any S,U WHERE U has_update_permission S, S eid %(s)s, U eid %(u)s']) |
117 |
116 |
118 def test_erqlexpression(self): |
117 def test_erqlexpression(self): |
119 self.assertRaises(RQLSyntaxError, ERQLExpression, '1') |
118 self.assertRaises(RQLSyntaxError, ERQLExpression, '1') |
120 expr = ERQLExpression('X travaille S, S owned_by U') |
119 expr = ERQLExpression('X travaille S, S owned_by U') |
121 self.assertEquals(str(expr), 'Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s') |
120 self.assertEquals(str(expr), 'Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s') |
122 |
121 |
123 def test_rrqlexpression(self): |
122 def test_rrqlexpression(self): |
124 self.assertRaises(Exception, RRQLExpression, '1') |
123 self.assertRaises(Exception, RRQLExpression, '1') |
125 self.assertRaises(RQLSyntaxError, RRQLExpression, 'O X Y') |
124 self.assertRaises(RQLSyntaxError, RRQLExpression, 'O X Y') |
126 expr = RRQLExpression('U has_update_permission O') |
125 expr = RRQLExpression('U has_update_permission O') |
127 self.assertEquals(str(expr), 'Any O WHERE U has_update_permission O, O eid %(o)s, U eid %(u)s') |
126 self.assertEquals(str(expr), 'Any O,U WHERE U has_update_permission O, O eid %(o)s, U eid %(u)s') |
128 |
127 |
129 loader = CubicWebSchemaLoader() |
128 loader = CubicWebSchemaLoader() |
130 config = TestConfiguration('data') |
129 config = TestConfiguration('data') |
131 config.bootstrap_cubes() |
130 config.bootstrap_cubes() |
132 |
131 |
213 'use_email']) |
212 'use_email']) |
214 rels = sorted(r.type for r in eschema.object_relations()) |
213 rels = sorted(r.type for r in eschema.object_relations()) |
215 self.assertListEquals(rels, ['bookmarked_by', 'created_by', 'for_user', |
214 self.assertListEquals(rels, ['bookmarked_by', 'created_by', 'for_user', |
216 'identity', 'owned_by', 'wf_info_for']) |
215 'identity', 'owned_by', 'wf_info_for']) |
217 rschema = schema.rschema('relation_type') |
216 rschema = schema.rschema('relation_type') |
218 properties = rschema.rproperties('CWAttribute', 'CWRType') |
217 properties = rschema.rdef('CWAttribute', 'CWRType') |
219 self.assertEquals(properties['cardinality'], '1*') |
218 self.assertEquals(properties.cardinality, '1*') |
220 constraints = properties['constraints'] |
219 constraints = properties.constraints |
221 self.failUnlessEqual(len(constraints), 1, constraints) |
220 self.failUnlessEqual(len(constraints), 1, constraints) |
222 constraint = constraints[0] |
221 constraint = constraints[0] |
223 self.failUnless(isinstance(constraint, RQLConstraint)) |
222 self.failUnless(isinstance(constraint, RQLConstraint)) |
224 self.failUnlessEqual(constraint.restriction, 'O final TRUE') |
223 self.failUnlessEqual(constraint.restriction, 'O final TRUE') |
225 |
224 |
244 |
243 |
245 def test_rrqlexpr_on_etype(self): |
244 def test_rrqlexpr_on_etype(self): |
246 self._test('rrqlexpr_on_eetype.py', "can't use RRQLExpression on an entity type, use an ERQLExpression (ToTo)") |
245 self._test('rrqlexpr_on_eetype.py', "can't use RRQLExpression on an entity type, use an ERQLExpression (ToTo)") |
247 |
246 |
248 def test_erqlexpr_on_rtype(self): |
247 def test_erqlexpr_on_rtype(self): |
249 self._test('erqlexpr_on_ertype.py', "can't use ERQLExpression on a relation type, use a RRQLExpression (toto)") |
248 self._test('erqlexpr_on_ertype.py', "can't use ERQLExpression on relation ToTo toto TuTu, use a RRQLExpression") |
250 |
249 |
251 def test_rqlexpr_on_rtype_read(self): |
250 def test_rqlexpr_on_rtype_read(self): |
252 self._test('rqlexpr_on_ertype_read.py', "can't use rql expression for read permission of a relation type (toto)") |
251 self._test('rqlexpr_on_ertype_read.py', "can't use rql expression for read permission of relation ToTo toto TuTu") |
253 |
252 |
254 def test_rrqlexpr_on_attr(self): |
253 def test_rrqlexpr_on_attr(self): |
255 self._test('rrqlexpr_on_attr.py', "can't use RRQLExpression on a final relation type (eg attribute relation), use an ERQLExpression (attr)") |
254 self._test('rrqlexpr_on_attr.py', "can't use RRQLExpression on attribute ToTo.attr[String], use an ERQLExpression") |
256 |
255 |
257 |
256 |
258 class NormalizeExpressionTC(TestCase): |
257 class NormalizeExpressionTC(TestCase): |
259 |
258 |
260 def test(self): |
259 def test(self): |
261 self.assertEquals(normalize_expression('X bla Y,Y blur Z , Z zigoulou X '), |
260 self.assertEquals(normalize_expression('X bla Y,Y blur Z , Z zigoulou X '), |
262 'X bla Y, Y blur Z, Z zigoulou X') |
261 'X bla Y, Y blur Z, Z zigoulou X') |
263 |
262 |
|
263 class RQLExpressionTC(TestCase): |
|
264 def test_comparison(self): |
|
265 self.assertEquals(ERQLExpression('X is CWUser', 'X', 0), ERQLExpression('X is CWUser', 'X', 0)) |
|
266 self.assertNotEquals(ERQLExpression('X is CWUser', 'X', 0), ERQLExpression('X is CWGroup', 'X', 0)) |
264 if __name__ == '__main__': |
267 if __name__ == '__main__': |
265 unittest_main() |
268 unittest_main() |