diff src/test/helpers.py @ 1271:2308f8405ffb

test: refactoring: - rename internal init methods to reinit - rename getSentMessage to getSentMessageXml - rename getSentMessageRaw to getSentMessage
author souliane <souliane@mailoo.org>
date Fri, 19 Dec 2014 11:36:00 +0100
parents 91e5becc6623
children 3a3e3014f9f8
line wrap: on
line diff
--- a/src/test/helpers.py	Mon Dec 15 15:29:08 2014 +0100
+++ b/src/test/helpers.py	Fri Dec 19 11:36:00 2014 +0100
@@ -64,12 +64,15 @@
         self.bridge = FakeBridge()
         self.memory = FakeMemory(self)
         self.trigger = FakeTriggerManager()
-        self.init()
+        self.profiles = {}
+        self.reinit()
 
-    def init(self):
+    def reinit(self):
         """This can be called by tests that check for sent and stored messages,
         uses FakeClient or get/set some other data that need to be cleaned"""
-        self.sent_messages = []
+        for profile in self.profiles:
+            self.profiles[profile].reinit()
+        self.memory.reinit()
         self.stored_messages = []
         self.plugins = {}
         self.profiles = {}
@@ -90,7 +93,7 @@
         @param mess_data: message data dictionnary
         @param client: profile's client
         """
-        self.sent_messages.append(mess_data["to"])
+        client.xmlstream.send(mess_data['xml'])
         return mess_data
 
     def _storeMessage(self, mess_data, client):
@@ -128,13 +131,18 @@
     def isConnected(self, profile):
         return True
 
-    def getSentMessageRaw(self, profile_index):
-        """Pop and return the sent message in first position (works like a FIFO).
-        Called by tests. FakeClient instances associated to each profile must have
-        been previously initialized with the method FakeSAT.getClient.
-        @return: the sent message for given profile, or None"""
+    def getSentMessages(self, profile_index):
+        """Return all the sent messages (in the order they have been sent) and
+        empty the list. Called by tests. FakeClient instances associated to each
+        profile must have been previously initialized with the method
+        FakeSAT.getClient.
+
+        @param profile_index: index of the profile to consider (cf. C.PROFILE)
+        @return: the sent messages for given profile, or None"""
         try:
-            return self.profiles[C.PROFILE[profile_index]].xmlstream.sent.pop(0)
+            tmp = self.profiles[C.PROFILE[profile_index]].xmlstream.sent
+            self.profiles[C.PROFILE[profile_index]].xmlstream.sent = []
+            return tmp
         except IndexError:
             return None
 
@@ -142,8 +150,20 @@
         """Pop and return the sent message in first position (works like a FIFO).
         Called by tests. FakeClient instances associated to each profile must have
         been previously initialized with the method FakeSAT.getClient.
+
+        @param profile_index: index of the profile to consider (cf. C.PROFILE)
+        @return: the sent message for given profile, or None"""
+        try:
+            return self.profiles[C.PROFILE[profile_index]].xmlstream.sent.pop(0)
+        except IndexError:
+            return None
+
+    def getSentMessageXml(self, profile_index):
+        """Pop and return the sent message in first position (works like a FIFO).
+        Called by tests. FakeClient instances associated to each profile must have
+        been previously initialized with the method FakeSAT.getClient.
         @return: XML representation of the sent message for given profile, or None"""
-        entry = self.getSentMessageRaw(profile_index)
+        entry = self.getSentMessage(profile_index)
         return entry.toXml() if entry else None
 
     def findFeaturesSet(self, features, category=None, type_=None, jid_=None, profile_key=None):
@@ -258,9 +278,9 @@
         self.host = host
         self.params = FakeParams(host, None)
         self.config = self.parseMainConf()
-        self.init()
+        self.reinit()
 
-    def init(self):
+    def reinit(self):
         """Tests that manipulate params, entities, features should
         re-initialise the memory first to not fake the result."""
         self.params.load_default_params()
@@ -350,6 +370,9 @@
         self.roster = FakeRosterProtocol(host, self)
         self.xmlstream = FakeXmlStream()
 
+    def reinit(self):
+        self.xmlstream = FakeXmlStream()
+
     def send(self, obj):
         return self.xmlstream.send(obj)