diff idavoll/test/test_storage.py @ 206:274a45d2a5ab

Implement root collection that includes all leaf nodes.
author Ralph Meijer <ralphm@ik.nu>
date Mon, 04 Aug 2008 13:47:10 +0000
parents b4bf0a5ce50d
children bb7103da9879
line wrap: on
line diff
--- a/idavoll/test/test_storage.py	Mon Aug 04 07:10:45 2008 +0000
+++ b/idavoll/test/test_storage.py	Mon Aug 04 13:47:10 2008 +0000
@@ -5,6 +5,7 @@
 Tests for L{idavoll.memory_storage} and L{idavoll.pgsql_storage}.
 """
 
+from zope.interface.verify import verifyObject
 from twisted.trial import unittest
 from twisted.words.protocols.jabber import jid
 from twisted.internet import defer
@@ -12,7 +13,7 @@
 
 from wokkel import pubsub
 
-from idavoll import error
+from idavoll import error, iidavoll
 
 OWNER = jid.JID('owner@example.com')
 SUBSCRIBER = jid.JID('subscriber@example.com/Home')
@@ -20,17 +21,16 @@
 SUBSCRIBER_TO_BE_DELETED = jid.JID('to_be_deleted@example.com/Home')
 SUBSCRIBER_PENDING = jid.JID('pending@example.com/Home')
 PUBLISHER = jid.JID('publisher@example.com')
-ITEM = domish.Element((pubsub.NS_PUBSUB, 'item'), pubsub.NS_PUBSUB)
+ITEM = domish.Element((None, 'item'))
 ITEM['id'] = 'current'
 ITEM.addElement(('testns', 'test'), content=u'Test \u2083 item')
-ITEM_NEW = domish.Element((pubsub.NS_PUBSUB, 'item'), pubsub.NS_PUBSUB)
+ITEM_NEW = domish.Element((None, 'item'))
 ITEM_NEW['id'] = 'new'
 ITEM_NEW.addElement(('testns', 'test'), content=u'Test \u2083 item')
-ITEM_UPDATED = domish.Element((pubsub.NS_PUBSUB, 'item'), pubsub.NS_PUBSUB)
+ITEM_UPDATED = domish.Element((None, 'item'))
 ITEM_UPDATED['id'] = 'current'
 ITEM_UPDATED.addElement(('testns', 'test'), content=u'Test \u2084 item')
-ITEM_TO_BE_DELETED = domish.Element((pubsub.NS_PUBSUB, 'item'),
-                                    pubsub.NS_PUBSUB)
+ITEM_TO_BE_DELETED = domish.Element((None, 'item'))
 ITEM_TO_BE_DELETED['id'] = 'to-be-deleted'
 ITEM_TO_BE_DELETED.addElement(('testns', 'test'), content=u'Test \u2083 item')
 
@@ -53,6 +53,18 @@
         return d
 
 
+    def test_interfaceIStorage(self):
+        self.assertTrue(verifyObject(iidavoll.IStorage, self.s))
+
+
+    def test_interfaceINode(self):
+        self.assertTrue(verifyObject(iidavoll.INode, self.node))
+
+
+    def test_interfaceILeafNode(self):
+        self.assertTrue(verifyObject(iidavoll.ILeafNode, self.node))
+
+
     def test_getNode(self):
         return self.s.getNode('pre-existing')
 
@@ -72,7 +84,9 @@
 
 
     def test_createExistingNode(self):
-        d = self.s.createNode('pre-existing', OWNER)
+        config = self.s.getDefaultConfiguration('leaf')
+        config['pubsub#node_type'] = 'leaf'
+        d = self.s.createNode('pre-existing', OWNER, config)
         self.assertFailure(d, error.NodeExists)
         return d
 
@@ -82,7 +96,9 @@
             d = self.s.getNode('new 1')
             return d
 
-        d = self.s.createNode('new 1', OWNER)
+        config = self.s.getDefaultConfiguration('leaf')
+        config['pubsub#node_type'] = 'leaf'
+        d = self.s.createNode('new 1', OWNER, config)
         d.addCallback(cb)
         return d
 
@@ -115,7 +131,13 @@
 
     def test_getSubscriptions(self):
         def cb(subscriptions):
-            self.assertIn(('pre-existing', SUBSCRIBER, 'subscribed'), subscriptions)
+            found = False
+            for subscription in subscriptions:
+                if (subscription.nodeIdentifier == 'pre-existing' and
+                    subscription.subscriber == SUBSCRIBER and
+                    subscription.state == 'subscribed'):
+                    found = True
+            self.assertTrue(found)
 
         d = self.s.getSubscriptions(SUBSCRIBER)
         d.addCallback(cb)
@@ -192,30 +214,30 @@
         def cb1(void):
             return self.node.getSubscription(SUBSCRIBER_NEW)
 
-        def cb2(state):
-            self.assertEqual(state, 'pending')
+        def cb2(subscription):
+            self.assertEqual(subscription.state, 'pending')
 
-        d = self.node.addSubscription(SUBSCRIBER_NEW, 'pending')
+        d = self.node.addSubscription(SUBSCRIBER_NEW, 'pending', {})
         d.addCallback(cb1)
         d.addCallback(cb2)
         return d
 
 
     def test_addExistingSubscription(self):
-        d = self.node.addSubscription(SUBSCRIBER, 'pending')
+        d = self.node.addSubscription(SUBSCRIBER, 'pending', {})
         self.assertFailure(d, error.SubscriptionExists)
         return d
 
 
     def test_getSubscription(self):
         def cb(subscriptions):
-            self.assertEquals(subscriptions[0][1], 'subscribed')
-            self.assertEquals(subscriptions[1][1], 'pending')
-            self.assertEquals(subscriptions[2][1], None)
+            self.assertEquals(subscriptions[0].state, 'subscribed')
+            self.assertEquals(subscriptions[1].state, 'pending')
+            self.assertEquals(subscriptions[2], None)
 
-        d = defer.DeferredList([self.node.getSubscription(SUBSCRIBER),
-                                self.node.getSubscription(SUBSCRIBER_PENDING),
-                                self.node.getSubscription(OWNER)])
+        d = defer.gatherResults([self.node.getSubscription(SUBSCRIBER),
+                                 self.node.getSubscription(SUBSCRIBER_PENDING),
+                                 self.node.getSubscription(OWNER)])
         d.addCallback(cb)
         return d
 
@@ -230,13 +252,17 @@
         return d
 
 
-    def test_getSubscribers(self):
+    def test_getNodeSubscriptions(self):
+        def extractSubscribers(subscriptions):
+            return [subscription.subscriber for subscription in subscriptions]
+
         def cb(subscribers):
             self.assertIn(SUBSCRIBER, subscribers)
             self.assertNotIn(SUBSCRIBER_PENDING, subscribers)
             self.assertNotIn(OWNER, subscribers)
 
-        d = self.node.getSubscribers()
+        d = self.node.getSubscriptions('subscribed')
+        d.addCallback(extractSubscribers)
         d.addCallback(cb)
         return d
 
@@ -381,7 +407,9 @@
 
     def setUp(self):
         from idavoll.memory_storage import Storage, PublishedItem, LeafNode
-        from idavoll.memory_storage import Subscription, defaultConfig
+        from idavoll.memory_storage import Subscription
+
+        defaultConfig = Storage.defaultConfig['leaf']
 
         self.s = Storage()
         self.s._nodes['pre-existing'] = \
@@ -394,11 +422,15 @@
                 LeafNode('to-be-purged', OWNER, None)
 
         subscriptions = self.s._nodes['pre-existing']._subscriptions
-        subscriptions[SUBSCRIBER.full()] = Subscription('subscribed')
+        subscriptions[SUBSCRIBER.full()] = Subscription('pre-existing',
+                                                        SUBSCRIBER,
+                                                        'subscribed')
         subscriptions[SUBSCRIBER_TO_BE_DELETED.full()] = \
-                Subscription('subscribed')
+                Subscription('pre-existing', SUBSCRIBER_TO_BE_DELETED,
+                             'subscribed')
         subscriptions[SUBSCRIBER_PENDING.full()] = \
-                Subscription('pending')
+                Subscription('pre-existing', SUBSCRIBER_PENDING,
+                             'pending')
 
         item = PublishedItem(ITEM_TO_BE_DELETED, PUBLISHER)
         self.s._nodes['pre-existing']._items['to-be-deleted'] = item
@@ -436,7 +468,9 @@
 
     def init(self, cursor):
         self.cleandb(cursor)
-        cursor.execute("""INSERT INTO nodes (node) VALUES ('pre-existing')""")
+        cursor.execute("""INSERT INTO nodes
+                          (node, node_type, persist_items)
+                          VALUES ('pre-existing', 'leaf', TRUE)""")
         cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-deleted')""")
         cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-reconfigured')""")
         cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-purged')""")
