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_fromdb_withargs(): # initial data data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(':memory:') c = connection.cursor() c.execute('create table foobar (foo, bar)') for row in data: c.execute('insert into foobar values (?, ?)', row) connection.commit() c.close() # test the function actual = fromdb( connection, 'select * from foobar where bar > ? and bar < ?', (1, 3) ) expect = (('foo', 'bar'), ('b', 2), ('c', 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_fromdb_mkcursor(): # initial data data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(':memory:') c = connection.cursor() c.execute('create table foobar (foo, bar)') for row in data: c.execute('insert into foobar values (?, ?)', row) connection.commit() c.close() # test the function mkcursor = lambda: connection.cursor() actual = fromdb(mkcursor, 'select * from foobar') expect = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice # test iterators are isolated i1 = iter(actual) i2 = iter(actual) eq_(('foo', 'bar'), i1.next()) eq_(('a', 1), i1.next()) eq_(('foo', 'bar'), i2.next()) eq_(('b', 2), i1.next())
def test_fromdb_mkcursor(): # initial data data = (("a", 1), ("b", 2), ("c", 2.0)) connection = sqlite3.connect(":memory:") c = connection.cursor() c.execute("create table foobar (foo, bar)") for row in data: c.execute("insert into foobar values (?, ?)", row) connection.commit() c.close() # test the function mkcursor = lambda: connection.cursor() actual = fromdb(mkcursor, "select * from foobar") expect = (("foo", "bar"), ("a", 1), ("b", 2), ("c", 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice # test iterators are isolated i1 = iter(actual) i2 = iter(actual) eq_(("foo", "bar"), next(i1)) eq_(("a", 1), next(i1)) eq_(("foo", "bar"), next(i2)) eq_(("b", 2), next(i1))
def test_fromsqlite3_withargs(): # initial data data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(':memory:') c = connection.cursor() c.execute('CREATE TABLE foobar (foo, bar)') for row in data: c.execute('INSERT INTO foobar VALUES (?, ?)', row) connection.commit() c.close() # test the function actual = fromdb( connection, 'SELECT * FROM foobar WHERE bar > ? AND bar < ?', (1, 3) ) expect = (('foo', 'bar'), ('b', 2), ('c', 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_fromsqlite3(): # initial data f = NamedTemporaryFile(delete=False) f.close() data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(f.name) c = connection.cursor() c.execute('CREATE TABLE foobar (foo, bar)') for row in data: c.execute('INSERT INTO foobar VALUES (?, ?)', row) connection.commit() c.close() connection.close() # test the function actual = fromdb(f.name, 'SELECT * FROM foobar') expect = (('foo', 'bar'), ('a', 1), ('b', 2), ('c', 2.0)) ieq(expect, actual, cast=tuple) ieq(expect, actual, cast=tuple) # verify can iterate twice
def test_fromdb_withargs(): # initial data data = (("a", 1), ("b", 2), ("c", 2.0)) connection = sqlite3.connect(":memory:") c = connection.cursor() c.execute("create table foobar (foo, bar)") for row in data: c.execute("insert into foobar values (?, ?)", row) connection.commit() c.close() # test the function actual = fromdb(connection, "select * from foobar where bar > ? and bar < ?", (1, 3)) expect = (("foo", "bar"), ("b", 2), ("c", 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice
def test_fromsqlite3_withargs(): # initial data data = (('a', 1), ('b', 2), ('c', 2.0)) connection = sqlite3.connect(':memory:') c = connection.cursor() c.execute('CREATE TABLE foobar (foo, bar)') for row in data: c.execute('INSERT INTO foobar VALUES (?, ?)', row) connection.commit() c.close() # test the function actual = fromdb(connection, 'SELECT * FROM foobar WHERE bar > ? AND bar < ?', (1, 3)) expect = (('foo', 'bar'), ('b', 2), ('c', 2.0)) ieq(expect, actual) ieq(expect, actual) # verify can iterate twice