-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from mezdahun/feature/enhance-motor-control
Feature/enhance motor control
- Loading branch information
Showing
10 changed files
with
279 additions
and
129 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
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,36 @@ | ||
from unittest import TestCase, mock | ||
|
||
from dbus.exceptions import DBusException | ||
|
||
from visualswarm.control import motorinterface | ||
|
||
|
||
class MotorInterfaceTest(TestCase): | ||
|
||
def DBusException_raise(self, *args, **kwargs): | ||
raise DBusException | ||
|
||
def test_asebamedulla_health(self): | ||
network = mock.MagicMock() | ||
network.GetVariable.return_value = None | ||
|
||
# Case 1 : connection to thymio successful | ||
motorinterface.asebamedulla_health(network) | ||
network.GetVariable.assert_called_once_with("thymio-II", "acc", timeout=5) | ||
|
||
# Case 2 : DBusException is raised during communication | ||
network.GetVariable.reset_mock() | ||
network.GetVariable.side_effect = self.DBusException_raise | ||
self.assertEqual(motorinterface.asebamedulla_health(network), False) | ||
|
||
@mock.patch('os.system', return_value=None) | ||
@mock.patch('time.sleep', return_value=None) | ||
def test_asebamedulla_init(self, mock_sleep, mock_os): | ||
motorinterface.asebamedulla_init() | ||
mock_os.assert_called_once_with("(asebamedulla ser:name=Thymio-II &)") | ||
mock_sleep.assert_called_once_with(5) | ||
|
||
@mock.patch('os.system', return_value=None) | ||
def test_asebamedulla_end(self, mock_os): | ||
motorinterface.asebamedulla_end() | ||
mock_os.assert_called_once_with("pkill -f asebamedulla") |
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 @@ | ||
from unittest import TestCase, mock | ||
|
||
from visualswarm.control import motoroutput | ||
|
||
|
||
class MotorInterfaceTest(TestCase): | ||
|
||
@mock.patch('visualswarm.env.EXIT_CONDITION', True) | ||
@mock.patch('dbus.mainloop.glib.DBusGMainLoop', return_value=None) | ||
@mock.patch('dbus.SessionBus') | ||
@mock.patch('dbus.Interface') | ||
def test_control_thymio(self, mock_network_init, mock_dbus_sessionbus, mock_dbus_init): | ||
mock_network = mock.MagicMock(return_value=None) | ||
mock_network.SetVariable.return_value = None | ||
mock_network_init.return_value = mock_network | ||
|
||
mock_dbus_sessionbus.return_value.get_object.return_value = None | ||
control_stream = mock.MagicMock() | ||
control_stream.get.return_value = (1, 1) | ||
|
||
# Case 1 : healthy connection via asebamedulla | ||
with mock.patch('visualswarm.control.motorinterface.asebamedulla_health') as mock_health: | ||
mock_health.return_value = True | ||
# Case 1/a : with no control | ||
motoroutput.control_thymio(control_stream, with_control=False) | ||
control_stream.get.assert_called_once() | ||
|
||
control_stream.get.reset_mock() | ||
mock_health.reset_mock() | ||
# Case 1/b : with control | ||
motoroutput.control_thymio(control_stream, with_control=True) | ||
mock_dbus_init.assert_called_once_with(set_as_default=True) | ||
mock_dbus_sessionbus.assert_called_once() | ||
mock_network_init.assert_called_once() | ||
mock_health.assert_called_once() | ||
control_stream.get.assert_called_once() | ||
self.assertEqual(mock_network.SetVariable.call_count, 2) | ||
|
||
# Case 2 : unhealthy connection via asebamedulla | ||
with mock.patch('visualswarm.control.motorinterface.asebamedulla_health') as mock_health: | ||
with mock.patch('visualswarm.control.motorinterface.asebamedulla_end'): | ||
mock_health.return_value = False | ||
control_stream.reset_mock() | ||
motoroutput.control_thymio(control_stream, with_control=False) | ||
control_stream.get.assert_called_once() | ||
|
||
with self.assertRaises(Exception): | ||
motoroutput.control_thymio(control_stream, with_control=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
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
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
Oops, something went wrong.