From 05f8d4c24c77f09b173d4ef90284c35f56438edf Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Mon, 12 Mar 2012 17:39:26 -0700 Subject: [PATCH] fix SqlDumpEmitter and write basic test --- saucebrush/emitters.py | 16 ++++++++++------ saucebrush/tests/emitters.py | 14 +++++++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/saucebrush/emitters.py b/saucebrush/emitters.py index 1138581..b165407 100644 --- a/saucebrush/emitters.py +++ b/saucebrush/emitters.py @@ -180,21 +180,25 @@ class SqlDumpEmitter(Emitter): table_name, '`,`'.join(fieldnames)) def quote(self, item): + if item is None: return "null" - elif isinstance(item, (unicode, str)): + + try: + types = (basestring,) + except NameError: + types = (str,) + + if isinstance(item, types): item = item.replace("\\","\\\\").replace("'","\\'").replace(chr(0),'0') return "'%s'" % item - else: - return "%s" % item + + return "%s" % item def emit_record(self, record): quoted_data = [self.quote(record[field]) for field in self._fieldnames] self._outfile.write(self._insert_str % ','.join(quoted_data)) - def done(self): - self._outfile.close() - class DjangoModelEmitter(Emitter): """ Emitter that populates a table corresponding to a django model. diff --git a/saucebrush/tests/emitters.py b/saucebrush/tests/emitters.py index 14ec8d8..606178e 100644 --- a/saucebrush/tests/emitters.py +++ b/saucebrush/tests/emitters.py @@ -4,7 +4,8 @@ from io import StringIO import os import unittest -from saucebrush.emitters import DebugEmitter, CSVEmitter, CountEmitter, SqliteEmitter +from saucebrush.emitters import ( + DebugEmitter, CSVEmitter, CountEmitter, SqliteEmitter, SqlDumpEmitter) class EmitterTestCase(unittest.TestCase): @@ -70,5 +71,16 @@ class EmitterTestCase(unittest.TestCase): self.assertEqual(results, [('1', '2', '3')]) + def test_sql_dump_emitter(self): + + with closing(StringIO()) as bffr: + + sde = SqlDumpEmitter(bffr, 'testtable', ('a', 'b')) + list(sde.attach([{'a': 1, 'b': '2'}])) + sde.done() + + self.assertEqual(bffr.getvalue(), "INSERT INTO `testtable` (`a`,`b`) VALUES (1,'2');\n") + + if __name__ == '__main__': unittest.main()