Files
TK-BOM-Project/backend/tests/conftest.py
Hyungi Ahn 4f8e395f87
Some checks failed
SonarQube Analysis / SonarQube Scan (push) Has been cancelled
feat: SWG 가스켓 전체 구성 정보 표시 개선
- H/F/I/O SS304/GRAPHITE/CS/CS 패턴에서 4개 구성요소 모두 표시
- 기존 SS304 + GRAPHITE → SS304/GRAPHITE/CS/CS로 완전한 구성 표시
- 외부링/필러/내부링/추가구성 모든 정보 포함
- 구매수량 계산 모달에서 정확한 재질 정보 확인 가능
2025-08-30 14:23:01 +09:00

161 lines
3.9 KiB
Python

"""
pytest 설정 및 공통 픽스처
"""
import pytest
import asyncio
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from fastapi.testclient import TestClient
import tempfile
import os
from app.main import app
from app.database import get_db
from app.models import Base
# 테스트용 데이터베이스 설정
TEST_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(
TEST_DATABASE_URL,
connect_args={"check_same_thread": False}
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def override_get_db():
"""테스트용 데이터베이스 세션"""
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
@pytest.fixture(scope="session")
def event_loop():
"""이벤트 루프 픽스처"""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="function")
def db_session():
"""테스트용 데이터베이스 세션 픽스처"""
# 테이블 생성
Base.metadata.create_all(bind=engine)
# 세션 생성
session = TestingSessionLocal()
try:
yield session
finally:
session.close()
# 테이블 삭제 (테스트 격리)
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="function")
def client(db_session):
"""테스트 클라이언트 픽스처"""
# 데이터베이스 의존성 오버라이드
app.dependency_overrides[get_db] = override_get_db
with TestClient(app) as test_client:
yield test_client
# 의존성 오버라이드 정리
app.dependency_overrides.clear()
@pytest.fixture
def sample_excel_file():
"""샘플 엑셀 파일 픽스처"""
import pandas as pd
# 샘플 데이터 생성
data = {
'Description': [
'PIPE, SEAMLESS, A333-6, 6", SCH40',
'ELBOW, 90DEG, A234-WPB, 4", SCH40',
'VALVE, GATE, A216-WCB, 2", 150LB',
'FLANGE, WELD NECK, A105, 3", 150LB',
'BOLT, HEX HEAD, A193-B7, M16X50'
],
'Quantity': [10, 8, 2, 4, 20],
'Unit': ['EA', 'EA', 'EA', 'EA', 'EA']
}
df = pd.DataFrame(data)
# 임시 파일 생성
with tempfile.NamedTemporaryFile(suffix='.xlsx', delete=False) as tmp_file:
df.to_excel(tmp_file.name, index=False)
yield tmp_file.name
# 파일 정리
os.unlink(tmp_file.name)
@pytest.fixture
def sample_csv_file():
"""샘플 CSV 파일 픽스처"""
import pandas as pd
# 샘플 데이터 생성
data = {
'Description': [
'PIPE, SEAMLESS, A333-6, 8", SCH40',
'TEE, EQUAL, A234-WPB, 6", SCH40',
'VALVE, BALL, A216-WCB, 4", 150LB'
],
'Quantity': [5, 3, 1],
'Unit': ['EA', 'EA', 'EA']
}
df = pd.DataFrame(data)
# 임시 파일 생성
with tempfile.NamedTemporaryFile(suffix='.csv', delete=False, mode='w') as tmp_file:
df.to_csv(tmp_file.name, index=False)
yield tmp_file.name
# 파일 정리
os.unlink(tmp_file.name)
@pytest.fixture
def sample_job_data():
"""샘플 작업 데이터 픽스처"""
return {
"job_no": "TEST-2025-001",
"job_name": "테스트 프로젝트",
"client_name": "테스트 고객사",
"end_user": "테스트 사용자",
"epc_company": "테스트 EPC",
"status": "active"
}
@pytest.fixture
def sample_material_data():
"""샘플 자재 데이터 픽스처"""
return {
"original_description": "PIPE, SEAMLESS, A333-6, 6\", SCH40",
"classified_category": "PIPE",
"classified_subcategory": "SEAMLESS",
"material_grade": "A333-6",
"schedule": "SCH40",
"size_spec": "6\"",
"quantity": 10.0,
"unit": "EA",
"classification_confidence": 0.95
}
# 테스트 설정
pytest_plugins = []