diff src/test/helpers.py @ 786:c3acc1298a2f

test: FakeMemory inherits from Memory + more helpers basic support + cleaning
author souliane <souliane@mailoo.org>
date Sun, 05 Jan 2014 13:04:54 +0100
parents 0e5807193721
children 0cb423500fbb
line wrap: on
line diff
--- a/src/test/helpers.py	Sun Jan 05 13:00:17 2014 +0100
+++ b/src/test/helpers.py	Sun Jan 05 13:04:54 2014 +0100
@@ -18,15 +18,16 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.i18n import _
+from sat.core import exceptions
 from constants import Const
 from wokkel.xmppim import RosterItem
 from sat.core.xmpp import SatRosterProtocol
-from sat.memory.memory import Params
+from sat.memory.memory import Params, Memory
 from twisted.trial.unittest import FailTest
 from twisted.trial import unittest
 from twisted.internet import defer
+from twisted.words.protocols.jabber.jid import JID
 from xml.etree import cElementTree as etree
-from sat.core import exceptions
 import re
 
 
@@ -54,6 +55,12 @@
         self.bridge = FakeBridge()
         self.memory = FakeMemory(self)
         self.trigger = FakeTriggerManager()
+        self.init()
+
+    def init(self):
+        """This can be called by tests that check for sent and stored messages"""
+        self.sent_messages = []
+        self.stored_messages = []
 
     def delContact(self, to, profile_key):
         #TODO
@@ -66,8 +73,34 @@
         pass
 
     def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'):
+        self.sendAndStoreMessage({"to": JID(to_s)})
+
+    def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None):
+        if not skip_send:
+            self.sent_messages.append(mess_data["to"])
+        self.stored_messages.append(mess_data["to"])
         pass
 
+    def requestServerDisco(self, feature, jid_=None, cache_only=False, profile_key="@NONE"):
+        """Discover if a server or its items offer a given feature
+        @param feature: the feature to check
+        @param jid_: the jid of the server, local server if None
+        @param cache_only: expect the result to be in cache and don't actually
+        make any request. This can be used anytime for requesting a feature on
+        the local server because the data are cached for sure.
+        @result: the Deferred entity jid offering the feature, or None
+        """
+        profile = self.memory.getProfileName(profile_key)
+        self.memory.server_features.setdefault(profile, {})
+        if jid_ is None:
+            jid_ = self.getClientHostJid(profile_key)
+        # call FakeMemory.init and FakeMemory.addServerFeature
+        # in your tests to change the return value of this method
+        return defer.succeed(jid_ if self.memory.hasServerFeature(feature, jid_, profile_key) else None)
+
+    def getClientHostJid(self, profile_key):
+        return JID(Const.TEST_JID.host)
+
 
 class FakeBridge(object):
     """Class to simulate and test bridge calls"""
@@ -122,20 +155,23 @@
         return defer.succeed(None)
 
 
-class FakeMemory(object):
+class FakeMemory(Memory):
     """Class to simulate and test memory object"""
 
     def __init__(self, host):
+        # do not call Memory.__init__, we just want to call the methods that are
+        # manipulating basic stuff, the others should be overwritten when needed
         self.host = host
         self.params = FakeParams(host, None)
         self.init()
 
     def init(self):
-        """Tests that manipulate params and/or entities should
+        """Tests that manipulate params, entities, features should
         re-initialise the memory first to not fake the result."""
         self.params.load_default_params()
         self.params.params.clear()
-        self.entities_data = {}  # naive simulation of entities
+        self.entities_data = {}
+        self.server_features = {}
 
     def getProfileName(self, profile_key, return_profile_keys=False):
         return self.params.getProfileName(profile_key, return_profile_keys)
@@ -155,22 +191,6 @@
     def delWaitingSub(self, contact_jid, profile_key):
         pass
 
-    def getParams(self, security_limit=Const.NO_SECURITY_LIMIT, app='', profile_key='@NONE@'):
-        """profile_key is set to @DEFAULT@ to avoid specifying it always"""
-        return self.params.getParams(security_limit, app, profile_key='@DEFAULT@')
-
-    def updateParams(self, xml, security_limit=Const.SECURITY_LIMIT, app=''):
-        self.params.updateParams(xml, security_limit, app)
-
-    def paramsRegisterApp(self, xml, security_limit=Const.NO_SECURITY_LIMIT, app=''):
-        return self.params.paramsRegisterApp(xml, security_limit, app)
-
-    def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'):
-        self.params.setParam(name, value, category, security_limit, profile_key)
-
-    def getParamA(self, name, category, attr="value", profile_key='@NONE@'):
-        return self.params.getParamA(name, category, attr, profile_key)
-
     def updateEntityData(self, entity_jid, key, value, profile_key):
         self.entities_data.setdefault(entity_jid, {})
         self.entities_data[entity_jid][key] = value
@@ -184,7 +204,7 @@
 
 class FakeTriggerManager(object):
 
-    def add(self, point_name, callback):
+    def add(self, point_name, callback, priority=0):
         pass
 
     def point(self, point_name, *args, **kwargs):