7.1 KiB
7.1 KiB
模拟数据验证指南
概述
由于门户依赖 JNPF 平台环境,测试时需要使用 Mock 数据来验证代码逻辑。
Mock 数据结构
门户基础数据
MOCK_PORTAL = {
"id": "test_portal_001",
"fullName": "测试门户",
"enCode": "test_portal",
"category": "test_category",
"type": 0,
"state": 1,
"enabledLock": 0,
"platformRelease": '{"pc":1,"app":1}',
"systemId": "test_system_001"
}
组件配置数据
# 数据面板
MOCK_DATA_BOARD = {
"jnpfKey": "dataBoard",
"title": "测试数据面板",
"x": 0, "y": 0, "w": 12, "h": 4,
"option": {
"styleType": 1,
"rowNumber": 4,
"defaultValue": [
{"fullName": "测试指标1", "num": 100, "unit": "个"},
{"fullName": "测试指标2", "num": 200, "unit": "元"}
]
}
}
# 柱状图
MOCK_BAR_CHART = {
"jnpfKey": "barChart",
"title": "测试柱状图",
"x": 0, "y": 4, "w": 6, "h": 8,
"dataType": "dynamic",
"propsApi": "test_di_001",
"mappingConfig": [
{"field": "系列", "value": ""},
{"field": "维度", "value": "x"},
{"field": "数值", "value": "y"}
]
}
地图数据
MOCK_ATLAS_RESPONSE = {
"code": 200,
"data": [
{"code": "110000", "name": "北京市"},
{"code": "120000", "name": "天津市"},
{"code": "310000", "name": "上海市"},
{"code": "440000", "name": "广东省"}
],
"msg": "操作成功"
}
MOCK_MAP_GEOJSON = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "北京市"},
"geometry": {"type": "Polygon", "coordinates": [[[116,40],[117,40],[117,39],[116,39],[116,40]]]}
}
]
}
通知公告
MOCK_NOTICE_RESPONSE = {
"code": 200,
"data": [
{
"classify": "系统公告",
"fullName": "系统升级通知",
"content": "系统将于本周六进行升级...",
"creatorUser": "admin",
"releaseTime": "2024-01-15 10:00:00"
}
],
"msg": "操作成功"
}
使用 unittest.mock 测试
测试门户 CRUD
import unittest
from unittest.mock import patch, MagicMock
import json
class TestPortalAPI(unittest.TestCase):
def setUp(self):
# Mock HTTP响应
self.mock_response = MagicMock()
self.mock_response.json.return_value = {"code": 200, "data": {}, "msg": "操作成功"}
@patch('requests.Session.request')
def test_get_list(self, mock_request):
mock_request.return_value = self.mock_response
mock_response.json.return_value = {
"code": 200,
"data": {
"list": [MOCK_PORTAL],
"pagination": {"currentPage": 1, "pageSize": 20, "total": 1}
},
"msg": "操作成功"
}
from aibridge import JNPFClient
client = JNPFClient(base_url="http://localhost:30000", app_code="test")
client.login("admin", "password")
result = client.portal.get_list()
self.assertTrue(result.success)
@patch('requests.Session.request')
def test_create_portal(self, mock_request):
mock_request.return_value = self.mock_response
mock_response.json.return_value = {
"code": 200,
"data": {"id": "new_portal_id"},
"msg": "操作成功"
}
from aibridge import JNPFClient
client = JNPFClient(base_url="http://localhost:30000", app_code="test")
client.login("admin", "password")
result = client.portal.create({
"fullName": "测试门户",
"enCode": "test_portal",
"type": 0
})
self.assertTrue(result.success)
测试组件配置
def test_component_config_structure(self):
"""验证组件配置结构"""
config = {
"layoutId": 100,
"layout": [MOCK_DATA_BOARD, MOCK_BAR_CHART]
}
config_str = json.dumps(config)
parsed = json.loads(config_str)
self.assertEqual(parsed["layoutId"], 100)
self.assertEqual(len(parsed["layout"]), 2)
self.assertEqual(parsed["layout"][0]["jnpfKey"], "dataBoard")
本地测试脚本
创建 aibridge/tests/test_portal_mock.py:
"""门户模块 Mock 测试"""
import unittest
from unittest.mock import patch, MagicMock
import json
MOCK_PORTAL_DATA = {
"id": "test_portal_001",
"fullName": "测试门户",
"enCode": "test_portal",
"type": 0,
"state": 1,
"systemId": "test_system"
}
MOCK_LAYOUT_CONFIG = {
"layoutId": 100,
"layout": [
{
"jnpfKey": "dataBoard",
"title": "测试指标",
"w": 12, "h": 4,
"option": {
"defaultValue": [
{"fullName": "测试", "num": 100, "unit": "个"}
]
}
}
]
}
class TestPortalMock(unittest.TestCase):
@patch('aibridge.client.JNPFClient.request')
def test_get_portal_info(self, mock_request):
mock_request.return_value = MagicMock(
success=True,
code=200,
data=MOCK_PORTAL_DATA,
msg="操作成功"
)
from aibridge import JNPFClient
client = JNPFClient()
result = client.portal.get_info("test_portal_001")
self.assertTrue(result.success)
self.assertEqual(result.data["fullName"], "测试门户")
@patch('aibridge.client.JNPFClient.request')
def test_create_portal_with_layout(self, mock_request):
mock_request.return_value = MagicMock(
success=True,
code=200,
data={"id": "new_portal"},
msg="操作成功"
)
from aibridge import JNPFClient
client = JNPFClient()
data = {
"fullName": "新门户",
"enCode": "new_portal",
"type": 0,
"formData": json.dumps(MOCK_LAYOUT_CONFIG)
}
result = client.portal.create(data)
self.assertTrue(result.success)
def test_layout_config_json(self):
"""测试布局配置序列化"""
config_str = json.dumps(MOCK_LAYOUT_CONFIG)
self.assertIsInstance(config_str, str)
parsed = json.loads(config_str)
self.assertEqual(parsed["layout"][0]["jnpfKey"], "dataBoard")
if __name__ == '__main__':
unittest.main()
验证检查清单
| 检查项 | 说明 |
|---|---|
| 组件 jnpfKey 有效 | 必须在 21 种组件类型中 |
| w/h 在范围内 | w: 1-12, h: 1-20 |
| option 结构正确 | 对应组件的必填字段 |
| dataType 匹配 | static 需 defaultValue, dynamic 需 propsApi |
| visibility 有效 | 必须包含 "pc" 和/或 "app" |
| JSON 序列化正常 | formData 必须是有效 JSON |
运行测试
# 运行所有测试
cd $ccWorkDir
python -m pytest aibridge/tests/test_portal_mock.py -v
# 运行特定测试
python -m pytest aibridge/tests/test_portal_mock.py::TestPortalMock::test_create_portal_with_layout -v