From 5c1fc0114dfb1081fd960a211b33bc07f8a87b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Wed, 22 Oct 2003 19:52:05 +0000 Subject: [PATCH] Added optional argument to FunDB's excuse --- plugins/FunDB.py | 26 ++++++++++++++++++++------ test/test_FunDB.py | 9 +++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plugins/FunDB.py b/plugins/FunDB.py index f5ec4f13b..f54681f5b 100755 --- a/plugins/FunDB.py +++ b/plugins/FunDB.py @@ -182,15 +182,29 @@ class FunDB(callbacks.Privmsg): irc.reply(msg, ', '.join(words)) def excuse(self, irc, msg, args): - """takes no arguments + """[] - Gives you a standard BOFH excuse. + Gives you a standard, random BOFH excuse or the excuse with the given + . """ + id = privmsgs.getArgs(args, needed=0, optional=1) cursor = self.db.cursor() - cursor.execute("""SELECT id, excuse FROM excuses - WHERE excuse NOTNULL - ORDER BY random() - LIMIT 1""") + if id: + try: + id = int(id) + except ValueError: + irc.error(msg, 'The argument must be an integer.') + return + cursor.execute("""SELECT id, excuse FROM excuses WHERE id=%s""", + id) + if cursor.rowcount == 0: + irc.error(msg, 'There is no such excuse.') + return + else: + cursor.execute("""SELECT id, excuse FROM excuses + WHERE excuse NOTNULL + ORDER BY random() + LIMIT 1""") if cursor.rowcount == 0: irc.error(msg, 'There are currently no available excuses.') else: diff --git a/test/test_FunDB.py b/test/test_FunDB.py index 6121af2f1..53cdac46a 100644 --- a/test/test_FunDB.py +++ b/test/test_FunDB.py @@ -72,11 +72,16 @@ if sqlite is not None: def testExcuse(self): self.assertNotError('dbadd excuse Power failure') - self.assertNotError('excuse') - self.assertNotError('excuse a few random words') + self.assertResponse('excuse', 'Power failure (#1)') + self.assertError('excuse a few random words') self.assertNotError('dbnum excuse') + self.assertNotError('dbadd excuse /pub/lunch') + self.assertResponse('excuse 1', 'Power failure (#1)') self.assertNotError('dbremove excuse 1') self.assertNotError('dbnum excuse') + self.assertResponse('excuse', '/pub/lunch (#2)') + self.assertNotError('dbremove excuse 2') + self.assertNotError('dbnum excuse') self.assertError('excuse') def testInsult(self):