annotate idavoll/test/test_storage.py @ 114:e7cfe05bc1d2

Initial revision.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 08 Apr 2005 16:33:40 +0000
parents
children bc872c33e88c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.trial import unittest
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2 from twisted.trial.assertions import *
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.words.protocols.jabber import jid
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 from idavoll import storage
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 OWNER = jid.JID('owner@example.com')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8 SUBSCRIBER = jid.JID('subscriber@example.com/Home')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
9
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10 class StorageTests:
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 def testGetNode(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13 return self.s.get_node('pre-existing')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 def testGetNonExistingNode(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 d = self.s.get_node('non-existing')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17 assertFailure(d, storage.NodeNotFound)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20 def testGetNodeIDs(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21 def cb(node_ids):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 assertIn('pre-existing', node_ids)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 assertNotIn('non-existing', node_ids)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 return self.s.get_node_ids().addCallback(cb)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27 def testCreateExistingNode(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 d = self.s.create_node('pre-existing', OWNER)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 assertFailure(d, storage.NodeExists)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 def testCreateNode(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33 def cb(void):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34 d = self.s.get_node('new 1')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 d = self.s.create_node('new 1', OWNER)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 d.addCallback(cb)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41 def testDeleteNonExistingNode(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 d = self.s.delete_node('non-existing')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43 assertFailure(d, storage.NodeNotFound)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46 def testDeleteNode(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 def cb(void):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 d = self.s.get_node('to-be-deleted')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 assertFailure(d, storage.NodeNotFound)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52 d = self.s.delete_node('to-be-deleted')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
53 d.addCallback(cb)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 def testGetAffiliations(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 def cb(affiliations):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 assertIn(('pre-existing', 'owner'), affiliations)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 d = self.s.get_affiliations(OWNER)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61 d.addCallback(cb)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64 def testGetSubscriptions(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 def cb(subscriptions):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68 d = self.s.get_subscriptions(SUBSCRIBER)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 d.addCallback(cb)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 return d
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74 def setUpClass(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 from idavoll.memory_storage import Storage, LeafNode, Subscription
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 self.s = Storage()
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77 self.s._nodes['pre-existing'] = LeafNode('pre-existing', OWNER, None)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78 self.s._nodes['to-be-deleted'] = LeafNode('to-be-deleted', OWNER, None)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 self.s._nodes['pre-existing']._subscriptions[SUBSCRIBER.full()] = \
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 Subscription('subscribed')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 def setUpClass(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84 from idavoll.pgsql_storage import Storage
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 self.s = Storage('ralphm', 'pubsub_test')
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86 self.s._dbpool.start()
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 return self.s._dbpool.runInteraction(self.init)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
88
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89 def tearDownClass(self):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
90 return self.s._dbpool.runInteraction(self.cleandb)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92 def init(self, cursor):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93 self.cleandb(cursor)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""")
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
95 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""")
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
97 OWNER.userhost().encode('utf-8'))
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
98 cursor.execute("""INSERT INTO affiliations
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 (node_id, entity_id, affiliation)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100 SELECT nodes.id, entities.id, 'owner'
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 FROM nodes, entities
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102 WHERE node='pre-existing' AND jid=%s""",
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 OWNER.userhost().encode('utf-8'))
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105 SUBSCRIBER.userhost().encode('utf-8'))
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
106 cursor.execute("""INSERT INTO subscriptions
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107 (node_id, entity_id, resource, subscription)
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108 SELECT nodes.id, entities.id, %s, 'subscribed'
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
109 FROM nodes, entities
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
110 WHERE node='pre-existing' AND jid=%s""",
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
111 (SUBSCRIBER.resource.encode('utf-8'),
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
112 SUBSCRIBER.userhost().encode('utf-8')))
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
113
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
114 def cleandb(self, cursor):
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
115 cursor.execute("""DELETE FROM nodes WHERE node in
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
116 ('pre-existing', 'new 1', 'new 2', 'new 3')""")
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
117 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 OWNER.userhost().encode('utf-8'))
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119 cursor.execute("""DELETE FROM entities WHERE jid=%s""",
e7cfe05bc1d2 Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120 SUBSCRIBER.userhost().encode('utf-8'))