Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test suite for [PikalaxBOT](/pikalaxalt/pikalaxbot)
import unittest
import sqlite3
from utils import sql
from collections import namedtuple
User = namedtuple('User', ['id', 'name'])
Context = namedtuple('Context', ['author'])
me = User(148462033770119168, 'PikalaxALT')
ctx = Context(me)
class TestSql(unittest.TestCase):
@unittest.expectedFailure
def test_db_clear(self):
sql.db_clear()
sql.read_bag()
def test_db_init(self):
sql.db_clear()
sql.db_init()
with sqlite3.connect(sql.dbname) as conn:
conn.execute("insert into game values (?, ?, 1337)", me)
c = conn.execute("select score from game where id = ? limit 1", (me.id,))
self.assertEqual(c.fetchone()[0], 1337)
c = conn.execute("select bag from meme")
for i, texttup in enumerate(c.fetchall()):
with self.subTest(i=i):
text, = texttup
self.assertIn(text, sql.default_bag)
def test_get_score(self):
sql.db_clear()
sql.db_init()
with sqlite3.connect(sql.dbname) as conn:
conn.execute("insert into game values (?, ?, 1337)", me)
self.assertEqual(sql.get_score(me), 1337)
def test_increment_score(self):
sql.db_clear()
sql.db_init()
with sqlite3.connect(sql.dbname) as conn:
conn.execute("insert into game values (?, ?, 1337)", me)
sql.increment_score(ctx)
self.assertEqual(sql.get_score(me), 1338)
sql.db_clear()
sql.db_init()
sql.increment_score(ctx)
self.assertEqual(sql.get_score(me), 1)
def test_get_all_scores(self):
sql.db_clear()
sql.db_init()
with sqlite3.connect(sql.dbname) as conn:
conn.execute("insert into game values (?, ?, 1337)", me)
for i, row in enumerate(sql.get_all_scores()):
with self.subTest(i=i):
self.assertEqual(row, (me.id, me.name, 1337))
def test_add_bag(self):
sql.db_clear()
sql.db_init()
self.assertFalse(sql.add_bag('happily jumped into the bag!'))
self.assertTrue(sql.add_bag('squealed in delight!'))
with sqlite3.connect(sql.dbname) as conn:
c = conn.execute("select bag from meme")
messages = [row[0] for row in c.fetchall()]
self.assertIn('squealed in delight!', messages)
def test_read_bag(self):
sql.db_clear()
sql.db_init()
msg = sql.read_bag()
self.assertNotEqual(msg, None)
self.assertIn(msg, sql.default_bag)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.