Mercurial > libervia-pubsub
comparison idavoll/test/test_storage.py @ 123:8f99b4f7aea2
Add tests for all Node methods.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 12 Apr 2005 12:28:07 +0000 |
parents | bc872c33e88c |
children | fe8cb99363ff |
comparison
equal
deleted
inserted
replaced
122:8527bce09862 | 123:8f99b4f7aea2 |
---|---|
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 | 5 |
5 from idavoll import storage | 6 from idavoll import storage |
6 | 7 |
7 OWNER = jid.JID('owner@example.com') | 8 OWNER = jid.JID('owner@example.com') |
8 SUBSCRIBER = jid.JID('subscriber@example.com/Home') | 9 SUBSCRIBER = jid.JID('subscriber@example.com/Home') |
10 SUBSCRIBER_NEW = jid.JID('new@example.com/Home') | |
11 SUBSCRIBER_TO_BE_DELETED = jid.JID('to_be_deleted@example.com/Home') | |
12 SUBSCRIBER_PENDING = jid.JID('pending@example.com/Home') | |
9 | 13 |
10 class StorageTests: | 14 class StorageTests: |
11 | 15 |
12 def _assignTestNode(self, node): | 16 def _assignTestNode(self, node): |
13 self.node = node | 17 self.node = node |
86 config = self.node.get_configuration() | 90 config = self.node.get_configuration() |
87 assertIn('pubsub#persist_items', config.iterkeys()) | 91 assertIn('pubsub#persist_items', config.iterkeys()) |
88 assertIn('pubsub#deliver_payloads', config.iterkeys()) | 92 assertIn('pubsub#deliver_payloads', config.iterkeys()) |
89 assertEqual(config['pubsub#persist_items'], True) | 93 assertEqual(config['pubsub#persist_items'], True) |
90 assertEqual(config['pubsub#deliver_payloads'], True) | 94 assertEqual(config['pubsub#deliver_payloads'], True) |
95 | |
96 def testSetConfiguration(self): | |
97 def get_config(node): | |
98 d = node.set_configuration({'pubsub#persist_items': False}) | |
99 d.addCallback(lambda _: node) | |
100 return d | |
101 | |
102 def check_object_config(node): | |
103 config = node.get_configuration() | |
104 assertEqual(config['pubsub#persist_items'], False) | |
105 | |
106 def get_node(void): | |
107 return self.s.get_node('to-be-reconfigured') | |
108 | |
109 def check_storage_config(node): | |
110 config = node.get_configuration() | |
111 assertEqual(config['pubsub#persist_items'], False) | |
112 | |
113 d = self.s.get_node('to-be-reconfigured') | |
114 d.addCallback(get_config) | |
115 d.addCallback(check_object_config) | |
116 d.addCallback(get_node) | |
117 d.addCallback(check_storage_config) | |
118 return d | |
91 | 119 |
92 def testGetMetaData(self): | 120 def testGetMetaData(self): |
93 meta_data = self.node.get_meta_data() | 121 meta_data = self.node.get_meta_data() |
94 for key, value in self.node.get_configuration().iteritems(): | 122 for key, value in self.node.get_configuration().iteritems(): |
95 assertIn(key, meta_data.iterkeys()) | 123 assertIn(key, meta_data.iterkeys()) |
96 assertEqual(value, meta_data[key]) | 124 assertEqual(value, meta_data[key]) |
97 assertIn('pubsub#node_type', meta_data.iterkeys()) | 125 assertIn('pubsub#node_type', meta_data.iterkeys()) |
98 assertEqual(meta_data['pubsub#node_type'], 'leaf') | 126 assertEqual(meta_data['pubsub#node_type'], 'leaf') |
99 | 127 |
128 def testGetAffiliation(self): | |
129 def cb(affiliation): | |
130 assertEqual(affiliation, 'owner') | |
131 | |
132 d = self.node.get_affiliation(OWNER) | |
133 d.addCallback(cb) | |
134 return d | |
135 | |
136 def testGetNonExistingAffiliation(self): | |
137 def cb(affiliation): | |
138 assertEqual(affiliation, None) | |
139 | |
140 d = self.node.get_affiliation(SUBSCRIBER) | |
141 d.addCallback(cb) | |
142 return d | |
143 | |
144 def testAddSubscription(self): | |
145 def cb1(void): | |
146 return self.node.get_subscription(SUBSCRIBER_NEW) | |
147 | |
148 def cb2(state): | |
149 assertEqual(state, 'pending') | |
150 | |
151 d = self.node.add_subscription(SUBSCRIBER_NEW, 'pending') | |
152 d.addCallback(cb1) | |
153 d.addCallback(cb2) | |
154 return d | |
155 | |
156 def testAddExistingSubscription(self): | |
157 d = self.node.add_subscription(SUBSCRIBER, 'pending') | |
158 assertFailure(d, storage.SubscriptionExists) | |
159 return d | |
160 | |
161 def testGetSubscription(self): | |
162 def cb(subscriptions): | |
163 assertEquals(subscriptions[0][1], 'subscribed') | |
164 assertEquals(subscriptions[1][1], 'pending') | |
165 assertEquals(subscriptions[2][1], None) | |
166 | |
167 d = defer.DeferredList([self.node.get_subscription(SUBSCRIBER), | |
168 self.node.get_subscription(SUBSCRIBER_PENDING), | |
169 self.node.get_subscription(OWNER)]) | |
170 d.addCallback(cb) | |
171 return d | |
172 | |
173 def testRemoveSubscription(self): | |
174 return self.node.remove_subscription(SUBSCRIBER_TO_BE_DELETED) | |
175 | |
176 def testRemoveNonExistingSubscription(self): | |
177 d = self.node.remove_subscription(OWNER) | |
178 assertFailure(d, storage.SubscriptionNotFound) | |
179 return d | |
180 | |
181 def testGetSubscribers(self): | |
182 def cb(subscribers): | |
183 assertIn(SUBSCRIBER, subscribers) | |
184 assertNotIn(SUBSCRIBER_PENDING, subscribers) | |
185 assertNotIn(OWNER, subscribers) | |
186 | |
187 d = self.node.get_subscribers() | |
188 d.addCallback(cb) | |
189 return d | |
190 | |
191 def testIsSubscriber(self): | |
192 def cb(subscribed): | |
193 assertEquals(subscribed[0][1], True) | |
194 assertEquals(subscribed[1][1], False) | |
195 assertEquals(subscribed[2][1], False) | |
196 | |
197 d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER), | |
198 self.node.is_subscribed(SUBSCRIBER_PENDING), | |
199 self.node.is_subscribed(OWNER)]) | |
200 d.addCallback(cb) | |
201 return d | |
100 | 202 |
101 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): | 203 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): |
102 | 204 |
103 def setUpClass(self): | 205 def setUpClass(self): |
104 from idavoll.memory_storage import Storage, LeafNode, Subscription, \ | 206 from idavoll.memory_storage import Storage, LeafNode, Subscription, \ |
105 default_config | 207 default_config |
106 self.s = Storage() | 208 self.s = Storage() |
107 self.s._nodes['pre-existing'] = LeafNode('pre-existing', OWNER, | 209 self.s._nodes['pre-existing'] = \ |
108 default_config) | 210 LeafNode('pre-existing', OWNER, default_config) |
109 self.s._nodes['to-be-deleted'] = LeafNode('to-be-deleted', OWNER, None) | 211 self.s._nodes['to-be-deleted'] = \ |
110 self.s._nodes['pre-existing']._subscriptions[SUBSCRIBER.full()] = \ | 212 LeafNode('to-be-deleted', OWNER, None) |
213 self.s._nodes['to-be-reconfigured'] = \ | |
214 LeafNode('to-be-reconfigured', OWNER, default_config) | |
215 | |
216 subscriptions = self.s._nodes['pre-existing']._subscriptions | |
217 subscriptions[SUBSCRIBER.full()] = Subscription('subscribed') | |
218 subscriptions[SUBSCRIBER_TO_BE_DELETED.full()] = \ | |
111 Subscription('subscribed') | 219 Subscription('subscribed') |
220 subscriptions[SUBSCRIBER_PENDING.full()] = \ | |
221 Subscription('pending') | |
112 | 222 |
113 return StorageTests.setUpClass(self) | 223 return StorageTests.setUpClass(self) |
114 | 224 |
115 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): | 225 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): |
116 def _callSuperSetUpClass(self, void): | 226 def _callSuperSetUpClass(self, void): |
123 d = self.s._dbpool.runInteraction(self.init) | 233 d = self.s._dbpool.runInteraction(self.init) |
124 d.addCallback(self._callSuperSetUpClass) | 234 d.addCallback(self._callSuperSetUpClass) |
125 return d | 235 return d |
126 | 236 |
127 def tearDownClass(self): | 237 def tearDownClass(self): |
128 return self.s._dbpool.runInteraction(self.cleandb) | 238 # return self.s._dbpool.runInteraction(self.cleandb) |
239 pass | |
129 | 240 |
130 def init(self, cursor): | 241 def init(self, cursor): |
131 self.cleandb(cursor) | 242 self.cleandb(cursor) |
132 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""") | 243 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""") |
133 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""") | 244 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""") |
245 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-reconfigured')""") | |
134 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", | 246 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", |
135 OWNER.userhost().encode('utf-8')) | 247 OWNER.userhost().encode('utf-8')) |
136 cursor.execute("""INSERT INTO affiliations | 248 cursor.execute("""INSERT INTO affiliations |
137 (node_id, entity_id, affiliation) | 249 (node_id, entity_id, affiliation) |
138 SELECT nodes.id, entities.id, 'owner' | 250 SELECT nodes.id, entities.id, 'owner' |
146 SELECT nodes.id, entities.id, %s, 'subscribed' | 258 SELECT nodes.id, entities.id, %s, 'subscribed' |
147 FROM nodes, entities | 259 FROM nodes, entities |
148 WHERE node='pre-existing' AND jid=%s""", | 260 WHERE node='pre-existing' AND jid=%s""", |
149 (SUBSCRIBER.resource.encode('utf-8'), | 261 (SUBSCRIBER.resource.encode('utf-8'), |
150 SUBSCRIBER.userhost().encode('utf-8'))) | 262 SUBSCRIBER.userhost().encode('utf-8'))) |
263 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", | |
264 SUBSCRIBER_TO_BE_DELETED.userhost().encode('utf-8')) | |
265 cursor.execute("""INSERT INTO subscriptions | |
266 (node_id, entity_id, resource, subscription) | |
267 SELECT nodes.id, entities.id, %s, 'subscribed' | |
268 FROM nodes, entities | |
269 WHERE node='pre-existing' AND jid=%s""", | |
270 (SUBSCRIBER_TO_BE_DELETED.resource.encode('utf-8'), | |
271 SUBSCRIBER_TO_BE_DELETED.userhost().encode('utf-8'))) | |
272 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", | |
273 SUBSCRIBER_PENDING.userhost().encode('utf-8')) | |
274 cursor.execute("""INSERT INTO subscriptions | |
275 (node_id, entity_id, resource, subscription) | |
276 SELECT nodes.id, entities.id, %s, 'pending' | |
277 FROM nodes, entities | |
278 WHERE node='pre-existing' AND jid=%s""", | |
279 (SUBSCRIBER_PENDING.resource.encode('utf-8'), | |
280 SUBSCRIBER_PENDING.userhost().encode('utf-8'))) | |
151 | 281 |
152 def cleandb(self, cursor): | 282 def cleandb(self, cursor): |
153 cursor.execute("""DELETE FROM nodes WHERE node in | 283 cursor.execute("""DELETE FROM nodes WHERE node in |
154 ('non-existing', 'pre-existing', 'to-be-deleted', | 284 ('non-existing', 'pre-existing', 'to-be-deleted', |
155 'new 1', 'new 2', 'new 3')""") | 285 'new 1', 'new 2', 'new 3', 'to-be-reconfigured')""") |
156 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | 286 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
157 OWNER.userhost().encode('utf-8')) | 287 OWNER.userhost().encode('utf-8')) |
158 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | 288 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
159 SUBSCRIBER.userhost().encode('utf-8')) | 289 SUBSCRIBER.userhost().encode('utf-8')) |
290 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | |
291 SUBSCRIBER_TO_BE_DELETED.userhost().encode('utf-8')) | |
292 cursor.execute("""DELETE FROM entities WHERE jid=%s""", | |
293 SUBSCRIBER_PENDING.userhost().encode('utf-8')) |