Mercurial > libervia-backend
annotate src/test/helpers.py @ 781:80ab2b58e205
test: added support of basic memory stuff in helpers.py
author | souliane <souliane@mailoo.org> |
---|---|
date | Sat, 04 Jan 2014 15:46:30 +0100 |
parents | 9810f22ba733 |
children | 0e5807193721 |
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 |
771 | 20 from sat.core.i18n import _ |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
21 from constants import Const |
649 | 22 from wokkel.xmppim import RosterItem |
23 from sat.core.xmpp import SatRosterProtocol | |
781
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
24 from sat.memory.memory import Params |
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
|
25 from twisted.trial.unittest import FailTest |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
26 from twisted.trial import unittest |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
27 from xml.etree import cElementTree as etree |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
28 import re |
335 | 29 |
30 | |
649 | 31 def b2s(value): |
32 """Convert a bool to a unicode string used in bridge | |
33 @param value: boolean value | |
34 @return: unicode conversion, according to bridge convention | |
35 | |
36 """ | |
37 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
|
38 |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
39 |
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
|
40 class DifferentArgsException(FailTest): |
335 | 41 pass |
42 | |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
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 |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
47 |
335 | 48 class FakeSAT(object): |
49 """Class to simulate a SAT instance""" | |
50 | |
51 def __init__(self): | |
52 self.bridge = FakeBridge() | |
781
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
53 self.memory = FakeMemory(self) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
54 self.trigger = FakeTriggerManager() |
335 | 55 |
649 | 56 def delContact(self, to, profile_key): |
57 #TODO | |
58 pass | |
59 | |
781
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
60 def registerCallback(self, callback, *args, **kwargs): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
61 pass |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
62 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
63 def registerNewAccountCB(self, data, profile): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
64 pass |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
65 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
66 def sendMessage(self, to_s, msg, subject=None, mess_type='auto', extra={}, profile_key='@NONE@'): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
67 pass |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
68 |
335 | 69 |
70 class FakeBridge(object): | |
71 """Class to simulate and test bridge calls""" | |
72 | |
73 def expectCall(self, name, *check_args, **check_kwargs): | |
74 def checkCall(*args, **kwargs): | |
75 if args != check_args or kwargs != check_kwargs: | |
76 print "\n\n--------------------" | |
77 print "Args are not equals:" | |
78 print "args\n----\n%s (sent)\n%s (wanted)" % (args, check_args) | |
79 print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs) | |
80 print "--------------------\n\n" | |
81 raise DifferentArgsException | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
82 |
335 | 83 setattr(self, name, checkCall) |
84 | |
694
4284b6ad8aa3
tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents:
693
diff
changeset
|
85 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False): |
4284b6ad8aa3
tests: plugin text syntaxes sanitisation tests
Goffi <goffi@goffi.org>
parents:
693
diff
changeset
|
86 pass |
335 | 87 |
781
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
88 def addSignal(self, name, int_suffix, signature): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
89 pass |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
90 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
91 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
92 class FakeParams(Params): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
93 """Class to simulate and test params object. The methods of Params that could |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
94 not be run (for example those using the storage attribute must be overwritten |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
95 by a naive simulation of what they should do.""" |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
96 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
97 def __init__(self, host, storage): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
98 Params.__init__(self, host, storage) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
99 self.values = {} # naive simulation of values storage |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
100 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
101 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
102 self.values.setdefault(category, {}) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
103 self.values[category][name] = value |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
104 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
105 def getParamA(self, name, category, attr="value", profile_key='@NONE@'): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
106 return self.values[category][name] |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
107 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
108 |
335 | 109 class FakeMemory(object): |
110 """Class to simulate and test memory object""" | |
111 | |
781
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
112 def __init__(self, host): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
113 self.host = host |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
114 self.params = FakeParams(host, None) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
115 self.init() |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
116 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
117 def init(self): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
118 """Tests that manipulate params and/or entities should |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
119 re-initialise the memory first to not fake the result.""" |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
120 self.params.load_default_params() |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
121 self.params.values.clear() |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
122 self.entities_data = {} # naive simulation of entities |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
123 |
335 | 124 def getProfileName(self, profile_key): |
125 return profile_key | |
126 | |
669
ffb716804580
core, bridge: extra parameter is saved in history:
Goffi <goffi@goffi.org>
parents:
649
diff
changeset
|
127 def addToHistory(self, from_jid, to_jid, message, _type='chat', extra=None, timestamp=None, profile="@NONE@"): |
335 | 128 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
129 |
335 | 130 def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'): |
131 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
132 |
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
|
133 def setPresenceStatus(self, contact_jid, show, priority, statuses, profile_key='@DEFAULT@'): |
335 | 134 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
135 |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
136 def addWaitingSub(self, type_, contact_jid, profile_key): |
335 | 137 pass |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
138 |
335 | 139 def delWaitingSub(self, contact_jid, profile_key): |
140 pass | |
141 | |
781
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
142 def updateParams(self, xml, security_limit=Const.SECURITY_LIMIT, app=''): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
143 self.params.updateParams(xml, security_limit, app) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
144 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
145 def setParam(self, name, value, category, security_limit=-1, profile_key='@NONE@'): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
146 self.params.setParam(name, value, category, security_limit, profile_key) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
147 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
148 def getParamA(self, name, category, attr="value", profile_key='@NONE@'): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
149 return self.params.getParamA(name, category, attr, profile_key) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
150 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
151 def updateEntityData(self, entity_jid, key, value, profile_key): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
152 self.entities_data.setdefault(entity_jid, {}) |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
153 self.entities_data[entity_jid][key] = value |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
154 |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
155 def getEntityData(self, entity_jid, keys, profile_key): |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
156 result = {} |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
157 for key in keys: |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
158 result[key] = self.entities_data[entity_jid][key] |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
159 return result |
80ab2b58e205
test: added support of basic memory stuff in helpers.py
souliane <souliane@mailoo.org>
parents:
780
diff
changeset
|
160 |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
161 |
335 | 162 class FakeTriggerManager(object): |
163 | |
164 def add(self, point_name, callback): | |
165 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
166 |
335 | 167 def point(self, point_name, *args, **kwargs): |
168 """We always return true to continue the action""" | |
169 return True | |
170 | |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
171 |
649 | 172 class FakeRosterProtocol(SatRosterProtocol): |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
173 |
649 | 174 def __init__(self, host, parent): |
175 SatRosterProtocol.__init__(self, host) | |
176 self.parent = parent | |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
177 self.addItem(Const.TEST_JID) |
649 | 178 |
179 def addItem(self, jid, *args, **kwargs): | |
180 if not args and not kwargs: | |
181 # defaults values setted for the tests only | |
182 kwargs["subscriptionTo"] = True | |
183 kwargs["subscriptionFrom"] = True | |
184 roster_item = RosterItem(jid, *args, **kwargs) | |
185 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)} | |
186 if roster_item.name: | |
187 attrs['name'] = roster_item.name | |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
188 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile) |
649 | 189 self.onRosterSet(roster_item) |
190 | |
191 | |
192 class FakeClient(object): | |
193 def __init__(self, host): | |
194 self.host = host | |
335 | 195 self.profile = 'test_profile' |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
196 self.jid = Const.TEST_JID |
649 | 197 self.roster = FakeRosterProtocol(host, self) |
198 | |
199 def send(self, obj): | |
200 pass | |
335 | 201 |
591
65821b3fa7ab
Fix pep8 support in src/test.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
202 |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
203 class SatTestCase(unittest.TestCase): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
204 |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
205 def assertEqualXML(self, xml, expected, ignore_blank=False): |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
206 def equalElt(got_elt, exp_elt): |
696
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
207 if ignore_blank: |
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
208 for elt in got_elt, exp_elt: |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
209 for attr in ('text', 'tail'): |
696
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
210 value = getattr(elt, attr) |
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
211 try: |
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
212 value = value.strip() or None |
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
213 except AttributeError: |
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
214 value = None |
f1a2831d549d
test: better ignore_blank in helpers's assertEqualXML
Goffi <goffi@goffi.org>
parents:
694
diff
changeset
|
215 setattr(elt, attr, value) |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
216 if (got_elt.tag != exp_elt.tag): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
217 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
|
218 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
|
219 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
220 if (got_elt.attrib != exp_elt.attrib): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
221 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
|
222 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
|
223 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
224 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
|
225 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
|
226 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
|
227 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
|
228 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
229 if (len(got_elt) != len(exp_elt)): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
230 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
|
231 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
|
232 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
233 for idx, child in enumerate(got_elt): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
234 if not equalElt(child, exp_elt[idx]): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
235 return False |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
236 return True |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
237 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
238 def remove_blank(xml): |
780
9810f22ba733
test: store the constants in constants.py + better PEP8 compliance
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
239 lines = [line.strip() for line in re.sub(r'[ \t\r\f\v]+', ' ', xml).split('\n')] |
693
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
240 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
|
241 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
242 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
|
243 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
|
244 |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
245 if not equalElt(xml_elt, expected_elt): |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
246 print "---" |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
247 print "XML are not equals:" |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
248 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
|
249 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
|
250 print "---" |
65b30bc7f1b3
tests: added XML comparaison method to helpers
Goffi <goffi@goffi.org>
parents:
669
diff
changeset
|
251 raise DifferentXMLException |