comparison src/test/helpers.py @ 782:0e5807193721

test: added some tests for Memory
author souliane <souliane@mailoo.org>
date Sat, 04 Jan 2014 17:16:40 +0100
parents 80ab2b58e205
children c3acc1298a2f
comparison
equal deleted inserted replaced
781:80ab2b58e205 782:0e5807193721
22 from wokkel.xmppim import RosterItem 22 from wokkel.xmppim import RosterItem
23 from sat.core.xmpp import SatRosterProtocol 23 from sat.core.xmpp import SatRosterProtocol
24 from sat.memory.memory import Params 24 from sat.memory.memory import Params
25 from twisted.trial.unittest import FailTest 25 from twisted.trial.unittest import FailTest
26 from twisted.trial import unittest 26 from twisted.trial import unittest
27 from twisted.internet import defer
27 from xml.etree import cElementTree as etree 28 from xml.etree import cElementTree as etree
29 from sat.core import exceptions
28 import re 30 import re
29 31
30 32
31 def b2s(value): 33 def b2s(value):
32 """Convert a bool to a unicode string used in bridge 34 """Convert a bool to a unicode string used in bridge
94 not be run (for example those using the storage attribute must be overwritten 96 not be run (for example those using the storage attribute must be overwritten
95 by a naive simulation of what they should do.""" 97 by a naive simulation of what they should do."""
96 98
97 def __init__(self, host, storage): 99 def __init__(self, host, storage):
98 Params.__init__(self, host, storage) 100 Params.__init__(self, host, storage)
99 self.values = {} # naive simulation of values storage 101 self.params = {} # naive simulation of values storage
100 102
101 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): 103 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'):
102 self.values.setdefault(category, {}) 104 profile = self.getProfileName(profile_key)
103 self.values[category][name] = value 105 self.params.setdefault(profile, {})
106 self.params[profile_key][(category, name)] = value
104 107
105 def getParamA(self, name, category, attr="value", profile_key='@NONE@'): 108 def getParamA(self, name, category, attr="value", profile_key='@NONE@'):
106 return self.values[category][name] 109 profile = self.getProfileName(profile_key)
110 return self.params[profile][(category, name)]
111
112 def getProfileName(self, profile_key, return_profile_keys=False):
113 if profile_key == '@DEFAULT@':
114 return Const.TEST_PROFILE
115 elif profile_key == '@NONE@':
116 raise exceptions.ProfileNotSetError
117 else:
118 return profile_key
119
120 def loadIndParams(self, profile, cache=None):
121 self.params[profile] = {}
122 return defer.succeed(None)
107 123
108 124
109 class FakeMemory(object): 125 class FakeMemory(object):
110 """Class to simulate and test memory object""" 126 """Class to simulate and test memory object"""
111 127
116 132
117 def init(self): 133 def init(self):
118 """Tests that manipulate params and/or entities should 134 """Tests that manipulate params and/or entities should
119 re-initialise the memory first to not fake the result.""" 135 re-initialise the memory first to not fake the result."""
120 self.params.load_default_params() 136 self.params.load_default_params()
121 self.params.values.clear() 137 self.params.params.clear()
122 self.entities_data = {} # naive simulation of entities 138 self.entities_data = {} # naive simulation of entities
123 139
124 def getProfileName(self, profile_key): 140 def getProfileName(self, profile_key, return_profile_keys=False):
125 return profile_key 141 return self.params.getProfileName(profile_key, return_profile_keys)
126 142
127 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): 143 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"):
128 pass 144 pass
129 145
130 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'): 146 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'):
136 def addWaitingSub(self, type_, contact_jid, profile_key): 152 def addWaitingSub(self, type_, contact_jid, profile_key):
137 pass 153 pass
138 154
139 def delWaitingSub(self, contact_jid, profile_key): 155 def delWaitingSub(self, contact_jid, profile_key):
140 pass 156 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@')
141 161
142 def updateParams(self, xml, security_limit=Const.SECURITY_LIMIT, app=''): 162 def updateParams(self, xml, security_limit=Const.SECURITY_LIMIT, app=''):
143 self.params.updateParams(xml, 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)
144 167
145 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): 168 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'):
146 self.params.setParam(name, value, category, security_limit, profile_key) 169 self.params.setParam(name, value, category, security_limit, profile_key)
147 170
148 def getParamA(self, name, category, attr="value", profile_key='@NONE@'): 171 def getParamA(self, name, category, attr="value", profile_key='@NONE@'):