def conn(): """Fixture to create db connection for tests, then close the connection and delete the db in teardown. """ conn = model.LEIE("test", connect=False) if os.path.exists(conn.db_conf['open']): os.unlink(conn.db_conf['open']) conn = model.LEIE("test") conn.migrate() yield conn conn.close() os.unlink(conn.db_conf['open'])
def test_sql(): """Make sure sql function returns something. We don't check that the migrations are valid sql. Maybe we should. """ conn = model.LEIE(connect=False, db_conf_file="db/dbconf.yml") # At a minimum, there is one migration and it is a string assert type(conn.up(0)) == type("") assert type(conn.down(0)) == type("") # Make sure we get back a string and then None above a certain point max_reached = False for x in range(100): sql = conn.up(x) if not max_reached: if sql: assert type(sql) == type("") else: assert sql == None max_reached = True else: assert sql == None conn.db_conf['driver'] = "LibraryDB" with pytest.raises(model.UnsupportedDBType) as exc_info: conn.up(x) exception = exc_info.value assert str(exception) == "We don't have migrations for LibraryDB"
def main(): os.chdir(os.path.dirname(__file__)) logger = log.logger() info('Starting ETL of LEIE data.') # Figure out where we put data datadir = get_datadir() dbdir = get_dbdir() # Get a database connection, create db if needed conn = model.LEIE("development", db_conf_file=os.path.join(dbdir, "dbconf.yml")) # Make sure the db schema is up to date, create tables, etc. conn.migrate() assert os.path.exists(datadir) # Do our ETL download(datadir, conn) excl = Exclusions(conn) excl.etl_from_dir(datadir) rein = Reinstatements(conn) rein.etl_from_dir(datadir) # Close the db connection conn.close() info('Finished ETL of LEIE data.')
def test_migrate(): """Make sure goose migrations runs against the correct db and puts some sql there.""" conn = model.LEIE("test", connect=False, db_conf_file="db/dbconf.yml") if os.path.exists(conn.db_conf['open']): os.unlink(conn.db_conf['open']) conn = model.LEIE("test", db_conf_file="db/dbconf.yml") assert subprocess.check_output("echo .schema | sqlite3 %s" % conn.db_conf['open'], shell=True).decode("utf-8") == "" conn.migrate() assert subprocess.check_output( "echo .schema | sqlite3 %s" % conn.db_conf['open'], shell=True).decode("utf-8") != "" assert conn.get_header("exclusion")[0] == "lastname" conn.close() # Check that migrate complains about non-existent directory tmp = conn.db_conf['open'] conn.db_conf['open'] = "/nodir/does_not_exist" with pytest.raises(FileNotFoundError) as exc_info: conn.migrate() exception = exc_info.value assert str(exception) == "[Errno 2] No such file or directory: '/nodir'" # Check that migrate complains about non-existent sqlite3 db conn.db_conf['open'] = "does_not_exist" with pytest.raises(model.DBNotFound) as exc_info: conn.migrate() exception = exc_info.value assert str(exception ) == "DB does_not_exist doesn't exist, so we can't migrate it." conn.db_conf['open'] = tmp # Check that migrate complains if Goose pukes tmp = conn.db_conf['driver'] conn.db_conf['driver'] = "does_not_exist" with pytest.raises(subprocess.CalledProcessError) as exc_info: conn.migrate() exception = exc_info.value assert str( exception ) == "Command 'goose -dir db/does_not_exist does_not_exist test.sqlite3 up' returned non-zero exit status 0" conn.db_conf['driver'] = tmp
def test_goose_write(): """Exercise the goose_writes part of the model""" conn = model.LEIE(connect=False) subprocess.run("rm -rf tests/db", shell=True) fnames = conn.goose_write("tests/db") assert os.path.exists("tests/db/sqlite3") assert len(fnames) >= 1 for fname in fnames: assert os.path.exists(fname) # Again, to we make sure we bail if sql files already exist assert len(conn.goose_write("tests/db")) == 0
def test_goose_write(): """Exercise the goose_writes part of the model""" conn = model.LEIE(connect=False) subprocess.run("rm -rf tests/db", shell=True) fnames = conn.goose_write("tests/db") assert os.path.exists("tests/db/sqlite3") assert len(fnames) >= 1 for fname in fnames: assert os.path.exists(fname) # Can we run it again without problems? fnames = conn.goose_write("tests/db") assert os.path.exists("tests/db/sqlite3") assert len(fnames) >= 1 for fname in fnames: assert os.path.exists(fname)
def main(): # Find data dir datadir = get_existing_file([ "/var/etl/leie/data", "data", "../data", os.path.join(os.path.dirname(__file__), "data"), os.path.join(os.path.dirname(__file__), "..", "data") ], default="data", create=True) info("Using '%s' as data directory" % datadir) # Figure out where our db is dbdir = get_existing_file([ "/var/etl/leie/db", "db", "../db", os.path.join(os.path.dirname(__file__), "db"), os.path.join(os.path.dirname(__file__), "..", "db") ], "db") info("Using '%s' as db directory" % dbdir) # Get a database connection, create db if needed conn = model.LEIE("development", db_conf_file=os.path.join(dbdir, "dbconf.yml")) # Make sure the db schema is up to date, create tables, etc. conn.migrate() assert os.path.exists(datadir) # Do our ETL extract(datadir) excl = Exclusions(conn) excl.etl_from_dir(datadir) rein = Reinstatements(conn) rein.etl_from_dir(datadir) # Close the db connection conn.close()
def test_unsupported_db_init(): """Make sure we throw an error if the user names a weird db type""" with pytest.raises(model.UnsupportedDBType) as exc_info: conn = model.LEIE("test2", db_conf_file="db/dbconf.yml") exception = exc_info.value assert str(exception) == "We don't support databases of type LibraryDB"
import simplejson as json import sys # Make sure we can load modules from this directory sys.path.insert(0, os.path.dirname(__file__)) # Load my modules import etl import model app = Flask("leie") baseurl = "" # Get a database connection, create db if needed conn = model.LEIE("development", db_conf_file=os.path.join(etl.get_dbdir(), "dbconf.yml")) def slurp(fname): """Read file named FNAME and return contents.""" with open(fname) as fh: return fh.read() @app.errorhandler(404) def not_found(error): return make_response(json.dumps({'error': 'Not found'}), 404) @app.route("/") def home():