test: improvements for the helpers classes:

- simplification for retrieving the sent messages
- an expected bridge call which is already set can no more be
  overwritten with new arguments for the same method name. The
  new arguments are stored in a FIFO list instead, and each call
  of the method also registers the arguments for the next call.
This commit is contained in:
souliane 2014-01-15 23:22:07 +01:00
commit cf8f9330b4
3 changed files with 105 additions and 40 deletions

View file

@ -130,30 +130,37 @@ class FakeSAT(object):
def isConnected(self, profile):
return True
def getSentMessage(self, message_index, profile_index):
"""Called by tests. FakeClient instances associated to each profile must have
def getSentMessageRaw(self, profile_index):
"""Pop and return the sent message in first position (works like a FIFO).
Called by tests. FakeClient instances associated to each profile must have
been previously initialized with the method FakeSAT.getClient.
@return: XML representation of the <message_index>th sent message for given profile"""
return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent[message_index]
@return: the sent message for given profile, or None"""
try:
return self.profiles[Const.PROFILE[profile_index]].xmlstream.sent.pop(0)
except IndexError:
return None
def countSentMessages(self, profiles=Const.PROFILE):
"""Called by tests. FakeClient instances associated to each profile must have
def getSentMessage(self, profile_index):
"""Pop and return the sent message in first position (works like a FIFO).
Called by tests. FakeClient instances associated to each profile must have
been previously initialized with the method FakeSAT.getClient.
@param profiles: list of profiles
@return: a list containing the number of sent messages for each of the specified profiles"""
result = []
for profile in profiles:
result.append(len(self.profiles[profile].xmlstream.sent))
return result
@return: XML representation of the sent message for given profile, or None"""
entry = self.getSentMessageRaw(profile_index)
return entry.toXml() if entry else None
class FakeBridge(object):
"""Class to simulate and test bridge calls"""
def __init__(self):
self.methods = {}
self.expected_calls = {}
def expectCall(self, name, *check_args, **check_kwargs):
if hasattr(self, name): # queue this new call as one already exists
self.expected_calls.setdefault(name, [])
self.expected_calls[name].append((check_args, check_kwargs))
return
def checkCall(*args, **kwargs):
if args != check_args or kwargs != check_kwargs:
print "\n\n--------------------"
@ -162,6 +169,13 @@ class FakeBridge(object):
print "kwargs\n------\n%s (sent)\n%s (wanted)" % (kwargs, check_kwargs)
print "--------------------\n\n"
raise DifferentArgsException
delattr(self, name)
if name in self.expected_calls: # register the next call
args, kwargs = self.expected_calls[name].pop(0)
if len(self.expected_calls[name]) == 0:
del self.expected_calls[name]
self.expectCall(name, *args, **kwargs)
setattr(self, name, checkCall)
@ -297,7 +311,7 @@ class FakeXmlStream(object):
def send(self, obj):
"""Save the sent messages to compare them later"""
self.sent.append(obj.toXml())
self.sent.append(obj)
class FakeClient(object):

View file

