Skip to content

Commit

Permalink
adding new test db for unit tests (#63)
Browse files Browse the repository at this point in the history
Problem
Unit tests currently use the development database

Solution
- updated database.py to create new database
- updated dockerfile to pass new db name to unit Tests
- updated README for running tests locally
- also updated seeder reqs to work on macos

Ticket URL
https://mediform.atlassian.net/browse/MEDI-16

Documentation
NA

Tests Run
Tested that unit tests now use the test db
  • Loading branch information
MadelaineJ authored Jan 14, 2024
1 parent 5ab1756 commit edf12ee
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ This should be done inside your dev container
To run API tests locally, run the following commands:

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

Expand Down
6 changes: 4 additions & 2 deletions app/api/main/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import database_exists, create_database

from api.config import load_env

Expand All @@ -20,19 +21,20 @@
)

engine = create_engine(SQLALCHEMY_DATABASE_URL, pool_pre_ping=True)
if not database_exists(engine.url): # Check if the db exists
create_database(engine.url) # Create new DB

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()


def get_db() -> Generator:
db = SessionLocal()
try:
yield db
finally:
db.close()


def create_all_tables() -> None:
Base.metadata.create_all(engine, checkfirst=True)

Expand Down
3 changes: 2 additions & 1 deletion app/api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ toml==0.10.2
typing_extensions==4.2.0
urllib3==1.26.9
uvicorn==0.13.4
gunicorn==20.1.0
gunicorn==20.1.0
SQLAlchemy-Utils==0.41.1
2 changes: 1 addition & 1 deletion app/api/seeder/seeder_reqs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cffi==1.15.0
email-validator==1.2.1
fastapi==0.65.1
passlib==1.7.4
psycopg2-binary==2.8.6
psycopg2-binary
py==1.11.0
pyasn1==0.4.8
python-dateutil==2.8.2
Expand Down
13 changes: 5 additions & 8 deletions app/api/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os

from functools import lru_cache
from typing import Generator

Expand Down Expand Up @@ -31,7 +30,6 @@ def pytest_configure(config):
"needs(*): mark test to run only when dependencies are available.",
)


@lru_cache
def postgres_is_running() -> bool:
"""
Expand Down Expand Up @@ -72,16 +70,15 @@ def setup_and_teardown_db(request) -> None:
Set up the database at the start of the pytest session, and then
tear down all created tables once tests have completed
"""
# TODO: Create a separate database only for testing. Otherwise
# uncommenting the following will delete all migrations in the database.
# database.drop_all_tables(check_first=True)

database.drop_all_tables(check_first=True)

database.create_all_tables()

# def teardown():
# database.drop_all_tables()
def teardown():
database.drop_all_tables()

# request.addfinalizer(teardown)
request.addfinalizer(teardown)


@pytest.fixture(scope="session")
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ services:
env_file: app/api/.env
environment:
- POSTGRES_HOST=db
- POSTGRES_DB=example_test
# run as module so basedir (root) is added to python path
command: python -m pytest api/tests/
volumes:
Expand Down

0 comments on commit edf12ee

Please sign in to comment.