Mercurial > libervia-pubsub
comparison idavoll/test/test_storage.py @ 157:21fcfb86433f
Fix tests to work with current Twisted. Really do 0.5.0 release.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Mon, 28 Aug 2006 12:48:45 +0000 |
parents | 5191ba7c4df8 |
children | ef22e4150caa |
comparison
equal
deleted
inserted
replaced
156:6250905b72f6 | 157:21fcfb86433f |
---|---|
1 # Copyright (c) 2003-2006 Ralph Meijer | 1 # Copyright (c) 2003-2006 Ralph Meijer |
2 # See LICENSE for details. | 2 # See LICENSE for details. |
3 | 3 |
4 from twisted.trial import unittest | 4 from twisted.trial import unittest |
5 from twisted.trial.assertions import * | |
6 from twisted.words.protocols.jabber import jid | 5 from twisted.words.protocols.jabber import jid |
7 from twisted.internet import defer | 6 from twisted.internet import defer |
8 from twisted.xish import domish | 7 from twisted.words.xish import domish |
9 | 8 |
10 from idavoll import storage, pubsub | 9 from idavoll import storage, pubsub |
11 | 10 |
12 OWNER = jid.JID('owner@example.com') | 11 OWNER = jid.JID('owner@example.com') |
13 SUBSCRIBER = jid.JID('subscriber@example.com/Home') | 12 SUBSCRIBER = jid.JID('subscriber@example.com/Home') |
47 def testGetNode(self): | 46 def testGetNode(self): |
48 return self.s.get_node('pre-existing') | 47 return self.s.get_node('pre-existing') |
49 | 48 |
50 def testGetNonExistingNode(self): | 49 def testGetNonExistingNode(self): |
51 d = self.s.get_node('non-existing') | 50 d = self.s.get_node('non-existing') |
52 assertFailure(d, storage.NodeNotFound) | 51 self.assertFailure(d, storage.NodeNotFound) |
53 return d | 52 return d |
54 | 53 |
55 def testGetNodeIDs(self): | 54 def testGetNodeIDs(self): |
56 def cb(node_ids): | 55 def cb(node_ids): |
57 assertIn('pre-existing', node_ids) | 56 self.assertIn('pre-existing', node_ids) |
58 assertNotIn('non-existing', node_ids) | 57 self.assertNotIn('non-existing', node_ids) |
59 | 58 |
60 return self.s.get_node_ids().addCallback(cb) | 59 return self.s.get_node_ids().addCallback(cb) |
61 | 60 |
62 def testCreateExistingNode(self): | 61 def testCreateExistingNode(self): |
63 d = self.s.create_node('pre-existing', OWNER) | 62 d = self.s.create_node('pre-existing', OWNER) |
64 assertFailure(d, storage.NodeExists) | 63 self.assertFailure(d, storage.NodeExists) |
65 return d | 64 return d |
66 | 65 |
67 def testCreateNode(self): | 66 def testCreateNode(self): |
68 def cb(void): | 67 def cb(void): |
69 d = self.s.get_node('new 1') | 68 d = self.s.get_node('new 1') |
73 d.addCallback(cb) | 72 d.addCallback(cb) |
74 return d | 73 return d |
75 | 74 |
76 def testDeleteNonExistingNode(self): | 75 def testDeleteNonExistingNode(self): |
77 d = self.s.delete_node('non-existing') | 76 d = self.s.delete_node('non-existing') |
78 assertFailure(d, storage.NodeNotFound) | 77 self.assertFailure(d, storage.NodeNotFound) |
79 return d | 78 return d |
80 | 79 |
81 def testDeleteNode(self): | 80 def testDeleteNode(self): |
82 def cb(void): | 81 def cb(void): |
83 d = self.s.get_node('to-be-deleted') | 82 d = self.s.get_node('to-be-deleted') |
84 assertFailure(d, storage.NodeNotFound) | 83 self.assertFailure(d, storage.NodeNotFound) |
85 return d | 84 return d |
86 | 85 |
87 d = self.s.delete_node('to-be-deleted') | 86 d = self.s.delete_node('to-be-deleted') |
88 d.addCallback(cb) | 87 d.addCallback(cb) |
89 return d | 88 return d |
90 | 89 |
91 def testGetAffiliations(self): | 90 def testGetAffiliations(self): |
92 def cb(affiliations): | 91 def cb(affiliations): |
93 assertIn(('pre-existing', 'owner'), affiliations) | 92 self.assertIn(('pre-existing', 'owner'), affiliations) |
94 | 93 |
95 d = self.s.get_affiliations(OWNER) | 94 d = self.s.get_affiliations(OWNER) |
96 d.addCallback(cb) | 95 d.addCallback(cb) |
97 return d | 96 return d |
98 | 97 |
99 def testGetSubscriptions(self): | 98 def testGetSubscriptions(self): |
100 def cb(subscriptions): | 99 def cb(subscriptions): |
101 assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) | 100 self.assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) |
102 | 101 |
103 d = self.s.get_subscriptions(SUBSCRIBER) | 102 d = self.s.get_subscriptions(SUBSCRIBER) |
104 d.addCallback(cb) | 103 d.addCallback(cb) |
105 return d | 104 return d |
106 | 105 |
107 # Node tests | 106 # Node tests |
108 | 107 |
109 def testGetType(self): | 108 def testGetType(self): |
110 assertEqual(self.node.get_type(), 'leaf') | 109 self.assertEqual(self.node.get_type(), 'leaf') |
111 | 110 |
112 def testGetConfiguration(self): | 111 def testGetConfiguration(self): |
113 config = self.node.get_configuration() | 112 config = self.node.get_configuration() |
114 assertIn('pubsub#persist_items', config.iterkeys()) | 113 self.assertIn('pubsub#persist_items', config.iterkeys()) |
115 assertIn('pubsub#deliver_payloads', config.iterkeys()) | 114 self.assertIn('pubsub#deliver_payloads', config.iterkeys()) |
116 assertEqual(config['pubsub#persist_items'], True) | 115 self.assertEqual(config['pubsub#persist_items'], True) |
117 assertEqual(config['pubsub#deliver_payloads'], True) | 116 self.assertEqual(config['pubsub#deliver_payloads'], True) |
118 | 117 |
119 def testSetConfiguration(self): | 118 def testSetConfiguration(self): |
120 def get_config(node): | 119 def get_config(node): |
121 d = node.set_configuration({'pubsub#persist_items': False}) | 120 d = node.set_configuration({'pubsub#persist_items': False}) |
122 d.addCallback(lambda _: node) | 121 d.addCallback(lambda _: node) |
123 return d | 122 return d |
124 | 123 |
125 def check_object_config(node): | 124 def check_object_config(node): |
126 config = node.get_configuration() | 125 config = node.get_configuration() |
127 assertEqual(config['pubsub#persist_items'], False) | 126 self.assertEqual(config['pubsub#persist_items'], False) |
128 | 127 |
129 def get_node(void): | 128 def get_node(void): |
130 return self.s.get_node('to-be-reconfigured') | 129 return self.s.get_node('to-be-reconfigured') |
131 | 130 |
132 def check_storage_config(node): | 131 def check_storage_config(node): |
133 config = node.get_configuration() | 132 config = node.get_configuration() |
134 assertEqual(config['pubsub#persist_items'], False) | 133 self.assertEqual(config['pubsub#persist_items'], False) |
135 | 134 |
136 d = self.s.get_node('to-be-reconfigured') | 135 d = self.s.get_node('to-be-reconfigured') |
137 d.addCallback(get_config) | 136 d.addCallback(get_config) |
138 d.addCallback(check_object_config) | 137 d.addCallback(check_object_config) |
139 d.addCallback(get_node) | 138 d.addCallback(get_node) |
141 return d | 140 return d |
142 | 141 |
143 def testGetMetaData(self): | 142 def testGetMetaData(self): |
144 meta_data = self.node.get_meta_data() | 143 meta_data = self.node.get_meta_data() |
145 for key, value in self.node.get_configuration().iteritems(): | 144 for key, value in self.node.get_configuration().iteritems(): |
146 assertIn(key, meta_data.iterkeys()) | 145 self.assertIn(key, meta_data.iterkeys()) |
147 assertEqual(value, meta_data[key]) | 146 self.assertEqual(value, meta_data[key]) |
148 assertIn('pubsub#node_type', meta_data.iterkeys()) | 147 self.assertIn('pubsub#node_type', meta_data.iterkeys()) |
149 assertEqual(meta_data['pubsub#node_type'], 'leaf') | 148 self.assertEqual(meta_data['pubsub#node_type'], 'leaf') |
150 | 149 |
151 def testGetAffiliation(self): | 150 def testGetAffiliation(self): |
152 def cb(affiliation): | 151 def cb(affiliation): |
153 assertEqual(affiliation, 'owner') | 152 self.assertEqual(affiliation, 'owner') |
154 | 153 |
155 d = self.node.get_affiliation(OWNER) | 154 d = self.node.get_affiliation(OWNER) |
156 d.addCallback(cb) | 155 d.addCallback(cb) |
157 return d | 156 return d |
158 | 157 |
159 def testGetNonExistingAffiliation(self): | 158 def testGetNonExistingAffiliation(self): |
160 def cb(affiliation): | 159 def cb(affiliation): |
161 assertEqual(affiliation, None) | 160 self.assertEqual(affiliation, None) |
162 | 161 |
163 d = self.node.get_affiliation(SUBSCRIBER) | 162 d = self.node.get_affiliation(SUBSCRIBER) |
164 d.addCallback(cb) | 163 d.addCallback(cb) |
165 return d | 164 return d |
166 | 165 |
167 def testAddSubscription(self): | 166 def testAddSubscription(self): |
168 def cb1(void): | 167 def cb1(void): |
169 return self.node.get_subscription(SUBSCRIBER_NEW) | 168 return self.node.get_subscription(SUBSCRIBER_NEW) |
170 | 169 |
171 def cb2(state): | 170 def cb2(state): |
172 assertEqual(state, 'pending') | 171 self.assertEqual(state, 'pending') |
173 | 172 |
174 d = self.node.add_subscription(SUBSCRIBER_NEW, 'pending') | 173 d = self.node.add_subscription(SUBSCRIBER_NEW, 'pending') |
175 d.addCallback(cb1) | 174 d.addCallback(cb1) |
176 d.addCallback(cb2) | 175 d.addCallback(cb2) |
177 return d | 176 return d |
178 | 177 |
179 def testAddExistingSubscription(self): | 178 def testAddExistingSubscription(self): |
180 d = self.node.add_subscription(SUBSCRIBER, 'pending') | 179 d = self.node.add_subscription(SUBSCRIBER, 'pending') |
181 assertFailure(d, storage.SubscriptionExists) | 180 self.assertFailure(d, storage.SubscriptionExists) |
182 return d | 181 return d |
183 | 182 |
184 def testGetSubscription(self): | 183 def testGetSubscription(self): |
185 def cb(subscriptions): | 184 def cb(subscriptions): |
186 assertEquals(subscriptions[0][1], 'subscribed') | 185 self.assertEquals(subscriptions[0][1], 'subscribed') |
187 assertEquals(subscriptions[1][1], 'pending') | 186 self.assertEquals(subscriptions[1][1], 'pending') |
188 assertEquals(subscriptions[2][1], None) | 187 self.assertEquals(subscriptions[2][1], None) |
189 | 188 |
190 d = defer.DeferredList([self.node.get_subscription(SUBSCRIBER), | 189 d = defer.DeferredList([self.node.get_subscription(SUBSCRIBER), |
191 self.node.get_subscription(SUBSCRIBER_PENDING), | 190 self.node.get_subscription(SUBSCRIBER_PENDING), |
192 self.node.get_subscription(OWNER)]) | 191 self.node.get_subscription(OWNER)]) |
193 d.addCallback(cb) | 192 d.addCallback(cb) |
196 def testRemoveSubscription(self): | 195 def testRemoveSubscription(self): |
197 return self.node.remove_subscription(SUBSCRIBER_TO_BE_DELETED) | 196 return self.node.remove_subscription(SUBSCRIBER_TO_BE_DELETED) |
198 | 197 |
199 def testRemoveNonExistingSubscription(self): | 198 def testRemoveNonExistingSubscription(self): |
200 d = self.node.remove_subscription(OWNER) | 199 d = self.node.remove_subscription(OWNER) |
201 assertFailure(d, storage.SubscriptionNotFound) | 200 self.assertFailure(d, storage.SubscriptionNotFound) |
202 return d | 201 return d |
203 | 202 |
204 def testGetSubscribers(self): | 203 def testGetSubscribers(self): |
205 def cb(subscribers): | 204 def cb(subscribers): |
206 assertIn(SUBSCRIBER, subscribers) | 205 self.assertIn(SUBSCRIBER, subscribers) |
207 assertNotIn(SUBSCRIBER_PENDING, subscribers) | 206 self.assertNotIn(SUBSCRIBER_PENDING, subscribers) |
208 assertNotIn(OWNER, subscribers) | 207 self.assertNotIn(OWNER, subscribers) |
209 | 208 |
210 d = self.node.get_subscribers() | 209 d = self.node.get_subscribers() |
211 d.addCallback(cb) | 210 d.addCallback(cb) |
212 return d | 211 return d |
213 | 212 |
214 def testIsSubscriber(self): | 213 def testIsSubscriber(self): |
215 def cb(subscribed): | 214 def cb(subscribed): |
216 assertEquals(subscribed[0][1], True) | 215 self.assertEquals(subscribed[0][1], True) |
217 assertEquals(subscribed[1][1], True) | 216 self.assertEquals(subscribed[1][1], True) |
218 assertEquals(subscribed[2][1], False) | 217 self.assertEquals(subscribed[2][1], False) |
219 assertEquals(subscribed[3][1], False) | 218 self.assertEquals(subscribed[3][1], False) |
220 | 219 |
221 d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER), | 220 d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER), |
222 self.node.is_subscribed(SUBSCRIBER.userhostJID()), | 221 self.node.is_subscribed(SUBSCRIBER.userhostJID()), |
223 self.node.is_subscribed(SUBSCRIBER_PENDING), | 222 self.node.is_subscribed(SUBSCRIBER_PENDING), |
224 self.node.is_subscribed(OWNER)]) | 223 self.node.is_subscribed(OWNER)]) |
228 def testStoreItems(self): | 227 def testStoreItems(self): |
229 def cb1(void): | 228 def cb1(void): |
230 return self.node.get_items_by_id(['new']) | 229 return self.node.get_items_by_id(['new']) |
231 | 230 |
232 def cb2(result): | 231 def cb2(result): |
233 assertEqual(result[0], decode(ITEM_NEW.toXml())) | 232 self.assertEqual(result[0], decode(ITEM_NEW.toXml())) |
234 | 233 |
235 d = self.node.store_items([ITEM_NEW], PUBLISHER) | 234 d = self.node.store_items([ITEM_NEW], PUBLISHER) |
236 d.addCallback(cb1) | 235 d.addCallback(cb1) |
237 d.addCallback(cb2) | 236 d.addCallback(cb2) |
238 return d | 237 return d |
240 def testStoreUpdatedItems(self): | 239 def testStoreUpdatedItems(self): |
241 def cb1(void): | 240 def cb1(void): |
242 return self.node.get_items_by_id(['current']) | 241 return self.node.get_items_by_id(['current']) |
243 | 242 |
244 def cb2(result): | 243 def cb2(result): |
245 assertEqual(result[0], decode(ITEM_UPDATED.toXml())) | 244 self.assertEqual(result[0], decode(ITEM_UPDATED.toXml())) |
246 | 245 |
247 d = self.node.store_items([ITEM_UPDATED], PUBLISHER) | 246 d = self.node.store_items([ITEM_UPDATED], PUBLISHER) |
248 d.addCallback(cb1) | 247 d.addCallback(cb1) |
249 d.addCallback(cb2) | 248 d.addCallback(cb2) |
250 return d | 249 return d |
251 | 250 |
252 def testRemoveItems(self): | 251 def testRemoveItems(self): |
253 def cb1(result): | 252 def cb1(result): |
254 assertEqual(result, ['to-be-deleted']) | 253 self.assertEqual(result, ['to-be-deleted']) |
255 return self.node.get_items_by_id(['to-be-deleted']) | 254 return self.node.get_items_by_id(['to-be-deleted']) |
256 | 255 |
257 def cb2(result): | 256 def cb2(result): |
258 assertEqual(len(result), 0) | 257 self.assertEqual(len(result), 0) |
259 | 258 |
260 d = self.node.remove_items(['to-be-deleted']) | 259 d = self.node.remove_items(['to-be-deleted']) |
261 d.addCallback(cb1) | 260 d.addCallback(cb1) |
262 d.addCallback(cb2) | 261 d.addCallback(cb2) |
263 return d | 262 return d |
264 | 263 |
265 def testRemoveNonExistingItems(self): | 264 def testRemoveNonExistingItems(self): |
266 def cb(result): | 265 def cb(result): |
267 assertEqual(result, []) | 266 self.assertEqual(result, []) |
268 | 267 |
269 d = self.node.remove_items(['non-existing']) | 268 d = self.node.remove_items(['non-existing']) |
270 d.addCallback(cb) | 269 d.addCallback(cb) |
271 return d | 270 return d |
272 | 271 |
273 def testGetItems(self): | 272 def testGetItems(self): |
274 def cb(result): | 273 def cb(result): |
275 assertIn(decode(ITEM.toXml()), result) | 274 self.assertIn(decode(ITEM.toXml()), result) |
276 | 275 |
277 d = self.node.get_items() | 276 d = self.node.get_items() |
278 d.addCallback(cb) | 277 d.addCallback(cb) |
279 return d | 278 return d |
280 | 279 |
281 def testLastItem(self): | 280 def testLastItem(self): |
282 def cb(result): | 281 def cb(result): |
283 assertEqual([decode(ITEM.toXml())], result) | 282 self.assertEqual([decode(ITEM.toXml())], result) |
284 | 283 |
285 d = self.node.get_items(1) | 284 d = self.node.get_items(1) |
286 d.addCallback(cb) | 285 d.addCallback(cb) |
287 return d | 286 return d |
288 | 287 |
289 def testGetItemsById(self): | 288 def testGetItemsById(self): |
290 def cb(result): | 289 def cb(result): |
291 assertEqual(len(result), 1) | 290 self.assertEqual(len(result), 1) |
292 | 291 |
293 d = self.node.get_items_by_id(['current']) | 292 d = self.node.get_items_by_id(['current']) |
294 d.addCallback(cb) | 293 d.addCallback(cb) |
295 return d | 294 return d |
296 | 295 |
297 def testGetNonExistingItemsById(self): | 296 def testGetNonExistingItemsById(self): |
298 def cb(result): | 297 def cb(result): |
299 assertEqual(len(result), 0) | 298 self.assertEqual(len(result), 0) |
300 | 299 |
301 d = self.node.get_items_by_id(['non-existing']) | 300 d = self.node.get_items_by_id(['non-existing']) |
302 d.addCallback(cb) | 301 d.addCallback(cb) |
303 return d | 302 return d |
304 | 303 |
310 | 309 |
311 def cb2(node): | 310 def cb2(node): |
312 return node.get_items() | 311 return node.get_items() |
313 | 312 |
314 def cb3(result): | 313 def cb3(result): |
315 assertEqual([], result) | 314 self.assertEqual([], result) |
316 | 315 |
317 d = self.s.get_node('to-be-purged') | 316 d = self.s.get_node('to-be-purged') |
318 d.addCallback(cb1) | 317 d.addCallback(cb1) |
319 d.addCallback(cb2) | 318 d.addCallback(cb2) |
320 d.addCallback(cb3) | 319 d.addCallback(cb3) |
324 def cb1(node): | 323 def cb1(node): |
325 return node.get_affiliations() | 324 return node.get_affiliations() |
326 | 325 |
327 def cb2(affiliations): | 326 def cb2(affiliations): |
328 affiliations = dict(((a[0].full(), a[1]) for a in affiliations)) | 327 affiliations = dict(((a[0].full(), a[1]) for a in affiliations)) |
329 assertEquals(affiliations[OWNER.full()], 'owner') | 328 self.assertEquals(affiliations[OWNER.full()], 'owner') |
330 | 329 |
331 d = self.s.get_node('pre-existing') | 330 d = self.s.get_node('pre-existing') |
332 d.addCallback(cb1) | 331 d.addCallback(cb1) |
333 d.addCallback(cb2) | 332 d.addCallback(cb2) |
334 return d | 333 return d |