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