changeset 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 29cea30f20f5
children a5ad628c3cbf
files src/test/helpers.py src/test/test_core_xmpp.py
diffstat 2 files changed, 42 insertions(+), 5 deletions(-) [+]
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):
--- a/src/test/test_core_xmpp.py	Thu Sep 26 17:33:04 2013 +0200
+++ b/src/test/test_core_xmpp.py	Sun Sep 29 16:29:36 2013 +0200
@@ -46,7 +46,7 @@
     def setUp(self):
         self.host = helpers.FakeSAT()
         self.message = xmpp.SatMessageProtocol(self.host)
-        self.message.parent = helpers.FakeParent()
+        self.message.parent = helpers.FakeClient(self.host)
 
     def test_onMessage(self):
         xml = """
@@ -64,7 +64,7 @@
     def setUp(self):
         self.host = helpers.FakeSAT()
         self.roster = xmpp.SatRosterProtocol(self.host)
-        self.roster.parent = helpers.FakeParent()
+        self.roster.parent = helpers.FakeClient(self.host)
 
     def test_onRosterSet(self):
         roster_item = RosterItem(helpers.TEST_JID)
@@ -82,7 +82,7 @@
     def setUp(self):
         self.host = helpers.FakeSAT()
         self.presence = xmpp.SatPresenceProtocol(self.host)
-        self.presence.parent = helpers.FakeParent()
+        self.presence.parent = helpers.FakeClient(self.host)
 
     def test_availableReceived(self):
         self.host.bridge.expectCall("presenceUpdate", helpers.TEST_JID_STR, "xa", 15, {'default': "test status", 'fr': 'statut de test'}, helpers.TEST_PROFILE)