comparison 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
comparison
equal deleted inserted replaced
828:8f335c03eebb 829:187d2443c82d
128 return (Const.PROFILE_DICT[profile_key], None) 128 return (Const.PROFILE_DICT[profile_key], None)
129 129
130 def isConnected(self, profile): 130 def isConnected(self, profile):
131 return True 131 return True
132 132
133 def getSentMessage(self, message_index, profile_index): 133 def getSentMessageRaw(self, profile_index):
134 """Called by tests. FakeClient instances associated to each profile must have 134 """Pop and return the sent message in first position (works like a FIFO).
135 Called by tests. FakeClient instances associated to each profile must have
135 been previously initialized with the method FakeSAT.getClient. 136 been previously initialized with the method FakeSAT.getClient.
136 @return: XML representation of the <message_index>th sent message for given profile""" 137 @return: the sent message for given profile, or None"""
137 return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent[message_index] 138 try:
138 139 return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent.pop(0)
139 def countSentMessages(self, profiles=Const.PROFILE): 140 except IndexError:
140 """Called by tests. FakeClient instances associated to each profile must have 141 return None
142
143 def getSentMessage(self, profile_index):
144 """Pop and return the sent message in first position (works like a FIFO).
145 Called by tests. FakeClient instances associated to each profile must have
141 been previously initialized with the method FakeSAT.getClient. 146 been previously initialized with the method FakeSAT.getClient.
142 @param profiles: list of profiles 147 @return: XML representation of the sent message for given profile, or None"""
143 @return: a list containing the number of sent messages for each of the specified profiles""" 148 entry = self.getSentMessageRaw(profile_index)
144 result = [] 149 return entry.toXml() if entry else None
145 for profile in profiles:
146 result.append(len(self.profiles[profile].xmlstream.sent))
147 return result
148 150
149 151
150 class FakeBridge(object): 152 class FakeBridge(object):
151 """Class to simulate and test bridge calls""" 153 """Class to simulate and test bridge calls"""
152 154
153 def __init__(self): 155 def __init__(self):
154 self.methods = {} 156 self.expected_calls = {}
155 157
156 def expectCall(self, name, *check_args, **check_kwargs): 158 def expectCall(self, name, *check_args, **check_kwargs):
159 if hasattr(self, name): # queue this new call as one already exists
160 self.expected_calls.setdefault(name, [])
161 self.expected_calls[name].append((check_args, check_kwargs))
162 return
163
157 def checkCall(*args, **kwargs): 164 def checkCall(*args, **kwargs):
158 if args != check_args or kwargs != check_kwargs: 165 if args != check_args or kwargs != check_kwargs:
159 print "\n\n--------------------" 166 print "\n\n--------------------"
160 print "Args are not equals:" 167 print "Args are not equals:"
161 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args) 168 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args)
162 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs) 169 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs)
163 print "--------------------\n\n" 170 print "--------------------\n\n"
164 raise DifferentArgsException 171 raise DifferentArgsException
172 delattr(self, name)
173
174 if name in self.expected_calls: # register the next call
175 args, kwargs = self.expected_calls[name].pop(0)
176 if len(self.expected_calls[name]) == 0:
177 del self.expected_calls[name]
178 self.expectCall(name, *args, **kwargs)
165 179
166 setattr(self, name, checkCall) 180 setattr(self, name, checkCall)
167 181
168 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False): 182 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False):
169 pass 183 pass
295 def __init__(self): 309 def __init__(self):
296 self.sent = [] 310 self.sent = []
297 311
298 def send(self, obj): 312 def send(self, obj):
299 """Save the sent messages to compare them later""" 313 """Save the sent messages to compare them later"""
300 self.sent.append(obj.toXml()) 314 self.sent.append(obj)
301 315
302 316
303 class FakeClient(object): 317 class FakeClient(object):
304 """Tests involving more than one profile need one instance of this class per profile""" 318 """Tests involving more than one profile need one instance of this class per profile"""
305 319