148 lines
3.8 KiB
Python
148 lines
3.8 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试客户机器环境和依赖包
|
||
"""
|
||
|
||
import sys
|
||
import os
|
||
from pathlib import Path
|
||
|
||
def test_python_version():
|
||
"""测试Python版本"""
|
||
print(f"Python版本: {sys.version}")
|
||
print(f"Python路径: {sys.executable}")
|
||
|
||
def test_influxdb_client():
|
||
"""测试InfluxDB客户端"""
|
||
try:
|
||
import influxdb_client
|
||
print(f"✓ influxdb_client 已安装,版本: {influxdb_client.__version__}")
|
||
return True
|
||
except ImportError as e:
|
||
print(f"✗ influxdb_client 未安装: {e}")
|
||
return False
|
||
|
||
def test_pandas():
|
||
"""测试pandas"""
|
||
try:
|
||
import pandas as pd
|
||
print(f"✓ pandas 已安装,版本: {pd.__version__}")
|
||
return True
|
||
except ImportError as e:
|
||
print(f"✗ pandas 未安装: {e}")
|
||
return False
|
||
|
||
def test_other_dependencies():
|
||
"""测试其他依赖"""
|
||
dependencies = [
|
||
'json',
|
||
'datetime',
|
||
'logging',
|
||
'os',
|
||
'sys'
|
||
]
|
||
|
||
for dep in dependencies:
|
||
try:
|
||
__import__(dep)
|
||
print(f"✓ {dep} 可用")
|
||
except ImportError as e:
|
||
print(f"✗ {dep} 不可用: {e}")
|
||
|
||
def test_environment_variables():
|
||
"""测试环境变量"""
|
||
required_vars = [
|
||
'INFLUX_URL',
|
||
'INFLUX_ORG',
|
||
'INFLUX_TOKEN',
|
||
'INFLUX_BUCKET',
|
||
'INFLUX_MEASUREMENT',
|
||
'EXPERIMENT_START',
|
||
'EXPERIMENT_END'
|
||
]
|
||
|
||
print("\n环境变量检查:")
|
||
all_set = True
|
||
for var in required_vars:
|
||
value = os.environ.get(var)
|
||
if value:
|
||
# 隐藏敏感信息
|
||
if 'TOKEN' in var and len(value) > 8:
|
||
display_value = f"{value[:4]}****"
|
||
else:
|
||
display_value = value
|
||
print(f"✓ {var} = {display_value}")
|
||
else:
|
||
print(f"✗ {var} 未设置")
|
||
all_set = False
|
||
|
||
return all_set
|
||
|
||
def create_mock_temperature_data():
|
||
"""创建模拟温度数据用于测试"""
|
||
mock_data = {
|
||
"tables": [{
|
||
"token": "scriptTable1",
|
||
"startRow": 0,
|
||
"startCol": 0,
|
||
"cells": [
|
||
{"row": 1, "col": 1, "value": "2025-11-27 11:30:00"},
|
||
{"row": 1, "col": 3, "value": "2025-11-27 15:00:00"},
|
||
{"row": 0, "col": 1, "value": "25.5"}, # 模拟环境温度
|
||
{"row": 4, "col": 0, "value": "45.2"}, # 模拟主轴承温度
|
||
{"row": 4, "col": 1, "value": "46.1"},
|
||
{"row": 5, "col": 0, "value": "44.8"},
|
||
]
|
||
}]
|
||
}
|
||
|
||
import json
|
||
print("\n模拟数据 (用于测试报告生成):")
|
||
print(json.dumps(mock_data, ensure_ascii=False, indent=2))
|
||
return mock_data
|
||
|
||
def main():
|
||
print("客户机器环境测试")
|
||
print("=" * 50)
|
||
|
||
# 测试Python环境
|
||
test_python_version()
|
||
print()
|
||
|
||
# 测试依赖包
|
||
print("依赖包检查:")
|
||
influx_ok = test_influxdb_client()
|
||
pandas_ok = test_pandas()
|
||
test_other_dependencies()
|
||
print()
|
||
|
||
# 测试环境变量
|
||
env_ok = test_environment_variables()
|
||
print()
|
||
|
||
# 创建模拟数据
|
||
mock_data = create_mock_temperature_data()
|
||
|
||
# 总结
|
||
print("\n" + "=" * 50)
|
||
print("总结:")
|
||
|
||
if not influx_ok:
|
||
print("❌ 需要安装 influxdb-client:")
|
||
print(" pip install influxdb-client==1.43.0")
|
||
|
||
if not pandas_ok:
|
||
print("❌ 需要安装 pandas:")
|
||
print(" pip install pandas")
|
||
|
||
if not env_ok:
|
||
print("❌ 需要设置所有必需的环境变量")
|
||
|
||
if influx_ok and pandas_ok and env_ok:
|
||
print("✅ 环境配置正确,可以运行temperature_table.py")
|
||
else:
|
||
print("⚠️ 环境配置不完整,但可以使用模拟数据测试报告生成")
|
||
|
||
if __name__ == "__main__":
|
||
main()
|