[Telepathy-commits] [telepathy-idle/master] Big test cleanup

Jonathon Jongsma jonathon.jongsma at collabora.co.uk
Wed Feb 18 21:40:24 PST 2009


Use constants rather than strings or magic numbers where it makes sense,
remove the duplicate 'irc-privmsg' event and simply use the more general
stream-PRIVMSG event everywhere, simplify room-contact-mixup significantly by
removing unnecessary setup
---
 tests/twisted/connect/invalid-nick.py        |   11 +++--
 tests/twisted/constants.py                   |    1 +
 tests/twisted/idletest.py                    |    8 ----
 tests/twisted/messages/long-message-split.py |   11 ++---
 tests/twisted/messages/message-order.py      |   18 +++++---
 tests/twisted/messages/room-contact-mixup.py |   53 +++++--------------------
 6 files changed, 34 insertions(+), 68 deletions(-)

diff --git a/tests/twisted/connect/invalid-nick.py b/tests/twisted/connect/invalid-nick.py
index 6221632..cb5e824 100644
--- a/tests/twisted/connect/invalid-nick.py
+++ b/tests/twisted/connect/invalid-nick.py
@@ -5,6 +5,7 @@ Test that we get an error when attempting to use an invalid nick
 
 import dbus
 from idletest import make_connection
+from constants import *
 
 def connect(nick):
     bus = dbus.SessionBus()
@@ -28,31 +29,31 @@ def test():
         connect('nick with spaces')
         raise RuntimeError('Invalid nick not rejected')
     except dbus.DBusException, e:
-        assert e.get_dbus_name() == 'org.freedesktop.Telepathy.Errors.InvalidHandle'
+        assert e.get_dbus_name() == INVALID_HANDLE
 
     try:
         connect('') # empty nick
         raise RuntimeError('Invalid nick not rejected')
     except dbus.DBusException, e:
-        assert e.get_dbus_name() == 'org.freedesktop.Telepathy.Errors.InvalidHandle'
+        assert e.get_dbus_name() == INVALID_HANDLE
 
     try:
         connect('#foo') # invalid chars
         raise RuntimeError('Invalid nick not rejected')
     except dbus.DBusException, e:
-        assert e.get_dbus_name() == 'org.freedesktop.Telepathy.Errors.InvalidHandle'
+        assert e.get_dbus_name() == INVALID_HANDLE
 
     try:
         connect(u'김정은') # unicode
         raise RuntimeError('Invalid nick not rejected')
     except dbus.DBusException, e:
-        assert e.get_dbus_name() == 'org.freedesktop.Telepathy.Errors.InvalidHandle'
+        assert e.get_dbus_name() == INVALID_HANDLE
 
     try:
         connect('-foo') # '-' not allowed as first char
         raise RuntimeError('Invalid nick not rejected')
     except dbus.DBusException, e:
-        assert e.get_dbus_name() == 'org.freedesktop.Telepathy.Errors.InvalidHandle'
+        assert e.get_dbus_name() == INVALID_HANDLE
 
     # should pass succeed without an exception
     connect('good_nick')
diff --git a/tests/twisted/constants.py b/tests/twisted/constants.py
index c3f4325..7a2b701 100644
--- a/tests/twisted/constants.py
+++ b/tests/twisted/constants.py
@@ -28,6 +28,7 @@ ERRORS = 'org.freedesktop.Telepathy.Errors'
 INVALID_ARGUMENT = ERRORS + '.InvalidArgument'
 NOT_IMPLEMENTED = ERRORS + '.NotImplemented'
 NOT_AVAILABLE = ERRORS + '.NotAvailable'
+INVALID_HANDLE = ERRORS + '.InvalidHandle'
 
 TUBE_PARAMETERS = CHANNEL_IFACE_TUBE + '.Parameters'
 TUBE_STATE = CHANNEL_IFACE_TUBE + '.State'
diff --git a/tests/twisted/idletest.py b/tests/twisted/idletest.py
index 80954bc..1598545 100644
--- a/tests/twisted/idletest.py
+++ b/tests/twisted/idletest.py
@@ -23,10 +23,6 @@ def make_disconnected_event():
     event = make_irc_event('irc-disconnected', None)
     return event
 
-def make_privmsg_event(recipient, msg):
-    event = make_irc_event('irc-privmsg', {'recipient':recipient, 'message':msg})
-    return event
-
 class BaseIRCServer(irc.IRC):
     verbose = (os.environ.get('CHECK_TWISTED_VERBOSE', '') != '' or '-v' in sys.argv)
 
@@ -54,9 +50,6 @@ class BaseIRCServer(irc.IRC):
         self.log ("connection Lost  %s" % reason)
         self.event_func(make_disconnected_event())
 
