fix SqlDumpEmitter and write basic test
This commit is contained in:
parent
5ac3948ffe
commit
05f8d4c24c
@ -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.
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user