Skip to content

Commit

Permalink
MNT Automatically create database for tests through pytest (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
maikia authored and glemaitre committed Nov 18, 2019
1 parent 2879a56 commit 1ba84e2
Show file tree
Hide file tree
Showing 32 changed files with 86 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ problems/*/old_submissions
test_submissions/submission*
.pytest_cache
*.egg-info
databoard.db
databoard.db
db_engine.yml
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ install:
- source activate testenv
- make inplace
script:
- psql -U postgres -c "CREATE USER mrramp WITH PASSWORD 'mrramp';ALTER USER mrramp WITH SUPERUSER;"
- createdb --owner=mrramp databoard_test
- python -m smtpd -n -c DebuggingServer localhost:8025 &
- bash ci_tools/travis/test_ramp_frontend.sh
- bash ci_tools/travis/test_ramp_engine.sh
Expand Down
3 changes: 3 additions & 0 deletions ci_tools/travis/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ set -e
cd $HOME
mkdir ramp_deployment
cd ramp_deployment
psql -U postgres -c "CREATE USER mrramp WITH PASSWORD 'mrramp';ALTER USER mrramp WITH SUPERUSER;"
createdb --owner=mrramp databoard_test
ramp setup init

echo "flask:
secret_key: abcdefghijkl
mail_server: smtp.gmail.com
Expand Down
51 changes: 51 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import contextlib
import os
import pytest
import smtpd
from sqlalchemy import create_engine, exc
from threading import Thread
from ramp_utils.testing import database_config_template
from yaml import safe_load

from ramp_utils import read_config


@pytest.fixture(scope='session')
def database_connection():
"""
Create a Postgres database for the tests,
and drop it when the tests are done.
"""
config = safe_load(open("db_engine.yml"))
dbowner = config.get('db_owner')
dbcluster = config.get('db_cluster_name')
engine = create_engine(f'postgresql://{dbowner}:@localhost/{dbcluster}',
isolation_level='AUTOCOMMIT')

connection = engine.connect()

database_config = read_config(database_config_template())
username = database_config['sqlalchemy']['username']
database_name = database_config['sqlalchemy']['database']
try:
connection.execute(f"""CREATE USER {username}
WITH PASSWORD '{username}';
ALTER USER {username} WITH SUPERUSER""")
except exc.ProgrammingError:
print(f'user {username} already exists')
raise

try:
connection.execute(f'CREATE DATABASE {database_name} OWNER {username}')
except exc.ProgrammingError:
print(f'{database_name} database used for testing already exists')
raise

# close the connection and remove the database in the end
yield
connection.execute("""SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'databoard_test';""")
connection.execute(f'DROP DATABASE {database_name}')
connection.execute(f'DROP USER {username}')
print(f"deleted database 'databoard_test' and removed user '{username}'")
2 changes: 2 additions & 0 deletions db_engine.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
db_owner: postgres # to find out your db_owner type psql -l in the terminal
db_cluster_name: postgres # name of the engine database you have created
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/model/tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def ramp_config():


@pytest.fixture
def session_scope_function(database_config, ramp_config):
def session_scope_function(database_config, ramp_config, database_connection):
try:
deployment_dir = create_test_db(database_config, ramp_config)
with session_scope(database_config['sqlalchemy']) as session:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


@pytest.fixture
def database():
def database(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tools/tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


@pytest.fixture
def session_scope_function():
def session_scope_function(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tools/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


@pytest.fixture
def session_scope_function():
def session_scope_function(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tools/tests/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


@pytest.fixture(scope='module')
def session_toy_db():
def session_toy_db(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@pytest.fixture(scope='module')
def session_toy_db():
def session_toy_db(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
4 changes: 2 additions & 2 deletions ramp-database/ramp_database/tools/tests/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@


@pytest.fixture
def base_db():
def base_db(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand All @@ -92,7 +92,7 @@ def _change_state_db(session):


@pytest.fixture(scope='module')
def session_scope_module():
def session_scope_module(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tools/tests/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@pytest.fixture
def session_scope_function():
def session_scope_function(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-database/ramp_database/tools/tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@pytest.fixture
def session_scope_function():
def session_scope_function(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-engine/ramp_engine/tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


@pytest.fixture
def session_toy():
def session_toy(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-frontend/ramp_frontend/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


@pytest.fixture(scope='module')
def client_session():
def client_session(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-frontend/ramp_frontend/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


@pytest.fixture(scope='module')
def client_session():
def client_session(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-frontend/ramp_frontend/tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@pytest.fixture(scope='module')
def client_session():
def client_session(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-frontend/ramp_frontend/tests/test_ramp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


@pytest.fixture(scope='module')
def client_session():
def client_session(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-frontend/ramp_frontend/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


@pytest.fixture
def client():
def client(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-frontend/ramp_frontend/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


@pytest.fixture(scope='module')
def client_session():
def client_session(database_connection):
database_config = read_config(database_config_template())
ramp_config = ramp_config_template()
try:
Expand Down
2 changes: 1 addition & 1 deletion ramp-utils/ramp_utils/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


@pytest.fixture
def deployment_dir():
def deployment_dir(database_connection):
ramp_config = read_config(ramp_config_template())
return os.path.commonpath(
[ramp_config['ramp']['kit_dir'], ramp_config['ramp']['data_dir']]
Expand Down
2 changes: 1 addition & 1 deletion ramp-utils/ramp_utils/tests/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@pytest.fixture
def simple_config():
def simple_config(database_connection):
data = {'sqlalchemy': {'username': 'mrramp', 'password': 'mrramp'},
'ramp': {'event_name': 'iris_test'}}
with tempfile.NamedTemporaryFile(mode='w', suffix='.yml') as config_file:
Expand Down
2 changes: 1 addition & 1 deletion ramp-utils/ramp_utils/tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


@pytest.fixture
def session_scope_function():
def session_scope_function(database_connection):
database_config = read_config(database_config_template())
ramp_config = read_config(ramp_config_template())
try:
Expand Down

0 comments on commit 1ba84e2

Please sign in to comment.