author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 11 Sep 2009 08:39:17 +0200 | |
branch | stable |
changeset 3174 | 97273eeaaead |
parent 2926 | 4484387ed012 |
child 2931 | 17224e90a1c4 |
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, \ |
|
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
23 |
normalize_expression, order_eschemas |
0 | 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 |
loader = CubicWebSchemaLoader() |
130 |
config = TestConfiguration('data') |
|
131 |
config.bootstrap_cubes() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
132 |
|
2926
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
133 |
class SchemaReaderClassTest(TestCase): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
134 |
|
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
135 |
def test_order_eschemas(self): |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
136 |
schema = loader.load(config) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
137 |
self.assertEquals(order_eschemas([schema['Note'], schema['SubNote']]), |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
138 |
[schema['Note'], schema['SubNote']]) |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
139 |
self.assertEquals(order_eschemas([schema['SubNote'], schema['Note']]), |
4484387ed012
when adding/removing cubes, we should add/remove entity types in correct order if one inherits from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2730
diff
changeset
|
140 |
[schema['Note'], schema['SubNote']]) |
0 | 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', |
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
153 |
'ExternalUri', 'File', 'Float', 'Image', 'Int', 'Interval', 'Note', |
750
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', |
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
166 |
'content_format', 'created_by', 'creation_date', 'cstrtype', 'cwuri', |
0 | 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 |
||
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
182 |
'mainvars', 'modification_date', |
0 | 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 |
|
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
196 |
'upassword', 'update_permission', 'uri', 'use_email', |
0 | 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()) |
2456
aa25d6b244c8
new cwuri metadata + a few tests fixes on the way
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
206 |
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
|
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 = [] |
2635
c94df21f7ab2
F [cw.test] OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2456
diff
changeset
|
236 |
self.loader._pyreader = PyFileReader(self.loader) |
0 | 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() |