Files

230 lines
4.9 KiB
Markdown
Raw Permalink Normal View History

# Skill: city-manual-test
## Description
城市手册项目测试技能。用于运行项目测试和 API 验证。
## Location
`/root/.openclaw/workspace/city-manual/skills/city-manual-test/`
## Capabilities
- 运行 Django 测试
- API 端点测试
- 数据库连接测试
- 前端构建测试
- 性能测试
## Usage
当用户提到以下关键词时激活此技能:
- "测试城市手册"
- "run tests"
- "API 测试"
- "验证功能"
## Scripts
### test_api.py
```python
#!/usr/bin/env python3
"""
城市手册 API 测试脚本
"""
import requests
import json
BASE_URL = "http://127.0.0.1:8000/api"
def test_auth():
"""测试认证 API"""
print("=== 测试认证 API ===")
# 登录
response = requests.post(f"{BASE_URL}/auth/token/", json={
"username": "demo",
"password": "demo123"
})
if response.status_code == 200:
token = response.json()["access"]
print("✅ 登录成功")
return token
else:
print(f"❌ 登录失败:{response.status_code}")
return None
def test_users(token):
"""测试用户 API"""
print("\n=== 测试用户 API ===")
headers = {"Authorization": f"Bearer {token}"}
# 获取当前用户
response = requests.get(f"{BASE_URL}/users/me/", headers=headers)
if response.status_code == 200:
print(f"✅ 获取用户信息成功:{response.json()['username']}")
else:
print(f"❌ 获取用户信息失败:{response.status_code}")
def test_regions(token):
"""测试版块 API"""
print("\n=== 测试版块 API ===")
# 获取版块列表
response = requests.get(f"{BASE_URL}/regions/")
if response.status_code == 200:
data = response.json()
print(f"✅ 获取版块列表成功:共 {data['count']} 个版块")
if data['results']:
print(f" 第一个版块:{data['results'][0]['name']}")
else:
print(f"❌ 获取版块列表失败:{response.status_code}")
def test_articles(token):
"""测试文章 API"""
print("\n=== 测试文章 API ===")
# 获取文章列表
response = requests.get(f"{BASE_URL}/articles/")
if response.status_code == 200:
data = response.json()
print(f"✅ 获取文章列表成功:共 {data['count']} 篇文章")
else:
print(f"❌ 获取文章列表失败:{response.status_code}")
def test_services(token):
"""测试特色服务 API"""
print("\n=== 测试特色服务 API ===")
# 获取特色服务列表
response = requests.get(f"{BASE_URL}/services/")
if response.status_code == 200:
data = response.json()
print(f"✅ 获取特色服务列表成功:共 {data['count']} 个服务")
else:
print(f"❌ 获取特色服务列表失败:{response.status_code}")
def test_database():
"""测试数据库连接"""
print("\n=== 测试数据库连接 ===")
import psycopg2
try:
conn = psycopg2.connect(
host="10.2.0.100",
port=5432,
database="cssc",
user="coder",
password="825670wl"
)
cur = conn.cursor()
cur.execute("SELECT 1")
cur.close()
conn.close()
print("✅ 数据库连接成功")
return True
except Exception as e:
print(f"❌ 数据库连接失败:{e}")
return False
def main():
"""主函数"""
print("城市手册 API 测试开始\n")
# 测试数据库
test_database()
# 测试认证
token = test_auth()
if not token:
print("\n❌ 认证失败,停止测试")
return
# 测试各 API
test_users(token)
test_regions(token)
test_articles(token)
test_services(token)
print("\n=== 测试完成 ===")
if __name__ == "__main__":
main()
```
## Commands
### 运行 Django 测试
```bash
cd /root/.openclaw/workspace/city-manual/backend
python manage.py test
```
### 运行 API 测试
```bash
cd /root/.openclaw/workspace/city-manual
python test_api.py
```
### 运行前端测试
```bash
cd /root/.openclaw/workspace/city-manual/frontend
npm test
```
### 数据库连接测试
```bash
psql -h 10.2.0.100 -U coder -d cssc -c "SELECT 1"
```
## Health Check
```bash
# 检查所有服务
curl http://127.0.0.1:8000/admin/
curl http://127.0.0.1/
psql -h 10.2.0.100 -U coder -d cssc -c "SELECT 1"
```
## Test Cases
### 用户认证测试
1. 正常登录
2. 错误密码
3. 令牌刷新
4. 令牌过期
### 版块管理测试
1. 获取版块列表
2. 获取版块详情
3. 创建版块(管理员)
4. 版块层级查询
### 内容管理测试
1. 获取文章列表
2. 获取文章详情
3. 创建文章
4. 更新文章
5. 删除文章
6. 文章审核流程
### 互动功能测试
1. 发表评论
2. 评分
3. 点赞
4. 收藏
## References
- [TESTING.md](../../TESTING.md)
- [test_api.py](../../test_api.py)