Skip to content

Commit

Permalink
Wipe database after running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raffomania committed Dec 5, 2023
1 parent e0331f4 commit d1b179d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
18 changes: 3 additions & 15 deletions backend/src/app/core/db/sql_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy_utils import create_database, database_exists, drop_database

from app.core.data.orm.orm_base import ORMBase
from app.core.db.import_all_orms import * # noqa: F401, F403
from app.util.singleton_meta import SingletonMeta
from config import conf
Expand Down Expand Up @@ -47,20 +46,9 @@ def __new__(cls, *args, **kwargs):
def __del__(self):
self.__engine.dispose()

# This method is unused and only left here for historic reference
def _create_database_and_tables(self, drop_if_exists: bool = False) -> None:
logger.info("Setting up PostgresSQL DB and tables...")
if drop_if_exists and database_exists(self.__engine.url):
logger.warning("Dropping existing DB!")
drop_database(self.__engine.url)

self.create_database_if_not_exists()

# create all tables from SQLAlchemy ORM Models
ORMBase.metadata.create_all(self.__engine)
logger.debug("Created Tables!")

logger.info("Done setting up PostgresSQL DB and tables!")
def drop_database(self):
logger.warning("Dropping existing DB!")
drop_database(self.__engine.url)

def create_database_if_not_exists(self):
if not database_exists(self.__engine.url):
Expand Down
5 changes: 5 additions & 0 deletions backend/src/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
from app.core.data.dto.user import UserCreate, UserRead


def pytest_sessionfinish():
# Make sure the next test session starts with a clean database
SQLService().drop_database()


# Always use the asyncio backend for async tests
@pytest.fixture
def anyio_backend():
Expand Down

0 comments on commit d1b179d

Please sign in to comment.