comparison src/test/helpers.py @ 649:e20c823f23e2

tests: helpers improvments: - FakeParent renamed FakeClient - added FakeRoster class, and an instance of it in FakeClient (FakeClient.roster) - b2s function to convert boolean value to strings used in bridge - some fake methods (FakeClient.send, FakeSat.delContact) to avoid exceptions
author Goffi <goffi@goffi.org>
date Sun, 29 Sep 2013 16:29:36 +0200
parents 63b6e684bbfa
children ffb716804580
comparison
equal deleted inserted replaced
648:29cea30f20f5 649:e20c823f23e2
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 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
23 from sat.core.xmpp import SatRosterProtocol
22 from twisted.trial.unittest import FailTest 24 from twisted.trial.unittest import FailTest
23 25
24 TEST_JID_STR = u"test@example.org/SàT" 26 TEST_JID_STR = u"test@example.org/SàT"
25 TEST_JID = JID(u"test@example.org/SàT") 27 TEST_JID = JID(u"test@example.org/SàT")
26 TEST_PROFILE = 'test_profile' 28 TEST_PROFILE = 'test_profile'
27 29
30 def b2s(value):
31 """Convert a bool to a unicode string used in bridge
32 @param value: boolean value
33 @return: unicode conversion, according to bridge convention
34
35 """
36 return u"True" if value else u"False"
28 37
29 class DifferentArgsException(FailTest): 38 class DifferentArgsException(FailTest):
30 pass 39 pass
31 40
32 41
35 44
36 def __init__(self): 45 def __init__(self):
37 self.bridge = FakeBridge() 46 self.bridge = FakeBridge()
38 self.memory = FakeMemory() 47 self.memory = FakeMemory()
39 self.trigger = FakeTriggerManager() 48 self.trigger = FakeTriggerManager()
49
50 def delContact(self, to, profile_key):
51 #TODO
52 pass
40 53
41 54
42 class FakeBridge(object): 55 class FakeBridge(object):
43 """Class to simulate and test bridge calls""" 56 """Class to simulate and test bridge calls"""
44 57
84 97
85 def point(self, point_name, *args, **kwargs): 98 def point(self, point_name, *args, **kwargs):
86 """We always return true to continue the action""" 99 """We always return true to continue the action"""
87 return True 100 return True
88 101
102 class FakeRosterProtocol(SatRosterProtocol):
89 103
90 class FakeParent(object): 104 def __init__(self, host, parent):
91 def __init__(self): 105 SatRosterProtocol.__init__(self, host)
106 self.parent = parent
107 self.addItem(TEST_JID)
108
109 def addItem(self, jid, *args, **kwargs):
110 if not args and not kwargs:
111 # defaults values setted for the tests only
112 kwargs["subscriptionTo"] = True
113 kwargs["subscriptionFrom"] = True
114 roster_item = RosterItem(jid, *args, **kwargs)
115 attrs = {'to': b2s(roster_item.subscriptionTo), 'from': b2s(roster_item.subscriptionFrom), 'ask': b2s(roster_item.pendingOut)}
116 if roster_item.name:
117 attrs['name'] = roster_item.name
118 self.host.bridge.expectCall("newContact", jid.full(), attrs, roster_item.groups, self.parent.profile)
119 self.onRosterSet(roster_item)
120
121
122 class FakeClient(object):
123 def __init__(self, host):
124 self.host = host
92 self.profile = 'test_profile' 125 self.profile = 'test_profile'
93 self.jid = TEST_JID 126 self.jid = TEST_JID
127 self.roster = FakeRosterProtocol(host, self)
128
129 def send(self, obj):
130 pass
94 131
95 132
96 def _(text): 133 def _(text):
97 return text 134 return text
98 135