-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import asyncio | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from nervous_sensors.connection_manager import ConnectionManager | ||
from nervous_sensors.nervous_sensor import NervousSensor | ||
|
||
|
||
@pytest.fixture | ||
def mock_nervous_sensor1(): | ||
mock_sensor = Mock(spec=NervousSensor) | ||
mock_sensor.is_connected.return_value = False | ||
return mock_sensor | ||
|
||
|
||
@pytest.fixture | ||
def mock_nervous_sensor2(): | ||
mock_sensor = Mock(spec=NervousSensor) | ||
mock_sensor.is_connected.return_value = False | ||
return mock_sensor | ||
|
||
|
||
@pytest.fixture | ||
def mock_nervous_sensor3(): | ||
mock_sensor = Mock(spec=NervousSensor) | ||
mock_sensor.is_connected.return_value = False | ||
return mock_sensor | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_start_connection(mock_nervous_sensor1, mock_nervous_sensor2, mock_nervous_sensor3): | ||
manager = ConnectionManager(sensor_names=[], gui=False, folder=False, lsl=False, parallel_connection_authorized=3) | ||
sensors = [mock_nervous_sensor1, mock_nervous_sensor2, mock_nervous_sensor3] | ||
manager._sensors = sensors | ||
|
||
try: | ||
await asyncio.wait_for(manager.start(), timeout=1) | ||
except asyncio.TimeoutError: | ||
pass | ||
|
||
for mock_sensor in sensors: | ||
assert mock_sensor.connect.call_count >= 1 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_stop_all_notifications(): | ||
assert True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import asyncio | ||
from asyncio import TaskGroup | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from bleak import BleakClient, BleakScanner, BLEDevice | ||
|
||
from nervous_sensors.connection_manager import ConnectionManager | ||
from nervous_sensors.nervous_sensor import NervousSensor | ||
|
||
|
||
@pytest.fixture | ||
def mock_bleak_client(): | ||
return Mock(spec=BleakClient) | ||
|
||
|
||
@pytest.fixture | ||
def mock_bleak_scanner(): | ||
return Mock(spec=BleakScanner) | ||
|
||
|
||
@pytest.fixture | ||
def mock_bleak_device(): | ||
return Mock(spec=BLEDevice) | ||
|
||
|
||
@pytest.fixture | ||
def mock_connection_manager(): | ||
return Mock(spec=ConnectionManager) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def tes_connect(mock_connection_manager, mock_bleak_client, mock_bleak_scanner, mock_bleak_device): | ||
sensor = NervousSensor("ECG73BA", "2023-03-01 12:00:00", 10, mock_connection_manager) | ||
sensor._client = mock_bleak_client | ||
sensor._scanner = mock_bleak_scanner | ||
|
||
mock_bleak_scanner.find_device_by_name.return_value = mock_bleak_device | ||
mock_bleak_client.connect.return_value = True | ||
|
||
async def disconnection_event(): | ||
await asyncio.sleep(1) | ||
await sensor.disconnect() | ||
|
||
async with TaskGroup() as tg: | ||
tg.create_task(sensor.connect()) | ||
tg.create_task(disconnection_event()) | ||
|
||
mock_connection_manager.on_sensor_connect.assert_called_once_with(sensor) | ||
mock_connection_manager.on_sensor_fail_to_connect.assert_not_called() | ||
mock_connection_manager.on_sensor_disconnect.assert_called_once_with(sensor) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def tes_connect_fail(mock_connection_manager, mock_bleak_client, mock_bleak_scanner): | ||
sensor = NervousSensor("ECG73BA", "2023-03-01 12:00:00", 10, mock_connection_manager) | ||
sensor._client = mock_bleak_client | ||
sensor._scanner = mock_bleak_scanner | ||
|
||
mock_bleak_scanner.find_device_by_name.return_value = None | ||
mock_bleak_client.connect.return_value = False | ||
await sensor.connect() | ||
|
||
mock_connection_manager.on_sensor_connect.assert_not_called() | ||
mock_connection_manager.on_sensor_fail_to_connect.assert_called_once_with(sensor) | ||
mock_connection_manager.on_sensor_disconnect.assert_not_called() |