@ -21,7 +21,7 @@
""" Helpers class for plugin dependencies """
from constants import Const
from sat.plugins.plugin_xep_0045 import XEP_0045
from sat.plugins import plugin_xep_0045
from twisted.internet import defer
from wokkel.muc import Room, User
@ -33,6 +33,12 @@ class FakeMUCClient(object):
self.joined_rooms = {}
def join(self, roomJID, nick, profile):
"""
@param roomJID: the room JID
@param nick: nick to be used in the room
@param profile: the profile of the user joining the room
@return: the deferred joined wokkel.muc.Room instance
"""
roster = {}
# ask the other profiles to fill our roster
@ -69,10 +75,30 @@ class FakeMUCClient(object):
except (AttributeError, KeyError):
pass
return room
return defer.succeed(room)
def leave(self, roomJID, profile):
"""
@param roomJID: the room JID
@param profile: the profile of the user joining the room
@return: a dummy deferred
"""
room = self.joined_rooms[roomJID.userhost()]
# remove ourself from the other rosters
for i in xrange(0, len(Const.PROFILE)):
other_profile = Const.PROFILE[i]
if other_profile == profile:
continue
try:
other_room = self.plugin_parent.clients[other_profile].joined_rooms[roomJID.userhost()]
del other_room.roster[room.nick]
except (AttributeError, KeyError):
pass
del self.joined_rooms[roomJID.userhost()]
return defer.Deferred()
class FakeXEP_0045(XEP_0045):
class FakeXEP_0045(plugin_xep_0045.XEP_0045):
def __init__(self, host):
self.host = host
@ -81,22 +107,50 @@ class FakeXEP_0045(XEP_0045):
self.clients[profile] = FakeMUCClient(self)
def join(self, room_jid, nick, options={}, profile_key='@DEFAULT@'):
"""
@param roomJID: the room JID
@param nick: nick to be used in the room
@param options: ignore
@param profile_key: the profile of the user joining the room
@return: the deferred joined wokkel.muc.Room instance or None
"""
profile = self.host.memory.getProfileName(profile_key)
room_jid_s = room_jid.userhost()
if room_jid_s in self.clients[profile].joined_rooms:
return defer.succeed(None)
room = self.clients[profile].join(room_jid, nick, profile)
return defer.succeed(room)
return room
def joinRoom(self, muc_index, user_index):
"""Called by tests
@return: the nickname of the user in the joined room"""
muc = Const.MUC[muc_index]
@return: the nickname of the user who joined room"""
muc_jid = Const.MUC[muc_index]
nick = Const.JID[user_index].user
profile = Const.PROFILE[user_index]
self.join(muc, nick, profile_key=profile)
self.join(muc_jid, nick, profile_key=profile)
return self.getNick(muc_index, user_index)
def leave(self, room_jid, profile_key='@DEFAULT@'):
"""
@param roomJID: the room JID
@param profile_key: the profile of the user leaving the room
@return: a dummy deferred
"""
profile = self.host.memory.getProfileName(profile_key)
room_jid_s = room_jid.userhost()
if room_jid_s not in self.clients[profile].joined_rooms:
raise plugin_xep_0045.UnknownRoom("This room has not been joined")
return self.clients[profile].leave(room_jid, profile)
def leaveRoom(self, muc_index, user_index):
"""Called by tests
@return: the nickname of the user who left the room"""
muc_jid = Const.MUC[muc_index]
nick = self.getNick(muc_index, user_index)
profile = Const.PROFILE[user_index]
self.leave(muc_jid, profile_key=profile)
return nick
def getRoom(self, muc_index, user_index):
"""Called by tests
@return: a wokkel.muc.Room instance"""

View file

@ -70,7 +70,7 @@ class RoomGameTest(helpers.SatTestCase):
self.plugin_0045.joinRoom(user_index, muc_index)
self.plugin._initGame(Const.MUC_STR[muc_index], Const.JID[user_index].user)
def expectedMessage(self, to, type_, tag, players=[]):
def _expectedMessage(self, to, type_, tag, players=[]):
content = "<%s" % tag
if not players:
content += "/>"
@ -142,21 +142,21 @@ class RoomGameTest(helpers.SatTestCase):
self.init()
self.initGame(0, 0)
self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0])
self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", []))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", []))
self.plugin.games[ROOM_JID_S]['players'].append("test1")
self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0])
self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", ["test1"]))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", ["test1"]))
self.plugin.games[ROOM_JID_S]['started'] = True
self.plugin.games[ROOM_JID_S]['players'].append("test2")
self.plugin._synchronizeRoom(ROOM_JID_S, [Const.MUC[0]], Const.PROFILE[0])
self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", ["test1", "test2"]))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", ["test1", "test2"]))
self.plugin.games[ROOM_JID_S]['players'].append("test3")
self.plugin.games[ROOM_JID_S]['players'].append("test4")
user1 = JID(ROOM_JID_S + "/" + Const.JID[0].user)
user2 = JID(ROOM_JID_S + "/" + Const.JID[1].user)
self.plugin._synchronizeRoom(ROOM_JID_S, [user1, user2], Const.PROFILE[0])
self.assertEqualXML(self.host.getSentMessage(3, 0), self.expectedMessage(user1.full(), "normal", "started", ["test1", "test2", "test3", "test4"]))
self.assertEqualXML(self.host.getSentMessage(4, 0), self.expectedMessage(user2.full(), "normal", "started", ["test1", "test2", "test3", "test4"]))
self.assertEqualXML(self.host.getSentMessage(0), self._expectedMessage(user1.full(), "normal", "started", ["test1", "test2", "test3", "test4"]))
self.assertEqualXML(self.host.getSentMessage(0), self._expectedMessage(user2.full(), "normal", "started", ["test1", "test2", "test3", "test4"]))
def test_invitePlayers(self):
self.init()
@ -328,50 +328,47 @@ class RoomGameTest(helpers.SatTestCase):
self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE)
nicks = [self.plugin_0045.getNick(0, 0)]
self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages
self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 1)
# wrong profile
user_nick = self.plugin_0045.joinRoom(0, 1)
room = self.plugin_0045.getRoom(0, 1)
self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), OTHER_PROFILE)
self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # no new message has been sent
self.assertEqual(self.host.getSentMessageRaw(0), None) # no new message has been sent
self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started
# referee profile, user is allowed, wait for one more
room = self.plugin_0045.getRoom(0, 0)
self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[1]), PROFILE)
nicks.append(user_nick)
self.assertEqual(self.host.countSentMessages(), [2, 0, 0, 0, 0])
self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "players", nicks))
self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started
# referee profile, user is not allowed
user_nick = self.plugin_0045.joinRoom(0, 4)
self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[4]), PROFILE)
self.assertEqual(self.host.countSentMessages(), [3, 0, 0, 0, 0])
self.assertEqual(self.host.getSentMessage(2, 0), self.expectedMessage(ROOM_JID_S + '/' + user_nick, "normal", "players", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S + '/' + user_nick, "normal", "players", nicks))
self.assertFalse(self.plugin._gameExists(ROOM_JID_S, True)) # game not started
# referee profile, user is allowed, everybody here
user_nick = self.plugin_0045.joinRoom(0, 3)
self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
nicks.append(user_nick)
self.assertEqual(self.host.getSentMessage(3, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True)) # game started
self.assertTrue(len(self.plugin.invitations[ROOM_JID_S]) == 0)
# wait for none
self.init()
self.plugin.prepareRoom(other_players, ROOM_JID_S, PROFILE)
self.assertEqual(self.host.countSentMessages(), [1, 0, 0, 0, 0]) # init messages
self.assertNotEqual(self.host.getSentMessageRaw(0), None) # init messages
room = self.plugin_0045.getRoom(0, 0)
nicks = [self.plugin_0045.getNick(0, 0)]
user_nick = self.plugin_0045.joinRoom(0, 3)
self.plugin.userJoinedTrigger(room, User(user_nick, Const.JID[3]), PROFILE)
nicks.append(user_nick)
self.assertEqual(self.host.getSentMessage(1, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True))
def test_userLeftTrigger(self):
@ -459,7 +456,7 @@ class RoomGameTest(helpers.SatTestCase):
self.plugin.createGame(ROOM_JID_S, nicks, PROFILE)
self.assertTrue(self.plugin._gameExists(ROOM_JID_S, True))
self.assertEqual(self.plugin.games[ROOM_JID_S]['players'], nicks)
self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
for nick in nicks:
self.assertEqual('init', self.plugin.games[ROOM_JID_S]['status'][nick])
self.assertEqual(self.plugin.player_init, self.plugin.games[ROOM_JID_S]['players_data'][nick])
@ -473,7 +470,7 @@ class RoomGameTest(helpers.SatTestCase):
self.initGame(0, 0)
self.plugin.games[ROOM_JID_S]['started'] = True
self.plugin.createGame(ROOM_JID_S, nicks, PROFILE)
self.assertEqual(self.host.getSentMessage(0, 0), self.expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
self.assertEqual(self.host.getSentMessage(0), self._expectedMessage(ROOM_JID_S, "groupchat", "started", nicks))
# game exists, current profile is not referee
self.init(player_init={"xxx": "xyz"})
@ -481,4 +478,4 @@ class RoomGameTest(helpers.SatTestCase):
self.plugin.games[ROOM_JID_S]['started'] = True
self.plugin_0045.joinRoom(0, 1)
self.plugin.createGame(ROOM_JID_S, nicks, OTHER_PROFILE)
self.assertEqual(self.host.countSentMessages(), [0, 0, 0, 0, 0]) # no sync message has been sent by other_profile
self.assertEqual(self.host.getSentMessageRaw(0), None) # no sync message has been sent by other_profile