comparison idavoll/test/test_storage.py @ 198:e404775b12df

Change naming and spacing conventions to match Twisted's.
author Ralph Meijer <ralphm@ik.nu>
date Tue, 10 Jun 2008 11:31:49 +0000
parents bc269696ef42
children 77c61e2b8c75
comparison
equal deleted inserted replaced
197:9da5a95d408d 198:e404775b12df
38 if isinstance(object, str): 38 if isinstance(object, str):
39 object = object.decode('utf-8') 39 object = object.decode('utf-8')
40 return object 40 return object
41 41
42 42
43
43 class StorageTests: 44 class StorageTests:
44 45
45 def _assignTestNode(self, node): 46 def _assignTestNode(self, node):
46 self.node = node 47 self.node = node
47 48
49
48 def setUp(self): 50 def setUp(self):
49 d = self.s.get_node('pre-existing') 51 d = self.s.getNode('pre-existing')
50 d.addCallback(self._assignTestNode) 52 d.addCallback(self._assignTestNode)
51 return d 53 return d
52 54
53 def testGetNode(self): 55
54 return self.s.get_node('pre-existing') 56 def test_getNode(self):
55 57 return self.s.getNode('pre-existing')
56 def testGetNonExistingNode(self): 58
57 d = self.s.get_node('non-existing') 59
60 def test_getNonExistingNode(self):
61 d = self.s.getNode('non-existing')
58 self.assertFailure(d, error.NodeNotFound) 62 self.assertFailure(d, error.NodeNotFound)
59 return d 63 return d
60 64
61 def testGetNodeIDs(self): 65
62 def cb(node_ids): 66 def test_getNodeIDs(self):
63 self.assertIn('pre-existing', node_ids) 67 def cb(nodeIdentifiers):
64 self.assertNotIn('non-existing', node_ids) 68 self.assertIn('pre-existing', nodeIdentifiers)
65 69 self.assertNotIn('non-existing', nodeIdentifiers)
66 return self.s.get_node_ids().addCallback(cb) 70
67 71 return self.s.getNodeIds().addCallback(cb)
68 def testCreateExistingNode(self): 72
69 d = self.s.create_node('pre-existing', OWNER) 73
74 def test_createExistingNode(self):
75 d = self.s.createNode('pre-existing', OWNER)
70 self.assertFailure(d, error.NodeExists) 76 self.assertFailure(d, error.NodeExists)
71 return d 77 return d
72 78
73 def testCreateNode(self): 79
80 def test_createNode(self):
74 def cb(void): 81 def cb(void):
75 d = self.s.get_node('new 1') 82 d = self.s.getNode('new 1')
76 return d 83 return d
77 84
78 d = self.s.create_node('new 1', OWNER) 85 d = self.s.createNode('new 1', OWNER)
79 d.addCallback(cb) 86 d.addCallback(cb)
80 return d 87 return d
81 88
82 def testDeleteNonExistingNode(self): 89
83 d = self.s.delete_node('non-existing') 90 def test_deleteNonExistingNode(self):
91 d = self.s.deleteNode('non-existing')
84 self.assertFailure(d, error.NodeNotFound) 92 self.assertFailure(d, error.NodeNotFound)
85 return d 93 return d
86 94
87 def testDeleteNode(self): 95
96 def test_deleteNode(self):
88 def cb(void): 97 def cb(void):
89 d = self.s.get_node('to-be-deleted') 98 d = self.s.getNode('to-be-deleted')
90 self.assertFailure(d, error.NodeNotFound) 99 self.assertFailure(d, error.NodeNotFound)
91 return d 100 return d
92 101
93 d = self.s.delete_node('to-be-deleted') 102 d = self.s.deleteNode('to-be-deleted')
94 d.addCallback(cb) 103 d.addCallback(cb)
95 return d 104 return d
96 105
97 def testGetAffiliations(self): 106
107 def test_getAffiliations(self):
98 def cb(affiliations): 108 def cb(affiliations):
99 self.assertIn(('pre-existing', 'owner'), affiliations) 109 self.assertIn(('pre-existing', 'owner'), affiliations)
100 110
101 d = self.s.get_affiliations(OWNER) 111 d = self.s.getAffiliations(OWNER)
102 d.addCallback(cb) 112 d.addCallback(cb)
103 return d 113 return d
104 114
105 def testGetSubscriptions(self): 115
116 def test_getSubscriptions(self):
106 def cb(subscriptions): 117 def cb(subscriptions):
107 self.assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) 118 self.assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions)
108 119
109 d = self.s.get_subscriptions(SUBSCRIBER) 120 d = self.s.getSubscriptions(SUBSCRIBER)
110 d.addCallback(cb) 121 d.addCallback(cb)
111 return d 122 return d
123
112 124
113 # Node tests 125 # Node tests
114 126
115 def testGetType(self): 127 def test_getType(self):
116 self.assertEqual(self.node.get_type(), 'leaf') 128 self.assertEqual(self.node.getType(), 'leaf')
117 129
118 def testGetConfiguration(self): 130
119 config = self.node.get_configuration() 131 def test_getConfiguration(self):
132 config = self.node.getConfiguration()
120 self.assertIn('pubsub#persist_items', config.iterkeys()) 133 self.assertIn('pubsub#persist_items', config.iterkeys())
121 self.assertIn('pubsub#deliver_payloads', config.iterkeys()) 134 self.assertIn('pubsub#deliver_payloads', config.iterkeys())
122 self.assertEqual(config['pubsub#persist_items'], True) 135 self.assertEqual(config['pubsub#persist_items'], True)
123 self.assertEqual(config['pubsub#deliver_payloads'], True) 136 self.assertEqual(config['pubsub#deliver_payloads'], True)
124 137
125 def testSetConfiguration(self): 138
126 def get_config(node): 139 def test_setConfiguration(self):
127 d = node.set_configuration({'pubsub#persist_items': False}) 140 def getConfig(node):
141 d = node.setConfiguration({'pubsub#persist_items': False})
128 d.addCallback(lambda _: node) 142 d.addCallback(lambda _: node)
129 return d 143 return d
130 144
131 def check_object_config(node): 145 def checkObjectConfig(node):
132 config = node.get_configuration() 146 config = node.getConfiguration()
133 self.assertEqual(config['pubsub#persist_items'], False) 147 self.assertEqual(config['pubsub#persist_items'], False)
134 148
135 def get_node(void): 149 def getNode(void):
136 return self.s.get_node('to-be-reconfigured') 150 return self.s.getNode('to-be-reconfigured')
137 151
138 def check_storage_config(node): 152 def checkStorageConfig(node):
139 config = node.get_configuration() 153 config = node.getConfiguration()
140 self.assertEqual(config['pubsub#persist_items'], False) 154 self.assertEqual(config['pubsub#persist_items'], False)
141 155
142 d = self.s.get_node('to-be-reconfigured') 156 d = self.s.getNode('to-be-reconfigured')
143 d.addCallback(get_config) 157 d.addCallback(getConfig)
144 d.addCallback(check_object_config) 158 d.addCallback(checkObjectConfig)
145 d.addCallback(get_node) 159 d.addCallback(getNode)
146 d.addCallback(check_storage_config) 160 d.addCallback(checkStorageConfig)
147 return d 161 return d
148 162
149 def testGetMetaData(self): 163
150 meta_data = self.node.get_meta_data() 164 def test_getMetaData(self):
151 for key, value in self.node.get_configuration().iteritems(): 165 metaData = self.node.getMetaData()
152 self.assertIn(key, meta_data.iterkeys()) 166 for key, value in self.node.getConfiguration().iteritems():
153 self.assertEqual(value, meta_data[key]) 167 self.assertIn(key, metaData.iterkeys())
154 self.assertIn('pubsub#node_type', meta_data.iterkeys()) 168 self.assertEqual(value, metaData[key])
155 self.assertEqual(meta_data['pubsub#node_type'], 'leaf') 169 self.assertIn('pubsub#node_type', metaData.iterkeys())
156 170 self.assertEqual(metaData['pubsub#node_type'], 'leaf')
157 def testGetAffiliation(self): 171
172
173 def test_getAffiliation(self):
158 def cb(affiliation): 174 def cb(affiliation):
159 self.assertEqual(affiliation, 'owner') 175 self.assertEqual(affiliation, 'owner')
160 176
161 d = self.node.get_affiliation(OWNER) 177 d = self.node.getAffiliation(OWNER)
162 d.addCallback(cb) 178 d.addCallback(cb)
163 return d 179 return d
164 180
165 def testGetNonExistingAffiliation(self): 181
182 def test_getNonExistingAffiliation(self):
166 def cb(affiliation): 183 def cb(affiliation):
167 self.assertEqual(affiliation, None) 184 self.assertEqual(affiliation, None)
168 185
169 d = self.node.get_affiliation(SUBSCRIBER) 186 d = self.node.getAffiliation(SUBSCRIBER)
170 d.addCallback(cb) 187 d.addCallback(cb)
171 return d 188 return d
172 189
173 def testAddSubscription(self): 190
191 def test_addSubscription(self):
174 def cb1(void): 192 def cb1(void):
175 return self.node.get_subscription(SUBSCRIBER_NEW) 193 return self.node.getSubscription(SUBSCRIBER_NEW)
176 194
177 def cb2(state): 195 def cb2(state):
178 self.assertEqual(state, 'pending') 196 self.assertEqual(state, 'pending')
179 197
180 d = self.node.add_subscription(SUBSCRIBER_NEW, 'pending') 198 d = self.node.addSubscription(SUBSCRIBER_NEW, 'pending')
181 d.addCallback(cb1) 199 d.addCallback(cb1)
182 d.addCallback(cb2) 200 d.addCallback(cb2)
183 return d 201 return d
184 202
185 def testAddExistingSubscription(self): 203
186 d = self.node.add_subscription(SUBSCRIBER, 'pending') 204 def test_addExistingSubscription(self):
205 d = self.node.addSubscription(SUBSCRIBER, 'pending')
187 self.assertFailure(d, error.SubscriptionExists) 206 self.assertFailure(d, error.SubscriptionExists)
188 return d 207 return d
189 208
190 def testGetSubscription(self): 209
210 def test_getSubscription(self):
191 def cb(subscriptions): 211 def cb(subscriptions):
192 self.assertEquals(subscriptions[0][1], 'subscribed') 212 self.assertEquals(subscriptions[0][1], 'subscribed')
193 self.assertEquals(subscriptions[1][1], 'pending') 213 self.assertEquals(subscriptions[1][1], 'pending')
194 self.assertEquals(subscriptions[2][1], None) 214 self.assertEquals(subscriptions[2][1], None)
195 215
196 d = defer.DeferredList([self.node.get_subscription(SUBSCRIBER), 216 d = defer.DeferredList([self.node.getSubscription(SUBSCRIBER),
197 self.node.get_subscription(SUBSCRIBER_PENDING), 217 self.node.getSubscription(SUBSCRIBER_PENDING),
198 self.node.get_subscription(OWNER)]) 218 self.node.getSubscription(OWNER)])
199 d.addCallback(cb) 219 d.addCallback(cb)
200 return d 220 return d
201 221
202 def testRemoveSubscription(self): 222
203 return self.node.remove_subscription(SUBSCRIBER_TO_BE_DELETED) 223 def test_removeSubscription(self):
204 224 return self.node.removeSubscription(SUBSCRIBER_TO_BE_DELETED)
205 def testRemoveNonExistingSubscription(self): 225
206 d = self.node.remove_subscription(OWNER) 226
227 def test_removeNonExistingSubscription(self):
228 d = self.node.removeSubscription(OWNER)
207 self.assertFailure(d, error.NotSubscribed) 229 self.assertFailure(d, error.NotSubscribed)
208 return d 230 return d
209 231
210 def testGetSubscribers(self): 232
233 def test_getSubscribers(self):
211 def cb(subscribers): 234 def cb(subscribers):
212 self.assertIn(SUBSCRIBER, subscribers) 235 self.assertIn(SUBSCRIBER, subscribers)
213 self.assertNotIn(SUBSCRIBER_PENDING, subscribers) 236 self.assertNotIn(SUBSCRIBER_PENDING, subscribers)
214 self.assertNotIn(OWNER, subscribers) 237 self.assertNotIn(OWNER, subscribers)
215 238
216 d = self.node.get_subscribers() 239 d = self.node.getSubscribers()
217 d.addCallback(cb) 240 d.addCallback(cb)
218 return d 241 return d
219 242
220 def testIsSubscriber(self): 243
244 def test_isSubscriber(self):
221 def cb(subscribed): 245 def cb(subscribed):
222 self.assertEquals(subscribed[0][1], True) 246 self.assertEquals(subscribed[0][1], True)
223 self.assertEquals(subscribed[1][1], True) 247 self.assertEquals(subscribed[1][1], True)
224 self.assertEquals(subscribed[2][1], False) 248 self.assertEquals(subscribed[2][1], False)
225 self.assertEquals(subscribed[3][1], False) 249 self.assertEquals(subscribed[3][1], False)
226 250
227 d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER), 251 d = defer.DeferredList([self.node.isSubscribed(SUBSCRIBER),
228 self.node.is_subscribed(SUBSCRIBER.userhostJID()), 252 self.node.isSubscribed(SUBSCRIBER.userhostJID()),
229 self.node.is_subscribed(SUBSCRIBER_PENDING), 253 self.node.isSubscribed(SUBSCRIBER_PENDING),
230 self.node.is_subscribed(OWNER)]) 254 self.node.isSubscribed(OWNER)])
231 d.addCallback(cb) 255 d.addCallback(cb)
232 return d 256 return d
233 257
234 def testStoreItems(self): 258
259 def test_storeItems(self):
235 def cb1(void): 260 def cb1(void):
236 return self.node.get_items_by_id(['new']) 261 return self.node.getItemsById(['new'])
237 262
238 def cb2(result): 263 def cb2(result):
239 self.assertEqual(result[0], decode(ITEM_NEW.toXml())) 264 self.assertEqual(result[0], decode(ITEM_NEW.toXml()))
240 265
241 d = self.node.store_items([ITEM_NEW], PUBLISHER) 266 d = self.node.storeItems([ITEM_NEW], PUBLISHER)
242 d.addCallback(cb1) 267 d.addCallback(cb1)
243 d.addCallback(cb2) 268 d.addCallback(cb2)
244 return d 269 return d
245 270
246 def testStoreUpdatedItems(self): 271
272 def test_storeUpdatedItems(self):
247 def cb1(void): 273 def cb1(void):
248 return self.node.get_items_by_id(['current']) 274 return self.node.getItemsById(['current'])
249 275
250 def cb2(result): 276 def cb2(result):
251 self.assertEqual(result[0], decode(ITEM_UPDATED.toXml())) 277 self.assertEqual(result[0], decode(ITEM_UPDATED.toXml()))
252 278
253 d = self.node.store_items([ITEM_UPDATED], PUBLISHER) 279 d = self.node.storeItems([ITEM_UPDATED], PUBLISHER)
254 d.addCallback(cb1) 280 d.addCallback(cb1)
255 d.addCallback(cb2) 281 d.addCallback(cb2)
256 return d 282 return d
257 283
258 def testRemoveItems(self): 284
285 def test_removeItems(self):
259 def cb1(result): 286 def cb1(result):
260 self.assertEqual(result, ['to-be-deleted']) 287 self.assertEqual(result, ['to-be-deleted'])
261 return self.node.get_items_by_id(['to-be-deleted']) 288 return self.node.getItemsById(['to-be-deleted'])
262 289
263 def cb2(result): 290 def cb2(result):
264 self.assertEqual(len(result), 0) 291 self.assertEqual(len(result), 0)
265 292
266 d = self.node.remove_items(['to-be-deleted']) 293 d = self.node.removeItems(['to-be-deleted'])
267 d.addCallback(cb1) 294 d.addCallback(cb1)
268 d.addCallback(cb2) 295 d.addCallback(cb2)
269 return d 296 return d
270 297
271 def testRemoveNonExistingItems(self): 298
299 def test_removeNonExistingItems(self):
272 def cb(result): 300 def cb(result):
273 self.assertEqual(result, []) 301 self.assertEqual(result, [])
274 302
275 d = self.node.remove_items(['non-existing']) 303 d = self.node.removeItems(['non-existing'])
276 d.addCallback(cb) 304 d.addCallback(cb)
277 return d 305 return d
278 306
279 def testGetItems(self): 307
308 def test_getItems(self):
280 def cb(result): 309 def cb(result):
281 self.assertIn(decode(ITEM.toXml()), result) 310 self.assertIn(decode(ITEM.toXml()), result)
282 311
283 d = self.node.get_items() 312 d = self.node.getItems()
284 d.addCallback(cb) 313 d.addCallback(cb)
285 return d 314 return d
286 315
287 def testLastItem(self): 316
317 def test_lastItem(self):
288 def cb(result): 318 def cb(result):
289 self.assertEqual([decode(ITEM.toXml())], result) 319 self.assertEqual([decode(ITEM.toXml())], result)
290 320
291 d = self.node.get_items(1) 321 d = self.node.getItems(1)
292 d.addCallback(cb) 322 d.addCallback(cb)
293 return d 323 return d
294 324
295 def testGetItemsById(self): 325
326 def test_getItemsById(self):
296 def cb(result): 327 def cb(result):
297 self.assertEqual(len(result), 1) 328 self.assertEqual(len(result), 1)
298 329
299 d = self.node.get_items_by_id(['current']) 330 d = self.node.getItemsById(['current'])
300 d.addCallback(cb) 331 d.addCallback(cb)
301 return d 332 return d
302 333
303 def testGetNonExistingItemsById(self): 334
335 def test_getNonExistingItemsById(self):
304 def cb(result): 336 def cb(result):
305 self.assertEqual(len(result), 0) 337 self.assertEqual(len(result), 0)
306 338
307 d = self.node.get_items_by_id(['non-existing']) 339 d = self.node.getItemsById(['non-existing'])
308 d.addCallback(cb) 340 d.addCallback(cb)
309 return d 341 return d
310 342
311 def testPurge(self): 343
344 def test_purge(self):
312 def cb1(node): 345 def cb1(node):
313 d = node.purge() 346 d = node.purge()
314 d.addCallback(lambda _: node) 347 d.addCallback(lambda _: node)
315 return d 348 return d
316 349
317 def cb2(node): 350 def cb2(node):
318 return node.get_items() 351 return node.getItems()
319 352
320 def cb3(result): 353 def cb3(result):
321 self.assertEqual([], result) 354 self.assertEqual([], result)
322 355
323 d = self.s.get_node('to-be-purged') 356 d = self.s.getNode('to-be-purged')
324 d.addCallback(cb1) 357 d.addCallback(cb1)
325 d.addCallback(cb2) 358 d.addCallback(cb2)
326 d.addCallback(cb3) 359 d.addCallback(cb3)
327 return d 360 return d
328 361
329 def testGetNodeAffilatiations(self): 362
363 def test_getNodeAffilatiations(self):
330 def cb1(node): 364 def cb1(node):
331 return node.get_affiliations() 365 return node.getAffiliations()
332 366
333 def cb2(affiliations): 367 def cb2(affiliations):
334 affiliations = dict(((a[0].full(), a[1]) for a in affiliations)) 368 affiliations = dict(((a[0].full(), a[1]) for a in affiliations))
335 self.assertEquals(affiliations[OWNER.full()], 'owner') 369 self.assertEquals(affiliations[OWNER.full()], 'owner')
336 370
337 d = self.s.get_node('pre-existing') 371 d = self.s.getNode('pre-existing')
338 d.addCallback(cb1) 372 d.addCallback(cb1)
339 d.addCallback(cb2) 373 d.addCallback(cb2)
340 return d 374 return d
341 375
342 376
377
343 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): 378 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests):
344 379
345 def setUp(self): 380 def setUp(self):
346 from idavoll.memory_storage import Storage, LeafNode, Subscription, \ 381 from idavoll.memory_storage import Storage, LeafNode, Subscription, \
347 default_config 382 defaultConfig
348 self.s = Storage() 383 self.s = Storage()
349 self.s._nodes['pre-existing'] = \ 384 self.s._nodes['pre-existing'] = \
350 LeafNode('pre-existing', OWNER, default_config) 385 LeafNode('pre-existing', OWNER, defaultConfig)
351 self.s._nodes['to-be-deleted'] = \ 386 self.s._nodes['to-be-deleted'] = \
352 LeafNode('to-be-deleted', OWNER, None) 387 LeafNode('to-be-deleted', OWNER, None)
353 self.s._nodes['to-be-reconfigured'] = \ 388 self.s._nodes['to-be-reconfigured'] = \
354 LeafNode('to-be-reconfigured', OWNER, default_config) 389 LeafNode('to-be-reconfigured', OWNER, defaultConfig)
355 self.s._nodes['to-be-purged'] = \ 390 self.s._nodes['to-be-purged'] = \
356 LeafNode('to-be-purged', OWNER, None) 391 LeafNode('to-be-purged', OWNER, None)
357 392
358 subscriptions = self.s._nodes['pre-existing']._subscriptions 393 subscriptions = self.s._nodes['pre-existing']._subscriptions
359 subscriptions[SUBSCRIBER.full()] = Subscription('subscribed') 394 subscriptions[SUBSCRIBER.full()] = Subscription('subscribed')
372 self.s._nodes['pre-existing']._itemlist.append(item) 407 self.s._nodes['pre-existing']._itemlist.append(item)
373 408
374 return StorageTests.setUp(self) 409 return StorageTests.setUp(self)
375 410
376 411
412
377 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): 413 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests):
378 def _callSuperSetUp(self, void): 414 def _callSuperSetUp(self, void):
379 return StorageTests.setUp(self) 415 return StorageTests.setUp(self)
416
380 417
381 def setUp(self): 418 def setUp(self):
382 from idavoll.pgsql_storage import Storage 419 from idavoll.pgsql_storage import Storage
383 self.s = Storage('ralphm', 'pubsub_test') 420 self.s = Storage('ralphm', 'pubsub_test')
384 self.s._dbpool.start() 421 self.s._dbpool.start()
385 d = self.s._dbpool.runInteraction(self.init) 422 d = self.s._dbpool.runInteraction(self.init)
386 d.addCallback(self._callSuperSetUp) 423 d.addCallback(self._callSuperSetUp)
387 return d 424 return d
388 425
426
389 def tearDownClass(self): 427 def tearDownClass(self):
390 #return self.s._dbpool.runInteraction(self.cleandb) 428 #return self.s._dbpool.runInteraction(self.cleandb)
391 pass 429 pass
430
392 431
393 def init(self, cursor): 432 def init(self, cursor):
394 self.cleandb(cursor) 433 self.cleandb(cursor)
395 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""") 434 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""")
396 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""") 435 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""")
452 FROM nodes 491 FROM nodes
453 WHERE node='pre-existing'""", 492 WHERE node='pre-existing'""",
454 (PUBLISHER.userhost(), 493 (PUBLISHER.userhost(),
455 ITEM.toXml())) 494 ITEM.toXml()))
456 495
496
457 def cleandb(self, cursor): 497 def cleandb(self, cursor):
458 cursor.execute("""DELETE FROM nodes WHERE node in 498 cursor.execute("""DELETE FROM nodes WHERE node in
459 ('non-existing', 'pre-existing', 'to-be-deleted', 499 ('non-existing', 'pre-existing', 'to-be-deleted',
460 'new 1', 'new 2', 'new 3', 'to-be-reconfigured', 500 'new 1', 'new 2', 'new 3', 'to-be-reconfigured',
461 'to-be-purged')""") 501 'to-be-purged')""")