comparison 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
comparison
equal deleted inserted replaced
1270:037ec0795a85 1271:2308f8405ffb
62 62
63 def __init__(self): 63 def __init__(self):
64 self.bridge = FakeBridge() 64 self.bridge = FakeBridge()
65 self.memory = FakeMemory(self) 65 self.memory = FakeMemory(self)
66 self.trigger = FakeTriggerManager() 66 self.trigger = FakeTriggerManager()
67 self.init() 67 self.profiles = {}
68 68 self.reinit()
69 def init(self): 69
70 def reinit(self):
70 """This can be called by tests that check for sent and stored messages, 71 """This can be called by tests that check for sent and stored messages,
71 uses FakeClient or get/set some other data that need to be cleaned""" 72 uses FakeClient or get/set some other data that need to be cleaned"""
72 self.sent_messages = [] 73 for profile in self.profiles:
74 self.profiles[profile].reinit()
75 self.memory.reinit()
73 self.stored_messages = [] 76 self.stored_messages = []
74 self.plugins = {} 77 self.plugins = {}
75 self.profiles = {} 78 self.profiles = {}
76 79
77 def delContact(self, to, profile_key): 80 def delContact(self, to, profile_key):
88 """Save the information to check later to whom messages have been sent. 91 """Save the information to check later to whom messages have been sent.
89 92
90 @param mess_data: message data dictionnary 93 @param mess_data: message data dictionnary
91 @param client: profile's client 94 @param client: profile's client
92 """ 95 """
93 self.sent_messages.append(mess_data["to"]) 96 client.xmlstream.send(mess_data['xml'])
94 return mess_data 97 return mess_data
95 98
96 def _storeMessage(self, mess_data, client): 99 def _storeMessage(self, mess_data, client):
97 """Save the information to check later if entries have been added to the history. 100 """Save the information to check later if entries have been added to the history.
98 101
126 return (C.PROFILE_DICT[profile_key], None) 129 return (C.PROFILE_DICT[profile_key], None)
127 130
128 def isConnected(self, profile): 131 def isConnected(self, profile):
129 return True 132 return True
130 133
131 def getSentMessageRaw(self, profile_index): 134 def getSentMessages(self, profile_index):
135 """Return all the sent messages (in the order they have been sent) and
136 empty the list. Called by tests. FakeClient instances associated to each
137 profile must have been previously initialized with the method
138 FakeSAT.getClient.
139
140 @param profile_index: index of the profile to consider (cf. C.PROFILE)
141 @return: the sent messages for given profile, or None"""
142 try:
143 tmp = self.profiles[C.PROFILE[profile_index]].xmlstream.sent
144 self.profiles[C.PROFILE[profile_index]].xmlstream.sent = []
145 return tmp
146 except IndexError:
147 return None
148
149 def getSentMessage(self, profile_index):
132 """Pop and return the sent message in first position (works like a FIFO). 150 """Pop and return the sent message in first position (works like a FIFO).
133 Called by tests. FakeClient instances associated to each profile must have 151 Called by tests. FakeClient instances associated to each profile must have
134 been previously initialized with the method FakeSAT.getClient. 152 been previously initialized with the method FakeSAT.getClient.
153
154 @param profile_index: index of the profile to consider (cf. C.PROFILE)
135 @return: the sent message for given profile, or None""" 155 @return: the sent message for given profile, or None"""
136 try: 156 try:
137 return self.profiles[C.PROFILE[profile_index]].xmlstream.sent.pop(0) 157 return self.profiles[C.PROFILE[profile_index]].xmlstream.sent.pop(0)
138 except IndexError: 158 except IndexError:
139 return None 159 return None
140 160
141 def getSentMessage(self, profile_index): 161 def getSentMessageXml(self, profile_index):
142 """Pop and return the sent message in first position (works like a FIFO). 162 """Pop and return the sent message in first position (works like a FIFO).
143 Called by tests. FakeClient instances associated to each profile must have 163 Called by tests. FakeClient instances associated to each profile must have
144 been previously initialized with the method FakeSAT.getClient. 164 been previously initialized with the method FakeSAT.getClient.
145 @return: XML representation of the sent message for given profile, or None""" 165 @return: XML representation of the sent message for given profile, or None"""
146 entry = self.getSentMessageRaw(profile_index) 166 entry = self.getSentMessage(profile_index)
147 return entry.toXml() if entry else None 167 return entry.toXml() if entry else None
148 168
149 def findFeaturesSet(self, features, category=None, type_=None, jid_=None, profile_key=None): 169 def findFeaturesSet(self, features, category=None, type_=None, jid_=None, profile_key=None):
150 """Call self.addFeature from your tests to change the return value. 170 """Call self.addFeature from your tests to change the return value.
151 171
256 # do not call Memory.__init__, we just want to call the methods that are 276 # do not call Memory.__init__, we just want to call the methods that are
257 # manipulating basic stuff, the others should be overwritten when needed 277 # manipulating basic stuff, the others should be overwritten when needed
258 self.host = host 278 self.host = host
259 self.params = FakeParams(host, None) 279 self.params = FakeParams(host, None)
260 self.config = self.parseMainConf() 280 self.config = self.parseMainConf()
261 self.init() 281 self.reinit()
262 282
263 def init(self): 283 def reinit(self):
264 """Tests that manipulate params, entities, features should 284 """Tests that manipulate params, entities, features should
265 re-initialise the memory first to not fake the result.""" 285 re-initialise the memory first to not fake the result."""
266 self.params.load_default_params() 286 self.params.load_default_params()
267 self.params.params.clear() 287 self.params.params.clear()
268 self.params.frontends_cache = [] 288 self.params.frontends_cache = []
346 def __init__(self, host, profile=None): 366 def __init__(self, host, profile=None):
347 self.host = host 367 self.host = host
348 self.profile = profile if profile else C.PROFILE[0] 368 self.profile = profile if profile else C.PROFILE[0]
349 self.jid = C.PROFILE_DICT[self.profile] 369 self.jid = C.PROFILE_DICT[self.profile]
350 self.roster = FakeRosterProtocol(host, self) 370 self.roster = FakeRosterProtocol(host, self)
371 self.xmlstream = FakeXmlStream()
372
373 def reinit(self):
351 self.xmlstream = FakeXmlStream() 374 self.xmlstream = FakeXmlStream()
352 375
353 def send(self, obj): 376 def send(self, obj):
354 return self.xmlstream.send(obj) 377 return self.xmlstream.send(obj)
355 378