From 4225b96ae124e5e71e7a092caa4de8382ac16923 Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Mon, 12 Mar 2012 00:03:20 -0700 Subject: [PATCH] fix sqlite emitter and add test --- saucebrush/emitters.py | 2 +- saucebrush/tests/emitters.py | 49 +++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/saucebrush/emitters.py b/saucebrush/emitters.py index acd5665..1138581 100644 --- a/saucebrush/emitters.py +++ b/saucebrush/emitters.py @@ -146,7 +146,7 @@ class SqliteEmitter(Emitter): ','.join(record.keys()), qmarks) try: - self._cursor.execute(insert, record.values()) + self._cursor.execute(insert, list(record.values())) except sqlite3.IntegrityError as ie: if not self._quiet: raise ie diff --git a/saucebrush/tests/emitters.py b/saucebrush/tests/emitters.py index 612cd8e..14ec8d8 100644 --- a/saucebrush/tests/emitters.py +++ b/saucebrush/tests/emitters.py @@ -1,9 +1,10 @@ from __future__ import unicode_literals from contextlib import closing from io import StringIO +import os import unittest -from saucebrush.emitters import DebugEmitter, CSVEmitter, CountEmitter +from saucebrush.emitters import DebugEmitter, CSVEmitter, CountEmitter, SqliteEmitter class EmitterTestCase(unittest.TestCase): @@ -13,19 +14,6 @@ class EmitterTestCase(unittest.TestCase): list(de.attach([1,2,3])) self.assertEqual(output.getvalue(), '1\n2\n3\n') - def test_csv_emitter(self): - - try: - import cStringIO # if Python 2.x then use old cStringIO - io = cStringIO.StringIO() - except: - io = StringIO() # if Python 3.x then use StringIO - - with closing(io) as output: - ce = CSVEmitter(output, ('x','y','z')) - list(ce.attach([{'x':1, 'y':2, 'z':3}, {'x':5, 'y':5, 'z':5}])) - self.assertEqual(output.getvalue(), 'x,y,z\r\n1,2,3\r\n5,5,5\r\n') - def test_count_emitter(self): # values for test @@ -49,5 +37,38 @@ class EmitterTestCase(unittest.TestCase): ce.done() self.assertEqual(output.getvalue(), '10 of 22\n20 of 22\n22 of 22\n') + def test_csv_emitter(self): + + try: + import cStringIO # if Python 2.x then use old cStringIO + io = cStringIO.StringIO() + except: + io = StringIO() # if Python 3.x then use StringIO + + with closing(io) as output: + ce = CSVEmitter(output, ('x','y','z')) + list(ce.attach([{'x':1, 'y':2, 'z':3}, {'x':5, 'y':5, 'z':5}])) + self.assertEqual(output.getvalue(), 'x,y,z\r\n1,2,3\r\n5,5,5\r\n') + + def test_sqlite_emitter(self): + + import sqlite3, tempfile + + with closing(tempfile.NamedTemporaryFile(suffix='.db')) as f: + db_path = f.name + + sle = SqliteEmitter(db_path, 'testtable', fieldnames=('a','b','c')) + list(sle.attach([{'a': '1', 'b': '2', 'c': '3'}])) + sle.done() + + with closing(sqlite3.connect(db_path)) as conn: + cur = conn.cursor() + cur.execute("""SELECT a, b, c FROM testtable""") + results = cur.fetchall() + + os.unlink(db_path) + + self.assertEqual(results, [('1', '2', '3')]) + if __name__ == '__main__': unittest.main()