Skip to content
Snippets Groups Projects
Commit 36626215 authored by Vermaat's avatar Vermaat
Browse files

Customizable database connection uri for unit tests

parent a30a0d2f
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,22 @@ the Mutalyzer source directory. ...@@ -15,6 +15,22 @@ the Mutalyzer source directory.
.. note:: The Mutalyzer package must be installed before running the unit .. note:: The Mutalyzer package must be installed before running the unit
tests. tests.
By default, the tests use an in-memory SQLite database. This can be customized
by setting the `MUTALYZER_TEST_DATABASE_URI` to a valid `SQLAlchemy connection
URI
<http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#database-urls>`_
(obviously, the contents of this database will be lost). For example, to use
an SQLite database on the filesystem::
$ MUTALYZER_TEST_DATABASE_URI=sqlite:////tmp/mutalyzer.sql py.test
Or, using `pg_virtualenv
<https://alioth.debian.org/scm/loggerhead/pkg-postgresql/postgresql-common/trunk/view/head:/pg_virtualenv>`_
(included with the Debian PostgreSQL packages), to run the tests with
PostgreSQL::
$ pg_virtualenv bash -c 'MUTALYZER_TEST_DATABASE_URI=postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}:${PGPORT}/${PGDATABASE} py.test'
Tests are `run automatically on Travis CI Tests are `run automatically on Travis CI
<https://travis-ci.org/LUMC/mutalyzer>`_ for each pull request and push on <https://travis-ci.org/LUMC/mutalyzer>`_ for each pull request and push on
GitHub. GitHub.
......
...@@ -47,7 +47,7 @@ def create_engine(): ...@@ -47,7 +47,7 @@ def create_engine():
engine = sqlalchemy.create_engine(url, **options) engine = sqlalchemy.create_engine(url, **options)
# For convenience, we also create tables if we're using an SQLite # For convenience, we also create tables if we're using an SQLite
# in-memory database. By definition they won't yet exist # in-memory database. By definition they won't yet exist.
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
return engine return engine
......
...@@ -11,6 +11,7 @@ import shutil ...@@ -11,6 +11,7 @@ import shutil
import tempfile import tempfile
from mutalyzer.config import settings from mutalyzer.config import settings
from mutalyzer import db
class TestEnvironment(object): class TestEnvironment(object):
...@@ -25,13 +26,21 @@ class TestEnvironment(object): ...@@ -25,13 +26,21 @@ class TestEnvironment(object):
log_handle, self.log_file = tempfile.mkstemp() log_handle, self.log_file = tempfile.mkstemp()
os.close(log_handle) os.close(log_handle)
database_uri = os.getenv('MUTALYZER_TEST_DATABASE_URI', 'sqlite://')
settings.configure({'DEBUG': False, settings.configure({'DEBUG': False,
'TESTING': True, 'TESTING': True,
'CACHE_DIR': self.cache_dir, 'CACHE_DIR': self.cache_dir,
'REDIS_URI': None, 'REDIS_URI': None,
'DATABASE_URI': 'sqlite://', 'DATABASE_URI': database_uri,
'LOG_FILE': self.log_file}) 'LOG_FILE': self.log_file})
# Mutalyzer create tables automatically if we're using an SQLite
# in-memory database.
if database_uri != 'sqlite://':
db.Base.metadata.drop_all(db.session.get_bind())
db.Base.metadata.create_all(db.session.get_bind())
for fixture in fixtures: for fixture in fixtures:
fixture() fixture()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment