Fix test setup for backend
This commit is contained in:
20
tests/config/database.py
Normal file
20
tests/config/database.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.database import Base
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "postgresql://admin:WeSign123!@localhost/wesigntest"
|
||||
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||
|
||||
TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def override_get_db():
|
||||
try:
|
||||
db = TestSessionLocal()
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
27
tests/config/setup.py
Normal file
27
tests/config/setup.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import sys
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.database import Base as ProductionBase
|
||||
from tests.config.database import (SQLALCHEMY_DATABASE_URL, TestBase,
|
||||
TestSessionLocal)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||
print(SQLALCHEMY_DATABASE_URL)
|
||||
ProductionBase.metadata.create_all(bind=engine)
|
||||
TestBase.metadata.create_all(bind=engine)
|
||||
session = TestSessionLocal(bind=engine)
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.rollback()
|
||||
TestBase.metadata.drop_all(bind=engine)
|
||||
ProductionBase.metadata.drop_all(bind=engine)
|
||||
session.close()
|
||||
Reference in New Issue
Block a user