diff src/test/helpers.py @ 649:e20c823f23e2

tests: helpers improvments: - FakeParent renamed FakeClient - added FakeRoster class, and an instance of it in FakeClient (FakeClient.roster) - b2s function to convert boolean value to strings used in bridge - some fake methods (FakeClient.send, FakeSat.delContact) to avoid exceptions
author Goffi <goffi@goffi.org>
date Sun, 29 Sep 2013 16:29:36 +0200
parents 63b6e684bbfa
children ffb716804580
line wrap: on
line diff
--- a/src/test/helpers.py	Thu Sep 26 17:33:04 2013 +0200
+++ b/src/test/helpers.py	Sun Sep 29 16:29:36 2013 +0200
@@ -19,12 +19,21 @@
 
 import __builtin__
 from twisted.words.protocols.jabber.jid import JID
+from wokkel.xmppim import RosterItem
+from sat.core.xmpp import SatRosterProtocol
 from twisted.trial.unittest import FailTest
 
 TEST_JID_STR = u"test@example.org/SàT"
 TEST_JID = JID(u"test@example.org/SàT")
 TEST_PROFILE = 'test_profile'
 
+def b2s(value):
+    """Convert a bool to a unicode string used in bridge
+    @param value: boolean value
+    @return: unicode conversion, according to bridge convention
+
+    """
+    return  u"True" if value else u"False"
 
 class DifferentArgsException(FailTest):
     pass
@@ -38,6 +47,10 @@
         self.memory = FakeMemory()
         self.trigger = FakeTriggerManager()
 
+    def delContact(self, to, profile_key):
+        #TODO
+        pass
+
 
 class FakeBridge(object):
     """Class to simulate and test bridge calls"""
@@ -86,11 +99,35 @@
         """We always return true to continue the action"""
         return True
 
+class FakeRosterProtocol(SatRosterProtocol):
 
-class FakeParent(object):
-    def __init__(self):
+    def __init__(self, host, parent):
+        SatRosterProtocol.__init__(self, host)
+        self.parent = parent
+        self.addItem(TEST_JID)
+
+    def addItem(self, jid, *args, **kwargs):
+        if not args and not kwargs:
+            # defaults values setted for the tests only
+            kwargs["subscriptionTo"] = True
+            kwargs["subscriptionFrom"] = True
+        roster_item = RosterItem(jid, *args, **kwargs)
+        attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)}
+        if roster_item.name:
+            attrs['name'] = roster_item.name
+        self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) 
+        self.onRosterSet(roster_item)
+
+
+class FakeClient(object):
+    def __init__(self, host):
+        self.host = host
         self.profile = 'test_profile'
         self.jid = TEST_JID
+        self.roster = FakeRosterProtocol(host, self)
+
+    def send(self, obj):
+        pass
 
 
 def _(text):