first commit
This commit is contained in:
BIN
tests/test_control/__pycache__/test_feeding.cpython-39.pyc
Normal file
BIN
tests/test_control/__pycache__/test_feeding.cpython-39.pyc
Normal file
Binary file not shown.
3
tests/test_control/logs/app.log
Normal file
3
tests/test_control/logs/app.log
Normal file
@ -0,0 +1,3 @@
|
||||
2025-09-12 19:40:24,636 - FeedingControl.FeedingController - INFO - <20>豸<EFBFBD><E8B1B8>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
2025-09-12 19:40:27,379 - FeedingControl.FeedingController - INFO - <20>豸<EFBFBD><E8B1B8>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
2025-09-12 19:40:31,216 - FeedingControl.FeedingController - INFO - <20>豸<EFBFBD><E8B1B8>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
75
tests/test_control/test_feeding.py
Normal file
75
tests/test_control/test_feeding.py
Normal file
@ -0,0 +1,75 @@
|
||||
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()
|
||||
BIN
tests/test_devices/__pycache__/test_relay.cpython-39.pyc
Normal file
BIN
tests/test_devices/__pycache__/test_relay.cpython-39.pyc
Normal file
Binary file not shown.
1
tests/test_devices/logs/app.log
Normal file
1
tests/test_devices/logs/app.log
Normal file
@ -0,0 +1 @@
|
||||
2025-09-13 10:37:43,979 - FeedingControl.RelayController - INFO - <20><><EFBFBD>Ƽ̵<C6BC><CCB5><EFBFBD> door_upper (0) open
|
||||
48
tests/test_devices/test_relay.py
Normal file
48
tests/test_devices/test_relay.py
Normal file
@ -0,0 +1,48 @@
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加src目录到Python路径
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'src'))
|
||||
|
||||
from src.divices.relay import RelayController
|
||||
|
||||
|
||||
class TestRelayController(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.relay = RelayController('192.168.0.18', 50000)
|
||||
self.relay.set_device_mapping({
|
||||
'door_upper': 0,
|
||||
'door_lower_1': 1,
|
||||
'door_lower_2': 2,
|
||||
'break_arch_upper': 3,
|
||||
'break_arch_lower': 4
|
||||
})
|
||||
|
||||
@patch('devices.relay.socket.socket')
|
||||
def test_send_command_success(self, mock_socket):
|
||||
# 模拟成功的socket通信
|
||||
mock_socket_instance = MagicMock()
|
||||
mock_socket.return_value.__enter__.return_value = mock_socket_instance
|
||||
mock_socket_instance.recv.return_value = b'\x00\x00\x00\x00\x00\x06\x01\x05\x00\x00\xff\x00'
|
||||
|
||||
result = self.relay.send_command('00000000000601050000FF00')
|
||||
self.assertIsNotNone(result)
|
||||
|
||||
def test_control_valid_device_and_action(self):
|
||||
with patch.object(self.relay, 'send_command') as mock_send:
|
||||
mock_send.return_value = b'some_response'
|
||||
result = self.relay.control('door_upper', 'open')
|
||||
self.assertTrue(result)
|
||||
|
||||
def test_control_invalid_device(self):
|
||||
with patch.object(self.relay, 'send_command') as mock_send:
|
||||
result = self.relay.control('invalid_device', 'open')
|
||||
self.assertFalse(result)
|
||||
mock_send.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user