Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
785:ff9a52077b36 | 786:c3acc1298a2f |
---|---|
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core import exceptions | |
21 from constants import Const | 22 from constants import Const |
22 from wokkel.xmppim import RosterItem | 23 from wokkel.xmppim import RosterItem |
23 from sat.core.xmpp import SatRosterProtocol | 24 from sat.core.xmpp import SatRosterProtocol |
24 from sat.memory.memory import Params | 25 from sat.memory.memory import Params, Memory |
25 from twisted.trial.unittest import FailTest | 26 from twisted.trial.unittest import FailTest |
26 from twisted.trial import unittest | 27 from twisted.trial import unittest |
27 from twisted.internet import defer | 28 from twisted.internet import defer |
29 from twisted.words.protocols.jabber.jid import JID | |
28 from xml.etree import cElementTree as etree | 30 from xml.etree import cElementTree as etree |
29 from sat.core import exceptions | |
30 import re | 31 import re |
31 | 32 |
32 | 33 |
33 def b2s(value): | 34 def b2s(value): |
34 """Convert a bool to a unicode string used in bridge | 35 """Convert a bool to a unicode string used in bridge |
52 | 53 |
53 def __init__(self): | 54 def __init__(self): |
54 self.bridge = FakeBridge() | 55 self.bridge = FakeBridge() |
55 self.memory = FakeMemory(self) | 56 self.memory = FakeMemory(self) |
56 self.trigger = FakeTriggerManager() | 57 self.trigger = FakeTriggerManager() |
58 self.init() | |
59 | |
60 def init(self): | |
61 """This can be called by tests that check for sent and stored messages""" | |
62 self.sent_messages = [] | |
63 self.stored_messages = [] | |
57 | 64 |
58 def delContact(self, to, profile_key): | 65 def delContact(self, to, profile_key): |
59 #TODO | 66 #TODO |
60 pass | 67 pass |
61 | 68 |
64 | 71 |
65 def registerNewAccountCB(self, data, profile): | 72 def registerNewAccountCB(self, data, profile): |
66 pass | 73 pass |
67 | 74 |
68 def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'): | 75 def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'): |
69 pass | 76 self.sendAndStoreMessage({"to": JID(to_s)}) |
77 | |
78 def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None): | |
79 if not skip_send: | |
80 self.sent_messages.append(mess_data["to"]) | |
81 self.stored_messages.append(mess_data["to"]) | |
82 pass | |
83 | |
84 def requestServerDisco(self, feature, jid_=None, cache_only=False, profile_key="@NONE"): | |
85 """Discover if a server or its items offer a given feature | |
86 @param feature: the feature to check | |
87 @param jid_: the jid of the server, local server if None | |
88 @param cache_only: expect the result to be in cache and don't actually | |
89 make any request. This can be used anytime for requesting a feature on | |
90 the local server because the data are cached for sure. | |
91 @result: the Deferred entity jid offering the feature, or None | |
92 """ | |
93 profile = self.memory.getProfileName(profile_key) | |
94 self.memory.server_features.setdefault(profile, {}) | |
95 if jid_ is None: | |
96 jid_ = self.getClientHostJid(profile_key) | |
97 # call FakeMemory.init and FakeMemory.addServerFeature | |
98 # in your tests to change the return value of this method | |
99 return defer.succeed(jid_ if self.memory.hasServerFeature(feature, jid_, profile_key) else None) | |
100 | |
101 def getClientHostJid(self, profile_key): | |
102 return JID(Const.TEST_JID.host) | |
70 | 103 |
71 | 104 |
72 class FakeBridge(object): | 105 class FakeBridge(object): |
73 """Class to simulate and test bridge calls""" | 106 """Class to simulate and test bridge calls""" |
74 | 107 |
120 def loadIndParams(self, profile, cache=None): | 153 def loadIndParams(self, profile, cache=None): |
121 self.params[profile] = {} | 154 self.params[profile] = {} |
122 return defer.succeed(None) | 155 return defer.succeed(None) |
123 | 156 |
124 | 157 |
125 class FakeMemory(object): | 158 class FakeMemory(Memory): |
126 """Class to simulate and test memory object""" | 159 """Class to simulate and test memory object""" |
127 | 160 |
128 def __init__(self, host): | 161 def __init__(self, host): |
162 # do not call Memory.__init__, we just want to call the methods that are | |
163 # manipulating basic stuff, the others should be overwritten when needed | |
129 self.host = host | 164 self.host = host |
130 self.params = FakeParams(host, None) | 165 self.params = FakeParams(host, None) |
131 self.init() | 166 self.init() |
132 | 167 |
133 def init(self): | 168 def init(self): |
134 """Tests that manipulate params and/or entities should | 169 """Tests that manipulate params, entities, features should |
135 re-initialise the memory first to not fake the result.""" | 170 re-initialise the memory first to not fake the result.""" |
136 self.params.load_default_params() | 171 self.params.load_default_params() |
137 self.params.params.clear() | 172 self.params.params.clear() |
138 self.entities_data = {} # naive simulation of entities | 173 self.entities_data = {} |
174 self.server_features = {} | |
139 | 175 |
140 def getProfileName(self, profile_key, return_profile_keys=False): | 176 def getProfileName(self, profile_key, return_profile_keys=False): |
141 return self.params.getProfileName(profile_key, return_profile_keys) | 177 return self.params.getProfileName(profile_key, return_profile_keys) |
142 | 178 |
143 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): | 179 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): |
152 def addWaitingSub(self, type_, contact_jid, profile_key): | 188 def addWaitingSub(self, type_, contact_jid, profile_key): |
153 pass | 189 pass |
154 | 190 |
155 def delWaitingSub(self, contact_jid, profile_key): | 191 def delWaitingSub(self, contact_jid, profile_key): |
156 pass | 192 pass |
157 | |
158 def getParams(self, security_limit=Const.NO_SECURITY_LIMIT, app='', profile_key='@NONE@'): | |
159 """profile_key is set to @DEFAULT@ to avoid specifying it always""" | |
160 return self.params.getParams(security_limit, app, profile_key='@DEFAULT@') | |
161 | |
162 def updateParams(self, xml, security_limit=Const.SECURITY_LIMIT, app=''): | |
163 self.params.updateParams(xml, security_limit, app) | |
164 | |
165 def paramsRegisterApp(self, xml, security_limit=Const.NO_SECURITY_LIMIT, app=''): | |
166 return self.params.paramsRegisterApp(xml, security_limit, app) | |
167 | |
168 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): | |
169 self.params.setParam(name, value, category, security_limit, profile_key) | |
170 | |
171 def getParamA(self, name, category, attr="value", profile_key='@NONE@'): | |
172 return self.params.getParamA(name, category, attr, profile_key) | |
173 | 193 |
174 def updateEntityData(self, entity_jid, key, value, profile_key): | 194 def updateEntityData(self, entity_jid, key, value, profile_key): |
175 self.entities_data.setdefault(entity_jid, {}) | 195 self.entities_data.setdefault(entity_jid, {}) |
176 self.entities_data[entity_jid][key] = value | 196 self.entities_data[entity_jid][key] = value |
177 | 197 |
182 return result | 202 return result |
183 | 203 |
184 | 204 |
185 class FakeTriggerManager(object): | 205 class FakeTriggerManager(object): |
186 | 206 |
187 def add(self, point_name, callback): | 207 def add(self, point_name, callback, priority=0): |
188 pass | 208 pass |
189 | 209 |
190 def point(self, point_name, *args, **kwargs): | 210 def point(self, point_name, *args, **kwargs): |
191 """We always return true to continue the action""" | 211 """We always return true to continue the action""" |
192 return True | 212 return True |