Mercurial > libervia-backend
annotate 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 |
rev | line source |
---|---|
335 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
4 # SAT: a jabber client |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
335 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
10 # (at your option) any later version. |
335 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
15 # GNU Affero General Public License for more details. |
335 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
591
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
335 | 19 |
20 import __builtin__ | |
21 from twisted.words.protocols.jabber.jid import JID | |
649 | 22 from wokkel.xmppim import RosterItem |
23 from sat.core.xmpp import SatRosterProtocol | |
647
63b6e684bbfa
tests: Use of FailTest for DifferentArgsException instead of Exception, so the test fail instead of raising en error.
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
24 from twisted.trial.unittest import FailTest |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
25 from twisted.trial import unittest |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
26 from xml.etree import cElementTree as etree |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
27 import re |
335 | 28 |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
29 TEST_JID_STR = u"test@example.org/SàT" |
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
30 TEST_JID = JID(u"test@example.org/SàT") |
335 | 31 TEST_PROFILE = 'test_profile' |
32 | |
649 | 33 def b2s(value): |
34 """Convert a bool to a unicode string used in bridge | |
35 @param value: boolean value | |
36 @return: unicode conversion, according to bridge convention | |
37 | |
38 """ | |
39 return u"True" if value else u"False" | |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
40 |
647
63b6e684bbfa
tests: Use of FailTest for DifferentArgsException instead of Exception, so the test fail instead of raising en error.
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
41 class DifferentArgsException(FailTest): |
335 | 42 pass |
43 | |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
44 class DifferentXMLException(FailTest): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
45 pass |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
46 |
335 | 47 class FakeSAT(object): |
48 """Class to simulate a SAT instance""" | |
49 | |
50 def __init__(self): | |
51 self.bridge = FakeBridge() | |
52 self.memory = FakeMemory() | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
53 self.trigger = FakeTriggerManager() |
335 | 54 |
649 | 55 def delContact(self, to, profile_key): |
56 #TODO | |
57 pass | |
58 | |
335 | 59 |
60 class FakeBridge(object): | |
61 """Class to simulate and test bridge calls""" | |
62 | |
63 def expectCall(self, name, *check_args, **check_kwargs): | |
64 def checkCall(*args, **kwargs): | |
65 if args != check_args or kwargs != check_kwargs: | |
66 print "\n\n--------------------" | |
67 print "Args are not equals:" | |
68 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args) | |
69 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs) | |
70 print "--------------------\n\n" | |
71 raise DifferentArgsException | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
72 |
335 | 73 setattr(self, name, checkCall) |
74 | |
75 | |
76 class FakeMemory(object): | |
77 """Class to simulate and test memory object""" | |
78 | |
79 def getProfileName(self, profile_key): | |
80 return profile_key | |
81 | |
669
ffb716804580
core, bridge: extra parameter is saved in history:
Goffi <goffi@goffi.org>
parents:
649
diff
changeset
|
82 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): |
335 | 83 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
84 |
335 | 85 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'): |
86 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
87 |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
88 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'): |
335 | 89 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
90 |
335 | 91 def addWaitingSub(self, type, contact_jid, profile_key): |
92 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
93 |
335 | 94 def delWaitingSub(self, contact_jid, profile_key): |
95 pass | |
96 | |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
97 |
335 | 98 class FakeTriggerManager(object): |
99 | |
100 def add(self, point_name, callback): | |
101 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
102 |
335 | 103 def point(self, point_name, *args, **kwargs): |
104 """We always return true to continue the action""" | |
105 return True | |
106 | |
649 | 107 class FakeRosterProtocol(SatRosterProtocol): |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
108 |
649 | 109 def __init__(self, host, parent): |
110 SatRosterProtocol.__init__(self, host) | |
111 self.parent = parent | |
112 self.addItem(TEST_JID) | |
113 | |
114 def addItem(self, jid, *args, **kwargs): | |
115 if not args and not kwargs: | |
116 # defaults values setted for the tests only | |
117 kwargs["subscriptionTo"] = True | |
118 kwargs["subscriptionFrom"] = True | |
119 roster_item = RosterItem(jid, *args, **kwargs) | |
120 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)} | |
121 if roster_item.name: | |
122 attrs['name'] = roster_item.name | |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
123 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) |
649 | 124 self.onRosterSet(roster_item) |
125 | |
126 | |
127 class FakeClient(object): | |
128 def __init__(self, host): | |
129 self.host = host | |
335 | 130 self.profile = 'test_profile' |
131 self.jid = TEST_JID | |
649 | 132 self.roster = FakeRosterProtocol(host, self) |
133 | |
134 def send(self, obj): | |
135 pass | |
335 | 136 |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
137 |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
138 class SatTestCase(unittest.TestCase): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
139 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
140 def assertEqualXML(self, xml, expected, ignore_blank = False): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
141 def equalElt(got_elt, exp_elt): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
142 if (got_elt.tag != exp_elt.tag): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
143 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
144 print "tag: got [%s] expected: [%s]" % (got_elt.tag, exp_elt.tag) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
145 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
146 if (got_elt.attrib != exp_elt.attrib): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
147 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
148 print "attribs: got %s expected %s" % (got_elt.attrib, exp_elt.attrib) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
149 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
150 if (got_elt.tail != exp_elt.tail or got_elt.text != exp_elt.text): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
151 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
152 print "text: got [%s] expected: [%s]" % (got_elt.text, exp_elt.text) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
153 print "tail: got [%s] expected: [%s]" % (got_elt.tail, exp_elt.tail) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
154 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
155 if (len(got_elt) != len(exp_elt)): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
156 print "XML are not equals (elt %s/%s):" % (got_elt, exp_elt) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
157 print "children len: got %d expected: %d" % (len(got_elt), len(exp_elt)) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
158 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
159 for idx, child in enumerate(got_elt): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
160 if not equalElt(child, exp_elt[idx]): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
161 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
162 return True |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
163 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
164 def remove_blank(xml): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
165 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+',' ',xml).split('\n')] |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
166 return '\n'.join([line for line in lines if line]) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
167 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
168 xml_elt = etree.fromstring(remove_blank(xml) if ignore_blank else xml) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
169 expected_elt = etree.fromstring(remove_blank(expected) if ignore_blank else expected) |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
170 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
171 if not equalElt(xml_elt, expected_elt): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
172 print "---" |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
173 print "XML are not equals:" |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
174 print "got:\n-\n%s\n-\n\n" % etree.tostring(xml_elt, encoding='utf-8') |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
175 print "was expecting:\n-\n%s\n-\n\n" % etree.tostring(expected_elt, encoding='utf-8') |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
176 print "---" |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
177 raise DifferentXMLException |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
178 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
179 |
335 | 180 def _(text): |
181 return text | |
182 | |
183 __builtin__.__dict__['_'] = _ |