comparison idavoll/test/test_storage.py @ 131:fe8cb99363ff

Add test cases for LeafNodes.
author Ralph Meijer <ralphm@ik.nu>
date Sun, 24 Apr 2005 17:26:30 +0000
parents 8f99b4f7aea2
children aca904a41a43
comparison
equal deleted inserted replaced
130:3d77f3808bfa 131:fe8cb99363ff
1 from twisted.trial import unittest 1 from twisted.trial import unittest
2 from twisted.trial.assertions import * 2 from twisted.trial.assertions import *
3 from twisted.words.protocols.jabber import jid 3 from twisted.words.protocols.jabber import jid
4 from twisted.internet import defer 4 from twisted.internet import defer
5 5 from twisted.xish import domish
6 from idavoll import storage 6
7 from idavoll import storage, pubsub
7 8
8 OWNER = jid.JID('owner@example.com') 9 OWNER = jid.JID('owner@example.com')
9 SUBSCRIBER = jid.JID('subscriber@example.com/Home') 10 SUBSCRIBER = jid.JID('subscriber@example.com/Home')
10 SUBSCRIBER_NEW = jid.JID('new@example.com/Home') 11 SUBSCRIBER_NEW = jid.JID('new@example.com/Home')
11 SUBSCRIBER_TO_BE_DELETED = jid.JID('to_be_deleted@example.com/Home') 12 SUBSCRIBER_TO_BE_DELETED = jid.JID('to_be_deleted@example.com/Home')
12 SUBSCRIBER_PENDING = jid.JID('pending@example.com/Home') 13 SUBSCRIBER_PENDING = jid.JID('pending@example.com/Home')
14 PUBLISHER = jid.JID('publisher@example.com')
15 ITEM = domish.Element((pubsub.NS_PUBSUB, 'item'), pubsub.NS_PUBSUB)
16 ITEM['id'] = 'current'
17 ITEM.addElement(('testns', 'test'), content=u'Test \u2083 item')
18 ITEM_NEW = domish.Element((pubsub.NS_PUBSUB, 'item'), pubsub.NS_PUBSUB)
19 ITEM_NEW['id'] = 'new'
20 ITEM_NEW.addElement(('testns', 'test'), content=u'Test \u2083 item')
21 ITEM_UPDATED = domish.Element((pubsub.NS_PUBSUB, 'item'), pubsub.NS_PUBSUB)
22 ITEM_UPDATED['id'] = 'current'
23 ITEM_UPDATED.addElement(('testns', 'test'), content=u'Test \u2084 item')
24 ITEM_TO_BE_DELETED = domish.Element((pubsub.NS_PUBSUB, 'item'),
25 pubsub.NS_PUBSUB)
26 ITEM_TO_BE_DELETED['id'] = 'to-be-deleted'
27 ITEM_TO_BE_DELETED.addElement(('testns', 'test'), content=u'Test \u2083 item')
28 ITEM_TO_NOT_BE_DELETED = domish.Element((pubsub.NS_PUBSUB, 'item'),
29 pubsub.NS_PUBSUB)
30 ITEM_TO_NOT_BE_DELETED['id'] = 'to-not-be-deleted'
31 ITEM_TO_NOT_BE_DELETED.addElement(('testns', 'test'),
32 content=u'Test \u2083 item')
13 33
14 class StorageTests: 34 class StorageTests:
15 35
16 def _assignTestNode(self, node): 36 def _assignTestNode(self, node):
17 self.node = node 37 self.node = node
196 216
197 d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER), 217 d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER),
198 self.node.is_subscribed(SUBSCRIBER_PENDING), 218 self.node.is_subscribed(SUBSCRIBER_PENDING),
199 self.node.is_subscribed(OWNER)]) 219 self.node.is_subscribed(OWNER)])
200 d.addCallback(cb) 220 d.addCallback(cb)
221 return d
222
223 def testStoreItems(self):
224 return self.node.store_items([ITEM_NEW], PUBLISHER)
225
226 def testStoreUpdatedItems(self):
227 def cb1(void):
228 return self.node.get_items_by_id(['current'])
229
230 def cb2(result):
231 assertEqual(result[0], unicode(ITEM_UPDATED.toXml(), 'utf-8'))
232
233 d = self.node.store_items([ITEM_UPDATED], PUBLISHER)
234 d.addCallback(cb1)
235 d.addCallback(cb2)
236 return d
237
238 def testRemoveItems(self):
239 def cb1(void):
240 return self.node.get_items_by_id(['to-be-deleted'])
241
242 def cb2(result):
243 assertEqual(len(result), 0)
244
245 d = self.node.remove_items(['to-be-deleted'])
246 d.addCallback(cb1)
247 d.addCallback(cb2)
248 return d
249
250 def testRemoveNonExistingItems(self):
251 d = self.node.remove_items(['to-not-be-deleted', 'non-existing'])
252 assertFailure(d, storage.ItemNotFound)
253 return d
254
255 def testGetItems(self):
256 def cb(result):
257 assertIn(unicode(ITEM.toXml(), 'utf-8'), result)
258
259 d = self.node.get_items()
260 d.addCallback(cb)
261 return d
262
263 def testLastItem(self):
264 def cb(result):
265 assertEqual([unicode(ITEM.toXml(), 'utf-8')], result)
266
267 d = self.node.get_items(1)
268 d.addCallback(cb)
269 return d
270
271 def testGetItemsById(self):
272 def cb(result):
273 assertEqual(len(result), 1)
274
275 d = self.node.get_items_by_id(['current'])
276 d.addCallback(cb)
277 return d
278
279 def testGetNonExistingItemsById(self):
280 def cb(result):
281 assertEqual(len(result), 0)
282
283 d = self.node.get_items_by_id(['non-existing'])
284 d.addCallback(cb)
285 return d
286
287 def testPurge(self):
288 def cb1(node):
289 d = node.purge()
290 d.addCallback(lambda _: node)
291 return d
292
293 def cb2(node):
294 return node.get_items()
295
296 def cb3(result):
297 assertEqual([], result)
298
299 d = self.s.get_node('to-be-purged')
300 d.addCallback(cb1)
301 d.addCallback(cb2)
302 d.addCallback(cb3)
201 return d 303 return d
202 304
203 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): 305 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests):
204 306
205 def setUpClass(self): 307 def setUpClass(self):
210 LeafNode('pre-existing', OWNER, default_config) 312 LeafNode('pre-existing', OWNER, default_config)
211 self.s._nodes['to-be-deleted'] = \ 313 self.s._nodes['to-be-deleted'] = \
212 LeafNode('to-be-deleted', OWNER, None) 314 LeafNode('to-be-deleted', OWNER, None)
213 self.s._nodes['to-be-reconfigured'] = \ 315 self.s._nodes['to-be-reconfigured'] = \
214 LeafNode('to-be-reconfigured', OWNER, default_config) 316 LeafNode('to-be-reconfigured', OWNER, default_config)
317 self.s._nodes['to-be-purged'] = \
318 LeafNode('to-be-purged', OWNER, None)
215 319
216 subscriptions = self.s._nodes['pre-existing']._subscriptions 320 subscriptions = self.s._nodes['pre-existing']._subscriptions
217 subscriptions[SUBSCRIBER.full()] = Subscription('subscribed') 321 subscriptions[SUBSCRIBER.full()] = Subscription('subscribed')
218 subscriptions[SUBSCRIBER_TO_BE_DELETED.full()] = \ 322 subscriptions[SUBSCRIBER_TO_BE_DELETED.full()] = \
219 Subscription('subscribed') 323 Subscription('subscribed')
220 subscriptions[SUBSCRIBER_PENDING.full()] = \ 324 subscriptions[SUBSCRIBER_PENDING.full()] = \
221 Subscription('pending') 325 Subscription('pending')
222 326
327 item = (ITEM_TO_BE_DELETED.toXml(), PUBLISHER)
328 self.s._nodes['pre-existing']._items['to-be-deleted'] = item
329 self.s._nodes['pre-existing']._itemlist.append(item)
330 self.s._nodes['pre-existing']._items['to-not-be-deleted'] = item
331 self.s._nodes['pre-existing']._itemlist.append(item)
332 self.s._nodes['to-be-purged']._items['to-be-deleted'] = item
333 self.s._nodes['to-be-purged']._itemlist.append(item)
334 item = (ITEM.toXml(), PUBLISHER)
335 self.s._nodes['pre-existing']._items['current'] = item
336 self.s._nodes['pre-existing']._itemlist.append(item)
337
223 return StorageTests.setUpClass(self) 338 return StorageTests.setUpClass(self)
224 339
225 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): 340 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests):
226 def _callSuperSetUpClass(self, void): 341 def _callSuperSetUpClass(self, void):
227 return StorageTests.setUpClass(self) 342 return StorageTests.setUpClass(self)
233 d = self.s._dbpool.runInteraction(self.init) 348 d = self.s._dbpool.runInteraction(self.init)
234 d.addCallback(self._callSuperSetUpClass) 349 d.addCallback(self._callSuperSetUpClass)
235 return d 350 return d
236 351
237 def tearDownClass(self): 352 def tearDownClass(self):
238 # return self.s._dbpool.runInteraction(self.cleandb) 353 #return self.s._dbpool.runInteraction(self.cleandb)
239 pass 354 pass
240 355
241 def init(self, cursor): 356 def init(self, cursor):
242 self.cleandb(cursor) 357 self.cleandb(cursor)
243 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""") 358 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""")
244 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""") 359 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""")
245 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-reconfigured')""") 360 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-reconfigured')""")
361 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-purged')""")
246 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", 362 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
247 OWNER.userhost().encode('utf-8')) 363 OWNER.userhost().encode('utf-8'))
248 cursor.execute("""INSERT INTO affiliations 364 cursor.execute("""INSERT INTO affiliations
249 (node_id, entity_id, affiliation) 365 (node_id, entity_id, affiliation)
250 SELECT nodes.id, entities.id, 'owner' 366 SELECT nodes.id, entities.id, 'owner'
276 SELECT nodes.id, entities.id, %s, 'pending' 392 SELECT nodes.id, entities.id, %s, 'pending'
277 FROM nodes, entities 393 FROM nodes, entities
278 WHERE node='pre-existing' AND jid=%s""", 394 WHERE node='pre-existing' AND jid=%s""",
279 (SUBSCRIBER_PENDING.resource.encode('utf-8'), 395 (SUBSCRIBER_PENDING.resource.encode('utf-8'),
280 SUBSCRIBER_PENDING.userhost().encode('utf-8'))) 396 SUBSCRIBER_PENDING.userhost().encode('utf-8')))
397 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
398 PUBLISHER.userhost().encode('utf-8'))
399 cursor.execute("""INSERT INTO items
400 (node_id, publisher, item, data, date)
401 SELECT nodes.id, %s, 'to-be-deleted', %s,
402 now() - interval '1 day'
403 FROM nodes
404 WHERE node='pre-existing'""",
405 (PUBLISHER.userhost().encode('utf-8'),
406 ITEM_TO_BE_DELETED.toXml()))
407 cursor.execute("""INSERT INTO items
408 (node_id, publisher, item, data, date)
409 SELECT nodes.id, %s, 'to-not-be-deleted', %s,
410 now() - interval '1 day'
411 FROM nodes
412 WHERE node='pre-existing'""",
413 (PUBLISHER.userhost().encode('utf-8'),
414 ITEM_TO_NOT_BE_DELETED.toXml()))
415 cursor.execute("""INSERT INTO items (node_id, publisher, item, data)
416 SELECT nodes.id, %s, 'to-be-deleted', %s
417 FROM nodes
418 WHERE node='to-be-purged'""",
419 (PUBLISHER.userhost().encode('utf-8'),
420 ITEM_TO_BE_DELETED.toXml()))
421 cursor.execute("""INSERT INTO items (node_id, publisher, item, data)
422 SELECT nodes.id, %s, 'current', %s
423 FROM nodes
424 WHERE node='pre-existing'""",
425 (PUBLISHER.userhost().encode('utf-8'),
426 ITEM.toXml()))
281 427
282 def cleandb(self, cursor): 428 def cleandb(self, cursor):
283 cursor.execute("""DELETE FROM nodes WHERE node in 429 cursor.execute("""DELETE FROM nodes WHERE node in
284 ('non-existing', 'pre-existing', 'to-be-deleted', 430 ('non-existing', 'pre-existing', 'to-be-deleted',
285 'new 1', 'new 2', 'new 3', 'to-be-reconfigured')""") 431 'new 1', 'new 2', 'new 3', 'to-be-reconfigured',
432 'to-be-purged')""")
286 cursor.execute("""DELETE FROM entities WHERE jid=%s""", 433 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
287 OWNER.userhost().encode('utf-8')) 434 OWNER.userhost().encode('utf-8'))
288 cursor.execute("""DELETE FROM entities WHERE jid=%s""", 435 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
289 SUBSCRIBER.userhost().encode('utf-8')) 436 SUBSCRIBER.userhost().encode('utf-8'))
290 cursor.execute("""DELETE FROM entities WHERE jid=%s""", 437 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
291 SUBSCRIBER_TO_BE_DELETED.userhost().encode('utf-8')) 438 SUBSCRIBER_TO_BE_DELETED.userhost().encode('utf-8'))
292 cursor.execute("""DELETE FROM entities WHERE jid=%s""", 439 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
293 SUBSCRIBER_PENDING.userhost().encode('utf-8')) 440 SUBSCRIBER_PENDING.userhost().encode('utf-8'))
441 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
442 PUBLISHER.userhost().encode('utf-8'))