ETest-Vue-FastAPI/test_fastapi.py

40 lines
1.3 KiB
Python
Raw Normal View History

2026-04-15 19:06:01 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import asyncio
import sys
sys.path.insert(0, 'ruoyi-fastapi-backend')
from datetime import date, datetime
from fastapi import FastAPI, Depends
from fastapi.testclient import TestClient
from sqlalchemy.ext.asyncio import AsyncSession
# 创建测试应用
app = FastAPI()
# 模拟依赖
async def mock_db():
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from urllib.parse import quote_plus
DB_URL = f'mysql+asyncmy://cpy_admin:{quote_plus("Tgzz2025+")}@localhost:3307/ruoyi-fastapi'
engine = create_async_engine(DB_URL, echo=False)
AsyncSession = async_sessionmaker(engine, expire_on_commit=False)
async with AsyncSession() as session:
yield session
# 导入实际 controller 函数
from module_admin.controller.warehouse_receipt_controller import WarehouseReceiptController
# 注册路由
@app.get("/warehouse/receipt/{receipt_id}")
async def get_receipt(receipt_id: int, query_db: AsyncSession = Depends(mock_db)):
return await WarehouseReceiptController.get_receipt_detail(query_db, receipt_id)
# 测试
client = TestClient(app)
print("Testing /warehouse/receipt/685...")
response = client.get("/warehouse/receipt/685")
print(f"Status: {response.status_code}")
print(f"Response: {response.text[:500]}")