Skip to content

Commit

Permalink
add option for in memory sqlite db (for tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
TorecLuik committed Aug 7, 2024
1 parent 389bafc commit 0639508
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
27 changes: 19 additions & 8 deletions biomero/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Read database configuration from environment variables
database_url = URL.create(
drivername="postgresql+psycopg2",
username=os.getenv('POSTGRES_USER'),
password=os.getenv('POSTGRES_PASSWORD'),
host=os.getenv('POSTGRES_HOST', 'localhost'),
port=os.getenv('POSTGRES_PORT', 5432),
database=os.getenv('POSTGRES_DBNAME')
)
persistence_mod = os.getenv('PERSISTENCE_MODULE')
if 'postgres' in persistence_mod:
logger.info("Using postgres database")
database_url = URL.create(
drivername="postgresql+psycopg2",
username=os.getenv('POSTGRES_USER'),
password=os.getenv('POSTGRES_PASSWORD'),
host=os.getenv('POSTGRES_HOST', 'localhost'),
port=os.getenv('POSTGRES_PORT', 5432),
database=os.getenv('POSTGRES_DBNAME')
)
elif 'sqlite' in persistence_mod:
logger.info("Using sqlite in-mem database")
database_url = URL.create(
drivername="sqlite",
database=os.getenv('SQLITE_DBNAME')
)
else:
raise NotImplementedError(f"Can't handle {persistence_mod}")

# Set up SQLAlchemy engine and session
self.engine = create_engine(database_url)
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_slurm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@
import mock
from mock import patch, MagicMock
from paramiko import SSHException
import os

# using actual env vars
# @pytest.fixture(scope='session', autouse=True)
# def set_env_vars():
# # Set environment variables
# os.environ["PERSISTENCE_MODULE"] = "eventsourcing.sqlite"
# os.environ["SQLITE_DBNAME"] = ":memory:"

# # Optional: Return a dictionary of the set variables if needed elsewhere
# yield {
# "PERSISTENCE_MODULE": "eventsourcing.sqlite",
# "SQLITE_DBNAME": ":memory:",
# }

# # Optionally, clean up the environment variables after tests are done
# del os.environ["PERSISTENCE_MODULE"]
# del os.environ["SQLITE_DBNAME"]


@pytest.fixture(autouse=True)
def mock_env_vars():
# Define mock environment variables
mock_env = {
"PERSISTENCE_MODULE": "eventsourcing.sqlite",
"SQLITE_DBNAME": ":memory:",
}

# Patch os.getenv to return values from the mock environment
with patch('os.getenv', lambda key, default=None: mock_env.get(key, default)):
yield


@pytest.fixture
Expand Down

0 comments on commit 0639508

Please sign in to comment.