I am trying to setup testdb for production environment FastAPI application
below is my project structure
my_project
main.py
api
v1
users
app.py
database
connection.py
tests
conftest.py
users_test.py
conftest.py looks like this
DATABASE_URL = settings.mysql_test_url
print("Database URL used for tests:", DATABASE_URL)
engine = create_engine(DATABASE_URL)
TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
print("Using test database session")
db = TestSessionLocal()
try:
yield db
finally:
db.close()
@pytest.fixture(scope="module")
def test_db():
app.dependency_overrides[get_db] = override_get_db
Base.metadata.create_all(bind=engine)
client = TestClient(app)
yield client
Base.metadata.drop_all(bind=engine)
one of my test cases look this
from tests.conftest import test_db,TestSessionLocal
def test_signup(test_db,setup_db):
response = test_db.post("/public/user/sign-up", json={"email": "[email protected]", "password": "password"})
assert response.status_code == 200
Problem
schema is creating in testdb (i verified by uncommenting drop all line)
but data is writing in main db(prod db)
I have used override of dependency but still its not working.
Anything i need to change here
Thanks for time and response.