author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 09 Jul 2009 15:15:18 +0200 | |
branch | stable |
changeset 2356 | ef9e30485ee7 |
parent 1977 | 606923dff11b |
child 2456 | aa25d6b244c8 |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
1 |
"""unit tests for module cubicweb.schema |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
""" |
0 | 8 |
|
9 |
import sys |
|
10 |
from os.path import join, isabs, basename, dirname |
|
11 |
||
12 |
from logilab.common.testlib import TestCase, unittest_main |
|
13 |
||
14 |
from rql import RQLSyntaxError |
|
15 |
||
16 |
from yams import BadSchemaDefinition |
|
17 |
from yams.constraints import SizeConstraint, StaticVocabularyConstraint |
|
18 |
from yams.buildobjs import RelationDefinition, EntityType, RelationType |
|
19 |
||
20 |
from cubicweb.schema import CubicWebSchema, CubicWebEntitySchema, \ |
|
21 |
RQLConstraint, CubicWebSchemaLoader, ERQLExpression, RRQLExpression, \ |
|
22 |
normalize_expression |
|
23 |
from cubicweb.devtools import TestServerConfiguration as TestConfiguration |
|
24 |
||
25 |
DATADIR = join(dirname(__file__), 'data') |
|
26 |
||
27 |
# build a dummy schema ######################################################## |
|
28 |
||
29 |
||
30 |
PERSONNE_PERMISSIONS = { |
|
31 |
'read': ('managers', 'users', 'guests'), |
|
32 |
'update': ('managers', 'owners'), |
|
33 |
'add': ('managers', ERQLExpression('X travaille S, S owned_by U')), |
|
34 |
'delete': ('managers', 'owners',), |
|
35 |
} |
|
36 |
||
37 |
CONCERNE_PERMISSIONS = { |
|
38 |
'read': ('managers', 'users', 'guests'), |
|
39 |
'add': ('managers', RRQLExpression('U has_update_permission S')), |
|
40 |
'delete': ('managers', RRQLExpression('O owned_by U')), |
|
41 |
} |
|
42 |
||
43 |
schema = CubicWebSchema('Test Schema') |
|
44 |
enote = schema.add_entity_type(EntityType('Note')) |
|
45 |
eaffaire = schema.add_entity_type(EntityType('Affaire')) |
|
46 |
eperson = schema.add_entity_type(EntityType('Personne', permissions=PERSONNE_PERMISSIONS)) |
|
47 |
esociete = schema.add_entity_type(EntityType('Societe')) |
|
48 |
||
49 |
RELS = ( |
|
50 |
# attribute relations |
|
51 |
('Note date String'), |
|
52 |
('Note type String'), |
|
53 |
('Affaire sujet String'), |
|
54 |
('Affaire ref String'), |
|
55 |
('Personne nom String'), |
|
56 |
('Personne prenom String'), |
|
57 |
('Personne sexe String'), |
|
58 |
('Personne tel Int'), |
|
59 |
('Personne fax Int'), |
|
60 |
('Personne datenaiss Date'), |
|
61 |
('Personne TEST Boolean'), |
|
62 |
('Personne promo String'), |
|
63 |
# real relations |
|
64 |
('Personne travaille Societe'), |
|
65 |
('Personne evaluee Note'), |
|
66 |
('Societe evaluee Note'), |
|
67 |
('Personne concerne Affaire'), |
|
68 |
('Personne concerne Societe'), |
|
69 |
('Affaire Concerne Societe'), |
|
70 |
) |
|
71 |
done = {} |
|
72 |
for rel in RELS: |
|
73 |
_from, _type, _to = rel.split() |
|
74 |
if not _type.lower() in done: |
|
75 |
if _type == 'concerne': |
|
76 |
schema.add_relation_type(RelationType(_type, permissions=CONCERNE_PERMISSIONS)) |
|
77 |
else: |
|
78 |
schema.add_relation_type(RelationType(_type)) |
|
79 |
done[_type.lower()] = True |
|
80 |
schema.add_relation_def(RelationDefinition(_from, _type, _to)) |
|
81 |
||
82 |
class CubicWebSchemaTC(TestCase): |
|
83 |
||
84 |
def test_normalize(self): |
|
85 |
"""test that entities, relations and attributes name are normalized |
|
86 |
""" |
|
87 |
self.assertEqual(esociete.type, 'Societe') |
|
88 |
self.assertEqual(schema.has_relation('TEST'), 0) |
|
89 |
self.assertEqual(schema.has_relation('test'), 1) |
|
90 |
self.assertEqual(eperson.subject_relation('test').type, 'test') |
|
91 |
self.assertEqual(schema.has_relation('Concerne'), 0) |
|
92 |
self.assertEqual(schema.has_relation('concerne'), 1) |
|
93 |
self.assertEqual(schema.rschema('concerne').type, 'concerne') |
|
94 |
||
95 |
def test_entity_perms(self): |
|
96 |
eperson.set_default_groups() |
|
97 |
self.assertEqual(eperson.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
98 |
self.assertEqual(eperson.get_groups('update'), set(('managers', 'owners',))) |
|
99 |
self.assertEqual(eperson.get_groups('delete'), set(('managers', 'owners'))) |
|
100 |
self.assertEqual(eperson.get_groups('add'), set(('managers',))) |
|
101 |
self.assertEqual([str(e) for e in eperson.get_rqlexprs('add')], |
|
102 |
['Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s']) |
|
103 |
eperson.set_groups('read', ('managers',)) |
|
104 |
self.assertEqual(eperson.get_groups('read'), set(('managers',))) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
105 |
|
0 | 106 |
def test_relation_perms(self): |
107 |
rconcerne = schema.rschema('concerne') |
|
108 |
rconcerne.set_default_groups() |
|
109 |
self.assertEqual(rconcerne.get_groups('read'), set(('managers', 'users', 'guests'))) |
|
110 |
self.assertEqual(rconcerne.get_groups('delete'), set(('managers',))) |
|
111 |
self.assertEqual(rconcerne.get_groups('add'), set(('managers', ))) |
|
112 |
rconcerne.set_groups('read', ('managers',)) |
|
113 |
self.assertEqual(rconcerne.get_groups('read'), set(('managers',))) |
|
114 |
self.assertEqual([str(e) for e in rconcerne.get_rqlexprs('add')], |
|
115 |
['Any S WHERE U has_update_permission S, S eid %(s)s, U eid %(u)s']) |
|
116 |
||
117 |
def test_erqlexpression(self): |
|
118 |
self.assertRaises(RQLSyntaxError, ERQLExpression, '1') |
|
119 |
expr = ERQLExpression('X travaille S, S owned_by U') |
|
120 |
self.assertEquals(str(expr), 'Any X WHERE X travaille S, S owned_by U, X eid %(x)s, U eid %(u)s') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
121 |
|
0 | 122 |
def test_rrqlexpression(self): |
123 |
self.assertRaises(Exception, RRQLExpression, '1') |
|
124 |
self.assertRaises(RQLSyntaxError, RRQLExpression, 'O X Y') |
|
125 |
expr = RRQLExpression('U has_update_permission O') |
|
126 |
self.assertEquals(str(expr), 'Any O WHERE U has_update_permission O, O eid %(o)s, U eid %(u)s') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
127 |
|
0 | 128 |
|
129 |
loader = CubicWebSchemaLoader() |
|
130 |
config = TestConfiguration('data') |
|
131 |
config.bootstrap_cubes() |
|
132 |
loader.lib_directory = config.schemas_lib_dir() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
133 |
|
0 | 134 |
class SQLSchemaReaderClassTest(TestCase): |
135 |
||
136 |
def test_knownValues_include_schema_files(self): |
|
137 |
schema_files = loader.include_schema_files('Bookmark') |
|
138 |
for file in schema_files: |
|
139 |
self.assert_(isabs(file)) |
|
140 |
self.assertListEquals([basename(f) for f in schema_files], ['Bookmark.py']) |
|
141 |
||
142 |
def test_knownValues_load_schema(self): |
|
143 |
schema = loader.load(config) |
|
144 |
self.assert_(isinstance(schema, CubicWebSchema)) |
|
145 |
self.assertEquals(schema.name, 'data') |
|
146 |
entities = [str(e) for e in schema.entities()] |
|
147 |
entities.sort() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
148 |
expected_entities = ['Bookmark', 'Boolean', 'Bytes', 'Card', |
0 | 149 |
'Date', 'Datetime', 'Decimal', |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
150 |
'CWCache', 'CWConstraint', 'CWConstraintType', 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
151 |
'CWAttribute', 'CWGroup', 'EmailAddress', 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
152 |
'CWPermission', 'CWProperty', 'CWRType', 'CWUser', |
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
153 |
'File', 'Float', 'Image', 'Int', 'Interval', 'Note', |
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
154 |
'Password', 'Personne', |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
155 |
'RQLExpression', |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
156 |
'Societe', 'State', 'String', 'SubNote', 'Tag', 'Time', |
0 | 157 |
'Transition', 'TrInfo'] |
158 |
self.assertListEquals(entities, sorted(expected_entities)) |
|
159 |
relations = [str(r) for r in schema.relations()] |
|
160 |
relations.sort() |
|
161 |
expected_relations = ['add_permission', 'address', 'alias', |
|
162 |
'allowed_transition', 'bookmarked_by', 'canonical', |
|
163 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
164 |
'cardinality', 'comment', 'comment_format', |
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
165 |
'composite', 'condition', 'connait', 'constrained_by', 'content', |
0 | 166 |
'content_format', 'created_by', 'creation_date', 'cstrtype', |
167 |
||
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
168 |
'data', 'data_encoding', 'data_format', 'defaultval', 'delete_permission', |
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
169 |
'description', 'description_format', 'destination_state', |
0 | 170 |
|
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
171 |
'ecrit_par', 'eid', 'evaluee', 'expression', 'exprtype', |
0 | 172 |
|
173 |
'final', 'firstname', 'for_user', |
|
174 |
'from_entity', 'from_state', 'fulltext_container', 'fulltextindexed', |
|
175 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
176 |
'has_text', |
0 | 177 |
'identical_to', 'identity', 'in_group', 'in_state', 'indexed', |
178 |
'initial_state', 'inlined', 'internationalizable', 'is', 'is_instance_of', |
|
179 |
||
180 |
'label', 'last_login_time', 'login', |
|
181 |
||
182 |
'mainvars', 'meta', 'modification_date', |
|
183 |
||
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
184 |
'name', 'nom', |
0 | 185 |
|
186 |
'ordernum', 'owned_by', |
|
187 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
188 |
'path', 'pkey', 'prenom', 'primary_email', |
0 | 189 |
|
190 |
'read_permission', 'relation_type', 'require_group', |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
191 |
|
0 | 192 |
'specializes', 'state_of', 'surname', 'symetric', 'synopsis', |
193 |
||
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
194 |
'tags', 'timestamp', 'title', 'to_entity', 'to_state', 'transition_of', 'travaille', 'type', |
0 | 195 |
|
196 |
'upassword', 'update_permission', 'use_email', |
|
197 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
198 |
'value', |
0 | 199 |
|
200 |
'wf_info_for', 'wikiid'] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
201 |
|
0 | 202 |
self.assertListEquals(relations, expected_relations) |
203 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
204 |
eschema = schema.eschema('CWUser') |
0 | 205 |
rels = sorted(str(r) for r in eschema.subject_relations()) |
206 |
self.assertListEquals(rels, ['created_by', 'creation_date', 'eid', |
|
750
89e997bc2bf1
update test for new test schema
sylvain.thenault@logilab.fr
parents:
624
diff
changeset
|
207 |
'evaluee', 'firstname', 'has_text', 'identity', |
0 | 208 |
'in_group', 'in_state', 'is', |
209 |
'is_instance_of', 'last_login_time', |
|
210 |
'login', 'modification_date', 'owned_by', |
|
211 |
'primary_email', 'surname', 'upassword', |
|
212 |
'use_email']) |
|
213 |
rels = sorted(r.type for r in eschema.object_relations()) |
|
214 |
self.assertListEquals(rels, ['bookmarked_by', 'created_by', 'for_user', |
|
215 |
'identity', 'owned_by', 'wf_info_for']) |
|
216 |
rschema = schema.rschema('relation_type') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
217 |
properties = rschema.rproperties('CWAttribute', 'CWRType') |
0 | 218 |
self.assertEquals(properties['cardinality'], '1*') |
219 |
constraints = properties['constraints'] |
|
220 |
self.failUnlessEqual(len(constraints), 1, constraints) |
|
221 |
constraint = constraints[0] |
|
222 |
self.failUnless(isinstance(constraint, RQLConstraint)) |
|
223 |
self.failUnlessEqual(constraint.restriction, 'O final TRUE') |
|
224 |
||
225 |
def test_fulltext_container(self): |
|
226 |
schema = loader.load(config) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
227 |
self.failUnless('has_text' in schema['CWUser'].subject_relations()) |
0 | 228 |
self.failIf('has_text' in schema['EmailAddress'].subject_relations()) |
229 |
||
230 |
||
231 |
class BadSchemaRQLExprTC(TestCase): |
|
232 |
def setUp(self): |
|
233 |
self.loader = CubicWebSchemaLoader() |
|
234 |
self.loader.defined = {} |
|
951 | 235 |
self.loader.loaded_files = [] |
0 | 236 |
self.loader._instantiate_handlers() |
237 |
||
238 |
def _test(self, schemafile, msg): |
|
239 |
self.loader.handle_file(join(DATADIR, schemafile)) |
|
240 |
ex = self.assertRaises(BadSchemaDefinition, |
|
241 |
self.loader._build_schema, 'toto', False) |
|
242 |
self.assertEquals(str(ex), msg) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
243 |
|
0 | 244 |
def test_rrqlexpr_on_etype(self): |
245 |
self._test('rrqlexpr_on_eetype.py', "can't use RRQLExpression on an entity type, use an ERQLExpression (ToTo)") |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
246 |
|
0 | 247 |
def test_erqlexpr_on_rtype(self): |
248 |
self._test('erqlexpr_on_ertype.py', "can't use ERQLExpression on a relation type, use a RRQLExpression (toto)") |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
249 |
|
0 | 250 |
def test_rqlexpr_on_rtype_read(self): |
251 |
self._test('rqlexpr_on_ertype_read.py', "can't use rql expression for read permission of a relation type (toto)") |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
252 |
|
0 | 253 |
def test_rrqlexpr_on_attr(self): |
254 |
self._test('rrqlexpr_on_attr.py', "can't use RRQLExpression on a final relation type (eg attribute relation), use an ERQLExpression (attr)") |
|
255 |
||
256 |
||
257 |
class NormalizeExpressionTC(TestCase): |
|
258 |
||
259 |
def test(self): |
|
260 |
self.assertEquals(normalize_expression('X bla Y,Y blur Z , Z zigoulou X '), |
|
261 |
'X bla Y, Y blur Z, Z zigoulou X') |
|
262 |
||
263 |
if __name__ == '__main__': |
|
264 |
unittest_main() |