mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
FunDB: Made more general get/remove/num/add commands to consolidate getlart, numinsults, et.al.
test_FunDB: Started adding tests for FunDB
This commit is contained in:
parent
11a3cf195c
commit
24005d3df1
176
plugins/FunDB.py
176
plugins/FunDB.py
@ -183,43 +183,6 @@ class FunDB(callbacks.Privmsg):
|
|||||||
else:
|
else:
|
||||||
irc.reply(msg, cursor.fetchone()[0])
|
irc.reply(msg, cursor.fetchone()[0])
|
||||||
|
|
||||||
def addinsult(self, irc, msg, args):
|
|
||||||
"""<insult>
|
|
||||||
|
|
||||||
Adds an insult to the insult database.
|
|
||||||
"""
|
|
||||||
insult = privmsgs.getArgs(args)
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute("""INSERT INTO insults VALUES (NULL, %s)""", insult)
|
|
||||||
self.db.commit()
|
|
||||||
irc.reply(msg, conf.replySuccess)
|
|
||||||
|
|
||||||
def removeinsult(self, irc, msg, args):
|
|
||||||
"""<id>
|
|
||||||
|
|
||||||
Removes the insult with id <id> from the insult database.
|
|
||||||
"""
|
|
||||||
id = privmsgs.getArgs(args)
|
|
||||||
try:
|
|
||||||
id = int(id)
|
|
||||||
except ValueError:
|
|
||||||
irc.error(msg, 'You must give a numeric id.')
|
|
||||||
return
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute("""DELETE FROM insults WHERE id=%s""", id)
|
|
||||||
self.db.commit()
|
|
||||||
irc.reply(msg, conf.replySuccess)
|
|
||||||
|
|
||||||
def numinsults(self, irc, msg, args):
|
|
||||||
"""takes no arguments
|
|
||||||
|
|
||||||
Returns the number of insults currently in the database.
|
|
||||||
"""
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute('SELECT count(*) FROM insults')
|
|
||||||
total = cursor.fetchone()[0]
|
|
||||||
irc.reply(msg, 'There are currently %s insults in my database' % total)
|
|
||||||
|
|
||||||
def crossword(self, irc, msg, args):
|
def crossword(self, irc, msg, args):
|
||||||
"""<word>
|
"""<word>
|
||||||
|
|
||||||
@ -268,42 +231,92 @@ class FunDB(callbacks.Privmsg):
|
|||||||
else:
|
else:
|
||||||
irc.reply(msg, cursor.fetchone()[0])
|
irc.reply(msg, cursor.fetchone()[0])
|
||||||
|
|
||||||
def addexcuse(self, irc, msg, args):
|
_tables = ['lart','excuse','insult','praise']
|
||||||
"""<excuse>
|
def adddb(self, irc, msg, args):
|
||||||
|
"""<lart|excuse|insult|praise> <text>
|
||||||
|
|
||||||
Adds another excuse to the database.
|
Adds another record to the data referred to in the first argument.
|
||||||
"""
|
"""
|
||||||
excuse = privmsgs.getArgs(args)
|
(table, s) = privmsgs.getArgs(args, needed=2)
|
||||||
|
table = str.lower(table)
|
||||||
|
if table == "lart" or table == "praise":
|
||||||
|
if '$who' not in s:
|
||||||
|
irc.error(msg, 'There must be an $who in the lart/praise '\
|
||||||
|
'somewhere.')
|
||||||
|
return
|
||||||
|
elif table not in self._tables:
|
||||||
|
irc.error(msg, '\"%s\" is an invalid choice. Must be one of: '\
|
||||||
|
'lart, excuse, insult, praise.' % table)
|
||||||
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""INSERT INTO excuses VALUES (NULL, %s)""", excuse)
|
cursor.execute("""INSERT INTO %s VALUES (NULL, %s)""", table+'s', s)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
def removeexcuse(self, irc, msg, args):
|
def removedb(self, irc, msg, args):
|
||||||
"""<id>
|
"""<lart|excuse|insult|praise> <id>
|
||||||
|
|
||||||
Removes the excuse with the id number <id> from the database.
|
Removes the data, referred to in the first argument, with the id
|
||||||
|
number <id> from the database.
|
||||||
"""
|
"""
|
||||||
id = privmsgs.getArgs(args)
|
(table, id) = privmsgs.getArgs(args, needed=2)
|
||||||
|
table = str.lower(table)
|
||||||
try:
|
try:
|
||||||
id = int(id)
|
id = int(id)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'You must give a numeric id.')
|
irc.error(msg, 'You must give a numeric id.')
|
||||||
return
|
return
|
||||||
|
if table not in self._tables:
|
||||||
|
irc.error(msg, '\"%s\" is an invalid choice. Must be one of: '\
|
||||||
|
'lart, excuse, insult, praise.' % table)
|
||||||
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute("""DELETE FROM excuses WHERE id=%s""", id)
|
cursor.execute("""DELETE FROM %s WHERE id=%s""", table+'s', id)
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
|
|
||||||
def numexcuses(self, irc, msg, args):
|
def numdb(self, irc, msg, args):
|
||||||
"""takes no arguments
|
"""<lart|excuse|insult|praise>
|
||||||
|
|
||||||
Returns the number of excuses currently in the database.
|
Returns the number of records, of the type specified, currently in
|
||||||
|
the database.
|
||||||
"""
|
"""
|
||||||
|
table = privmsgs.getArgs(args)
|
||||||
|
table = str.lower(table)
|
||||||
|
if table not in self._tables:
|
||||||
|
irc.error(msg, '\"%s\" is an invalid choice. Must be one of: '\
|
||||||
|
'lart, excuse, insult, praise.' % table)
|
||||||
|
return
|
||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
cursor.execute('SELECT count(*) FROM excuses')
|
cursor.execute('SELECT count(*) FROM %s', table+'s')
|
||||||
total = cursor.fetchone()[0]
|
total = cursor.fetchone()[0]
|
||||||
irc.reply(msg, 'There are currently %s excuses in my database' % total)
|
irc.reply(msg, 'There are currently %s %s in my database' %\
|
||||||
|
(total,table+'s'))
|
||||||
|
|
||||||
|
def getdb(self, irc, msg, args):
|
||||||
|
"""<lart|excuse|insult|praise> <id>
|
||||||
|
|
||||||
|
Gets the record, of the type specified, with the id number <id>.
|
||||||
|
"""
|
||||||
|
(table, id) = privmsgs.getArgs(args, needed=2)
|
||||||
|
table = str.lower(table)
|
||||||
|
try:
|
||||||
|
id = int(id)
|
||||||
|
except ValueError:
|
||||||
|
irc.error(msg, 'The id must be an integer.')
|
||||||
|
return
|
||||||
|
if table not in self._tables:
|
||||||
|
irc.error(msg, '\"%s\" is an invalid choice. Must be one of: '\
|
||||||
|
'lart, excuse, insult, praise.' % table)
|
||||||
|
return
|
||||||
|
cursor = self.db.cursor()
|
||||||
|
print "%s %s" % (table, table+'s')
|
||||||
|
cursor.execute("""SELECT %s FROM %s WHERE id=%s""", table, table+'s',
|
||||||
|
id)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.error(msg, 'There is no such %s.' % table)
|
||||||
|
else:
|
||||||
|
irc.reply(msg, cursor.fetchone()[0])
|
||||||
|
|
||||||
def lart(self, irc, msg, args):
|
def lart(self, irc, msg, args):
|
||||||
"""[<channel>] <nick>
|
"""[<channel>] <nick>
|
||||||
@ -326,65 +339,6 @@ class FunDB(callbacks.Privmsg):
|
|||||||
lart = lart.replace("$who", lartee)
|
lart = lart.replace("$who", lartee)
|
||||||
irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (lart, id)))
|
irc.queueMsg(ircmsgs.action(channel, '%s (#%s)' % (lart, id)))
|
||||||
|
|
||||||
def getlart(self, irc, msg, args):
|
|
||||||
"""<id>
|
|
||||||
|
|
||||||
Gets the lart with the id number <id>.
|
|
||||||
"""
|
|
||||||
id = privmsgs.getArgs(args)
|
|
||||||
try:
|
|
||||||
id = int(id)
|
|
||||||
except ValueError:
|
|
||||||
irc.error(msg, 'The id must be an integer.')
|
|
||||||
return
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute("""SELECT lart FROM larts WHERE id=%s""", id)
|
|
||||||
if cursor.rowcount == 0:
|
|
||||||
irc.error(msg, 'There is no such lart.')
|
|
||||||
else:
|
|
||||||
irc.reply(msg, cursor.fetchone()[0])
|
|
||||||
|
|
||||||
def addlart(self, irc, msg, args):
|
|
||||||
"""<lart>
|
|
||||||
|
|
||||||
The target of the lart is represented with '$who'. And example might
|
|
||||||
be "addlart chops $who in half with an AOL cd."
|
|
||||||
"""
|
|
||||||
lart = privmsgs.getArgs(args)
|
|
||||||
if '$who' in lart:
|
|
||||||
irc.error(msg, 'There must be an $who in the lart somewhere.')
|
|
||||||
return
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute("""INSERT INTO larts VALUES (NULL, %s)""", lart)
|
|
||||||
self.db.commit()
|
|
||||||
irc.reply(msg, conf.replySuccess)
|
|
||||||
|
|
||||||
def removelart(self, irc, msg, args):
|
|
||||||
"""<id>
|
|
||||||
|
|
||||||
Removes the lart with id number <id> from the database.
|
|
||||||
"""
|
|
||||||
id = privmsgs.getArgs(args)
|
|
||||||
try:
|
|
||||||
id = int(id)
|
|
||||||
except ValueError:
|
|
||||||
irc.error(msg, 'You must give a numeric id.')
|
|
||||||
return
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute("""DELETE FROM larts WHERE id=%s""", id)
|
|
||||||
self.db.commit()
|
|
||||||
irc.reply(msg, conf.replySuccess)
|
|
||||||
|
|
||||||
def numlarts(self, irc, msg, args):
|
|
||||||
"""takes no arguments
|
|
||||||
|
|
||||||
Returns the number of larts currently in the database.
|
|
||||||
"""
|
|
||||||
cursor = self.db.cursor()
|
|
||||||
cursor.execute('SELECT count(*) FROM larts')
|
|
||||||
total = cursor.fetchone()[0]
|
|
||||||
irc.reply(msg, 'There are currently %s larts in my database' % total)
|
|
||||||
|
|
||||||
def addword(self, irc, msg, args):
|
def addword(self, irc, msg, args):
|
||||||
"""<word>
|
"""<word>
|
||||||
|
|
||||||
|
@ -32,7 +32,17 @@
|
|||||||
from test import *
|
from test import *
|
||||||
|
|
||||||
class TestFunDB(PluginTestCase):
|
class TestFunDB(PluginTestCase):
|
||||||
pass
|
plugins = ('FunDB',)
|
||||||
|
|
||||||
|
def testAdddb(self):
|
||||||
|
self.assertError('adddb l4rt foo')
|
||||||
|
self.assertError('adddb lart foo')
|
||||||
|
self.assertNotError('adddb lart jabs $who')
|
||||||
|
|
||||||
|
def testRemovedb(self):
|
||||||
|
self.assertError('removedb l4rt foo')
|
||||||
|
self.assertError('removedb lart foo')
|
||||||
|
self.assertNotError('removedb lart 1')
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user