-    def handlePRIVMSG(self, args, prefix):
-            self.event_func(make_privmsg_event(args[0], ' '.join(args[1:]).rstrip('\r\n')))
-
         #handle 'login' handshake
     def handlePASS(self, args, prefix):
         self.passwd = args[0]
@@ -89,7 +82,6 @@ class BaseIRCServer(irc.IRC):
         self.sendMessage('001', self.nick, ':Welcome to the test IRC Network', prefix='idle.test.server')
 
     def dataReceived(self, data):
-        self.log ("data received: %s" % (data,))
         (_prefix, _command, _args) = irc.parsemsg(data)
         self.event_func(make_irc_event('stream-%s' % _command, _args))
         try:
diff --git a/tests/twisted/messages/long-message-split.py b/tests/twisted/messages/long-message-split.py
index 6b45337..88c11aa 100644
--- a/tests/twisted/messages/long-message-split.py
+++ b/tests/twisted/messages/long-message-split.py
@@ -7,6 +7,7 @@ message delivery timeout (Bug #17392)
 
 from idletest import exec_test, BaseIRCServer
 from servicetest import EventPattern, call_async
+from constants import *
 import dbus
 
 class LongMessageMangler(BaseIRCServer):
@@ -15,8 +16,6 @@ class LongMessageMangler(BaseIRCServer):
         return '%s!%s@%s' % (self.nick, self.user, self.host)
 
     def handlePRIVMSG(self, args, prefix):
-        #chain up to the base class implementation which simply signals a privmsg event
-        #BaseIRCServer.handlePRIVMSG(self, args, prefix)
         sender = prefix
         recipient = args[0]
         sent_message = args[1]
@@ -36,15 +35,15 @@ def test(q, bus, conn, stream):
     conn.Connect()
     q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
     CHANNEL_NAME = '#idletest'
-    room_handles = conn.RequestHandles(2, [CHANNEL_NAME])
-    call_async(q, conn, 'RequestChannel', 'org.freedesktop.Telepathy.Channel.Type.Text', 2, room_handles[0], True)
+    room_handles = conn.RequestHandles(HT_ROOM, [CHANNEL_NAME])
+    call_async(q, conn, 'RequestChannel', CHANNEL_TYPE_TEXT, HT_ROOM,
+            room_handles[0], True)
 
     ret = q.expect('dbus-return', method='RequestChannel')
     q.expect('dbus-signal', signal='MembersChanged')
     chan = bus.get_object(conn.bus_name, ret.value[0])
 
-    text_chan = dbus.Interface(chan,
-        u'org.freedesktop.Telepathy.Channel.Type.Text')
+    text_chan = dbus.Interface(chan, CHANNEL_TYPE_TEXT)
     # send a whole bunch of messages in a row
     call_async(q, text_chan, 'Send', 0, LONG_MESSAGE)
 
diff --git a/tests/twisted/messages/message-order.py b/tests/twisted/messages/message-order.py
index 39bfe7c..2d07bda 100644
--- a/tests/twisted/messages/message-order.py
+++ b/tests/twisted/messages/message-order.py
@@ -7,29 +7,33 @@ Test that messages that are sent within the same message delivery timeout
 
 from idletest import exec_test
 from servicetest import EventPattern, call_async
+from constants import *
 import dbus
 
 def test(q, bus, conn, stream):
     conn.Connect()
     q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
     CHANNEL_NAME = '#idletest'
-    room_handles = conn.RequestHandles(2, [CHANNEL_NAME])
-    call_async(q, conn, 'RequestChannel', 'org.freedesktop.Telepathy.Channel.Type.Text', 2, room_handles[0], True)
+    room_handles = conn.RequestHandles(HT_ROOM, [CHANNEL_NAME])
+    call_async(q, conn, 'RequestChannel', CHANNEL_TYPE_TEXT, HT_ROOM,
+            room_handles[0], True)
 
     ret = q.expect('dbus-return', method='RequestChannel')
     q.expect('dbus-signal', signal='MembersChanged')
     chan = bus.get_object(conn.bus_name, ret.value[0])
 
-    text_chan = dbus.Interface(chan,
-        u'org.freedesktop.Telepathy.Channel.Type.Text')
+    text_chan = dbus.Interface(chan, CHANNEL_TYPE_TEXT)
 
     # send a whole bunch of messages in a row and make sure they get delivered
     # in the proper order
-    for i in range(4):
+    NUM_MESSAGES = 4
+    for i in range(NUM_MESSAGES):
         call_async(q, text_chan, 'Send', 0, str(i))
 
-    for i in range(4):
-        q.expect('irc-privmsg', data={'message':str(i),'recipient':CHANNEL_NAME})
+    for i in range(NUM_MESSAGES):
+        message = q.expect('stream-PRIVMSG')
+        assert message.data[0] == CHANNEL_NAME
+        assert message.data[1].rstrip('\r\n') == str(i)
 
     call_async(q, conn, 'Disconnect')
     return True
diff --git a/tests/twisted/messages/room-contact-mixup.py b/tests/twisted/messages/room-contact-mixup.py
index dfd116f..2c0ab8c 100644
--- a/tests/twisted/messages/room-contact-mixup.py
+++ b/tests/twisted/messages/room-contact-mixup.py
@@ -7,36 +7,14 @@ to foo would appear to also be coming through room #foo as well (bug #19766)
 
 from idletest import exec_test, BaseIRCServer
 from servicetest import EventPattern, call_async, TimeoutError, sync_dbus
+from constants import *
 import dbus
 
-
-HANDLE_TYPE_CONTACT=1
-HANDLE_TYPE_ROOM=2
-
 # same nick and channel
 CHANNEL = '#foo'
 NICK = 'foo'
 REMOTEUSER = 'remoteuser'
 
-class CustomIRCServer(BaseIRCServer):
-
-    def handlePRIVMSG(self, args, prefix):
-        #chain up to the base class implementation which simply signals a privmsg event
-        BaseIRCServer.handlePRIVMSG(self, args, prefix)
-        sender = prefix
-        recipient = args[0]
-        if (recipient == REMOTEUSER):
-            # auto-reply with a private message
-            self.sendMessage('PRIVMSG', self.nick, ':PRIVATE', prefix=REMOTEUSER)
-        elif (recipient == self.room):
-            # auto-reply to the group
-            self.sendMessage('PRIVMSG', self.room, ':GROUP', prefix=REMOTEUSER)
-
-    def handleJOIN(self, args, prefix):
-        self.room = args[0]
-        self.sendMessage('JOIN', self.room, prefix=self.nick)
-        self._sendNameReply(self.room, [self.nick, REMOTEUSER])
-
 group_received_flag = False;
 def group_received_cb(id, timestamp, sender, type, flags, text):
     global group_received_flag
@@ -47,32 +25,23 @@ def test(q, bus, conn, stream):
     q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
 
     # join a chat room with the same name as our nick
-    room_handles = conn.RequestHandles(HANDLE_TYPE_ROOM, [CHANNEL])
-    call_async(q, conn, 'RequestChannel',
-            'org.freedesktop.Telepathy.Channel.Type.Text', HANDLE_TYPE_ROOM,
+    room_handles = conn.RequestHandles(HT_ROOM, [CHANNEL])
+    call_async(q, conn, 'RequestChannel', CHANNEL_TYPE_TEXT, HT_ROOM,
             room_handles[0], True)
     # wait for the join to finish
     ret = q.expect('dbus-return', method='RequestChannel')
+    muc_path = ret.value
     chan = bus.get_object(conn.bus_name, ret.value[0])
-    group_text_chan = dbus.Interface(chan,
-            u'org.freedesktop.Telepathy.Channel.Type.Text')
+    group_text_chan = dbus.Interface(chan, CHANNEL_TYPE_TEXT)
     group_text_chan.connect_to_signal('Received', group_received_cb)
     q.expect('dbus-signal', signal='MembersChanged')
 
-    # now request a private chat channel with the remote contact
-    contact_handles = conn.RequestHandles(HANDLE_TYPE_CONTACT, [REMOTEUSER])
-    chan_path = conn.RequestChannel('org.freedesktop.Telepathy.Channel.Type.Text',
-            HANDLE_TYPE_CONTACT, contact_handles[0], True)
-    chan = bus.get_object(conn.bus_name, chan_path)
-    priv_text_chan = dbus.Interface(chan,
-            u'org.freedesktop.Telepathy.Channel.Type.Text')
+    stream.sendMessage('PRIVMSG', NICK, ':PRIVATE', prefix=REMOTEUSER)
 
-    # send a private chat message -- the test server is rigged to send a private
-    # chat response
-    call_async(q, priv_text_chan, 'Send', 0, 'foo')
-    q.expect('irc-privmsg', data={'message': 'foo', 'recipient': REMOTEUSER})
-    event = q.expect('dbus-signal', signal='Received',
-            predicate=lambda x: x.args[5]=='PRIVATE' and 'ImChannel' in x.path)
+    event = q.expect('dbus-signal', signal='Received')
+    # this seems a bit fragile, but I'm not entirely sure how else to ensure
+    # that the message is not delivered to the MUC channel
+    assert event.path not in muc_path
 
     # verify that we didn't receive a 'Received' D-Bus signal on the group text
     # channel
@@ -84,5 +53,5 @@ def test(q, bus, conn, stream):
     return True
 
 if __name__ == '__main__':
-    exec_test(test, {'account':NICK}, protocol=CustomIRCServer)
+    exec_test(test, {'account':NICK})
 
-- 
1.5.6.5




More information about the telepathy-commits mailing list