75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
import unittest
|
|
from unittest.mock import patch, MagicMock
|
|
import sys
|
|
import os
|
|
|
|
from src.control.feeding_controller import FeedingController
|
|
|
|
# 添加src目录到Python路径
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
|
|
|
|
|
|
from src.control.state_machine import FeedingState
|
|
|
|
|
|
class TestFeedingController(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
# Mock配置
|
|
self.mock_config = {
|
|
'network': {
|
|
'relay': {'host': '192.168.0.18', 'port': 50000},
|
|
'inverter': {'host': '192.168.0.20', 'port': 502, 'slave_id': 1},
|
|
'transmitters': {
|
|
'upper': {'host': '192.168.0.21', 'port': 502, 'slave_id': 1, 'weight_register': 0,
|
|
'register_count': 2},
|
|
'lower': {'host': '192.168.0.22', 'port': 502, 'slave_id': 2, 'weight_register': 0,
|
|
'register_count': 2}
|
|
}
|
|
},
|
|
'parameters': {
|
|
'min_required_weight': 50,
|
|
'max_error_count': 3,
|
|
'monitoring_interval': 1,
|
|
'timeout': 30
|
|
},
|
|
'feeding': {
|
|
'stage1_frequency': 30.0,
|
|
'stage2_frequency': 40.0,
|
|
'stage3_frequency': 50.0,
|
|
'target_weight_per_stage': 33.3
|
|
},
|
|
'relay': {
|
|
'door_upper': 0,
|
|
'door_lower_1': 1,
|
|
'door_lower_2': 2,
|
|
'break_arch_upper': 3,
|
|
'break_arch_lower': 4
|
|
}
|
|
}
|
|
|
|
@patch('control.feeding_controller.config')
|
|
def test_initialization(self, mock_config):
|
|
mock_config.get.side_effect = lambda key, default=None: self._get_nested_config(key, default)
|
|
|
|
with patch('control.feeding_controller.RelayController') as mock_relay, \
|
|
patch('control.feeding_controller.InverterController') as mock_inverter, \
|
|
patch('control.feeding_controller.TransmitterController') as mock_transmitter:
|
|
controller = FeedingController()
|
|
self.assertIsNotNone(controller)
|
|
|
|
def _get_nested_config(self, key_path, default=None):
|
|
"""辅助函数:获取嵌套配置值"""
|
|
keys = key_path.split('.')
|
|
value = self.mock_config
|
|
|
|
try:
|
|
for key in keys:
|
|
value = value[key]
|
|
return value
|
|
except (KeyError, TypeError):
|
|
return default
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |