mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
feat: Refactor binary commands and apply BLE fixes
Refactored the BinaryCommandHandler to align with the other command handlers, inheriting from CommandHandlerBase. This resolves an AttributeError and simplifies the command structure. Moved binary_commands.py into the commands module. Applied fixes to the BLE connection handler based on feedback, improving reliability on macOS and ensuring the device address is correctly handled.
This commit is contained in:
parent
c19fd166f8
commit
36727f4ea3
22 changed files with 1603 additions and 1206 deletions
|
|
@ -2,10 +2,15 @@ import asyncio
|
|||
import unittest
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from meshcore.ble_cx import BLEConnection, UART_SERVICE_UUID, UART_TX_CHAR_UUID, UART_RX_CHAR_UUID
|
||||
from meshcore.ble_cx import (
|
||||
BLEConnection,
|
||||
UART_TX_CHAR_UUID,
|
||||
UART_RX_CHAR_UUID,
|
||||
)
|
||||
|
||||
|
||||
class TestBLEConnection(unittest.TestCase):
|
||||
@patch('meshcore.ble_cx.BleakClient')
|
||||
@patch("meshcore.ble_cx.BleakClient")
|
||||
def test_ble_connection_and_disconnection(self, mock_bleak_client):
|
||||
"""
|
||||
Tests the BLEConnection class for connecting and disconnecting from a BLE device.
|
||||
|
|
@ -13,7 +18,7 @@ class TestBLEConnection(unittest.TestCase):
|
|||
# Arrange
|
||||
mock_client_instance = self._get_mock_bleak_client()
|
||||
mock_bleak_client.return_value = mock_client_instance
|
||||
|
||||
|
||||
address = "00:11:22:33:44:55"
|
||||
ble_conn = BLEConnection(address=address)
|
||||
|
||||
|
|
@ -23,10 +28,12 @@ class TestBLEConnection(unittest.TestCase):
|
|||
|
||||
# Assert
|
||||
mock_client_instance.connect.assert_called_once()
|
||||
mock_client_instance.start_notify.assert_called_once_with(UART_TX_CHAR_UUID, ble_conn.handle_rx)
|
||||
mock_client_instance.start_notify.assert_called_once_with(
|
||||
UART_TX_CHAR_UUID, ble_conn.handle_rx
|
||||
)
|
||||
mock_client_instance.disconnect.assert_called_once()
|
||||
|
||||
@patch('meshcore.ble_cx.BleakClient')
|
||||
@patch("meshcore.ble_cx.BleakClient")
|
||||
def test_send_data(self, mock_bleak_client):
|
||||
"""
|
||||
Tests the send method of the BLEConnection class.
|
||||
|
|
@ -34,7 +41,7 @@ class TestBLEConnection(unittest.TestCase):
|
|||
# Arrange
|
||||
mock_client_instance = self._get_mock_bleak_client()
|
||||
mock_bleak_client.return_value = mock_client_instance
|
||||
|
||||
|
||||
address = "00:11:22:33:44:55"
|
||||
ble_conn = BLEConnection(address=address)
|
||||
asyncio.run(ble_conn.connect())
|
||||
|
|
@ -44,7 +51,9 @@ class TestBLEConnection(unittest.TestCase):
|
|||
asyncio.run(ble_conn.send(data_to_send))
|
||||
|
||||
# Assert
|
||||
ble_conn.rx_char.write_gatt_char.assert_called_once_with(ble_conn.rx_char, data_to_send, response=False)
|
||||
ble_conn.rx_char.write_gatt_char.assert_called_once_with(
|
||||
ble_conn.rx_char, data_to_send, response=False
|
||||
)
|
||||
|
||||
def _get_mock_bleak_client(self):
|
||||
"""
|
||||
|
|
@ -60,12 +69,13 @@ class TestBLEConnection(unittest.TestCase):
|
|||
mock_service = MagicMock()
|
||||
mock_char = MagicMock()
|
||||
mock_char.uuid = UART_RX_CHAR_UUID
|
||||
mock_char.write_gatt_char = mock_client.write_gatt_char
|
||||
|
||||
mock_char.write_gatt_char = mock_client.write_gatt_char
|
||||
|
||||
mock_service.get_characteristic.return_value = mock_char
|
||||
mock_client.services.get_service.return_value = mock_service
|
||||
|
||||
|
||||
return mock_client
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue