Skip to content

Instantly share code, notes, and snippets.

@WindfallLabs
Created May 26, 2016 21:34
Show Gist options
  • Save WindfallLabs/97b2fc24a8240344236d904f1a90b723 to your computer and use it in GitHub Desktop.
Save WindfallLabs/97b2fc24a8240344236d904f1a90b723 to your computer and use it in GitHub Desktop.
Test apsw shell.py for `.dump > file.sql` functionality
# -*- coding: utf-8 -*-
import os
import codecs
from subprocess import Popen, PIPE
import apsw
db = "test.sqlite"
sqlite = "sqlite3_test.sql"
shell = "apsw_shell_test.sql"
err_msg = "'COMMIT' command not found at end of file."
for sql_file in [db, sqlite, shell]:
if os.path.exists(sql_file):
os.remove(sql_file)
def make_db():
conn = apsw.Connection(db)
cur = conn.cursor()
cur.execute("CREATE TABLE test (id, name)")
print("Database made")
cur.execute(u"INSERT INTO test VALUES (1, 'Gröditzberg')")
print("Data inserted")
def test_select():
conn = apsw.Connection(db)
cur = conn.cursor()
cur.execute("SELECT * FROM test")
result = cur.fetchone()
conn.close()
assert result == (1, u'Gröditzberg'), "Selection not equal to input"
print("Data exists")
def test_dump(exe, out_file):
cmd = exe.split()
cmd.extend(["test.sqlite", ".dump"])
p = Popen(cmd, stdout=PIPE).communicate()
uni = unicode(p[0].decode("utf-8"))
with codecs.open(out_file, "wb", "utf-8") as f:
f.write(uni)
return True
if __name__ == "__main__":
make_db()
test_select()
test_dump("sqlite3.exe", sqlite)
test_dump("python shell.py", shell)
with open(sqlite, "rb") as lite:
assert "commit" in lite.readlines()[-1].lower(), err_msg
with open(shell, "rb") as sh:
assert "commit" in sh.readlines()[-1].lower(), err_msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment