split mock objects into their own module

This commit is contained in:
Brendan Taylor 2008-06-03 23:54:22 +00:00
parent cb1c24ff7d
commit cc307aed70
2 changed files with 83 additions and 54 deletions

68
test/mocks.py Normal file
View file

@ -0,0 +1,68 @@
# gajim-specific mock objects
from mock import Mock
from common import gajim
class MockConnection(Mock):
def __init__(self, name, *args):
Mock.__init__(self, *args)
self.name = name
gajim.connections[name] = self
class MockWindow(Mock):
def __init__(self, *args):
Mock.__init__(self, *args)
self.window = Mock()
class MockChatControl(Mock):
def __init__(self, *args):
Mock.__init__(self, *args)
self.parent_win = MockWindow({'get_active_control': self})
self.session = None
def set_session(self, sess):
self.session = sess
def __nonzero__(self):
return True
def __eq__(self, other):
return self is other
class MockInterface(Mock):
def __init__(self, acct, *args):
Mock.__init__(self, *args)
self.msg_win_mgr = Mock()
self.roster = Mock()
self.remote_ctrl = None
self.minimized_controls = { acct: {} }
class MockLogger(Mock):
def __init__(self):
Mock.__init__(self, {'write': None})
class MockContact(Mock):
def __nonzero__(self):
return True
import random
class MockSession(Mock):
def __init__(self, conn, jid, thread_id, type):
Mock.__init__(self)
self.conn = conn
self.jid = jid
self.type = type
self.thread_id = thread_id
if not self.thread_id:
self.thread_id = '%0x' % random.randint(0, 10000)
def __repr__(self):
print '<MockSession %s>' % self.thread_id
def __nonzero__(self):
return True

View file

@ -10,10 +10,6 @@ sys.path.append(gajim_root + '/src')
# a temporary version of ~/.gajim for testing # a temporary version of ~/.gajim for testing
configdir = gajim_root + '/test/tmp' configdir = gajim_root + '/test/tmp'
import time
from mock import Mock
# define _ for i18n # define _ for i18n
import __builtin__ import __builtin__
__builtin__._ = lambda x: x __builtin__._ = lambda x: x
@ -30,10 +26,15 @@ import common.configpaths
common.configpaths.gajimpaths.init(configdir) common.configpaths.gajimpaths.init(configdir)
common.configpaths.gajimpaths.init_profile() common.configpaths.gajimpaths.init_profile()
import time
# for some reason common.gajim needs to be imported before xmpppy? # for some reason common.gajim needs to be imported before xmpppy?
from common import gajim from common import gajim
from common import xmpp from common import xmpp
from mock import Mock, expectParams
from mocks import *
gajim.DATA_DIR = gajim_root + '/data' gajim.DATA_DIR = gajim_root + '/data'
from common.stanza_session import StanzaSession from common.stanza_session import StanzaSession
@ -41,16 +42,10 @@ from common.stanza_session import StanzaSession
# name to use for the test account # name to use for the test account
account_name = 'test' account_name = 'test'
class MockConnection(Mock):
def __init__(self, *args):
Mock.__init__(self, *args)
self.name = account_name
gajim.connections[self.name] = self
class TestStanzaSession(unittest.TestCase): class TestStanzaSession(unittest.TestCase):
def setUp(self): def setUp(self):
self.jid = 'test@example.org/Gajim' self.jid = 'test@example.org/Gajim'
self.conn = MockConnection({'send_stanza': None}) self.conn = MockConnection(account_name, {'send_stanza': None})
self.sess = StanzaSession(self.conn, self.jid, None, 'chat') self.sess = StanzaSession(self.conn, self.jid, None, 'chat')
def test_generate_thread_id(self): def test_generate_thread_id(self):
@ -77,7 +72,7 @@ class TestStanzaSession(unittest.TestCase):
self.assertEqual(msg.getThread(), self.sess.thread_id) self.assertEqual(msg.getThread(), self.sess.thread_id)
def test_terminate_without_sendng(self): def test_terminate_without_sending(self):
# no termination is sent if no messages have been sent in the session # no termination is sent if no messages have been sent in the session
self.sess.terminate() self.sess.terminate()
@ -100,45 +95,7 @@ class TestStanzaSession(unittest.TestCase):
from session import ChatControlSession from session import ChatControlSession
class MockWindow(Mock): gajim.interface = MockInterface(account_name)
def __init__(self, *args):
Mock.__init__(self, *args)
self.window = Mock()
class MockChatControl(Mock):
def __init__(self, *args):
Mock.__init__(self, *args)
self.parent_win = MockWindow({'get_active_control': self})
self.session = None
def set_session(self, sess):
self.session = sess
def __nonzero__(self):
return True
def __eq__(self, other):
return self is other
class MockInterface(Mock):
def __init__(self, *args):
Mock.__init__(self, *args)
self.msg_win_mgr = Mock()
self.roster = Mock()
self.remote_ctrl = None
self.minimized_controls = { account_name: {} }
class MockLogger(Mock):
def __init__(self):
Mock.__init__(self, {'write': None})
class MockContact(Mock):
def __nonzero__(self):
return True
gajim.interface = MockInterface()
gajim.contacts.add_account(account_name) gajim.contacts.add_account(account_name)
import notify import notify
@ -146,7 +103,7 @@ import notify
class TestChatControlSession(unittest.TestCase): class TestChatControlSession(unittest.TestCase):
def setUp(self): def setUp(self):
self.jid = 'test@example.org/Gajim' self.jid = 'test@example.org/Gajim'
self.conn = MockConnection({'send_stanza': None}) self.conn = MockConnection(account_name, {'send_stanza': None})
self.sess = ChatControlSession(self.conn, self.jid, None) self.sess = ChatControlSession(self.conn, self.jid, None)
gajim.logger = MockLogger() gajim.logger = MockLogger()
@ -217,6 +174,7 @@ class TestChatControlSession(unittest.TestCase):
def test_receive_already_has_control(self): def test_receive_already_has_control(self):
'''test receiving a message with a session already attached to a control''' '''test receiving a message with a session already attached to a control'''
jid = 'bct@necronomicorp.com/Gajim' jid = 'bct@necronomicorp.com/Gajim'
msgtxt = 'testing one two three' msgtxt = 'testing one two three'
@ -240,13 +198,16 @@ class TestChatControlSession(unittest.TestCase):
def test_received_orphaned_control(self): def test_received_orphaned_control(self):
'''test receiving a message when a control that doesn't have a session attached exists''' '''test receiving a message when a control that doesn't have a session attached exists'''
jid = 'bct@necronomicorp.com/Gajim' jid = 'bct@necronomicorp.com'
fjid = jid + '/Gajim'
msgtxt = 'testing one two three' msgtxt = 'testing one two three'
ctrl = MockChatControl() ctrl = MockChatControl()
gajim.interface.msg_win_mgr = Mock({'get_sessionless_ctrl': ctrl}) gajim.interface.msg_win_mgr = Mock({'get_sessionless_ctrl': ctrl})
gajim.interface.msg_win_mgr.mockSetExpectation('get_sessionless_ctrl',
expectParams(account_name, jid))
self.receive_chat_msg(jid, msgtxt) self.receive_chat_msg(fjid, msgtxt)
# message was logged # message was logged
calls = gajim.logger.mockGetNamedCalls('write') calls = gajim.logger.mockGetNamedCalls('write')