start on tests for irc.py

master
mutantmonkey 2012-05-30 20:52:29 -07:00
parent 946171fccb
commit 172349def2
2 changed files with 90 additions and 1 deletions

View File

@ -8,7 +8,6 @@ http://inamidst.com/phenny/
"""
import sys, os, time, threading, signal
import bot
class Watcher(object):
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
@ -38,6 +37,7 @@ def run_phenny(config):
else: delay = 20
def connect(config):
import bot
p = bot.Phenny(config)
p.run(config.host, config.port, config.ssl, config.ipv6)

89
tests/test_irc.py Normal file
View File

@ -0,0 +1,89 @@
"""
Tests for phenny's irc.py
"""
# add current working directory to path
import sys
sys.path.append('.')
import irc
import unittest
from mock import call, patch, Mock
class OriginTest(unittest.TestCase):
def setUp(self):
self.bot = Mock()
def test_server(self):
source = "foobar.example.com"
origin = irc.Origin(self.bot, source, [])
self.assertEqual(origin.host, '')
def test_privmsg(self):
source = "Foobar!foo@bar.example.com"
args = ['PRIVMSG', '#phenny']
origin = irc.Origin(self.bot, source, args)
self.assertEqual(origin.nick, 'Foobar')
self.assertEqual(origin.user, 'foo')
self.assertEqual(origin.host, 'bar.example.com')
self.assertEqual(origin.sender, '#phenny')
class BotTest(unittest.TestCase):
@patch('threading.RLock')
@patch('asynchat.async_chat')
def setUp(self, mock_async, mock_thread):
self.nick = 'foo'
self.name = 'Phenny'
self.bot = irc.Bot(self.nick, self.name, '#phenny')
@patch('irc.Bot.write')
def test_login(self, mock_write):
self.bot.verbose = False
self.bot.handle_connect()
mock_write.assert_has_calls([
call(('NICK', self.nick)),
call(('USER', self.nick, '+iw', self.nick), self.name)
])
@patch('irc.Bot.write')
def test_ping(self, mock_write):
self.bot.buffer = b"PING"
self.bot.found_terminator()
mock_write.assert_called_once_with(('PONG', ''))
@patch('irc.Bot.push')
def test_msg(self, mock_push):
self.bot.msg('#phenny', 'hi')
mock_push.assert_called_once_with(b'PRIVMSG #phenny :hi\r\n')
@patch('time.sleep') # patch sleep so test runs faster
@patch('irc.Bot.push')
def test_msgflood(self, mock_push, mock_sleep):
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
self.bot.msg('#phenny', 'flood')
mock_push.assert_called_with(b'PRIVMSG #phenny :...\r\n')
self.assertEqual(mock_sleep.call_count, 5)
@patch('irc.Bot.msg')
def test_action(self, mock_msg):
self.bot.action('foo', 'is')
mock_msg.assert_called_once_with('foo', '\x01ACTION is\x01')
@patch('irc.Bot.write')
def test_notice(self, mock_write):
notice = "This is a notice!"
self.bot.notice('jqh', notice)
mock_write.assert_called_once_with((b'NOTICE', 'jqh'), notice)