38 self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"') |
38 self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"') |
39 self.assertRaises(ValidationError, self.commit) |
39 self.assertRaises(ValidationError, self.commit) |
40 self.execute('DELETE X in_group Y WHERE X login "toto"') |
40 self.execute('DELETE X in_group Y WHERE X login "toto"') |
41 self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"') |
41 self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"') |
42 self.commit() |
42 self.commit() |
43 |
43 |
44 def test_delete_required_relations_object(self): |
44 def test_delete_required_relations_object(self): |
45 self.skip('no sample in the schema ! YAGNI ? Kermaat ?') |
45 self.skip('no sample in the schema ! YAGNI ? Kermaat ?') |
46 |
46 |
47 def test_static_vocabulary_check(self): |
47 def test_static_vocabulary_check(self): |
48 self.assertRaises(ValidationError, |
48 self.assertRaises(ValidationError, |
49 self.execute, |
49 self.execute, |
50 'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"') |
50 'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"') |
51 |
51 |
52 def test_missing_required_relations_subject_inline(self): |
52 def test_missing_required_relations_subject_inline(self): |
53 # missing in_group relation |
53 # missing in_group relation |
54 self.execute('INSERT CWUser X: X login "toto", X upassword "hop"') |
54 self.execute('INSERT CWUser X: X login "toto", X upassword "hop"') |
55 self.assertRaises(ValidationError, |
55 self.assertRaises(ValidationError, |
56 self.commit) |
56 self.commit) |
57 |
57 |
58 def test_delete_if_singlecard1(self): |
58 def test_delete_if_singlecard1(self): |
74 eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
74 eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
75 'WHERE Y is EmailAddress, P is EmailPart')[0][0] |
75 'WHERE Y is EmailAddress, P is EmailPart')[0][0] |
76 self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress') |
76 self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress') |
77 rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid) |
77 rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid) |
78 self.assertEquals(len(rset), 1) |
78 self.assertEquals(len(rset), 1) |
79 |
79 |
80 def test_composite_1(self): |
80 def test_composite_1(self): |
81 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
81 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
82 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
82 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
83 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
83 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
84 'WHERE Y is EmailAddress, P is EmailPart') |
84 'WHERE Y is EmailAddress, P is EmailPart') |
88 rset = self.execute('Any X WHERE X is EmailPart') |
88 rset = self.execute('Any X WHERE X is EmailPart') |
89 self.assertEquals(len(rset), 1) |
89 self.assertEquals(len(rset), 1) |
90 self.commit() |
90 self.commit() |
91 rset = self.execute('Any X WHERE X is EmailPart') |
91 rset = self.execute('Any X WHERE X is EmailPart') |
92 self.assertEquals(len(rset), 0) |
92 self.assertEquals(len(rset), 0) |
93 |
93 |
94 def test_composite_2(self): |
94 def test_composite_2(self): |
95 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
95 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
96 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
96 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
97 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
97 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
98 'WHERE Y is EmailAddress, P is EmailPart') |
98 'WHERE Y is EmailAddress, P is EmailPart') |
100 self.execute('DELETE Email X') |
100 self.execute('DELETE Email X') |
101 self.execute('DELETE EmailPart X') |
101 self.execute('DELETE EmailPart X') |
102 self.commit() |
102 self.commit() |
103 rset = self.execute('Any X WHERE X is EmailPart') |
103 rset = self.execute('Any X WHERE X is EmailPart') |
104 self.assertEquals(len(rset), 0) |
104 self.assertEquals(len(rset), 0) |
105 |
105 |
106 def test_composite_redirection(self): |
106 def test_composite_redirection(self): |
107 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
107 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
108 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
108 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
109 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
109 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
110 'WHERE Y is EmailAddress, P is EmailPart') |
110 'WHERE Y is EmailAddress, P is EmailPart') |
146 entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
146 entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
147 self.assertEquals(entity.descr, u'yo') |
147 self.assertEquals(entity.descr, u'yo') |
148 self.execute('SET A descr "R&D<p>yo" WHERE A eid %s' % entity.eid) |
148 self.execute('SET A descr "R&D<p>yo" WHERE A eid %s' % entity.eid) |
149 entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
149 entity = self.execute('Any A WHERE A eid %s' % entity.eid).get_entity(0, 0) |
150 self.assertEquals(entity.descr, u'R&D<p>yo</p>') |
150 self.assertEquals(entity.descr, u'R&D<p>yo</p>') |
151 |
151 |
152 |
152 |
153 |
153 |
154 class UserGroupHooksTC(RepositoryBasedTC): |
154 class UserGroupHooksTC(RepositoryBasedTC): |
155 |
155 |
156 def test_user_synchronization(self): |
156 def test_user_synchronization(self): |
157 self.create_user('toto', password='hop', commit=False) |
157 self.create_user('toto', password='hop', commit=False) |
158 self.assertRaises(AuthenticationError, |
158 self.assertRaises(AuthenticationError, |
159 self.repo.connect, u'toto', 'hop') |
159 self.repo.connect, u'toto', 'hop') |
160 self.commit() |
160 self.commit() |
191 def test_no_created_by_on_deleted_entity(self): |
191 def test_no_created_by_on_deleted_entity(self): |
192 eid = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0] |
192 eid = self.execute('INSERT EmailAddress X: X address "toto@logilab.fr"')[0][0] |
193 self.execute('DELETE EmailAddress X WHERE X eid %s' % eid) |
193 self.execute('DELETE EmailAddress X WHERE X eid %s' % eid) |
194 self.commit() |
194 self.commit() |
195 self.failIf(self.execute('Any X WHERE X created_by Y, X eid >= %(x)s', {'x': eid})) |
195 self.failIf(self.execute('Any X WHERE X created_by Y, X eid >= %(x)s', {'x': eid})) |
196 |
196 |
197 class CWPropertyHooksTC(RepositoryBasedTC): |
197 class CWPropertyHooksTC(RepositoryBasedTC): |
198 |
198 |
199 def test_unexistant_eproperty(self): |
199 def test_unexistant_eproperty(self): |
200 ex = self.assertRaises(ValidationError, |
200 ex = self.assertRaises(ValidationError, |
201 self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop", X for_user U') |
201 self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop", X for_user U') |
202 self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
202 self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
203 ex = self.assertRaises(ValidationError, |
203 ex = self.assertRaises(ValidationError, |
204 self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop"') |
204 self.execute, 'INSERT CWProperty X: X pkey "bla.bla", X value "hop"') |
205 self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
205 self.assertEquals(ex.errors, {'pkey': 'unknown property key'}) |
206 |
206 |
207 def test_site_wide_eproperty(self): |
207 def test_site_wide_eproperty(self): |
208 ex = self.assertRaises(ValidationError, |
208 ex = self.assertRaises(ValidationError, |
209 self.execute, 'INSERT CWProperty X: X pkey "ui.site-title", X value "hop", X for_user U') |
209 self.execute, 'INSERT CWProperty X: X pkey "ui.site-title", X value "hop", X for_user U') |
210 self.assertEquals(ex.errors, {'for_user': "site-wide property can't be set for user"}) |
210 self.assertEquals(ex.errors, {'for_user': "site-wide property can't be set for user"}) |
211 |
211 |
212 def test_bad_type_eproperty(self): |
212 def test_bad_type_eproperty(self): |
213 ex = self.assertRaises(ValidationError, |
213 ex = self.assertRaises(ValidationError, |
214 self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop", X for_user U') |
214 self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop", X for_user U') |
215 self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
215 self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
216 ex = self.assertRaises(ValidationError, |
216 ex = self.assertRaises(ValidationError, |
217 self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop"') |
217 self.execute, 'INSERT CWProperty X: X pkey "ui.language", X value "hop"') |
218 self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
218 self.assertEquals(ex.errors, {'value': u'unauthorized value'}) |
219 |
219 |
220 |
220 |
221 class SchemaHooksTC(RepositoryBasedTC): |
221 class SchemaHooksTC(RepositoryBasedTC): |
222 |
222 |
223 def test_duplicate_etype_error(self): |
223 def test_duplicate_etype_error(self): |
224 # check we can't add a CWEType or CWRType entity if it already exists one |
224 # check we can't add a CWEType or CWRType entity if it already exists one |
225 # with the same name |
225 # with the same name |
226 # |
226 # |
227 # according to hook order, we'll get a repository or validation error |
227 # according to hook order, we'll get a repository or validation error |
228 self.assertRaises((ValidationError, RepositoryError), |
228 self.assertRaises((ValidationError, RepositoryError), |
229 self.execute, 'INSERT CWEType X: X name "Societe"') |
229 self.execute, 'INSERT CWEType X: X name "Societe"') |
230 self.assertRaises((ValidationError, RepositoryError), |
230 self.assertRaises((ValidationError, RepositoryError), |
231 self.execute, 'INSERT CWRType X: X name "in_group"') |
231 self.execute, 'INSERT CWRType X: X name "in_group"') |
232 |
232 |
233 def test_validation_unique_constraint(self): |
233 def test_validation_unique_constraint(self): |
234 self.assertRaises(ValidationError, |
234 self.assertRaises(ValidationError, |
235 self.execute, 'INSERT CWUser X: X login "admin"') |
235 self.execute, 'INSERT CWUser X: X login "admin"') |
236 try: |
236 try: |
237 self.execute('INSERT CWUser X: X login "admin"') |
237 self.execute('INSERT CWUser X: X login "admin"') |
251 repo.config._cubes = None |
251 repo.config._cubes = None |
252 repo.fill_schema() |
252 repo.fill_schema() |
253 RepositoryBasedTC.setUp(self) |
253 RepositoryBasedTC.setUp(self) |
254 |
254 |
255 def index_exists(self, etype, attr, unique=False): |
255 def index_exists(self, etype, attr, unique=False): |
256 dbhelper = self.session.pool.source('system').dbhelper |
256 dbhelper = self.session.pool.source('system').dbhelper |
257 sqlcursor = self.session.pool['system'] |
257 sqlcursor = self.session.pool['system'] |
258 return dbhelper.index_exists(sqlcursor, SQL_PREFIX + etype, SQL_PREFIX + attr, unique=unique) |
258 return dbhelper.index_exists(sqlcursor, SQL_PREFIX + etype, SQL_PREFIX + attr, unique=unique) |
259 |
259 |
260 def test_base(self): |
260 def test_base(self): |
261 schema = self.repo.schema |
261 schema = self.repo.schema |
262 dbhelper = self.session.pool.source('system').dbhelper |
262 dbhelper = self.session.pool.source('system').dbhelper |
263 sqlcursor = self.session.pool['system'] |
263 sqlcursor = self.session.pool['system'] |
264 self.failIf(schema.has_entity('Societe2')) |
264 self.failIf(schema.has_entity('Societe2')) |
265 self.failIf(schema.has_entity('concerne2')) |
265 self.failIf(schema.has_entity('concerne2')) |
266 # schema should be update on insertion (after commit) |
266 # schema should be update on insertion (after commit) |
267 self.execute('INSERT CWEType X: X name "Societe2", X description "", X meta FALSE, X final FALSE') |
267 self.execute('INSERT CWEType X: X name "Societe2", X description "", X meta FALSE, X final FALSE') |
330 self.failIf('subdiv' in snames) |
330 self.failIf('subdiv' in snames) |
331 snames = [name for name, in self.execute('Any N WHERE S is_instance_of Societe, S nom N')] |
331 snames = [name for name, in self.execute('Any N WHERE S is_instance_of Societe, S nom N')] |
332 self.failUnless('subdiv' in snames) |
332 self.failUnless('subdiv' in snames) |
333 snames = [name for name, in self.execute('Any N WHERE S is_instance_of Division, S nom N')] |
333 snames = [name for name, in self.execute('Any N WHERE S is_instance_of Division, S nom N')] |
334 self.failUnless('subdiv' in snames) |
334 self.failUnless('subdiv' in snames) |
335 |
335 |
336 |
336 |
337 def test_perms_synchronization_1(self): |
337 def test_perms_synchronization_1(self): |
338 schema = self.repo.schema |
338 schema = self.repo.schema |
339 self.assertEquals(schema['CWUser'].get_groups('read'), set(('managers', 'users'))) |
339 self.assertEquals(schema['CWUser'].get_groups('read'), set(('managers', 'users'))) |
340 self.failUnless(self.execute('Any X, Y WHERE X is CWEType, X name "CWUser", Y is CWGroup, Y name "users"')[0]) |
340 self.failUnless(self.execute('Any X, Y WHERE X is CWEType, X name "CWUser", Y is CWGroup, Y name "users"')[0]) |
341 self.execute('DELETE X read_permission Y WHERE X is CWEType, X name "CWUser", Y name "users"') |
341 self.execute('DELETE X read_permission Y WHERE X is CWEType, X name "CWUser", Y name "users"') |
372 % (eeid, groupeids[0], groupeids[1])) |
372 % (eeid, groupeids[0], groupeids[1])) |
373 self.commit() |
373 self.commit() |
374 self.execute('Any X WHERE X is CWEType, X name "CWEType"') |
374 self.execute('Any X WHERE X is CWEType, X name "CWEType"') |
375 |
375 |
376 # schema modification hooks tests ######################################### |
376 # schema modification hooks tests ######################################### |
377 |
377 |
378 def test_uninline_relation(self): |
378 def test_uninline_relation(self): |
379 dbhelper = self.session.pool.source('system').dbhelper |
379 dbhelper = self.session.pool.source('system').dbhelper |
380 sqlcursor = self.session.pool['system'] |
380 sqlcursor = self.session.pool['system'] |
381 # Personne inline2 Affaire inline |
381 # Personne inline2 Affaire inline |
382 # insert a person without inline2 relation (not mandatory) |
382 # insert a person without inline2 relation (not mandatory) |
383 self.execute('INSERT Personne X: X nom "toto"') |
383 self.execute('INSERT Personne X: X nom "toto"') |
384 peid = self.execute('INSERT Personne X: X nom "tutu"')[0][0] |
384 peid = self.execute('INSERT Personne X: X nom "tutu"')[0][0] |
408 rset = self.execute('Any X, Y WHERE X inline2 Y') |
408 rset = self.execute('Any X, Y WHERE X inline2 Y') |
409 self.assertEquals(len(rset), 1) |
409 self.assertEquals(len(rset), 1) |
410 self.assertEquals(rset.rows[0], [peid, aeid]) |
410 self.assertEquals(rset.rows[0], [peid, aeid]) |
411 |
411 |
412 def test_indexed_change(self): |
412 def test_indexed_change(self): |
413 dbhelper = self.session.pool.source('system').dbhelper |
413 dbhelper = self.session.pool.source('system').dbhelper |
414 sqlcursor = self.session.pool['system'] |
414 sqlcursor = self.session.pool['system'] |
415 try: |
415 try: |
416 self.execute('SET X indexed TRUE WHERE X relation_type R, R name "sujet"') |
416 self.execute('SET X indexed TRUE WHERE X relation_type R, R name "sujet"') |
417 self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
417 self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
418 self.failIf(self.index_exists('Affaire', 'sujet')) |
418 self.failIf(self.index_exists('Affaire', 'sujet')) |
426 self.commit() |
426 self.commit() |
427 self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
427 self.failIf(self.schema['sujet'].rproperty('Affaire', 'String', 'indexed')) |
428 self.failIf(self.index_exists('Affaire', 'sujet')) |
428 self.failIf(self.index_exists('Affaire', 'sujet')) |
429 |
429 |
430 def test_unique_change(self): |
430 def test_unique_change(self): |
431 dbhelper = self.session.pool.source('system').dbhelper |
431 dbhelper = self.session.pool.source('system').dbhelper |
432 sqlcursor = self.session.pool['system'] |
432 sqlcursor = self.session.pool['system'] |
433 try: |
433 try: |
434 try: |
434 try: |
435 self.execute('INSERT CWConstraint X: X cstrtype CT, DEF constrained_by X ' |
435 self.execute('INSERT CWConstraint X: X cstrtype CT, DEF constrained_by X ' |
436 'WHERE CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
436 'WHERE CT name "UniqueConstraint", DEF relation_type RT, DEF from_entity E,' |
493 cnx.close() |
493 cnx.close() |
494 # though managers can do whatever he want |
494 # though managers can do whatever he want |
495 self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, ' |
495 self.execute('INSERT CWUser X: X login "badaboum", X upassword %(pwd)s, ' |
496 'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'}) |
496 'X in_state S, X in_group G WHERE S name "deactivated", G name "users"', {'pwd': 'oops'}) |
497 self.commit() |
497 self.commit() |
498 |
498 |
499 # test that the workflow is correctly enforced |
499 # test that the workflow is correctly enforced |
500 def test_transition_checking1(self): |
500 def test_transition_checking1(self): |
501 cnx = self.login('stduser') |
501 cnx = self.login('stduser') |
502 cu = cnx.cursor() |
502 cu = cnx.cursor() |
503 ueid = cnx.user(self.current_session()).eid |
503 ueid = cnx.user(self.current_session()).eid |
504 self.assertRaises(ValidationError, |
504 self.assertRaises(ValidationError, |
505 cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
505 cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
506 {'x': ueid, 's': self.s_activated}, 'x') |
506 {'x': ueid, 's': self.s_activated}, 'x') |
507 cnx.close() |
507 cnx.close() |
508 |
508 |
509 def test_transition_checking2(self): |
509 def test_transition_checking2(self): |
510 cnx = self.login('stduser') |
510 cnx = self.login('stduser') |
511 cu = cnx.cursor() |
511 cu = cnx.cursor() |
512 ueid = cnx.user(self.current_session()).eid |
512 ueid = cnx.user(self.current_session()).eid |
513 self.assertRaises(ValidationError, |
513 self.assertRaises(ValidationError, |
514 cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
514 cu.execute, 'SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
515 {'x': ueid, 's': self.s_dummy}, 'x') |
515 {'x': ueid, 's': self.s_dummy}, 'x') |
516 cnx.close() |
516 cnx.close() |
517 |
517 |
518 def test_transition_checking3(self): |
518 def test_transition_checking3(self): |
519 cnx = self.login('stduser') |
519 cnx = self.login('stduser') |
520 cu = cnx.cursor() |
520 cu = cnx.cursor() |
521 ueid = cnx.user(self.current_session()).eid |
521 ueid = cnx.user(self.current_session()).eid |
522 cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
522 cu.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
557 tr = rset.get_entity(1, 0) |
557 tr = rset.get_entity(1, 0) |
558 #tr.complete() |
558 #tr.complete() |
559 self.assertEquals(tr.comment, None) |
559 self.assertEquals(tr.comment, None) |
560 self.assertEquals(tr.from_state[0].eid, self.s_activated) |
560 self.assertEquals(tr.from_state[0].eid, self.s_activated) |
561 self.assertEquals(tr.to_state[0].eid, self.s_deactivated) |
561 self.assertEquals(tr.to_state[0].eid, self.s_deactivated) |
562 |
562 |
563 self.session.set_shared_data('trcomment', u'il est pas sage celui-la') |
563 self.session.set_shared_data('trcomment', u'il est pas sage celui-la') |
564 self.session.set_shared_data('trcommentformat', u'text/plain') |
564 self.session.set_shared_data('trcommentformat', u'text/plain') |
565 self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
565 self.execute('SET X in_state S WHERE X eid %(x)s, S eid %(s)s', |
566 {'x': ueid, 's': self.s_activated}, 'x') |
566 {'x': ueid, 's': self.s_activated}, 'x') |
567 self.commit() |
567 self.commit() |