|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# || ____ _ __ |
| 4 | +# +------+ / __ )(_) /_______________ _____ ___ |
| 5 | +# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ |
| 6 | +# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ |
| 7 | +# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ |
| 8 | +# |
| 9 | +# Copyright (C) 2018 Bitcraze AB |
| 10 | +# |
| 11 | +# This program is free software; you can redistribute it and/or |
| 12 | +# modify it under the terms of the GNU General Public License |
| 13 | +# as published by the Free Software Foundation; either version 2 |
| 14 | +# of the License, or (at your option) any later version. |
| 15 | +# |
| 16 | +# This program is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +import unittest |
| 23 | +from threading import Event |
| 24 | +from unittest.mock import MagicMock |
| 25 | +from unittest.mock import patch |
| 26 | + |
| 27 | +from cflib.crazyflie import Crazyflie |
| 28 | +from cflib.crazyflie.syncCrazyflie import SyncCrazyflie |
| 29 | +from cflib.utils.param_file_helper import ParamFileHelper |
| 30 | + |
| 31 | + |
| 32 | +class ParamFileHelperTests(unittest.TestCase): |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + self.cf_mock = MagicMock(spec=Crazyflie) |
| 36 | + self.helper = ParamFileHelper(self.cf_mock) |
| 37 | + |
| 38 | + def test_ParamFileHelper_SyncCrazyflieAsParam_ThrowsException(self): |
| 39 | + cf_mock = MagicMock(spec=SyncCrazyflie) |
| 40 | + helper = None |
| 41 | + try: |
| 42 | + helper = ParamFileHelper(cf_mock) |
| 43 | + except Exception: |
| 44 | + self.assertIsNone(helper) |
| 45 | + else: |
| 46 | + self.fail('Expect exception') |
| 47 | + |
| 48 | + def test_ParamFileHelper_Crazyflie_Object(self): |
| 49 | + helper = ParamFileHelper(self.cf_mock) |
| 50 | + self.assertIsNotNone(helper) |
| 51 | + |
| 52 | + @patch('cflib.crazyflie.Param') |
| 53 | + def test_ParamFileHelper_writesAndStoresParamFromFileToCrazyflie(self, mock_Param): |
| 54 | + # Setup |
| 55 | + cf_mock = MagicMock(spec=Crazyflie) |
| 56 | + cf_mock.param = mock_Param |
| 57 | + helper = ParamFileHelper(cf_mock) |
| 58 | + # Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world |
| 59 | + |
| 60 | + def mock_wait(self, timeout=None): |
| 61 | + helper._persistent_stored_callback('activeMarker.back', True) |
| 62 | + return |
| 63 | + |
| 64 | + with patch.object(Event, 'wait', new=mock_wait): |
| 65 | + self.assertTrue(helper.store_params_from_file('test/utils/fixtures/single_param.yaml')) |
| 66 | + mock_Param.set_value.assert_called_once_with('activeMarker.back', 10) |
| 67 | + mock_Param.persistent_store.assert_called_once_with('activeMarker.back', helper._persistent_stored_callback) |
| 68 | + |
| 69 | + @patch('cflib.crazyflie.Param') |
| 70 | + def test_ParamFileHelper_writesParamAndFailsToSetPersistantShouldReturnFalse(self, mock_Param): |
| 71 | + # Setup |
| 72 | + cf_mock = MagicMock(spec=Crazyflie) |
| 73 | + cf_mock.param = mock_Param |
| 74 | + helper = ParamFileHelper(cf_mock) |
| 75 | + # Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world |
| 76 | + |
| 77 | + def mock_wait(self, timeout=None): |
| 78 | + helper._persistent_stored_callback('activeMarker.back', False) |
| 79 | + return |
| 80 | + |
| 81 | + with patch.object(Event, 'wait', new=mock_wait): |
| 82 | + self.assertFalse(helper.store_params_from_file('test/utils/fixtures/single_param.yaml')) |
| 83 | + mock_Param.set_value.assert_called_once_with('activeMarker.back', 10) |
| 84 | + mock_Param.persistent_store.assert_called_once_with('activeMarker.back', helper._persistent_stored_callback) |
| 85 | + |
| 86 | + @patch('cflib.crazyflie.Param') |
| 87 | + def test_ParamFileHelper_TryWriteSeveralParamsPersistantShouldBreakAndReturnFalse(self, mock_Param): |
| 88 | + # Setup |
| 89 | + cf_mock = MagicMock(spec=Crazyflie) |
| 90 | + cf_mock.param = mock_Param |
| 91 | + helper = ParamFileHelper(cf_mock) |
| 92 | + # Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world |
| 93 | + |
| 94 | + def mock_wait(self, timeout=None): |
| 95 | + helper._persistent_stored_callback('activeMarker.back', False) |
| 96 | + return |
| 97 | + |
| 98 | + with patch.object(Event, 'wait', new=mock_wait): |
| 99 | + # Test and assert |
| 100 | + self.assertFalse(helper.store_params_from_file('test/utils/fixtures/five_params.yaml')) |
| 101 | + # Assert it breaks directly by checking number of calls |
| 102 | + mock_Param.set_value.assert_called_once_with('activeMarker.back', 10) |
| 103 | + mock_Param.persistent_store.assert_called_once_with('activeMarker.back', helper._persistent_stored_callback) |
| 104 | + |
| 105 | + @patch('cflib.crazyflie.Param') |
| 106 | + def test_ParamFileHelper_writesAndStoresAllParamsFromFileToCrazyflie(self, mock_Param): |
| 107 | + # Setup |
| 108 | + cf_mock = MagicMock(spec=Crazyflie) |
| 109 | + cf_mock.param = mock_Param |
| 110 | + helper = ParamFileHelper(cf_mock) |
| 111 | + # Mock blocking wait and call callback instead. This lets the flow work as it would in the asynch world |
| 112 | + |
| 113 | + def mock_wait(self, timeout=None): |
| 114 | + helper._persistent_stored_callback('something', True) |
| 115 | + return |
| 116 | + with patch.object(Event, 'wait', new=mock_wait): |
| 117 | + # Test and Assert |
| 118 | + self.assertTrue(helper.store_params_from_file('test/utils/fixtures/five_params.yaml')) |
| 119 | + self.assertEquals(5, len(mock_Param.set_value.mock_calls)) |
| 120 | + self.assertEquals(5, len(mock_Param.persistent_store.mock_calls)) |
0 commit comments