@@ -444,15 +478,15 @@
                        OWNER.userhost())
         cursor.execute("""INSERT INTO affiliations
                           (node_id, entity_id, affiliation)
-                          SELECT nodes.id, entities.id, 'owner'
+                          SELECT node_id, entity_id, 'owner'
                           FROM nodes, entities
                           WHERE node='pre-existing' AND jid=%s""",
                        OWNER.userhost())
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
                        SUBSCRIBER.userhost())
         cursor.execute("""INSERT INTO subscriptions
-                          (node_id, entity_id, resource, subscription)
-                          SELECT nodes.id, entities.id, %s, 'subscribed'
+                          (node_id, entity_id, resource, state)
+                          SELECT node_id, entity_id, %s, 'subscribed'
                           FROM nodes, entities
                           WHERE node='pre-existing' AND jid=%s""",
                        (SUBSCRIBER.resource,
@@ -460,8 +494,8 @@
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
                        SUBSCRIBER_TO_BE_DELETED.userhost())
         cursor.execute("""INSERT INTO subscriptions
-                          (node_id, entity_id, resource, subscription)
-                          SELECT nodes.id, entities.id, %s, 'subscribed'
+                          (node_id, entity_id, resource, state)
+                          SELECT node_id, entity_id, %s, 'subscribed'
                           FROM nodes, entities
                           WHERE node='pre-existing' AND jid=%s""",
                        (SUBSCRIBER_TO_BE_DELETED.resource,
@@ -469,8 +503,8 @@
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
                        SUBSCRIBER_PENDING.userhost())
         cursor.execute("""INSERT INTO subscriptions
-                          (node_id, entity_id, resource, subscription)
-                          SELECT nodes.id, entities.id, %s, 'pending'
+                          (node_id, entity_id, resource, state)
+                          SELECT node_id, entity_id, %s, 'pending'
                           FROM nodes, entities
                           WHERE node='pre-existing' AND jid=%s""",
                        (SUBSCRIBER_PENDING.resource,
@@ -479,20 +513,20 @@
                        PUBLISHER.userhost())
         cursor.execute("""INSERT INTO items
                           (node_id, publisher, item, data, date)
-                          SELECT nodes.id, %s, 'to-be-deleted', %s,
+                          SELECT node_id, %s, 'to-be-deleted', %s,
                                  now() - interval '1 day'
                           FROM nodes
                           WHERE node='pre-existing'""",
                        (PUBLISHER.userhost(),
                         ITEM_TO_BE_DELETED.toXml()))
         cursor.execute("""INSERT INTO items (node_id, publisher, item, data)
-                          SELECT nodes.id, %s, 'to-be-deleted', %s
+                          SELECT node_id, %s, 'to-be-deleted', %s
                           FROM nodes
                           WHERE node='to-be-purged'""",
                        (PUBLISHER.userhost(),
                         ITEM_TO_BE_DELETED.toXml()))
         cursor.execute("""INSERT INTO items (node_id, publisher, item, data)
-                          SELECT nodes.id, %s, 'current', %s
+                          SELECT node_id, %s, 'current', %s
                           FROM nodes
                           WHERE node='pre-existing'""",
                        (PUBLISHER.userhost(),