Mercurial > libervia-backend
comparison src/test/helpers.py @ 693:65b30bc7f1b3
tests: added XML comparaison method to helpers
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 12 Nov 2013 14:16:11 +0100 |
parents | ffb716804580 |
children | 4284b6ad8aa3 |
comparison
equal
deleted
inserted
replaced
692:e98db42cd78c | 693:65b30bc7f1b3 |
---|---|
20 import __builtin__ | 20 import __builtin__ |
21 from twisted.words.protocols.jabber.jid import JID | 21 from twisted.words.protocols.jabber.jid import JID |
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 twisted.trial.unittest import FailTest | 24 from twisted.trial.unittest import FailTest |
25 from twisted.trial import unittest | |
26 from xml.etree import cElementTree as etree | |
27 import re | |
25 | 28 |
26 TEST_JID_STR = u"test@example.org/SàT" | 29 TEST_JID_STR = u"test@example.org/SàT" |
27 TEST_JID = JID(u"test@example.org/SàT") | 30 TEST_JID = JID(u"test@example.org/SàT") |
28 TEST_PROFILE = 'test_profile' | 31 TEST_PROFILE = 'test_profile' |
29 | 32 |
36 return u"True" if value else u"False" | 39 return u"True" if value else u"False" |
37 | 40 |
38 class DifferentArgsException(FailTest): | 41 class DifferentArgsException(FailTest): |
39 pass | 42 pass |
40 | 43 |
44 class DifferentXMLException(FailTest): | |
45 pass | |
41 | 46 |
42 class FakeSAT(object): | 47 class FakeSAT(object): |
43 """Class to simulate a SAT instance""" | 48 """Class to simulate a SAT instance""" |
44 | 49 |
45 def __init__(self): | 50 def __init__(self): |
113 kwargs["subscriptionFrom"] = True | 118 kwargs["subscriptionFrom"] = True |
114 roster_item = RosterItem(jid, *args, **kwargs) | 119 roster_item = RosterItem(jid, *args, **kwargs) |
115 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)} | 120 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)} |
116 if roster_item.name: | 121 if roster_item.name: |
117 attrs['name'] = roster_item.name | 122 attrs['name'] = roster_item.name |
118 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) | 123 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) |
119 self.onRosterSet(roster_item) | 124 self.onRosterSet(roster_item) |
120 | 125 |
121 | 126 |
122 class FakeClient(object): | 127 class FakeClient(object): |
123 def __init__(self, host): | 128 def __init__(self, host): |
128 | 133 |
129 def send(self, obj): | 134 def send(self, obj): |
130 pass | 135 pass |
131 | 136 |
132 | 137 |
138 class SatTestCase(unittest.TestCase): | |
139 | |
140 def assertEqualXML(self, xml, expected, ignore_blank = False): | |
141 def equalElt(got_elt, exp_elt): | |
142 if (got_elt.tag != exp_elt.tag): | |
143 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) | |
144 print "tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag) | |
145 return False | |
146 if (got_elt.attrib != exp_elt.attrib): | |
147 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) | |
148 print "attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib) | |
149 return False | |
150 if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text): | |
151 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) | |
152 print "text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text) | |
153 print "tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail) | |
154 return False | |
155 if (len(got_elt) != len(exp_elt)): | |
156 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) | |
157 print "children len: got %d expected: %d" % (len(got_elt), len(exp_elt)) | |
158 return False | |
159 for idx, child in enumerate(got_elt): | |
160 if not equalElt(child, exp_elt[idx]): | |
161 return False | |
162 return True | |
163 | |
164 def remove_blank(xml): | |
165 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+',' ',xml).split('\n')] | |
166 return '\n'.join([line for line in lines if line]) | |
167 | |
168 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml) | |
169 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected) | |
170 | |
171 if not equalElt(xml_elt, expected_elt): | |
172 print "---" | |
173 print "XML are not equals:" | |
174 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8') | |
175 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8') | |
176 print "---" | |
177 raise DifferentXMLException | |
178 | |
179 | |
133 def _(text): | 180 def _(text): |
134 return text | 181 return text |
135 | 182 |
136 __builtin__.__dict__['_'] = _ | 183 __builtin__.__dict__['_'] = _ |