Mercurial > libervia-pubsub
comparison idavoll/test/test_backend.py @ 198:e404775b12df
Change naming and spacing conventions to match Twisted's.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 10 Jun 2008 11:31:49 +0000 |
parents | 69cdd8c6a431 |
children | e6b710bf2b24 |
comparison
equal
deleted
inserted
replaced
197:9da5a95d408d | 198:e404775b12df |
---|---|
16 | 16 |
17 OWNER = jid.JID('owner@example.com') | 17 OWNER = jid.JID('owner@example.com') |
18 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' | 18 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' |
19 | 19 |
20 class BackendTest(unittest.TestCase): | 20 class BackendTest(unittest.TestCase): |
21 def test_delete_node(self): | 21 def test_deleteNode(self): |
22 class testNode: | 22 class testNode: |
23 id = 'to-be-deleted' | 23 nodeIdentifier = 'to-be-deleted' |
24 def get_affiliation(self, entity): | 24 def getAffiliation(self, entity): |
25 if entity is OWNER: | 25 if entity is OWNER: |
26 return defer.succeed('owner') | 26 return defer.succeed('owner') |
27 | 27 |
28 class testStorage: | 28 class testStorage: |
29 def get_node(self, node_id): | 29 def getNode(self, nodeIdentifier): |
30 return defer.succeed(testNode()) | 30 return defer.succeed(testNode()) |
31 | 31 |
32 def delete_node(self, node_id): | 32 def deleteNode(self, nodeIdentifier): |
33 if node_id in ['to-be-deleted']: | 33 if nodeIdentifier in ['to-be-deleted']: |
34 self.delete_called = True | 34 self.deleteCalled = True |
35 return defer.succeed(None) | 35 return defer.succeed(None) |
36 else: | 36 else: |
37 return defer.fail(error.NodeNotFound()) | 37 return defer.fail(error.NodeNotFound()) |
38 | 38 |
39 def pre_delete(node_id): | 39 def preDelete(nodeIdentifier): |
40 self.pre_delete_called = True | 40 self.preDeleteCalled = True |
41 return defer.succeed(None) | 41 return defer.succeed(None) |
42 | 42 |
43 def cb(result): | 43 def cb(result): |
44 self.assertTrue(self.pre_delete_called) | 44 self.assertTrue(self.preDeleteCalled) |
45 self.assertTrue(self.storage.delete_called) | 45 self.assertTrue(self.storage.deleteCalled) |
46 | 46 |
47 self.storage = testStorage() | 47 self.storage = testStorage() |
48 self.backend = backend.BackendService(self.storage) | 48 self.backend = backend.BackendService(self.storage) |
49 self.storage.backend = self.backend | 49 self.storage.backend = self.backend |
50 | 50 |
51 self.pre_delete_called = False | 51 self.preDeleteCalled = False |
52 self.delete_called = False | 52 self.deleteCalled = False |
53 | 53 |
54 self.backend.register_pre_delete(pre_delete) | 54 self.backend.registerPreDelete(preDelete) |
55 d = self.backend.delete_node('to-be-deleted', OWNER) | 55 d = self.backend.deleteNode('to-be-deleted', OWNER) |
56 d.addCallback(cb) | 56 d.addCallback(cb) |
57 return d | 57 return d |
58 | 58 |
59 def test_create_nodeNoID(self): | 59 |
60 def test_createNodeNoID(self): | |
60 """ | 61 """ |
61 Test creation of a node without a given node identifier. | 62 Test creation of a node without a given node identifier. |
62 """ | 63 """ |
63 class testStorage: | 64 class testStorage: |
64 def create_node(self, node_id, requestor): | 65 def createNode(self, nodeIdentifier, requestor): |
65 self.node_id = node_id | 66 self.nodeIdentifier = nodeIdentifier |
66 return defer.succeed(None) | 67 return defer.succeed(None) |
67 | 68 |
68 self.storage = testStorage() | 69 self.storage = testStorage() |
69 self.backend = backend.BackendService(self.storage) | 70 self.backend = backend.BackendService(self.storage) |
70 self.storage.backend = self.backend | 71 self.storage.backend = self.backend |
71 | 72 |
72 def checkID(nodeIdentifier): | 73 def checkID(nodeIdentifier): |
73 self.assertNotIdentical(None, nodeIdentifier) | 74 self.assertNotIdentical(None, nodeIdentifier) |
74 self.assertIdentical(self.storage.node_id, nodeIdentifier) | 75 self.assertIdentical(self.storage.nodeIdentifier, nodeIdentifier) |
75 | 76 |
76 d = self.backend.create_node(None, OWNER) | 77 d = self.backend.createNode(None, OWNER) |
77 d.addCallback(checkID) | 78 d.addCallback(checkID) |
78 return d | 79 return d |
79 | 80 |
81 | |
80 def test_publishNoID(self): | 82 def test_publishNoID(self): |
81 """ | 83 """ |
82 Test publish request with an item without a node identifier. | 84 Test publish request with an item without a node identifier. |
83 """ | 85 """ |
84 class testNode: | 86 class testNode: |
85 id = 'node' | 87 nodeIdentifier = 'node' |
86 def get_affiliation(self, entity): | 88 def getAffiliation(self, entity): |
87 if entity is OWNER: | 89 if entity is OWNER: |
88 return defer.succeed('owner') | 90 return defer.succeed('owner') |
89 def get_configuration(self): | 91 def getConfiguration(self): |
90 return {'pubsub#deliver_payloads': True, | 92 return {'pubsub#deliver_payloads': True, |
91 'pubsub#persist_items': False} | 93 'pubsub#persist_items': False} |
92 | 94 |
93 class testStorage: | 95 class testStorage: |
94 def get_node(self, node_id): | 96 def getNode(self, nodeIdentifier): |
95 return defer.succeed(testNode()) | 97 return defer.succeed(testNode()) |
96 | 98 |
97 def checkID(notification): | 99 def checkID(notification): |
98 self.assertNotIdentical(None, notification['items'][0]['id']) | 100 self.assertNotIdentical(None, notification['items'][0]['id']) |
99 | 101 |
100 self.storage = testStorage() | 102 self.storage = testStorage() |
101 self.backend = backend.BackendService(self.storage) | 103 self.backend = backend.BackendService(self.storage) |
102 self.storage.backend = self.backend | 104 self.storage.backend = self.backend |
103 | 105 |
104 self.backend.register_notifier(checkID) | 106 self.backend.registerNotifier(checkID) |
105 | 107 |
106 items = [pubsub.Item()] | 108 items = [pubsub.Item()] |
107 d = self.backend.publish('node', items, OWNER) | 109 d = self.backend.publish('node', items, OWNER) |
108 return d | 110 return d |
109 | 111 |
112 | |
110 def test_notifyOnSubscription(self): | 113 def test_notifyOnSubscription(self): |
111 """ | 114 """ |
112 Test notification of last published item on subscription. | 115 Test notification of last published item on subscription. |
113 """ | 116 """ |
114 ITEM = "<item xmlns='%s' id='1'/>" % NS_PUBSUB | 117 ITEM = "<item xmlns='%s' id='1'/>" % NS_PUBSUB |
115 | 118 |
116 class testNode: | 119 class testNode: |
117 id = 'node' | 120 nodeIdentifier = 'node' |
118 def get_affiliation(self, entity): | 121 def getAffiliation(self, entity): |
119 if entity is OWNER: | 122 if entity is OWNER: |
120 return defer.succeed('owner') | 123 return defer.succeed('owner') |
121 def get_configuration(self): | 124 def getConfiguration(self): |
122 return {'pubsub#deliver_payloads': True, | 125 return {'pubsub#deliver_payloads': True, |
123 'pubsub#persist_items': False, | 126 'pubsub#persist_items': False, |
124 'pubsub#send_last_published_item': 'on_sub'} | 127 'pubsub#send_last_published_item': 'on_sub'} |
125 def get_items(self, max_items): | 128 def getItems(self, maxItems): |
126 return [ITEM] | 129 return [ITEM] |
127 def add_subscription(self, subscriber, state): | 130 def addSubscription(self, subscriber, state): |
128 return defer.succeed(None) | 131 return defer.succeed(None) |
129 | 132 |
130 class testStorage: | 133 class testStorage: |
131 def get_node(self, node_id): | 134 def getNode(self, nodeIdentifier): |
132 return defer.succeed(testNode()) | 135 return defer.succeed(testNode()) |
133 | 136 |
134 def cb(data): | 137 def cb(data): |
135 self.assertEquals('node', data['node_id']) | 138 self.assertEquals('node', data['nodeIdentifier']) |
136 self.assertEquals([ITEM], data['items']) | 139 self.assertEquals([ITEM], data['items']) |
137 self.assertEquals(OWNER, data['subscriber']) | 140 self.assertEquals(OWNER, data['subscriber']) |
138 | 141 |
139 self.storage = testStorage() | 142 self.storage = testStorage() |
140 self.backend = backend.BackendService(self.storage) | 143 self.backend = backend.BackendService(self.storage) |
141 self.storage.backend = self.backend | 144 self.storage.backend = self.backend |
142 | 145 |
143 d1 = defer.Deferred() | 146 d1 = defer.Deferred() |
144 d1.addCallback(cb) | 147 d1.addCallback(cb) |
145 self.backend.register_notifier(d1.callback) | 148 self.backend.registerNotifier(d1.callback) |
146 d2 = self.backend.subscribe('node', OWNER, OWNER) | 149 d2 = self.backend.subscribe('node', OWNER, OWNER) |
147 return defer.gatherResults([d1, d2]) | 150 return defer.gatherResults([d1, d2]) |
148 | 151 |
149 test_notifyOnSubscription.timeout = 2 | 152 test_notifyOnSubscription.timeout = 2 |
153 | |
150 | 154 |
151 | 155 |
152 class BaseTestBackend(object): | 156 class BaseTestBackend(object): |
153 """ | 157 """ |
154 Base class for backend stubs. | 158 Base class for backend stubs. |
155 """ | 159 """ |
156 | 160 |
157 def supports_publisher_affiliation(self): | 161 def supportsPublisherAffiliation(self): |
158 return True | 162 return True |
159 | 163 |
160 def supports_outcast_affiliation(self): | 164 |
161 return True | 165 def supportsOutcastAffiliation(self): |
162 | 166 return True |
163 def supports_persistent_items(self): | 167 |
164 return True | 168 |
165 | 169 def supportsPersistentItems(self): |
166 def supports_instant_nodes(self): | 170 return True |
167 return True | 171 |
168 | 172 |
169 def register_notifier(self, observerfn, *args, **kwargs): | 173 def supportsInstantNodes(self): |
174 return True | |
175 | |
176 | |
177 def registerNotifier(self, observerfn, *args, **kwargs): | |
170 return | 178 return |
171 | 179 |
172 def register_pre_delete(self, pre_delete_fn): | 180 |
181 def registerPreDelete(self, preDeleteFn): | |
173 return | 182 return |
183 | |
174 | 184 |
175 | 185 |
176 class PubSubServiceFromBackendTest(unittest.TestCase): | 186 class PubSubServiceFromBackendTest(unittest.TestCase): |
177 | 187 |
178 def test_unsubscribeNotSubscribed(self): | 188 def test_unsubscribeNotSubscribed(self): |
191 d = s.unsubscribe(OWNER, 'test.example.org', 'test', OWNER) | 201 d = s.unsubscribe(OWNER, 'test.example.org', 'test', OWNER) |
192 self.assertFailure(d, StanzaError) | 202 self.assertFailure(d, StanzaError) |
193 d.addCallback(cb) | 203 d.addCallback(cb) |
194 return d | 204 return d |
195 | 205 |
206 | |
196 def test_getNodeInfo(self): | 207 def test_getNodeInfo(self): |
197 """ | 208 """ |
198 Test retrieving node information. | 209 Test retrieving node information. |
199 """ | 210 """ |
200 | 211 |
201 class TestBackend(BaseTestBackend): | 212 class TestBackend(BaseTestBackend): |
202 def get_node_type(self, nodeIdentifier): | 213 def getNodeType(self, nodeIdentifier): |
203 return defer.succeed('leaf') | 214 return defer.succeed('leaf') |
204 | 215 |
205 def get_node_meta_data(self, nodeIdentifier): | 216 def getNodeMetaData(self, nodeIdentifier): |
206 return defer.succeed({'pubsub#persist_items': True}) | 217 return defer.succeed({'pubsub#persist_items': True}) |
207 | 218 |
208 def cb(info): | 219 def cb(info): |
209 self.assertIn('type', info) | 220 self.assertIn('type', info) |
210 self.assertEquals('leaf', info['type']) | 221 self.assertEquals('leaf', info['type']) |