From 172349def2dec3ec03cb4de7a728fade2b3d226d Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Wed, 30 May 2012 20:52:29 -0700 Subject: [PATCH] start on tests for irc.py --- __init__.py | 2 +- tests/test_irc.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/test_irc.py diff --git a/__init__.py b/__init__.py index 2d8fa2b..d015b42 100755 --- a/__init__.py +++ b/__init__.py @@ -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) diff --git a/tests/test_irc.py b/tests/test_irc.py new file mode 100644 index 0000000..3f3e569 --- /dev/null +++ b/tests/test_irc.py @@ -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)