api/tests/conftest.py
Yoga Pangestu 7bbe4e7295 Implement subscription, tenant, and user services with CRUD operations and associated tests
- Added subscription_service.py for managing subscriptions with functions to create, read, update, and delete subscriptions.
- Added tenant_service.py for managing tenants with functions to create, read, update, delete tenants, and assign users to tenants.
- Added user_service.py for managing users with functions to create, read, update, and delete users.
- Created tests for business types, plans, subscriptions, tenants, and users, ensuring proper functionality and error handling.
- Implemented pagination in user listing and included user profiles in responses.
- Established fixtures for database setup and teardown in tests.
2026-07-17 23:36:31 +07:00

46 lines
1.1 KiB
Python

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import text
from sqlalchemy.orm import Session
from app.database import Base, engine, get_db
from app.models import * # noqa: F401, F403 — register all models
from main import app
@pytest.fixture(scope="session")
def setup_db():
Base.metadata.create_all(bind=engine)
yield
@pytest.fixture
def db_session(setup_db):
connection = engine.connect()
transaction = connection.begin()
connection.execute(text("SET FOREIGN_KEY_CHECKS = 0"))
for table in reversed(Base.metadata.sorted_tables):
connection.execute(text(f"TRUNCATE TABLE {table.name}"))
connection.execute(text("SET FOREIGN_KEY_CHECKS = 1"))
transaction.commit()
transaction = connection.begin()
session = Session(bind=connection)
yield session
session.close()
transaction.rollback()
connection.close()
@pytest.fixture
def client(db_session):
def override_get_db():
yield db_session
app.dependency_overrides[get_db] = override_get_db
yield TestClient(app)
app.dependency_overrides.clear()