Files
jungwoo choi 1d461a7ded test: Fix Pydantic v2 compatibility and comprehensive API testing
This commit migrates all models to Pydantic v2 and adds comprehensive
testing infrastructure for the news-engine-console backend.

Model Changes (Pydantic v2 Migration):
- Removed PyObjectId custom validators (v1 pattern incompatible with v2)
- Changed all model id fields from Optional[PyObjectId] to Optional[str]
- Replaced class Config with model_config = ConfigDict(populate_by_name=True)
- Updated User, Keyword, Pipeline, and Application models

Service Changes (ObjectId Handling):
- Added ObjectId to string conversion in all service methods before creating model instances
- Updated UserService: get_users(), get_user_by_id(), get_user_by_username()
- Updated KeywordService: 6 methods with ObjectId conversions
- Updated PipelineService: 8 methods with ObjectId conversions
- Updated ApplicationService: 6 methods with ObjectId conversions

Testing Infrastructure:
- Created comprehensive test_api.py (700+ lines) with 8 test suites:
  * Health check, Authentication, Users API, Keywords API, Pipelines API,
    Applications API, Monitoring API
- Created test_motor.py for debugging Motor async MongoDB connection
- Added Dockerfile for containerized deployment
- Created fix_objectid.py helper script for automated ObjectId conversion

Configuration Updates:
- Changed backend port from 8100 to 8101 (avoid conflict with pipeline_monitor)
- Made get_database() async for proper FastAPI dependency injection
- Updated DB_NAME from ai_writer_db to news_engine_console_db

Bug Fixes:
- Fixed environment variable override issue (system env > .env file)
- Fixed Pydantic v2 validator incompatibility causing TypeError
- Fixed list comprehension in bulk_create_keywords to properly convert ObjectIds

Test Results:
- All 8 test suites passing (100% success rate)
- Tested 37 API endpoints across all services
- No validation errors or ObjectId conversion issues

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 17:17:54 +09:00

58 lines
1.7 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import uvicorn
from app.core.config import settings
from app.core.database import connect_to_mongo, close_mongo_connection
from app.api import keywords, pipelines, users, applications, monitoring
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
await connect_to_mongo()
yield
# Shutdown
await close_mongo_connection()
app = FastAPI(
title="News Engine Console API",
description="뉴스 파이프라인 관리 및 모니터링 시스템",
version="1.0.0",
lifespan=lifespan
)
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Root endpoint
@app.get("/")
async def root():
return {"status": "News Engine Console API is running", "version": "1.0.0"}
# Health check
@app.get("/health")
async def health_check():
return {"status": "healthy", "service": settings.SERVICE_NAME}
# Include routers
app.include_router(keywords.router, prefix=f"{settings.API_V1_STR}/keywords", tags=["Keywords"])
app.include_router(pipelines.router, prefix=f"{settings.API_V1_STR}/pipelines", tags=["Pipelines"])
app.include_router(users.router, prefix=f"{settings.API_V1_STR}/users", tags=["Users"])
app.include_router(applications.router, prefix=f"{settings.API_V1_STR}/applications", tags=["Applications"])
app.include_router(monitoring.router, prefix=f"{settings.API_V1_STR}/monitoring", tags=["Monitoring"])
if __name__ == "__main__":
uvicorn.run(
"main:app",
host="0.0.0.0",
port=settings.PORT,
reload=True
)