Mercurial > libervia-pubsub
comparison idavoll/test/test_storage.py @ 167:ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Author: ralphm
Fixes: #4
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Wed, 03 Oct 2007 12:41:43 +0000 |
parents | 21fcfb86433f |
children | bc269696ef42 |
comparison
equal
deleted
inserted
replaced
166:5abb017bd687 | 167:ef22e4150caa |
---|---|
1 # Copyright (c) 2003-2006 Ralph Meijer | 1 # Copyright (c) 2003-2007 Ralph Meijer |
2 # See LICENSE for details. | 2 # See LICENSE for details. |
3 | |
4 """ | |
5 Tests for L{idavoll.memory_storage} and L{idavoll.pgsql_storage}. | |
6 """ | |
3 | 7 |
4 from twisted.trial import unittest | 8 from twisted.trial import unittest |
5 from twisted.words.protocols.jabber import jid | 9 from twisted.words.protocols.jabber import jid |
6 from twisted.internet import defer | 10 from twisted.internet import defer |
7 from twisted.words.xish import domish | 11 from twisted.words.xish import domish |
8 | 12 |
9 from idavoll import storage, pubsub | 13 from wokkel import pubsub |
14 | |
15 from idavoll import error | |
10 | 16 |
11 OWNER = jid.JID('owner@example.com') | 17 OWNER = jid.JID('owner@example.com') |
12 SUBSCRIBER = jid.JID('subscriber@example.com/Home') | 18 SUBSCRIBER = jid.JID('subscriber@example.com/Home') |
13 SUBSCRIBER_NEW = jid.JID('new@example.com/Home') | 19 SUBSCRIBER_NEW = jid.JID('new@example.com/Home') |
14 SUBSCRIBER_TO_BE_DELETED = jid.JID('to_be_deleted@example.com/Home') | 20 SUBSCRIBER_TO_BE_DELETED = jid.JID('to_be_deleted@example.com/Home') |
36 class StorageTests: | 42 class StorageTests: |
37 | 43 |
38 def _assignTestNode(self, node): | 44 def _assignTestNode(self, node): |
39 self.node = node | 45 self.node = node |
40 | 46 |
41 def setUpClass(self): | 47 def setUp(self): |
42 d = self.s.get_node('pre-existing') | 48 d = self.s.get_node('pre-existing') |
43 d.addCallback(self._assignTestNode) | 49 d.addCallback(self._assignTestNode) |
44 return d | 50 return d |
45 | 51 |
46 def testGetNode(self): | 52 def testGetNode(self): |
47 return self.s.get_node('pre-existing') | 53 return self.s.get_node('pre-existing') |
48 | 54 |
49 def testGetNonExistingNode(self): | 55 def testGetNonExistingNode(self): |
50 d = self.s.get_node('non-existing') | 56 d = self.s.get_node('non-existing') |
51 self.assertFailure(d, storage.NodeNotFound) | 57 self.assertFailure(d, error.NodeNotFound) |
52 return d | 58 return d |
53 | 59 |
54 def testGetNodeIDs(self): | 60 def testGetNodeIDs(self): |
55 def cb(node_ids): | 61 def cb(node_ids): |
56 self.assertIn('pre-existing', node_ids) | 62 self.assertIn('pre-existing', node_ids) |
58 | 64 |
59 return self.s.get_node_ids().addCallback(cb) | 65 return self.s.get_node_ids().addCallback(cb) |
60 | 66 |
61 def testCreateExistingNode(self): | 67 def testCreateExistingNode(self): |
62 d = self.s.create_node('pre-existing', OWNER) | 68 d = self.s.create_node('pre-existing', OWNER) |
63 self.assertFailure(d, storage.NodeExists) | 69 self.assertFailure(d, error.NodeExists) |
64 return d | 70 return d |
65 | 71 |
66 def testCreateNode(self): | 72 def testCreateNode(self): |
67 def cb(void): | 73 def cb(void): |
68 d = self.s.get_node('new 1') | 74 d = self.s.get_node('new 1') |
72 d.addCallback(cb) | 78 d.addCallback(cb) |
73 return d | 79 return d |
74 | 80 |
75 def testDeleteNonExistingNode(self): | 81 def testDeleteNonExistingNode(self): |
76 d = self.s.delete_node('non-existing') | 82 d = self.s.delete_node('non-existing') |
77 self.assertFailure(d, storage.NodeNotFound) | 83 self.assertFailure(d, error.NodeNotFound) |
78 return d | 84 return d |
79 | 85 |
80 def testDeleteNode(self): | 86 def testDeleteNode(self): |
81 def cb(void): | 87 def cb(void): |
82 d = self.s.get_node('to-be-deleted') | 88 d = self.s.get_node('to-be-deleted') |
83 self.assertFailure(d, storage.NodeNotFound) | 89 self.assertFailure(d, error.NodeNotFound) |
84 return d | 90 return d |
85 | 91 |
86 d = self.s.delete_node('to-be-deleted') | 92 d = self.s.delete_node('to-be-deleted') |
87 d.addCallback(cb) | 93 d.addCallback(cb) |
88 return d | 94 return d |
96 return d | 102 return d |
97 | 103 |
98 def testGetSubscriptions(self): | 104 def testGetSubscriptions(self): |
99 def cb(subscriptions): | 105 def cb(subscriptions): |
100 self.assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) | 106 self.assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) |
101 | 107 |
102 d = self.s.get_subscriptions(SUBSCRIBER) | 108 d = self.s.get_subscriptions(SUBSCRIBER) |
103 d.addCallback(cb) | 109 d.addCallback(cb) |
104 return d | 110 return d |
105 | 111 |
106 # Node tests | 112 # Node tests |
122 return d | 128 return d |
123 | 129 |
124 def check_object_config(node): | 130 def check_object_config(node): |
125 config = node.get_configuration() | 131 config = node.get_configuration() |
126 self.assertEqual(config['pubsub#persist_items'], False) | 132 self.assertEqual(config['pubsub#persist_items'], False) |
127 | 133 |
128 def get_node(void): | 134 def get_node(void): |
129 return self.s.get_node('to-be-reconfigured') | 135 return self.s.get_node('to-be-reconfigured') |
130 | 136 |
131 def check_storage_config(node): | 137 def check_storage_config(node): |
132 config = node.get_configuration() | 138 config = node.get_configuration() |
175 d.addCallback(cb2) | 181 d.addCallback(cb2) |
176 return d | 182 return d |
177 | 183 |
178 def testAddExistingSubscription(self): | 184 def testAddExistingSubscription(self): |
179 d = self.node.add_subscription(SUBSCRIBER, 'pending') | 185 d = self.node.add_subscription(SUBSCRIBER, 'pending') |
180 self.assertFailure(d, storage.SubscriptionExists) | 186 self.assertFailure(d, error.SubscriptionExists) |
181 return d | 187 return d |
182 | 188 |
183 def testGetSubscription(self): | 189 def testGetSubscription(self): |
184 def cb(subscriptions): | 190 def cb(subscriptions): |
185 self.assertEquals(subscriptions[0][1], 'subscribed') | 191 self.assertEquals(subscriptions[0][1], 'subscribed') |
186 self.assertEquals(subscriptions[1][1], 'pending') | 192 self.assertEquals(subscriptions[1][1], 'pending') |
187 self.assertEquals(subscriptions[2][1], None) | 193 self.assertEquals(subscriptions[2][1], None) |
195 def testRemoveSubscription(self): | 201 def testRemoveSubscription(self): |
196 return self.node.remove_subscription(SUBSCRIBER_TO_BE_DELETED) | 202 return self.node.remove_subscription(SUBSCRIBER_TO_BE_DELETED) |
197 | 203 |
198 def testRemoveNonExistingSubscription(self): | 204 def testRemoveNonExistingSubscription(self): |
199 d = self.node.remove_subscription(OWNER) | 205 d = self.node.remove_subscription(OWNER) |
200 self.assertFailure(d, storage.SubscriptionNotFound) | 206 self.assertFailure(d, error.SubscriptionNotFound) |
201 return d | 207 return d |
202 | 208 |
203 def testGetSubscribers(self): | 209 def testGetSubscribers(self): |
204 def cb(subscribers): | 210 def cb(subscribers): |
205 self.assertIn(SUBSCRIBER, subscribers) | 211 self.assertIn(SUBSCRIBER, subscribers) |
206 self.assertNotIn(SUBSCRIBER_PENDING, subscribers) | 212 self.assertNotIn(SUBSCRIBER_PENDING, subscribers) |
207 self.assertNotIn(OWNER, subscribers) | 213 self.assertNotIn(OWNER, subscribers) |
307 d.addCallback(lambda _: node) | 313 d.addCallback(lambda _: node) |
308 return d | 314 return d |
309 | 315 |
310 def cb2(node): | 316 def cb2(node): |
311 return node.get_items() | 317 return node.get_items() |
312 | 318 |
313 def cb3(result): | 319 def cb3(result): |
314 self.assertEqual([], result) | 320 self.assertEqual([], result) |
315 | 321 |
316 d = self.s.get_node('to-be-purged') | 322 d = self.s.get_node('to-be-purged') |
317 d.addCallback(cb1) | 323 d.addCallback(cb1) |
324 return node.get_affiliations() | 330 return node.get_affiliations() |
325 | 331 |
326 def cb2(affiliations): | 332 def cb2(affiliations): |
327 affiliations = dict(((a[0].full(), a[1]) for a in affiliations)) | 333 affiliations = dict(((a[0].full(), a[1]) for a in affiliations)) |
328 self.assertEquals(affiliations[OWNER.full()], 'owner') | 334 self.assertEquals(affiliations[OWNER.full()], 'owner') |
329 | 335 |
330 d = self.s.get_node('pre-existing') | 336 d = self.s.get_node('pre-existing') |
331 d.addCallback(cb1) | 337 d.addCallback(cb1) |
332 d.addCallback(cb2) | 338 d.addCallback(cb2) |
333 return d | 339 return d |
334 | 340 |
335 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): | 341 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): |
336 | 342 |
337 def setUpClass(self): | 343 def setUp(self): |
338 from idavoll.memory_storage import Storage, LeafNode, Subscription, \ | 344 from idavoll.memory_storage import Storage, LeafNode, Subscription, \ |
339 default_config | 345 default_config |
340 self.s = Storage() | 346 self.s = Storage() |
341 self.s._nodes['pre-existing'] = \ | 347 self.s._nodes['pre-existing'] = \ |
342 LeafNode('pre-existing', OWNER, default_config) | 348 LeafNode('pre-existing', OWNER, default_config) |
361 self.s._nodes['to-be-purged']._itemlist.append(item) | 367 self.s._nodes['to-be-purged']._itemlist.append(item) |
362 item = (decode(ITEM.toXml()), PUBLISHER) | 368 item = (decode(ITEM.toXml()), PUBLISHER) |
363 self.s._nodes['pre-existing']._items['current'] = item | 369 self.s._nodes['pre-existing']._items['current'] = item |
364 self.s._nodes['pre-existing']._itemlist.append(item) | 370 self.s._nodes['pre-existing']._itemlist.append(item) |
365 | 371 |
366 return StorageTests.setUpClass(self) | 372 return StorageTests.setUp(self) |
367 | 373 |
368 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): | 374 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): |
369 def _callSuperSetUpClass(self, void): | 375 def _callSuperSetUp(self, void): |
370 return StorageTests.setUpClass(self) | 376 return StorageTests.setUp(self) |
371 | 377 |
372 def setUpClass(self): | 378 def setUp(self): |
373 from idavoll.pgsql_storage import Storage | 379 from idavoll.pgsql_storage import Storage |
374 self.s = Storage('ralphm', 'pubsub_test') | 380 self.s = Storage('ralphm', 'pubsub_test') |
375 self.s._dbpool.start() | 381 self.s._dbpool.start() |
376 d = self.s._dbpool.runInteraction(self.init) | 382 d = self.s._dbpool.runInteraction(self.init) |
377 d.addCallback(self._callSuperSetUpClass) | 383 d.addCallback(self._callSuperSetUp) |
378 return d | 384 return d |
379 | 385 |
380 def tearDownClass(self): | 386 def tearDownClass(self): |
381 #return self.s._dbpool.runInteraction(self.cleandb) | 387 #return self.s._dbpool.runInteraction(self.cleandb) |
382 pass | 388 pass |
442 SELECT nodes.id, %s, 'current', %s | 448 SELECT nodes.id, %s, 'current', %s |
443 FROM nodes | 449 FROM nodes |
444 WHERE node='pre-existing'""", | 450 WHERE node='pre-existing'""", |
445 (PUBLISHER.userhost(), | 451 (PUBLISHER.userhost(), |
446 ITEM.toXml())) | 452 ITEM.toXml())) |
447 | 453 |
448 def cleandb(self, cursor): | 454 def cleandb(self, cursor): |
449 cursor.execute("""DELETE FROM nodes WHERE node in | 455 cursor.execute("""DELETE FROM nodes WHERE node in |
450 ('non-existing', 'pre-existing', 'to-be-deleted', | 456 ('non-existing', 'pre-existing', 'to-be-deleted', |
451 'new 1', 'new 2', 'new 3', 'to-be-reconfigured', | 457 'new 1', 'new 2', 'new 3', 'to-be-reconfigured', |
452 'to-be-purged')""") | 458 'to-be-purged')""") |
458 SUBSCRIBER_TO_BE_DELETED.userhost()) | 464 SUBSCRIBER_TO_BE_DELETED.userhost()) |
459 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | 465 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
460 SUBSCRIBER_PENDING.userhost()) | 466 SUBSCRIBER_PENDING.userhost()) |
461 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | 467 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
462 PUBLISHER.userhost()) | 468 PUBLISHER.userhost()) |
469 | |
470 try: | |
471 import pyPgSQL | |
472 pyPgSQL | |
473 except ImportError: | |
474 PgsqlStorageStorageTestCase.skip = "pyPgSQL not available" |