def test_tosqlite3_appendsqlite3(): # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) f = NamedTemporaryFile(delete=False) f.close() conn = sqlite3.connect(f.name) conn.execute('CREATE TABLE foobar (foo TEXT, bar INT)') conn.close() todb(table, f.name, 'foobar') # check what it did conn = sqlite3.connect(f.name) actual = conn.execute('SELECT * FROM foobar') expect = (('a', 1), ('b', 2), ('c', 2)) ieq(expect, actual) # check appending table2 = (('foo', 'bar'), ('d', 7), ('e', 9), ('f', 1)) appenddb(table2, f.name, 'foobar') # check what it did conn = sqlite3.connect(f.name) actual = conn.execute('SELECT * FROM foobar') expect = (('a', 1), ('b', 2), ('c', 2), ('d', 7), ('e', 9), ('f', 1)) ieq(expect, actual)
def test_todb_appenddb_cursor(): f = NamedTemporaryFile(delete=False) conn = sqlite3.connect(f.name) conn.execute('create table foobar (foo, bar)') conn.commit() # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) cursor = conn.cursor() todb(table, cursor, 'foobar') # check what it did actual = conn.execute('select * from foobar') expect = (('a', 1), ('b', 2), ('c', 2)) ieq(expect, actual) # try appending table2 = (('foo', 'bar'), ('d', 7), ('e', 9), ('f', 1)) appenddb(table2, cursor, 'foobar') # check what it did actual = conn.execute('select * from foobar') expect = (('a', 1), ('b', 2), ('c', 2), ('d', 7), ('e', 9), ('f', 1)) ieq(expect, actual)
def test_todb_appenddb_cursor(): f = NamedTemporaryFile(delete=False) conn = sqlite3.connect(f.name) conn.execute("create table foobar (foo, bar)") conn.commit() # exercise function table = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2)) cursor = conn.cursor() todb(table, cursor, "foobar") # check what it did actual = conn.execute("select * from foobar") expect = (("a", 1), ("b", 2), ("c", 2)) ieq(expect, actual) # try appending table2 = (("foo", "bar"), ("d", 7), ("e", 9), ("f", 1)) appenddb(table2, cursor, "foobar") # check what it did actual = conn.execute("select * from foobar") expect = (("a", 1), ("b", 2), ("c", 2), ("d", 7), ("e", 9), ("f", 1)) ieq(expect, actual)
def test_tosqlite3_appendsqlite3_connection(): conn = sqlite3.connect(':memory:') conn.execute('CREATE TABLE foobar (foo TEXT, bar INT)') # exercise function table = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2)) todb(table, conn, 'foobar') # check what it did actual = conn.execute('SELECT * FROM foobar') expect = (('a', 1), ('b', 2), ('c', 2)) ieq(expect, actual) # check appending table2 = (('foo', 'bar'), ('d', 7), ('e', 9), ('f', 1)) appenddb(table2, conn, 'foobar') # check what it did actual = conn.execute('SELECT * FROM foobar') expect = (('a', 1), ('b', 2), ('c', 2), ('d', 7), ('e', 9), ('f', 1)) ieq(expect, actual)
def _test_create(dbo): expect = (('foo', 'bar'), ('a', 1), ('b', 2)) expect_extended = (('foo', 'bar', 'baz'), ('a', 1, 2.3), ('b', 2, 4.1)) actual = fromdb(dbo, 'SELECT * FROM test_create') debug('verify table does not exist to start with') try: debug(look(actual)) except Exception as e: debug('expected exception: ' + str(e)) else: raise Exception('expected exception not raised') debug('verify cannot write without create') try: todb(expect, dbo, 'test_create') except Exception as e: debug('expected exception: ' + str(e)) else: raise Exception('expected exception not raised') debug('create table and verify') todb(expect, dbo, 'test_create', create=True) ieq(expect, actual) debug(look(actual)) debug('verify cannot overwrite with new cols without recreate') try: todb(expect_extended, dbo, 'test_create') except Exception as e: debug('expected exception: ' + str(e)) else: raise Exception('expected exception not raised') debug('verify recreate') todb(expect_extended, dbo, 'test_create', create=True, drop=True) ieq(expect_extended, actual) debug(look(actual)) debug('horrendous identifiers') table = (('foo foo', 'bar.baz."spong`'), ('a', 1), ('b', 2), ('c', 2)) todb(table, dbo, 'foo " bar`', create=True) actual = fromdb(dbo, 'SELECT * FROM "foo "" bar`"') ieq(table, actual)
def test_tosqlite3_identifiers(): # exercise function table = (('foo foo', 'bar.baz.spong`'), ('a', 1), ('b', 2), ('c', 2)) f = NamedTemporaryFile(delete=False) f.close() conn = sqlite3.connect(f.name) conn.execute('CREATE TABLE "foo "" bar`" ' '("foo foo" TEXT, "bar.baz.spong`" INT)') conn.close() todb(table, f.name, 'foo " bar`') # check what it did conn = sqlite3.connect(f.name) actual = conn.execute('SELECT * FROM `foo " bar```') expect = (('a', 1), ('b', 2), ('c', 2)) ieq(expect, actual)