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