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))
|
table_name, '`,`'.join(fieldnames))
|
||||||
|
|
||||||
def quote(self, item):
|
def quote(self, item):
|
||||||
|
|
||||||
if item is None:
|
if item is None:
|
||||||
return "null"
|
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')
|
item = item.replace("\\","\\\\").replace("'","\\'").replace(chr(0),'0')
|
||||||
return "'%s'" % item
|
return "'%s'" % item
|
||||||
else:
|
|
||||||
return "%s" % item
|
return "%s" % item
|
||||||
|
|
||||||
def emit_record(self, record):
|
def emit_record(self, record):
|
||||||
quoted_data = [self.quote(record[field]) for field in self._fieldnames]
|
quoted_data = [self.quote(record[field]) for field in self._fieldnames]
|
||||||
self._outfile.write(self._insert_str % ','.join(quoted_data))
|
self._outfile.write(self._insert_str % ','.join(quoted_data))
|
||||||
|
|
||||||
def done(self):
|
|
||||||
self._outfile.close()
|
|
||||||
|
|
||||||
|
|
||||||
class DjangoModelEmitter(Emitter):
|
class DjangoModelEmitter(Emitter):
|
||||||
""" Emitter that populates a table corresponding to a django model.
|
""" Emitter that populates a table corresponding to a django model.
|
||||||
|
@ -4,7 +4,8 @@ from io import StringIO
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from saucebrush.emitters import DebugEmitter, CSVEmitter, CountEmitter, SqliteEmitter
|
from saucebrush.emitters import (
|
||||||
|
DebugEmitter, CSVEmitter, CountEmitter, SqliteEmitter, SqlDumpEmitter)
|
||||||
|
|
||||||
class EmitterTestCase(unittest.TestCase):
|
class EmitterTestCase(unittest.TestCase):
|
||||||
|
|
||||||
@ -70,5 +71,16 @@ class EmitterTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(results, [('1', '2', '3')])
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user