P1:闭环时限管控、治理成效评估、站型阈值系数与接口测试
- 闭环状态流转自动记录接单/闭环时间,状态值校验;按断站分级判定响应时限 (及时/超时/超时未响应/未到期/无记录),新增及时响应率、平均闭环时长、超时未闭环指标与筛选 - 新增治理成效评估 /api/effect:分界日期前后指标对比与问题站点逐站改善判定 - 新增站型阈值系数(实际阈值 = 场景阈值 × 站型系数)及配置接口与页面 - 样例数据新增接单/闭环时间(独立随机数,原字段不变),导入支持并校验 - 新增 tests/test_api.py(权限、导入模板、闭环流程、站型系数、成效评估、无数据边界) - 修复:新流水无告警匹配时根因分析报错;空筛选时超长站点清单返回 500 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e9616af34d
commit
0e070a64fe
13 files changed
+6540
-6043
No files matched your search
@@ -0,0 +1,115 @@
|
||||
"""接口与业务流程测试:登录与权限、导入校验、闭环时限、站型阈值、治理成效、边界情况。
|
||||
|
||||
运行:python tests/test_api.py
|
||||
"""
|
||||
import io
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from harness import check, core, finish, load, sample_data # 先导入 harness:它把数据库指向 tests/_work
|
||||
|
||||
import app as webapp # noqa: E402
|
||||
|
||||
load('api', *sample_data())
|
||||
core.execute("INSERT INTO users(username, password, role, created_at) VALUES('user', ?, 'user', '')",
|
||||
(webapp.generate_password_hash('user123'),), changes_data=False)
|
||||
admin = webapp.app.test_client()
|
||||
user = webapp.app.test_client()
|
||||
|
||||
print('== 1. 登录与权限')
|
||||
check('错误密码登录失败', admin.post('/login', json={'username': 'admin', 'password': 'x'}).status_code == 400)
|
||||
check('管理员登录', admin.post('/login', json={'username': 'admin', 'password': 'admin123'}).status_code == 200)
|
||||
check('普通用户登录', user.post('/login', json={'username': 'user', 'password': 'user123'}).status_code == 200)
|
||||
check('未登录访问接口返回 401', webapp.app.test_client().get('/api/dashboard').status_code == 401)
|
||||
for method, url in [('get', '/api/logs'), ('get', '/api/users'), ('post', '/api/analyze'),
|
||||
('post', '/api/import/preview'), ('post', '/api/outages/batch'), ('put', '/api/site_types/宏站')]:
|
||||
code = getattr(user, method)(url, json={}).status_code
|
||||
check(f'普通用户 {method.upper()} {url} 被拒绝', code == 403, f'({code})')
|
||||
check('普通用户可看看板', user.get('/api/dashboard').status_code == 200)
|
||||
|
||||
print('\n== 2. 导入模板校验(模板含 5 行故意写错的数据)')
|
||||
template = admin.get('/api/template')
|
||||
preview = admin.post('/api/import/preview', data={'file': (io.BytesIO(template.data), 'template.xlsx')},
|
||||
content_type='multipart/form-data').get_json()
|
||||
reasons = {e['row']: e['reason'] for e in preview['errors']}
|
||||
check('合格 3 行、问题 5 行', (preview['ok'], preview['bad']) == (3, 5), f'({preview["ok"]} / {preview["bad"]})')
|
||||
expected = {4: '恢复时间早于开始时间', 5: '基站编号缺失', 6: '恢复时间缺失或格式错误', 7: '重复记录', 8: '基站不在站点库中'}
|
||||
for row, phrase in expected.items():
|
||||
check(f'第 {row} 行:{phrase}', phrase in reasons.get(row, ''), f'({reasons.get(row)})')
|
||||
check('上报时长差错 1 条(第 9 行 45 分 vs 实际 405 分)', preview['diff'] == 1)
|
||||
before = core.query('SELECT COUNT(*) AS n FROM outages')[0]['n']
|
||||
result = admin.post('/api/import/confirm', json={'token': preview['token']}).get_json()
|
||||
after = core.query('SELECT COUNT(*) AS n FROM outages')[0]['n']
|
||||
check('确认后入库 3 条', result['n'] == 3 and after - before == 3)
|
||||
check('预览令牌只能用一次', admin.post('/api/import/confirm', json={'token': preview['token']}).status_code == 400)
|
||||
|
||||
print('\n== 3. 闭环流程与响应时限')
|
||||
site = core.query('SELECT ne_id FROM sites LIMIT 1')[0]['ne_id']
|
||||
new = admin.post('/api/outages', json={'ne_id': site, 'start_time': '2026-10-20 08:00', 'end_time': '2026-10-20 08:40',
|
||||
'status': '待处理'}).get_json()
|
||||
oid = new['id']
|
||||
|
||||
|
||||
def row():
|
||||
core.data_changed()
|
||||
return core.full_frame().set_index('id').loc[oid]
|
||||
|
||||
|
||||
r = row()
|
||||
check('新增待处理:无接单 / 闭环时间', pd.isna(r['handled_at']) and pd.isna(r['closed_at']))
|
||||
check('早已超过时限 → 超时未响应', r['sla'] == '超时未响应', f'({r["sla"]},时限 {r["limit_h"]} h)')
|
||||
check('非法状态被拒绝', user.put(f'/api/outages/{oid}', json={'status': '随便'}).status_code == 400)
|
||||
check('不存在的记录返回 404', user.put('/api/outages/99999999', json={'status': '处理中'}).status_code == 404)
|
||||
user.put(f'/api/outages/{oid}', json={'status': '处理中', 'handler': '张工', 'remark': '已派单'})
|
||||
r = row()
|
||||
check('转处理中:自动记录接单时间', pd.notna(r['handled_at']) and pd.isna(r['closed_at']))
|
||||
check('接单晚于时限 → 超时', r['sla'] == '超时', f'({r["sla"]})')
|
||||
handled_first = r['handled_at']
|
||||
user.put(f'/api/outages/{oid}', json={'status': '已闭环'})
|
||||
r = row()
|
||||
check('转已闭环:自动记录闭环时间,接单时间不变', pd.notna(r['closed_at']) and r['handled_at'] == handled_first)
|
||||
user.put(f'/api/outages/{oid}', json={'status': '处理中'})
|
||||
check('重新打开:清空闭环时间', pd.isna(row()['closed_at']))
|
||||
|
||||
ids = [x['id'] for x in core.query("SELECT id FROM outages WHERE status = '待处理' LIMIT 3")]
|
||||
admin.post('/api/outages/batch', json={'ids': ids, 'action': 'status', 'status': '已闭环'})
|
||||
rows = core.query(f'SELECT handled_at, closed_at FROM outages WHERE id IN ({",".join(map(str, ids))})')
|
||||
check('批量闭环:都有接单与闭环时间', all(x['handled_at'] and x['closed_at'] for x in rows))
|
||||
overdue = admin.get('/api/outages', query_string={'overdue': 1, 'size': 5}).get_json()
|
||||
check('“仅超时未闭环”筛选只返回未闭环且超时的记录',
|
||||
overdue['total'] > 0 and all(x['status'] != '已闭环' and x['sla'] in ('超时', '超时未响应') for x in overdue['rows']))
|
||||
k = admin.get('/api/dashboard').get_json()['kpi']
|
||||
check('看板含及时响应率 / 平均闭环时长 / 超时未闭环',
|
||||
all(key in k for key in ('response_rate', 'avg_close_h', 'overdue_open')), f'({k["response_rate"]}%,'
|
||||
f'{k["avg_close_h"]} h,{k["overdue_open"]} 条)')
|
||||
|
||||
print('\n== 4. 站型阈值系数')
|
||||
core.data_changed()
|
||||
long_before = int(core.full_frame().query("site_type == '室分'")['is_long'].sum())
|
||||
check('系数越界被拒绝', admin.put('/api/site_types/室分', json={'long_factor': 0, 'freq_factor': 1}).status_code == 400)
|
||||
admin.put('/api/site_types/室分', json={'long_factor': 0.5, 'freq_factor': 1})
|
||||
core.data_changed()
|
||||
frame = core.full_frame()
|
||||
indoor = frame[frame['site_type'] == '室分']
|
||||
check('室分系数 0.5:实际阈值 = 场景阈值 × 0.5', (indoor['long_min'] == indoor['scene_long_min'] * 0.5).all())
|
||||
check('阈值从严后室分超长次数增加', int(indoor['is_long'].sum()) > long_before,
|
||||
f'({long_before} → {int(indoor["is_long"].sum())})')
|
||||
macro = frame[frame['site_type'] == '宏站']
|
||||
check('宏站不受影响', (macro['long_min'] == macro['scene_long_min']).all())
|
||||
|
||||
print('\n== 5. 治理成效评估')
|
||||
effect = admin.get('/api/effect', query_string={'split': '2026-06-01'}).get_json()
|
||||
check('返回 8 项指标对比', len(effect['rows']) == 8)
|
||||
check('问题站点改善 + 持平 + 恶化 = 问题站点数',
|
||||
effect['improved'] + effect['flat'] + effect['worsened'] + sum(s['verdict'] == '—' for s in effect['sites'])
|
||||
== len(effect['sites']), f'({len(effect["sites"])} 个)')
|
||||
check('分界日期超出周期返回 400', admin.get('/api/effect', query_string={'split': '2030-01-01'}).status_code == 400)
|
||||
check('默认分界日期可用', admin.get('/api/effect').status_code == 200)
|
||||
|
||||
print('\n== 6. 边界情况')
|
||||
for url, params in [('/api/dashboard', {'month': '2030-01'}), ('/api/stats', {'month': '2030-01'}),
|
||||
('/api/association', {'month': '2030-01'}), ('/api/sites/rank', {'site': '不存在的站'}),
|
||||
('/api/export/stats', {'month': '2030-01'}), ('/api/outages', {'site': '不存在的站'})]:
|
||||
code = admin.get(url, query_string=params).status_code
|
||||
check(f'无数据筛选 {url}', code == 200, f'({code})')
|
||||
finish()
|
||||
Reference in new issue
Block a user