first commit
This commit is contained in:
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