|
| 1 | +from cStringIO import StringIO |
| 2 | +import unittest |
| 3 | +import sqlite3 |
| 4 | +import sys |
| 5 | + |
| 6 | +import pandas.io.sql as sql |
| 7 | +import pandas.util.testing as tm |
| 8 | + |
| 9 | +class TestSQLite(unittest.TestCase): |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + self.db = sqlite3.connect(':memory:') |
| 13 | + |
| 14 | + def test_basic(self): |
| 15 | + frame = tm.makeTimeDataFrame() |
| 16 | + self._check_roundtrip(frame) |
| 17 | + |
| 18 | + def test_write_row_by_row(self): |
| 19 | + frame = tm.makeTimeDataFrame() |
| 20 | + create_sql = sql.get_sqlite_schema(frame, 'test') |
| 21 | + self.db.execute(create_sql) |
| 22 | + |
| 23 | + cur = self.db.cursor() |
| 24 | + |
| 25 | + ins = "INSERT INTO test VALUES (%s, %s, %s, %s)" |
| 26 | + for idx, row in frame.iterrows(): |
| 27 | + fmt_sql = sql.format_query(ins, *row) |
| 28 | + sql.tquery(fmt_sql, cur=cur) |
| 29 | + |
| 30 | + self.db.commit() |
| 31 | + |
| 32 | + result = sql.read_frame("select * from test", con=self.db) |
| 33 | + result.index = frame.index |
| 34 | + tm.assert_frame_equal(result, frame) |
| 35 | + |
| 36 | + def test_execute(self): |
| 37 | + frame = tm.makeTimeDataFrame() |
| 38 | + create_sql = sql.get_sqlite_schema(frame, 'test') |
| 39 | + self.db.execute(create_sql) |
| 40 | + ins = "INSERT INTO test VALUES (?, ?, ?, ?)" |
| 41 | + |
| 42 | + row = frame.ix[0] |
| 43 | + sql.execute(ins, self.db, params=tuple(row)) |
| 44 | + self.db.commit() |
| 45 | + |
| 46 | + result = sql.read_frame("select * from test", self.db) |
| 47 | + result.index = frame.index[:1] |
| 48 | + tm.assert_frame_equal(result, frame[:1]) |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + def test_execute_fail(self): |
| 53 | + create_sql = """ |
| 54 | + CREATE TABLE test |
| 55 | + ( |
| 56 | + a TEXT, |
| 57 | + b TEXT, |
| 58 | + c REAL, |
| 59 | + PRIMARY KEY (a, b) |
| 60 | + ); |
| 61 | + """ |
| 62 | + self.db.execute(create_sql) |
| 63 | + |
| 64 | + sql.execute('INSERT INTO test VALUES("foo", "bar", 1.234)', self.db) |
| 65 | + sql.execute('INSERT INTO test VALUES("foo", "baz", 2.567)', self.db) |
| 66 | + |
| 67 | + try: |
| 68 | + sys.stdout = StringIO() |
| 69 | + self.assertRaises(Exception, sql.execute, |
| 70 | + 'INSERT INTO test VALUES("foo", "bar", 7)', |
| 71 | + self.db) |
| 72 | + finally: |
| 73 | + sys.stdout = sys.__stdout__ |
| 74 | + |
| 75 | + def test_execute_closed_connection(self): |
| 76 | + create_sql = """ |
| 77 | + CREATE TABLE test |
| 78 | + ( |
| 79 | + a TEXT, |
| 80 | + b TEXT, |
| 81 | + c REAL, |
| 82 | + PRIMARY KEY (a, b) |
| 83 | + ); |
| 84 | + """ |
| 85 | + self.db.execute(create_sql) |
| 86 | + |
| 87 | + sql.execute('INSERT INTO test VALUES("foo", "bar", 1.234)', self.db) |
| 88 | + self.db.close() |
| 89 | + try: |
| 90 | + sys.stdout = StringIO() |
| 91 | + self.assertRaises(Exception, sql.tquery, "select * from test", |
| 92 | + con=self.db) |
| 93 | + finally: |
| 94 | + sys.stdout = sys.__stdout__ |
| 95 | + |
| 96 | + def test_na_roundtrip(self): |
| 97 | + pass |
| 98 | + |
| 99 | + def _check_roundtrip(self, frame): |
| 100 | + sql.write_frame(frame, name='test_table', con=self.db) |
| 101 | + result = sql.read_frame("select * from test_table", self.db) |
| 102 | + |
| 103 | + # HACK! |
| 104 | + result.index = frame.index |
| 105 | + |
| 106 | + expected = frame |
| 107 | + tm.assert_frame_equal(result, expected) |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + # unittest.main() |
| 111 | + import nose |
| 112 | + # nose.runmodule(argv=[__file__,'-vvs','-x', '--pdb-failure'], |
| 113 | + # exit=False) |
| 114 | + nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'], |
| 115 | + exit=False) |
| 116 | + |
0 commit comments