comparison src/test/helpers.py @ 780:9810f22ba733

test: store the constants in constants.py + better PEP8 compliance
author souliane <souliane@mailoo.org>
date Sat, 04 Jan 2014 15:45:08 +0100
parents bfabeedbf32e
children 80ab2b58e205
comparison
equal deleted inserted replaced
779:a978c703bf57 780:9810f22ba733
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 import __builtin__ 21 from constants import Const
22 from twisted.words.protocols.jabber.jid import JID
23 from wokkel.xmppim import RosterItem 22 from wokkel.xmppim import RosterItem
24 from sat.core.xmpp import SatRosterProtocol 23 from sat.core.xmpp import SatRosterProtocol
25 from twisted.trial.unittest import FailTest 24 from twisted.trial.unittest import FailTest
26 from twisted.trial import unittest 25 from twisted.trial import unittest
27 from xml.etree import cElementTree as etree 26 from xml.etree import cElementTree as etree
28 import re 27 import re
29 28
30 TEST_JID_STR = u"test@example.org/SàT"
31 TEST_JID = JID(u"test@example.org/SàT")
32 TEST_PROFILE = 'test_profile'
33 29
34 def b2s(value): 30 def b2s(value):
35 """Convert a bool to a unicode string used in bridge 31 """Convert a bool to a unicode string used in bridge
36 @param value: boolean value 32 @param value: boolean value
37 @return: unicode conversion, according to bridge convention 33 @return: unicode conversion, according to bridge convention
38 34
39 """ 35 """
40 return u"True" if value else u"False" 36 return u"True" if value else u"False"
41 37
38
42 class DifferentArgsException(FailTest): 39 class DifferentArgsException(FailTest):
43 pass 40 pass
44 41
42
45 class DifferentXMLException(FailTest): 43 class DifferentXMLException(FailTest):
46 pass 44 pass
45
47 46
48 class FakeSAT(object): 47 class FakeSAT(object):
49 """Class to simulate a SAT instance""" 48 """Class to simulate a SAT instance"""
50 49
51 def __init__(self): 50 def __init__(self):
89 pass 88 pass
90 89
91 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'): 90 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'):
92 pass 91 pass
93 92
94 def addWaitingSub(self, type, contact_jid, profile_key): 93 def addWaitingSub(self, type_, contact_jid, profile_key):
95 pass 94 pass
96 95
97 def delWaitingSub(self, contact_jid, profile_key): 96 def delWaitingSub(self, contact_jid, profile_key):
98 pass 97 pass
99 98
107 106
108 def point(self, point_name, *args, **kwargs): 107 def point(self, point_name, *args, **kwargs):
109 """We always return true to continue the action""" 108 """We always return true to continue the action"""
110 return True 109 return True
111 110
111
112 class FakeRosterProtocol(SatRosterProtocol): 112 class FakeRosterProtocol(SatRosterProtocol):
113 113
114 def __init__(self, host, parent): 114 def __init__(self, host, parent):
115 SatRosterProtocol.__init__(self, host) 115 SatRosterProtocol.__init__(self, host)
116 self.parent = parent 116 self.parent = parent
117 self.addItem(TEST_JID) 117 self.addItem(Const.TEST_JID)
118 118
119 def addItem(self, jid, *args, **kwargs): 119 def addItem(self, jid, *args, **kwargs):
120 if not args and not kwargs: 120 if not args and not kwargs:
121 # defaults values setted for the tests only 121 # defaults values setted for the tests only
122 kwargs["subscriptionTo"] = True 122 kwargs["subscriptionTo"] = True
131 131
132 class FakeClient(object): 132 class FakeClient(object):
133 def __init__(self, host): 133 def __init__(self, host):
134 self.host = host 134 self.host = host
135 self.profile = 'test_profile' 135 self.profile = 'test_profile'
136 self.jid = TEST_JID 136 self.jid = Const.TEST_JID
137 self.roster = FakeRosterProtocol(host, self) 137 self.roster = FakeRosterProtocol(host, self)
138 138
139 def send(self, obj): 139 def send(self, obj):
140 pass 140 pass
141 141
142 142
143 class SatTestCase(unittest.TestCase): 143 class SatTestCase(unittest.TestCase):
144 144
145 def assertEqualXML(self, xml, expected, ignore_blank = False): 145 def assertEqualXML(self, xml, expected, ignore_blank=False):
146 def equalElt(got_elt, exp_elt): 146 def equalElt(got_elt, exp_elt):
147 if ignore_blank: 147 if ignore_blank:
148 for elt in got_elt, exp_elt: 148 for elt in got_elt, exp_elt:
149 for attr in ('text','tail'): 149 for attr in ('text', 'tail'):
150 value = getattr(elt, attr) 150 value = getattr(elt, attr)
151 try: 151 try:
152 value = value.strip() or None 152 value = value.strip() or None
153 except AttributeError: 153 except AttributeError:
154 value = None 154 value = None
174 if not equalElt(child, exp_elt[idx]): 174 if not equalElt(child, exp_elt[idx]):
175 return False 175 return False
176 return True 176 return True
177 177
178 def remove_blank(xml): 178 def remove_blank(xml):
179 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+',' ',xml).split('\n')] 179 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+', ' ', xml).split('\n')]
180 return '\n'.join([line for line in lines if line]) 180 return '\n'.join([line for line in lines if line])
181 181
182 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml) 182 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml)
183 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected) 183 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected)
184 184
187 print "XML are not equals:" 187 print "XML are not equals:"
188 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8') 188 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8')
189 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8') 189 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8')
190 print "---" 190 print "---"
191 raise DifferentXMLException 191 raise DifferentXMLException
192
193
194 def _(text):
195 return text
196
197 __builtin__.__dict__['_'] = _