From 0fffbb82000a1a6c3c23d62fedcbd8e8141f8994 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 7 Oct 2010 10:58:13 -0400 Subject: [PATCH] Unit test reorganization. Moved SleekTest to sleekxmpp.test. Organized test suites by their focus. - Suites focused on testing stanza objects are named test_stanza_X.py - Suites focused on testing stream behavior are name test_stream_X.py --- sleekxmpp/test/__init__.py | 10 ++ sleekxmpp/test/mocksocket.py | 139 +++++++++++++++++ {tests => sleekxmpp/test}/sleektest.py | 144 +----------------- tests/__init__.py | 0 tests/test_events.py | 3 +- tests/test_jid.py | 4 +- ...test_stanzabase.py => test_stanza_base.py} | 4 +- ..._elementbase.py => test_stanza_element.py} | 3 +- ...t_errorstanzas.py => test_stanza_error.py} | 3 +- tests/{test_gmail.py => test_stanza_gmail.py} | 24 +-- .../{test_iqstanzas.py => test_stanza_iq.py} | 2 +- ...ssagestanzas.py => test_stanza_message.py} | 2 +- ...encestanzas.py => test_stanza_presence.py} | 3 +- .../{test_roster.py => test_stanza_roster.py} | 2 +- ...{test_forms.py => test_stanza_xep_0004.py} | 2 +- ...{test_disco.py => test_stanza_xep_0030.py} | 2 +- ...t_addresses.py => test_stanza_xep_0033.py} | 2 +- ...bsubstanzas.py => test_stanza_xep_0060.py} | 2 +- ..._chatstates.py => test_stanza_xep_0085.py} | 2 +- .../{test_streamtester.py => test_stream.py} | 4 +- ...st_handlers.py => test_stream_handlers.py} | 4 +- tests/test_tostring.py | 2 +- 22 files changed, 192 insertions(+), 171 deletions(-) create mode 100644 sleekxmpp/test/__init__.py create mode 100644 sleekxmpp/test/mocksocket.py rename {tests => sleekxmpp/test}/sleektest.py (83%) delete mode 100644 tests/__init__.py rename tests/{test_stanzabase.py => test_stanza_base.py} (98%) rename tests/{test_elementbase.py => test_stanza_element.py} (99%) rename tests/{test_errorstanzas.py => test_stanza_error.py} (98%) rename tests/{test_gmail.py => test_stanza_gmail.py} (90%) rename tests/{test_iqstanzas.py => test_stanza_iq.py} (98%) rename tests/{test_messagestanzas.py => test_stanza_message.py} (98%) rename tests/{test_presencestanzas.py => test_stanza_presence.py} (98%) rename tests/{test_roster.py => test_stanza_roster.py} (98%) rename tests/{test_forms.py => test_stanza_xep_0004.py} (99%) rename tests/{test_disco.py => test_stanza_xep_0030.py} (99%) rename tests/{test_addresses.py => test_stanza_xep_0033.py} (99%) rename tests/{test_pubsubstanzas.py => test_stanza_xep_0060.py} (99%) rename tests/{test_chatstates.py => test_stanza_xep_0085.py} (97%) rename tests/{test_streamtester.py => test_stream.py} (95%) rename tests/{test_handlers.py => test_stream_handlers.py} (98%) diff --git a/sleekxmpp/test/__init__.py b/sleekxmpp/test/__init__.py new file mode 100644 index 0000000..52321c9 --- /dev/null +++ b/sleekxmpp/test/__init__.py @@ -0,0 +1,10 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.test.mocksocket import TestSocket +from sleekxmpp.test.sleektest import * diff --git a/sleekxmpp/test/mocksocket.py b/sleekxmpp/test/mocksocket.py new file mode 100644 index 0000000..9310947 --- /dev/null +++ b/sleekxmpp/test/mocksocket.py @@ -0,0 +1,139 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +import socket +try: + import queue +except ImportError: + import Queue as queue + + +class TestSocket(object): + + """ + A dummy socket that reads and writes to queues instead + of an actual networking socket. + + Methods: + next_sent -- Return the next sent stanza. + recv_data -- Make a stanza available to read next. + recv -- Read the next stanza from the socket. + send -- Write a stanza to the socket. + makefile -- Dummy call, returns self. + read -- Read the next stanza from the socket. + """ + + def __init__(self, *args, **kwargs): + """ + Create a new test socket. + + Arguments: + Same as arguments for socket.socket + """ + self.socket = socket.socket(*args, **kwargs) + self.recv_queue = queue.Queue() + self.send_queue = queue.Queue() + + def __getattr__(self, name): + """ + Return attribute values of internal, dummy socket. + + Some attributes and methods are disabled to prevent the + socket from connecting to the network. + + Arguments: + name -- Name of the attribute requested. + """ + + def dummy(*args): + """Method to do nothing and prevent actual socket connections.""" + return None + + overrides = {'connect': dummy, + 'close': dummy, + 'shutdown': dummy} + + return overrides.get(name, getattr(self.socket, name)) + + # ------------------------------------------------------------------ + # Testing Interface + + def next_sent(self, timeout=None): + """ + Get the next stanza that has been 'sent'. + + Arguments: + timeout -- Optional timeout for waiting for a new value. + """ + args = {'block': False} + if timeout is not None: + args = {'block': True, 'timeout': timeout} + try: + return self.send_queue.get(**args) + except: + return None + + def recv_data(self, data): + """ + Add data to the receiving queue. + + Arguments: + data -- String data to 'write' to the socket to be received + by the XMPP client. + """ + self.recv_queue.put(data) + + # ------------------------------------------------------------------ + # Socket Interface + + def recv(self, *args, **kwargs): + """ + Read a value from the received queue. + + Arguments: + Placeholders. Same as for socket.Socket.recv. + """ + return self.read(block=True) + + def send(self, data): + """ + Send data by placing it in the send queue. + + Arguments: + data -- String value to write. + """ + self.send_queue.put(data) + + # ------------------------------------------------------------------ + # File Socket + + def makefile(self, *args, **kwargs): + """ + File socket version to use with ElementTree. + + Arguments: + Placeholders, same as socket.Socket.makefile() + """ + return self + + def read(self, block=True, timeout=None, **kwargs): + """ + Implement the file socket interface. + + Arguments: + block -- Indicate if the read should block until a + value is ready. + timeout -- Time in seconds a block should last before + returning None. + """ + if timeout is not None: + block = True + try: + return self.recv_queue.get(block, timeout) + except: + return None diff --git a/tests/sleektest.py b/sleekxmpp/test/sleektest.py similarity index 83% rename from tests/sleektest.py rename to sleekxmpp/test/sleektest.py index 2890187..fd4120d 100644 --- a/tests/sleektest.py +++ b/sleekxmpp/test/sleektest.py @@ -7,145 +7,15 @@ """ import unittest -import socket -try: - import queue -except ImportError: - import Queue as queue import sleekxmpp from sleekxmpp import ClientXMPP, ComponentXMPP from sleekxmpp.stanza import Message, Iq, Presence +from sleekxmpp.test import TestSocket from sleekxmpp.xmlstream.stanzabase import registerStanzaPlugin, ET from sleekxmpp.xmlstream.tostring import tostring -class TestSocket(object): - - """ - A dummy socket that reads and writes to queues instead - of an actual networking socket. - - Methods: - next_sent -- Return the next sent stanza. - recv_data -- Make a stanza available to read next. - recv -- Read the next stanza from the socket. - send -- Write a stanza to the socket. - makefile -- Dummy call, returns self. - read -- Read the next stanza from the socket. - """ - - def __init__(self, *args, **kwargs): - """ - Create a new test socket. - - Arguments: - Same as arguments for socket.socket - """ - self.socket = socket.socket(*args, **kwargs) - self.recv_queue = queue.Queue() - self.send_queue = queue.Queue() - - def __getattr__(self, name): - """ - Return attribute values of internal, dummy socket. - - Some attributes and methods are disabled to prevent the - socket from connecting to the network. - - Arguments: - name -- Name of the attribute requested. - """ - - def dummy(*args): - """Method to do nothing and prevent actual socket connections.""" - return None - - overrides = {'connect': dummy, - 'close': dummy, - 'shutdown': dummy} - - return overrides.get(name, getattr(self.socket, name)) - - # ------------------------------------------------------------------ - # Testing Interface - - def next_sent(self, timeout=None): - """ - Get the next stanza that has been 'sent'. - - Arguments: - timeout -- Optional timeout for waiting for a new value. - """ - args = {'block': False} - if timeout is not None: - args = {'block': True, 'timeout': timeout} - try: - return self.send_queue.get(**args) - except: - return None - - def recv_data(self, data): - """ - Add data to the receiving queue. - - Arguments: - data -- String data to 'write' to the socket to be received - by the XMPP client. - """ - self.recv_queue.put(data) - - # ------------------------------------------------------------------ - # Socket Interface - - def recv(self, *args, **kwargs): - """ - Read a value from the received queue. - - Arguments: - Placeholders. Same as for socket.Socket.recv. - """ - return self.read(block=True) - - def send(self, data): - """ - Send data by placing it in the send queue. - - Arguments: - data -- String value to write. - """ - self.send_queue.put(data) - - # ------------------------------------------------------------------ - # File Socket - - def makefile(self, *args, **kwargs): - """ - File socket version to use with ElementTree. - - Arguments: - Placeholders, same as socket.Socket.makefile() - """ - return self - - def read(self, block=True, timeout=None, **kwargs): - """ - Implement the file socket interface. - - Arguments: - block -- Indicate if the read should block until a - value is ready. - timeout -- Time in seconds a block should last before - returning None. - """ - if timeout is not None: - block = True - try: - return self.recv_queue.get(block, timeout) - except: - return None - - class SleekTest(unittest.TestCase): """ @@ -402,12 +272,12 @@ class SleekTest(unittest.TestCase): self.xmpp.socket.recv_data(data) def stream_make_header(self, sto='', - sfrom='', - sid='', - stream_ns="http://etherx.jabber.org/streams", - default_ns="jabber:client", - version="1.0", - xml_header=True): + sfrom='', + sid='', + stream_ns="http://etherx.jabber.org/streams", + default_ns="jabber:client", + version="1.0", + xml_header=True): """ Create a stream header to be received by the test XMPP agent. diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_events.py b/tests/test_events.py index d9d80c3..ea4cf8a 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -1,6 +1,5 @@ -import sleekxmpp import time -from . sleektest import * +from sleekxmpp.test import * class TestEvents(SleekTest): diff --git a/tests/test_jid.py b/tests/test_jid.py index cddac42..4504731 100644 --- a/tests/test_jid.py +++ b/tests/test_jid.py @@ -1,6 +1,7 @@ -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.xmlstream.jid import JID + class TestJIDClass(SleekTest): def testJIDfromfull(self): j = JID('user@someserver/some/resource') @@ -23,4 +24,5 @@ class TestJIDClass(SleekTest): self.assertEqual(j.full, 'user@someserver/some/resource', "Full does not match") self.assertEqual(str(j), 'user@someserver/some/resource', "String does not match") + suite = unittest.TestLoader().loadTestsFromTestCase(TestJIDClass) diff --git a/tests/test_stanzabase.py b/tests/test_stanza_base.py similarity index 98% rename from tests/test_stanzabase.py rename to tests/test_stanza_base.py index 682068d..9bd326b 100644 --- a/tests/test_stanzabase.py +++ b/tests/test_stanza_base.py @@ -1,7 +1,7 @@ -from . sleektest import * -import sleekxmpp +from sleekxmpp.test import * from sleekxmpp.xmlstream.stanzabase import ET, StanzaBase + class TestStanzaBase(SleekTest): def testTo(self): diff --git a/tests/test_elementbase.py b/tests/test_stanza_element.py similarity index 99% rename from tests/test_elementbase.py rename to tests/test_stanza_element.py index 19794c9..c157e0a 100644 --- a/tests/test_elementbase.py +++ b/tests/test_stanza_element.py @@ -1,6 +1,7 @@ -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.xmlstream.stanzabase import ElementBase + class TestElementBase(SleekTest): def testFixNs(self): diff --git a/tests/test_errorstanzas.py b/tests/test_stanza_error.py similarity index 98% rename from tests/test_errorstanzas.py rename to tests/test_stanza_error.py index a883b7e..f76ac62 100644 --- a/tests/test_errorstanzas.py +++ b/tests/test_stanza_error.py @@ -1,4 +1,5 @@ -from . sleektest import * +from sleekxmpp.test import * + class TestErrorStanzas(SleekTest): diff --git a/tests/test_gmail.py b/tests/test_stanza_gmail.py similarity index 90% rename from tests/test_gmail.py rename to tests/test_stanza_gmail.py index 5636e87..5c31a4e 100644 --- a/tests/test_gmail.py +++ b/tests/test_stanza_gmail.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.gmail_notify as gmail @@ -8,10 +8,10 @@ class TestGmail(SleekTest): registerStanzaPlugin(Iq, gmail.GmailQuery) registerStanzaPlugin(Iq, gmail.MailBox) registerStanzaPlugin(Iq, gmail.NewMail) - + def testCreateQuery(self): """Testing querying Gmail for emails.""" - + iq = self.Iq() iq['type'] = 'get' iq['gmail']['search'] = 'is:starred' @@ -29,20 +29,20 @@ class TestGmail(SleekTest): def testMailBox(self): """Testing reading from Gmail mailbox result""" - + # Use the example from Google's documentation at # http://code.google.com/apis/talk/jep_extensions/gmail.html#notifications xml = ET.fromstring(""" - @@ -63,7 +63,7 @@ class TestGmail(SleekTest): self.failUnless(mailbox['url'] == 'http://mail.google.com/mail', "url doesn't match") self.failUnless(mailbox['matched'] == '95', "total-matched incorrect") self.failUnless(mailbox['estimate'] == False, "total-estimate incorrect") - self.failUnless(len(mailbox['threads']) == 1, "could not extract message threads") + self.failUnless(len(mailbox['threads']) == 1, "could not extract message threads") thread = mailbox['threads'][0] self.failUnless(thread['tid'] == '1172320964060972012', "thread tid doesn't match") @@ -81,8 +81,8 @@ class TestGmail(SleekTest): self.failUnless(sender1['address'] == 'romeo@gmail.com', "sender address doesn't match") self.failUnless(sender1['originator'] == True, "sender originator incorrect") self.failUnless(sender1['unread'] == False, "sender unread incorrectly True") - - sender2 = thread['senders'][2] + + sender2 = thread['senders'][2] self.failUnless(sender2['unread'] == True, "sender unread incorrectly False") suite = unittest.TestLoader().loadTestsFromTestCase(TestGmail) diff --git a/tests/test_iqstanzas.py b/tests/test_stanza_iq.py similarity index 98% rename from tests/test_iqstanzas.py rename to tests/test_stanza_iq.py index 197bc00..3f6ce8d 100644 --- a/tests/test_iqstanzas.py +++ b/tests/test_stanza_iq.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.xmlstream.stanzabase import ET diff --git a/tests/test_messagestanzas.py b/tests/test_stanza_message.py similarity index 98% rename from tests/test_messagestanzas.py rename to tests/test_stanza_message.py index d57f5ad..d4f4d8d 100644 --- a/tests/test_messagestanzas.py +++ b/tests/test_stanza_message.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.stanza.message import Message from sleekxmpp.stanza.htmlim import HTMLIM diff --git a/tests/test_presencestanzas.py b/tests/test_stanza_presence.py similarity index 98% rename from tests/test_presencestanzas.py rename to tests/test_stanza_presence.py index 2452df4..ad357ec 100644 --- a/tests/test_presencestanzas.py +++ b/tests/test_stanza_presence.py @@ -1,5 +1,4 @@ -import sleekxmpp -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.stanza.presence import Presence diff --git a/tests/test_roster.py b/tests/test_stanza_roster.py similarity index 98% rename from tests/test_roster.py rename to tests/test_stanza_roster.py index f210551..a0e5798 100644 --- a/tests/test_roster.py +++ b/tests/test_stanza_roster.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.stanza.roster import Roster diff --git a/tests/test_forms.py b/tests/test_stanza_xep_0004.py similarity index 99% rename from tests/test_forms.py rename to tests/test_stanza_xep_0004.py index 67b0f6a..835f0dd 100644 --- a/tests/test_forms.py +++ b/tests/test_stanza_xep_0004.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.xep_0004 as xep_0004 diff --git a/tests/test_disco.py b/tests/test_stanza_xep_0030.py similarity index 99% rename from tests/test_disco.py rename to tests/test_stanza_xep_0030.py index f65fdaf..bb7ee6a 100644 --- a/tests/test_disco.py +++ b/tests/test_stanza_xep_0030.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.xep_0030 as xep_0030 diff --git a/tests/test_addresses.py b/tests/test_stanza_xep_0033.py similarity index 99% rename from tests/test_addresses.py rename to tests/test_stanza_xep_0033.py index 5c510c7..90f6374 100644 --- a/tests/test_addresses.py +++ b/tests/test_stanza_xep_0033.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.xep_0033 as xep_0033 diff --git a/tests/test_pubsubstanzas.py b/tests/test_stanza_xep_0060.py similarity index 99% rename from tests/test_pubsubstanzas.py rename to tests/test_stanza_xep_0060.py index 42a1b52..32ee37a 100644 --- a/tests/test_pubsubstanzas.py +++ b/tests/test_stanza_xep_0060.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.xep_0004 as xep_0004 import sleekxmpp.plugins.stanza_pubsub as pubsub diff --git a/tests/test_chatstates.py b/tests/test_stanza_xep_0085.py similarity index 97% rename from tests/test_chatstates.py rename to tests/test_stanza_xep_0085.py index d5e3979..5e63530 100644 --- a/tests/test_chatstates.py +++ b/tests/test_stanza_xep_0085.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.xep_0085 as xep_0085 class TestChatStates(SleekTest): diff --git a/tests/test_streamtester.py b/tests/test_stream.py similarity index 95% rename from tests/test_streamtester.py rename to tests/test_stream.py index 9bc9de6..3fbf86e 100644 --- a/tests/test_streamtester.py +++ b/tests/test_stream.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * import sleekxmpp.plugins.xep_0033 as xep_0033 @@ -55,6 +55,6 @@ class TestStreamTester(SleekTest): def testSendStreamHeader(self): """Test that we can check a sent stream header.""" self.stream_start(mode='client', skip=False) - self.streamSendHeader(sto='localhost') + self.stream_send_header(sto='localhost') suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamTester) diff --git a/tests/test_handlers.py b/tests/test_stream_handlers.py similarity index 98% rename from tests/test_handlers.py rename to tests/test_stream_handlers.py index 70eb024..8c05572 100644 --- a/tests/test_handlers.py +++ b/tests/test_stream_handlers.py @@ -1,8 +1,8 @@ -from . sleektest import * -import sleekxmpp +from sleekxmpp.test import * from sleekxmpp.xmlstream.handler import * from sleekxmpp.xmlstream.matcher import * + class TestHandlers(SleekTest): """ Test using handlers and waiters. diff --git a/tests/test_tostring.py b/tests/test_tostring.py index 2999949..45f405c 100644 --- a/tests/test_tostring.py +++ b/tests/test_tostring.py @@ -1,4 +1,4 @@ -from . sleektest import * +from sleekxmpp.test import * from sleekxmpp.stanza import Message from sleekxmpp.xmlstream.stanzabase import ET from sleekxmpp.xmlstream.tostring import tostring, xml_escape