2012-03-12 06:14:39 +00:00
|
|
|
from contextlib import closing
|
2012-03-12 06:17:02 +00:00
|
|
|
from io import StringIO
|
2012-03-12 07:03:20 +00:00
|
|
|
import os
|
2010-02-21 19:20:48 +00:00
|
|
|
import unittest
|
2012-03-12 06:14:39 +00:00
|
|
|
|
2012-03-13 00:39:26 +00:00
|
|
|
from saucebrush.emitters import (
|
2022-11-11 03:26:09 +00:00
|
|
|
DebugEmitter,
|
|
|
|
CSVEmitter,
|
|
|
|
CountEmitter,
|
|
|
|
SqliteEmitter,
|
|
|
|
SqlDumpEmitter,
|
|
|
|
)
|
2010-02-21 19:20:48 +00:00
|
|
|
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
class EmitterTestCase(unittest.TestCase):
|
2010-02-21 19:20:48 +00:00
|
|
|
def test_debug_emitter(self):
|
2012-03-12 06:14:39 +00:00
|
|
|
with closing(StringIO()) as output:
|
|
|
|
de = DebugEmitter(output)
|
2022-11-11 03:26:09 +00:00
|
|
|
list(de.attach([1, 2, 3]))
|
|
|
|
self.assertEqual(output.getvalue(), "1\n2\n3\n")
|
2010-02-21 19:20:48 +00:00
|
|
|
|
2010-07-14 18:54:26 +00:00
|
|
|
def test_count_emitter(self):
|
2012-03-12 06:14:39 +00:00
|
|
|
|
2011-09-14 17:49:23 +00:00
|
|
|
# values for test
|
2022-11-11 03:26:09 +00:00
|
|
|
values = [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
12,
|
|
|
|
13,
|
|
|
|
14,
|
|
|
|
15,
|
|
|
|
16,
|
|
|
|
17,
|
|
|
|
18,
|
|
|
|
19,
|
|
|
|
20,
|
|
|
|
21,
|
|
|
|
22,
|
|
|
|
]
|
2012-03-12 06:14:39 +00:00
|
|
|
|
|
|
|
with closing(StringIO()) as output:
|
|
|
|
|
|
|
|
# test without of parameter
|
|
|
|
ce = CountEmitter(every=10, outfile=output, format="%(count)s records\n")
|
|
|
|
list(ce.attach(values))
|
2022-11-11 03:26:09 +00:00
|
|
|
self.assertEqual(output.getvalue(), "10 records\n20 records\n")
|
2012-03-12 06:14:39 +00:00
|
|
|
ce.done()
|
2022-11-11 03:26:09 +00:00
|
|
|
self.assertEqual(output.getvalue(), "10 records\n20 records\n22 records\n")
|
2012-03-12 06:14:39 +00:00
|
|
|
|
|
|
|
with closing(StringIO()) as output:
|
|
|
|
|
|
|
|
# test with of parameter
|
|
|
|
ce = CountEmitter(every=10, outfile=output, of=len(values))
|
|
|
|
list(ce.attach(values))
|
2022-11-11 03:26:09 +00:00
|
|
|
self.assertEqual(output.getvalue(), "10 of 22\n20 of 22\n")
|
2012-03-12 06:14:39 +00:00
|
|
|
ce.done()
|
2022-11-11 03:26:09 +00:00
|
|
|
self.assertEqual(output.getvalue(), "10 of 22\n20 of 22\n22 of 22\n")
|
2010-02-21 19:20:48 +00:00
|
|
|
|
2012-03-12 07:03:20 +00:00
|
|
|
def test_csv_emitter(self):
|
|
|
|
|
|
|
|
try:
|
2022-11-11 03:26:09 +00:00
|
|
|
import cStringIO # if Python 2.x then use old cStringIO
|
|
|
|
|
2012-03-12 07:03:20 +00:00
|
|
|
io = cStringIO.StringIO()
|
2022-11-11 03:31:18 +00:00
|
|
|
except Exception:
|
2022-11-11 03:26:09 +00:00
|
|
|
io = StringIO() # if Python 3.x then use StringIO
|
2012-03-12 07:03:20 +00:00
|
|
|
|
|
|
|
with closing(io) as output:
|
2022-11-11 03:26:09 +00:00
|
|
|
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")
|
2012-03-12 07:03:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_emitter(self):
|
|
|
|
|
2022-11-11 03:31:18 +00:00
|
|
|
import sqlite3
|
|
|
|
import tempfile
|
2012-03-12 07:03:20 +00:00
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
with closing(tempfile.NamedTemporaryFile(suffix=".db")) as f:
|
2012-03-12 07:03:20 +00:00
|
|
|
db_path = f.name
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
sle = SqliteEmitter(db_path, "testtable", fieldnames=("a", "b", "c"))
|
|
|
|
list(sle.attach([{"a": "1", "b": "2", "c": "3"}]))
|
2012-03-12 07:03:20 +00:00
|
|
|
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)
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
self.assertEqual(results, [("1", "2", "3")])
|
2012-03-12 07:03:20 +00:00
|
|
|
|
2012-03-13 00:39:26 +00:00
|
|
|
def test_sql_dump_emitter(self):
|
|
|
|
|
|
|
|
with closing(StringIO()) as bffr:
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
sde = SqlDumpEmitter(bffr, "testtable", ("a", "b"))
|
|
|
|
list(sde.attach([{"a": 1, "b": "2"}]))
|
2012-03-13 00:39:26 +00:00
|
|
|
sde.done()
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
self.assertEqual(
|
|
|
|
bffr.getvalue(), "INSERT INTO `testtable` (`a`,`b`) VALUES (1,'2');\n"
|
|
|
|
)
|
2012-03-13 00:39:26 +00:00
|
|
|
|
|
|
|
|
2022-11-11 03:26:09 +00:00
|
|
|
if __name__ == "__main__":
|
2010-02-21 19:20:48 +00:00
|
|
|
unittest.main()
|