Skip to content

Commit

Permalink
Created sanctuary test db (#109)
Browse files Browse the repository at this point in the history
Problem
Sanctuary unit tests require their own test db

Solution
- updated init-db.sh script to create medical and sanctuary test db
- updated database.py to check if we're running unit tests
- removed setting POSTGRES_DB=example_test as it's no longer needed

Ticket URL
NA
Documentation
NA
Tests Run
NA
  • Loading branch information
MadelaineJ authored Feb 25, 2024
1 parent c3d8402 commit 80463cc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ To run API tests locally, run the following commands:

```bash
docker-compose up --detach db
export POSTGRES_DB=example_test
export TEST=TEST
pytest
```

Expand Down
33 changes: 23 additions & 10 deletions app/api/main/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,33 @@


load_env()
test = os.environ.get("IS_TEST")
user = os.environ.get("POSTGRES_USER")
password = os.environ.get("POSTGRES_PASSWORD")
hostname = os.environ.get("POSTGRES_HOST")
port = str(os.environ.get("POSTGRES_PORT"))
db = os.environ.get("POSTGRES_DB")
db_medical = os.environ.get("POSTGRES_DB")
db_sanctuary = os.environ.get("POSTGRES_SANCTUARY_DB")


if test:
SQLALCHEMY_DATABASE_URL = (
f"postgresql+psycopg2://{user}:{password}@{hostname}:{port}/{db_medical}_test"
)

SQLALCHEMY_DATABASE_URL_SANCTUARY = (
f"postgresql+psycopg2://{user}:{password}@{hostname}:{port}/{db_sanctuary}_test"
)
else:
SQLALCHEMY_DATABASE_URL = (
f"postgresql+psycopg2://{user}:{password}@{hostname}:{port}/{db_medical}"
)
# Define engine for the 'sanctuary' database
SQLALCHEMY_DATABASE_URL_SANCTUARY = (
f"postgresql+psycopg2://{user}:{password}@{hostname}:{port}/{db_sanctuary}"
)

SQLALCHEMY_DATABASE_URL = (
f"postgresql+psycopg2://{user}:{password}@{hostname}:{port}/{db}"
)

# Define engine for the 'sanctuary' database
SQLALCHEMY_DATABASE_URL_SANCTUARY = (
f"postgresql+psycopg2://{user}:{password}@{hostname}:{port}/sanctuary"
)
engine_sanctuary = create_engine(SQLALCHEMY_DATABASE_URL_SANCTUARY)
SessionLocalSanctuary = sessionmaker(
autocommit=False, autoflush=False, bind=engine_sanctuary
Expand Down Expand Up @@ -75,9 +88,9 @@ def get_db() -> Generator:

def create_all_tables() -> None:
Base.metadata.create_all(engine_example, checkfirst=True)
# BaseSanctuary.metadata.create_all(engine_example, checkfirst=True)
BaseSanctuary.metadata.create_all(engine_sanctuary, checkfirst=True)


def drop_all_tables(check_first: bool = False) -> None:
Base.metadata.drop_all(engine_example, checkfirst=check_first)
# BaseSanctuary.metadata.drop_all(engine_example, checkfirst=check_first)
BaseSanctuary.metadata.drop_all(engine_sanctuary, checkfirst=check_first)
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ services:
env_file: app/api/.env
environment:
- POSTGRES_HOST=db
- POSTGRES_DB=example_test
- IS_TEST=test
# run as module so basedir (root) is added to python path
command: python -m pytest api/tests/
volumes:
Expand Down
10 changes: 9 additions & 1 deletion scripts/init-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE sanctuary;
CREATE DATABASE "sanctuary-example";
EOSQL

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE "sanctuary-example_test";
EOSQL

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE example_test;
EOSQL

0 comments on commit 80463cc

Please sign in to comment.