Mercurial > libervia-pubsub
annotate idavoll/test/test_storage.py @ 117:bc872c33e88c
Added test cases for a few of the Node methods.
Clean up more in the pgsql database.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Fri, 08 Apr 2005 21:00:22 +0000 |
parents | e7cfe05bc1d2 |
children | 8f99b4f7aea2 |
rev | line source |
---|---|
114 | 1 from twisted.trial import unittest |
2 from twisted.trial.assertions import * | |
3 from twisted.words.protocols.jabber import jid | |
4 | |
5 from idavoll import storage | |
6 | |
7 OWNER = jid.JID('owner@example.com') | |
8 SUBSCRIBER = jid.JID('subscriber@example.com/Home') | |
9 | |
10 class StorageTests: | |
11 | |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
12 def _assignTestNode(self, node): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
13 self.node = node |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
14 |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
15 def setUpClass(self): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
16 d = self.s.get_node('pre-existing') |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
17 d.addCallback(self._assignTestNode) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
18 return d |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
19 |
114 | 20 def testGetNode(self): |
21 return self.s.get_node('pre-existing') | |
22 | |
23 def testGetNonExistingNode(self): | |
24 d = self.s.get_node('non-existing') | |
25 assertFailure(d, storage.NodeNotFound) | |
26 return d | |
27 | |
28 def testGetNodeIDs(self): | |
29 def cb(node_ids): | |
30 assertIn('pre-existing', node_ids) | |
31 assertNotIn('non-existing', node_ids) | |
32 | |
33 return self.s.get_node_ids().addCallback(cb) | |
34 | |
35 def testCreateExistingNode(self): | |
36 d = self.s.create_node('pre-existing', OWNER) | |
37 assertFailure(d, storage.NodeExists) | |
38 return d | |
39 | |
40 def testCreateNode(self): | |
41 def cb(void): | |
42 d = self.s.get_node('new 1') | |
43 return d | |
44 | |
45 d = self.s.create_node('new 1', OWNER) | |
46 d.addCallback(cb) | |
47 return d | |
48 | |
49 def testDeleteNonExistingNode(self): | |
50 d = self.s.delete_node('non-existing') | |
51 assertFailure(d, storage.NodeNotFound) | |
52 return d | |
53 | |
54 def testDeleteNode(self): | |
55 def cb(void): | |
56 d = self.s.get_node('to-be-deleted') | |
57 assertFailure(d, storage.NodeNotFound) | |
58 return d | |
59 | |
60 d = self.s.delete_node('to-be-deleted') | |
61 d.addCallback(cb) | |
62 return d | |
63 | |
64 def testGetAffiliations(self): | |
65 def cb(affiliations): | |
66 assertIn(('pre-existing', 'owner'), affiliations) | |
67 | |
68 d = self.s.get_affiliations(OWNER) | |
69 d.addCallback(cb) | |
70 return d | |
71 | |
72 def testGetSubscriptions(self): | |
73 def cb(subscriptions): | |
74 assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) | |
75 | |
76 d = self.s.get_subscriptions(SUBSCRIBER) | |
77 d.addCallback(cb) | |
78 return d | |
79 | |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
80 # Node tests |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
81 |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
82 def testGetType(self): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
83 assertEqual(self.node.get_type(), 'leaf') |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
84 |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
85 def testGetConfiguration(self): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
86 config = self.node.get_configuration() |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
87 assertIn('pubsub#persist_items', config.iterkeys()) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
88 assertIn('pubsub#deliver_payloads', config.iterkeys()) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
89 assertEqual(config['pubsub#persist_items'], True) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
90 assertEqual(config['pubsub#deliver_payloads'], True) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
91 |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
92 def testGetMetaData(self): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
93 meta_data = self.node.get_meta_data() |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
94 for key, value in self.node.get_configuration().iteritems(): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
95 assertIn(key, meta_data.iterkeys()) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
96 assertEqual(value, meta_data[key]) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
97 assertIn('pubsub#node_type', meta_data.iterkeys()) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
98 assertEqual(meta_data['pubsub#node_type'], 'leaf') |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
99 |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
100 |
114 | 101 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): |
102 | |
103 def setUpClass(self): | |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
104 from idavoll.memory_storage import Storage, LeafNode, Subscription, \ |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
105 default_config |
114 | 106 self.s = Storage() |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
107 self.s._nodes['pre-existing'] = LeafNode('pre-existing', OWNER, |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
108 default_config) |
114 | 109 self.s._nodes['to-be-deleted'] = LeafNode('to-be-deleted', OWNER, None) |
110 self.s._nodes['pre-existing']._subscriptions[SUBSCRIBER.full()] = \ | |
111 Subscription('subscribed') | |
112 | |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
113 return StorageTests.setUpClass(self) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
114 |
114 | 115 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
116 def _callSuperSetUpClass(self, void): |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
117 return StorageTests.setUpClass(self) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
118 |
114 | 119 def setUpClass(self): |
120 from idavoll.pgsql_storage import Storage | |
121 self.s = Storage('ralphm', 'pubsub_test') | |
122 self.s._dbpool.start() | |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
123 d = self.s._dbpool.runInteraction(self.init) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
124 d.addCallback(self._callSuperSetUpClass) |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
125 return d |
114 | 126 |
127 def tearDownClass(self): | |
128 return self.s._dbpool.runInteraction(self.cleandb) | |
129 | |
130 def init(self, cursor): | |
131 self.cleandb(cursor) | |
132 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""") | |
133 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""") | |
134 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", | |
135 OWNER.userhost().encode('utf-8')) | |
136 cursor.execute("""INSERT INTO affiliations | |
137 (node_id, entity_id, affiliation) | |
138 SELECT nodes.id, entities.id, 'owner' | |
139 FROM nodes, entities | |
140 WHERE node='pre-existing' AND jid=%s""", | |
141 OWNER.userhost().encode('utf-8')) | |
142 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", | |
143 SUBSCRIBER.userhost().encode('utf-8')) | |
144 cursor.execute("""INSERT INTO subscriptions | |
145 (node_id, entity_id, resource, subscription) | |
146 SELECT nodes.id, entities.id, %s, 'subscribed' | |
147 FROM nodes, entities | |
148 WHERE node='pre-existing' AND jid=%s""", | |
149 (SUBSCRIBER.resource.encode('utf-8'), | |
150 SUBSCRIBER.userhost().encode('utf-8'))) | |
151 | |
152 def cleandb(self, cursor): | |
153 cursor.execute("""DELETE FROM nodes WHERE node in | |
117
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
154 ('non-existing', 'pre-existing', 'to-be-deleted', |
bc872c33e88c
Added test cases for a few of the Node methods.
Ralph Meijer <ralphm@ik.nu>
parents:
114
diff
changeset
|
155 'new 1', 'new 2', 'new 3')""") |
114 | 156 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
157 OWNER.userhost().encode('utf-8')) | |
158 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | |
159 SUBSCRIBER.userhost().encode('utf-8')) |