diff src/test/helpers.py @ 829:187d2443c82d

test: improvements for the helpers classes: - simplification for retrieving the sent messages - an expected bridge call which is already set can no more be overwritten with new arguments for the same method name. The new arguments are stored in a FIFO list instead, and each call of the method also registers the arguments for the next call.
author souliane <souliane@mailoo.org>
date Wed, 15 Jan 2014 23:22:07 +0100
parents 1fe00f0c9a91
children 9bac2fc74968
line wrap: on
line diff
--- a/src/test/helpers.py	Fri Jan 17 15:02:46 2014 +0100
+++ b/src/test/helpers.py	Wed Jan 15 23:22:07 2014 +0100
@@ -130,30 +130,37 @@
     def isConnected(self, profile):
         return True
 
-    def getSentMessage(self, message_index, profile_index):
-        """Called by tests. FakeClient instances associated to each profile must have
+    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: XML representation of the <message_index>th sent message for given profile"""
-        return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent[message_index]
+        @return: the sent message for given profile, or None"""
+        try:
+            return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent.pop(0)
+        except IndexError:
+            return None
 
-    def countSentMessages(self, profiles=Const.PROFILE):
-        """Called by tests. FakeClient instances associated to each profile must have
+    def getSentMessage(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.
-        @param profiles: list of profiles
-        @return: a list containing the number of sent messages for each of the specified profiles"""
-        result = []
-        for profile in profiles:
-            result.append(len(self.profiles[profile].xmlstream.sent))
-        return result
+        @return: XML representation of the sent message for given profile, or None"""
+        entry = self.getSentMessageRaw(profile_index)
+        return entry.toXml() if entry else None
 
 
 class FakeBridge(object):
     """Class to simulate and test bridge calls"""
 
     def __init__(self):
-        self.methods = {}
+        self.expected_calls = {}
 
     def expectCall(self, name, *check_args, **check_kwargs):
+        if hasattr(self, name):  # queue this new call as one already exists
+            self.expected_calls.setdefault(name, [])
+            self.expected_calls[name].append((check_args, check_kwargs))
+            return
+
         def checkCall(*args, **kwargs):
             if args != check_args or kwargs != check_kwargs:
                 print "\n\n--------------------"
@@ -162,6 +169,13 @@
                 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs)
                 print "--------------------\n\n"
                 raise DifferentArgsException
+            delattr(self, name)
+
+            if name in self.expected_calls:  # register the next call
+                args, kwargs = self.expected_calls[name].pop(0)
+                if len(self.expected_calls[name]) == 0:
+                    del self.expected_calls[name]
+                self.expectCall(name, *args, **kwargs)
 
         setattr(self, name, checkCall)
 
@@ -297,7 +311,7 @@
 
     def send(self, obj):
         """Save the sent messages to compare them later"""
-        self.sent.append(obj.toXml())
+        self.sent.append(obj)
 
 
 class FakeClient(object):