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 |
|
12 def testGetNode(self): |
|
13 return self.s.get_node('pre-existing') |
|
14 |
|
15 def testGetNonExistingNode(self): |
|
16 d = self.s.get_node('non-existing') |
|
17 assertFailure(d, storage.NodeNotFound) |
|
18 return d |
|
19 |
|
20 def testGetNodeIDs(self): |
|
21 def cb(node_ids): |
|
22 assertIn('pre-existing', node_ids) |
|
23 assertNotIn('non-existing', node_ids) |
|
24 |
|
25 return self.s.get_node_ids().addCallback(cb) |
|
26 |
|
27 def testCreateExistingNode(self): |
|
28 d = self.s.create_node('pre-existing', OWNER) |
|
29 assertFailure(d, storage.NodeExists) |
|
30 return d |
|
31 |
|
32 def testCreateNode(self): |
|
33 def cb(void): |
|
34 d = self.s.get_node('new 1') |
|
35 return d |
|
36 |
|
37 d = self.s.create_node('new 1', OWNER) |
|
38 d.addCallback(cb) |
|
39 return d |
|
40 |
|
41 def testDeleteNonExistingNode(self): |
|
42 d = self.s.delete_node('non-existing') |
|
43 assertFailure(d, storage.NodeNotFound) |
|
44 return d |
|
45 |
|
46 def testDeleteNode(self): |
|
47 def cb(void): |
|
48 d = self.s.get_node('to-be-deleted') |
|
49 assertFailure(d, storage.NodeNotFound) |
|
50 return d |
|
51 |
|
52 d = self.s.delete_node('to-be-deleted') |
|
53 d.addCallback(cb) |
|
54 return d |
|
55 |
|
56 def testGetAffiliations(self): |
|
57 def cb(affiliations): |
|
58 assertIn(('pre-existing', 'owner'), affiliations) |
|
59 |
|
60 d = self.s.get_affiliations(OWNER) |
|
61 d.addCallback(cb) |
|
62 return d |
|
63 |
|
64 def testGetSubscriptions(self): |
|
65 def cb(subscriptions): |
|
66 assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions) |
|
67 |
|
68 d = self.s.get_subscriptions(SUBSCRIBER) |
|
69 d.addCallback(cb) |
|
70 return d |
|
71 |
|
72 class MemoryStorageStorageTestCase(unittest.TestCase, StorageTests): |
|
73 |
|
74 def setUpClass(self): |
|
75 from idavoll.memory_storage import Storage, LeafNode, Subscription |
|
76 self.s = Storage() |
|
77 self.s._nodes['pre-existing'] = LeafNode('pre-existing', OWNER, None) |
|
78 self.s._nodes['to-be-deleted'] = LeafNode('to-be-deleted', OWNER, None) |
|
79 self.s._nodes['pre-existing']._subscriptions[SUBSCRIBER.full()] = \ |
|
80 Subscription('subscribed') |
|
81 |
|
82 class PgsqlStorageStorageTestCase(unittest.TestCase, StorageTests): |
|
83 def setUpClass(self): |
|
84 from idavoll.pgsql_storage import Storage |
|
85 self.s = Storage('ralphm', 'pubsub_test') |
|
86 self.s._dbpool.start() |
|
87 return self.s._dbpool.runInteraction(self.init) |
|
88 |
|
89 def tearDownClass(self): |
|
90 return self.s._dbpool.runInteraction(self.cleandb) |
|
91 |
|
92 def init(self, cursor): |
|
93 self.cleandb(cursor) |
|
94 cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""") |
|
95 cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""") |
|
96 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", |
|
97 OWNER.userhost().encode('utf-8')) |
|
98 cursor.execute("""INSERT INTO affiliations |
|
99 (node_id, entity_id, affiliation) |
|
100 SELECT nodes.id, entities.id, 'owner' |
|
101 FROM nodes, entities |
|
102 WHERE node='pre-existing' AND jid=%s""", |
|
103 OWNER.userhost().encode('utf-8')) |
|
104 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""", |
|
105 SUBSCRIBER.userhost().encode('utf-8')) |
|
106 cursor.execute("""INSERT INTO subscriptions |
|
107 (node_id, entity_id, resource, subscription) |
|
108 SELECT nodes.id, entities.id, %s, 'subscribed' |
|
109 FROM nodes, entities |
|
110 WHERE node='pre-existing' AND jid=%s""", |
|
111 (SUBSCRIBER.resource.encode('utf-8'), |
|
112 SUBSCRIBER.userhost().encode('utf-8'))) |
|
113 |
|
114 def cleandb(self, cursor): |
|
115 cursor.execute("""DELETE FROM nodes WHERE node in |
|
116 ('pre-existing', 'new 1', 'new 2', 'new 3')""") |
|
117 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
|
118 OWNER.userhost().encode('utf-8')) |
|
119 cursor.execute("""DELETE FROM entities WHERE jid=%s""", |
|
120 SUBSCRIBER.userhost().encode('utf-8')) |