22 from cubicweb.devtools.testlib import CubicWebTC |
22 from cubicweb.devtools.testlib import CubicWebTC |
23 |
23 |
24 class CoreHooksTC(CubicWebTC): |
24 class CoreHooksTC(CubicWebTC): |
25 |
25 |
26 def test_delete_internal_entities(self): |
26 def test_delete_internal_entities(self): |
27 self.assertRaises(ValidationError, self.execute, |
27 with self.admin_access.repo_cnx() as cnx: |
28 'DELETE CWEType X WHERE X name "CWEType"') |
28 self.assertRaises(ValidationError, cnx.execute, |
29 self.assertRaises(ValidationError, self.execute, |
29 'DELETE CWEType X WHERE X name "CWEType"') |
30 'DELETE CWRType X WHERE X name "relation_type"') |
30 cnx.rollback() |
31 self.assertRaises(ValidationError, self.execute, |
31 self.assertRaises(ValidationError, cnx.execute, |
32 'DELETE CWGroup X WHERE X name "owners"') |
32 'DELETE CWRType X WHERE X name "relation_type"') |
|
33 cnx.rollback() |
|
34 self.assertRaises(ValidationError, cnx.execute, |
|
35 'DELETE CWGroup X WHERE X name "owners"') |
33 |
36 |
34 def test_delete_required_relations_subject(self): |
37 def test_delete_required_relations_subject(self): |
35 self.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y ' |
38 with self.admin_access.repo_cnx() as cnx: |
36 'WHERE Y name "users"') |
39 cnx.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y ' |
37 self.commit() |
40 'WHERE Y name "users"') |
38 self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"') |
41 cnx.commit() |
39 self.assertRaises(ValidationError, self.commit) |
42 cnx.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"') |
40 self.execute('DELETE X in_group Y WHERE X login "toto"') |
43 self.assertRaises(ValidationError, cnx.commit) |
41 self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"') |
44 cnx.rollback() |
42 self.commit() |
45 cnx.execute('DELETE X in_group Y WHERE X login "toto"') |
|
46 cnx.execute('SET X in_group Y WHERE X login "toto", Y name "guests"') |
|
47 cnx.commit() |
43 |
48 |
44 def test_static_vocabulary_check(self): |
49 def test_static_vocabulary_check(self): |
45 self.assertRaises(ValidationError, |
50 with self.admin_access.repo_cnx() as cnx: |
46 self.execute, |
51 self.assertRaises(ValidationError, |
47 'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"') |
52 cnx.execute, |
|
53 'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", ' |
|
54 'X relation_type RT, RT name "in_group"') |
48 |
55 |
49 def test_missing_required_relations_subject_inline(self): |
56 def test_missing_required_relations_subject_inline(self): |
50 # missing in_group relation |
57 with self.admin_access.repo_cnx() as cnx: |
51 self.execute('INSERT CWUser X: X login "toto", X upassword "hop"') |
58 # missing in_group relation |
52 self.assertRaises(ValidationError, |
59 cnx.execute('INSERT CWUser X: X login "toto", X upassword "hop"') |
53 self.commit) |
60 self.assertRaises(ValidationError, cnx.commit) |
54 |
61 |
55 def test_composite_1(self): |
62 def test_composite_1(self): |
56 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
63 with self.admin_access.repo_cnx() as cnx: |
57 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
64 cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
58 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
65 cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, ' |
59 'WHERE Y is EmailAddress, P is EmailPart') |
66 'X content "this is a test"') |
60 self.assertTrue(self.execute('Email X WHERE X sender Y')) |
67 cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, ' |
61 self.commit() |
68 'X recipients Y, X parts P ' |
62 self.execute('DELETE Email X') |
69 'WHERE Y is EmailAddress, P is EmailPart') |
63 rset = self.execute('Any X WHERE X is EmailPart') |
70 self.assertTrue(cnx.execute('Email X WHERE X sender Y')) |
64 self.assertEqual(len(rset), 0) |
71 cnx.commit() |
65 self.commit() |
72 cnx.execute('DELETE Email X') |
66 rset = self.execute('Any X WHERE X is EmailPart') |
73 rset = cnx.execute('Any X WHERE X is EmailPart') |
67 self.assertEqual(len(rset), 0) |
74 self.assertEqual(len(rset), 0) |
|
75 cnx.commit() |
|
76 rset = cnx.execute('Any X WHERE X is EmailPart') |
|
77 self.assertEqual(len(rset), 0) |
68 |
78 |
69 def test_composite_2(self): |
79 def test_composite_2(self): |
70 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
80 with self.admin_access.repo_cnx() as cnx: |
71 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
81 cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
72 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
82 cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, ' |
73 'WHERE Y is EmailAddress, P is EmailPart') |
83 'X content "this is a test"') |
74 self.commit() |
84 cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, ' |
75 self.execute('DELETE Email X') |
85 'X recipients Y, X parts P ' |
76 self.execute('DELETE EmailPart X') |
86 'WHERE Y is EmailAddress, P is EmailPart') |
77 self.commit() |
87 cnx.commit() |
78 rset = self.execute('Any X WHERE X is EmailPart') |
88 cnx.execute('DELETE Email X') |
79 self.assertEqual(len(rset), 0) |
89 cnx.execute('DELETE EmailPart X') |
|
90 cnx.commit() |
|
91 rset = cnx.execute('Any X WHERE X is EmailPart') |
|
92 self.assertEqual(len(rset), 0) |
80 |
93 |
81 def test_composite_redirection(self): |
94 def test_composite_redirection(self): |
82 self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
95 with self.admin_access.repo_cnx() as cnx: |
83 self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"') |
96 cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"') |
84 self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P ' |
97 cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, ' |
85 'WHERE Y is EmailAddress, P is EmailPart') |
98 'X content "this is a test"') |
86 self.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, X recipients Y ' |
99 cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, ' |
87 'WHERE Y is EmailAddress') |
100 'X recipients Y, X parts P ' |
88 self.commit() |
101 'WHERE Y is EmailAddress, P is EmailPart') |
89 self.execute('DELETE X parts Y WHERE X messageid "<1234>"') |
102 cnx.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, ' |
90 self.execute('SET X parts Y WHERE X messageid "<2345>"') |
103 'X recipients Y ' |
91 self.commit() |
104 'WHERE Y is EmailAddress') |
92 rset = self.execute('Any X WHERE X is EmailPart') |
105 cnx.commit() |
93 self.assertEqual(len(rset), 1) |
106 cnx.execute('DELETE X parts Y WHERE X messageid "<1234>"') |
94 self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>') |
107 cnx.execute('SET X parts Y WHERE X messageid "<2345>"') |
|
108 cnx.commit() |
|
109 rset = cnx.execute('Any X WHERE X is EmailPart') |
|
110 self.assertEqual(len(rset), 1) |
|
111 self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>') |
95 |
112 |
96 def test_composite_object_relation_deletion(self): |
113 def test_composite_object_relation_deletion(self): |
97 req = self.request() |
114 with self.admin_access.repo_cnx() as cnx: |
98 root = req.create_entity('Folder', name=u'root') |
115 root = cnx.create_entity('Folder', name=u'root') |
99 a = req.create_entity('Folder', name=u'a', parent=root) |
116 a = cnx.create_entity('Folder', name=u'a', parent=root) |
100 b = req.create_entity('Folder', name=u'b', parent=a) |
117 cnx.create_entity('Folder', name=u'b', parent=a) |
101 c = req.create_entity('Folder', name=u'c', parent=root) |
118 cnx.create_entity('Folder', name=u'c', parent=root) |
102 self.commit() |
119 cnx.commit() |
103 req = self.request() |
120 cnx.execute('DELETE Folder F WHERE F name "a"') |
104 req.execute('DELETE Folder F WHERE F name "a"') |
121 cnx.execute('DELETE F parent R WHERE R name "root"') |
105 req.execute('DELETE F parent R WHERE R name "root"') |
122 cnx.commit() |
106 self.commit() |
123 self.assertEqual([['root'], ['c']], |
107 req = self.request() |
124 cnx.execute('Any NF WHERE F is Folder, F name NF').rows) |
108 self.assertEqual([['root'], ['c']], |
125 self.assertEqual([], cnx.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows) |
109 req.execute('Any NF WHERE F is Folder, F name NF').rows) |
|
110 self.assertEqual([], |
|
111 req.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows) |
|
112 |
126 |
113 def test_composite_subject_relation_deletion(self): |
127 def test_composite_subject_relation_deletion(self): |
114 req = self.request() |
128 with self.admin_access.repo_cnx() as cnx: |
115 root = req.create_entity('Folder', name=u'root') |
129 root = cnx.create_entity('Folder', name=u'root') |
116 a = req.create_entity('Folder', name=u'a') |
130 a = cnx.create_entity('Folder', name=u'a') |
117 b = req.create_entity('Folder', name=u'b') |
131 b = cnx.create_entity('Folder', name=u'b') |
118 c = req.create_entity('Folder', name=u'c') |
132 c = cnx.create_entity('Folder', name=u'c') |
119 root.cw_set(children=(a, c)) |
133 root.cw_set(children=(a, c)) |
120 a.cw_set(children=b) |
134 a.cw_set(children=b) |
121 self.commit() |
135 cnx.commit() |
122 req = self.request() |
136 cnx.execute('DELETE Folder F WHERE F name "a"') |
123 req.execute('DELETE Folder F WHERE F name "a"') |
137 cnx.execute('DELETE R children F WHERE R name "root"') |
124 req.execute('DELETE R children F WHERE R name "root"') |
138 cnx.commit() |
125 self.commit() |
139 self.assertEqual([['root'], ['c']], |
126 req = self.request() |
140 cnx.execute('Any NF WHERE F is Folder, F name NF').rows) |
127 self.assertEqual([['root'], ['c']], |
141 self.assertEqual([], cnx.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows) |
128 req.execute('Any NF WHERE F is Folder, F name NF').rows) |
|
129 self.assertEqual([], |
|
130 req.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows) |
|
131 |
142 |
132 def test_unsatisfied_constraints(self): |
143 def test_unsatisfied_constraints(self): |
133 releid = self.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0] |
144 with self.admin_access.repo_cnx() as cnx: |
134 with self.assertRaises(ValidationError) as cm: |
145 cnx.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0] |
135 self.commit() |
146 with self.assertRaises(ValidationError) as cm: |
|
147 cnx.commit() |
136 self.assertEqual(cm.exception.errors, |
148 self.assertEqual(cm.exception.errors, |
137 {'in_group-object': u'RQLConstraint NOT O name "owners" failed'}) |
149 {'in_group-object': u'RQLConstraint NOT O name "owners" failed'}) |
138 |
150 |
139 def test_unique_constraint(self): |
151 def test_unique_constraint(self): |
140 req = self.request() |
152 with self.admin_access.repo_cnx() as cnx: |
141 entity = req.create_entity('CWGroup', name=u'trout') |
153 entity = cnx.create_entity('CWGroup', name=u'trout') |
142 self.commit() |
154 cnx.commit() |
143 self.assertRaises(ValidationError, req.create_entity, 'CWGroup', name=u'trout') |
155 self.assertRaises(ValidationError, cnx.create_entity, 'CWGroup', name=u'trout') |
144 self.rollback() |
156 cnx.rollback() |
145 req.execute('SET X name "trout" WHERE X eid %(x)s', {'x': entity.eid}) |
157 cnx.execute('SET X name "trout" WHERE X eid %(x)s', {'x': entity.eid}) |
146 self.commit() |
158 cnx.commit() |
147 |
159 |
148 if __name__ == '__main__': |
160 if __name__ == '__main__': |
149 from logilab.common.testlib import unittest_main |
161 from logilab.common.testlib import unittest_main |
150 unittest_main() |
162 unittest_main() |