311 lines
12 KiB
Python
311 lines
12 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
远程地震体 API 直接测试
|
||
|
||
使用 requests 库直接调用远程地震体 API 服务,
|
||
不通过 APICaller 类,以测试基本功能。
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import json
|
||
import logging
|
||
import unittest
|
||
import requests
|
||
from pathlib import Path
|
||
|
||
# 配置日志
|
||
logging.basicConfig(
|
||
level=logging.INFO,
|
||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||
)
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# 远程API服务器地址
|
||
REMOTE_API_SERVER = "http://localhost:5001"
|
||
|
||
class TestRemoteSeismicAPIDirect(unittest.TestCase):
|
||
"""远程地震体 API 直接测试类"""
|
||
|
||
@classmethod
|
||
def setUpClass(cls):
|
||
"""测试类开始前检查API服务器是否可用"""
|
||
cls.server_available = False
|
||
|
||
try:
|
||
# 尝试连接到服务器,只要能连接就认为服务器在运行
|
||
response = requests.get(f"{REMOTE_API_SERVER}/", timeout=2)
|
||
cls.server_available = True
|
||
logger.info(f"远程API服务器 {REMOTE_API_SERVER} 可访问,HTTP状态码: {response.status_code}")
|
||
except Exception as e:
|
||
logger.warning(f"无法连接到远程API服务器 {REMOTE_API_SERVER}: {str(e)}")
|
||
cls.server_available = False
|
||
|
||
def setUp(self):
|
||
"""为每个测试初始化环境"""
|
||
if not self.__class__.server_available:
|
||
self.skipTest(f"远程API服务器 {REMOTE_API_SERVER} 不可用")
|
||
|
||
# 保存测试中创建的资源ID,以便后续测试或清理
|
||
self.created_resource_ids = []
|
||
|
||
def test_create_seismic_file(self):
|
||
"""测试创建地震体文件"""
|
||
# 准备创建地震体的请求数据
|
||
seismic_data = {
|
||
"projectId": "testPrj1",
|
||
"surveyId": "20230117135924_2",
|
||
"seismicName": "测试地震体-直接调用",
|
||
"dsType": 1,
|
||
"dimensions": [
|
||
{
|
||
"dimensionNo": 1,
|
||
"dimensionName": "inline",
|
||
"serviceMin": 100,
|
||
"serviceMax": 500,
|
||
"serviceSpan": 1
|
||
},
|
||
{
|
||
"dimensionNo": 2,
|
||
"dimensionName": "xline",
|
||
"serviceMin": 200,
|
||
"serviceMax": 600,
|
||
"serviceSpan": 1
|
||
},
|
||
{
|
||
"dimensionNo": 3,
|
||
"dimensionName": "slice",
|
||
"serviceMin": 3500,
|
||
"serviceMax": 3600,
|
||
"serviceSpan": 4
|
||
}
|
||
]
|
||
}
|
||
|
||
try:
|
||
# 直接使用requests库调用API
|
||
response = requests.post(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/file/add",
|
||
json=seismic_data,
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(response.status_code, 200)
|
||
logger.info(f"创建地震体API调用返回状态码: {response.status_code}")
|
||
|
||
# 解析响应JSON
|
||
response_json = response.json()
|
||
self.assertIn("result", response_json)
|
||
|
||
# 保存创建的ID用于后续测试
|
||
created_id = response_json.get("result")
|
||
if created_id:
|
||
self.created_resource_ids.append(created_id)
|
||
logger.info(f"成功创建地震体,ID: {created_id}")
|
||
|
||
# 验证响应的其他字段
|
||
self.assertEqual(response_json.get("code"), "0")
|
||
self.assertTrue(response_json.get("flag", False))
|
||
except Exception as e:
|
||
logger.error(f"创建地震体过程中发生错误: {str(e)}")
|
||
raise
|
||
|
||
def test_query_trace_count(self):
|
||
"""测试查询地震体道数"""
|
||
# 使用样例ID
|
||
sample_id = "20221113181927_1"
|
||
|
||
try:
|
||
# 直接使用requests库调用API
|
||
response = requests.post(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/traces/count",
|
||
json={"seismicId": sample_id},
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(response.status_code, 200)
|
||
logger.info(f"查询道数API调用返回状态码: {response.status_code}")
|
||
|
||
# 解析响应JSON
|
||
response_json = response.json()
|
||
self.assertIn("result", response_json)
|
||
self.assertIn("code", response_json)
|
||
self.assertIn("msg", response_json)
|
||
|
||
trace_count = response_json.get("result")
|
||
logger.info(f"地震体 {sample_id} 的道数: {trace_count}")
|
||
except Exception as e:
|
||
logger.error(f"查询道数过程中发生错误: {str(e)}")
|
||
raise
|
||
|
||
def test_query_h3200_header(self):
|
||
"""测试查询地震体3200头"""
|
||
# 使用样例ID
|
||
sample_id = "20221113181927_1"
|
||
|
||
try:
|
||
# 直接使用requests库调用API
|
||
response = requests.post(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/head/h3200",
|
||
json={"seismicId": sample_id},
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(response.status_code, 200)
|
||
logger.info(f"查询3200头API调用返回状态码: {response.status_code}")
|
||
|
||
# 验证返回了二进制数据
|
||
self.assertTrue(len(response.content) > 0)
|
||
logger.info(f"地震体 {sample_id} 的3200头数据长度: {len(response.content)} 字节")
|
||
except Exception as e:
|
||
logger.error(f"查询3200头过程中发生错误: {str(e)}")
|
||
raise
|
||
|
||
def test_query_h400_keywords(self):
|
||
"""测试查询地震体卷头关键字列表"""
|
||
# 使用样例ID
|
||
sample_id = "20221113181927_1"
|
||
|
||
try:
|
||
# 直接使用requests库调用API
|
||
response = requests.post(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/h400/keyword/list",
|
||
json={"seismicId": sample_id},
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(response.status_code, 200)
|
||
logger.info(f"查询卷头关键字API调用返回状态码: {response.status_code}")
|
||
|
||
# 解析响应JSON
|
||
response_json = response.json()
|
||
self.assertIn("result", response_json)
|
||
self.assertTrue(response_json.get("flag", False))
|
||
|
||
keywords = response_json.get("result", [])
|
||
logger.info(f"地震体 {sample_id} 的卷头关键字数量: {len(keywords)}")
|
||
if keywords:
|
||
logger.info(f"第一个关键字: {keywords[0]}")
|
||
except Exception as e:
|
||
logger.error(f"查询卷头关键字过程中发生错误: {str(e)}")
|
||
raise
|
||
|
||
def test_coordinate_conversion(self):
|
||
"""测试坐标转换"""
|
||
# 使用样例ID
|
||
sample_id = "20221113181927_1"
|
||
|
||
# 测试点坐标
|
||
test_points = [
|
||
[606406.1281141682, 6082083.338731234], # 模拟服务器样例中的一个坐标
|
||
[609767.8725899048, 6080336.549935018],
|
||
[615271.9052119441, 6082017.422172886]
|
||
]
|
||
|
||
try:
|
||
# 直接使用requests库调用API
|
||
response = requests.post(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/coordinate/geodetic/toline",
|
||
json={
|
||
"seismicId": sample_id,
|
||
"points": test_points
|
||
},
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(response.status_code, 200)
|
||
logger.info(f"坐标转换API调用返回状态码: {response.status_code}")
|
||
|
||
# 解析响应JSON
|
||
response_json = response.json()
|
||
self.assertIn("result", response_json)
|
||
|
||
result = response_json.get("result", [])
|
||
self.assertEqual(len(result), len(test_points))
|
||
|
||
# 记录转换结果
|
||
for i, (point, line_point) in enumerate(zip(test_points, result)):
|
||
logger.info(f"坐标点 {i+1}: {point} → 线点: {line_point}")
|
||
except Exception as e:
|
||
logger.error(f"坐标转换过程中发生错误: {str(e)}")
|
||
raise
|
||
|
||
def test_export_task(self):
|
||
"""测试导出地震体任务"""
|
||
# 使用样例ID
|
||
sample_id = "20221113181927_1"
|
||
|
||
try:
|
||
# 1. 提交导出任务
|
||
submit_response = requests.post(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/export/submit",
|
||
json={
|
||
"seismicId": sample_id,
|
||
"saveDir": f"/export/{sample_id}.sgy"
|
||
},
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(submit_response.status_code, 200)
|
||
logger.info(f"提交导出任务API调用返回状态码: {submit_response.status_code}")
|
||
|
||
# 解析响应JSON
|
||
submit_json = submit_response.json()
|
||
self.assertEqual(submit_json.get("code"), "0")
|
||
self.assertTrue(submit_json.get("flag", False))
|
||
self.assertIn("result", submit_json)
|
||
|
||
# 获取任务ID
|
||
task_result = submit_json.get("result", {})
|
||
if isinstance(task_result, dict) and "taskId" in task_result:
|
||
task_id = task_result["taskId"]
|
||
else:
|
||
task_id = task_result # 某些实现可能直接返回任务ID
|
||
|
||
self.assertIsNotNone(task_id, "导出任务ID不应为空")
|
||
logger.info(f"创建导出任务成功,任务ID: {task_id}")
|
||
|
||
# 2. 查询导出进度
|
||
if task_id:
|
||
progress_response = requests.get(
|
||
f"{REMOTE_API_SERVER}/api/gsc/appmodel/api/v1/seismic/export/progress?taskId={task_id}",
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=10
|
||
)
|
||
|
||
# 验证响应状态码
|
||
self.assertEqual(progress_response.status_code, 200)
|
||
logger.info(f"查询导出进度API调用返回状态码: {progress_response.status_code}")
|
||
|
||
# 解析响应JSON
|
||
progress_json = progress_response.json()
|
||
self.assertEqual(progress_json.get("code"), "0")
|
||
self.assertTrue(progress_json.get("flag", False))
|
||
|
||
# 验证进度信息
|
||
progress_result = progress_json.get("result", {})
|
||
if isinstance(progress_result, dict):
|
||
self.assertIn("status", progress_result)
|
||
self.assertIn("progress", progress_result)
|
||
logger.info(f"导出任务 {task_id} 状态: {progress_result.get('status')}, 进度: {progress_result.get('progress')}%")
|
||
except Exception as e:
|
||
logger.error(f"导出任务测试过程中发生错误: {str(e)}")
|
||
raise
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main() |