def test_save(self, mock_communicator, mock_client, test_input, expected): mock_communicator.send_message = MagicMock() module = IqModule(mock_communicator) client_entry_name = test_input module.save(self._CLIENT_NAME, client_entry_name) assert mock_communicator.send_message.call_args == call( bytearray(expected))
def test_add_client(self, mock_communicator, mock_client): module = IqModule(mock_communicator) client_file_path = os.path.join(os.path.dirname(__file__), ("client_files/extra_client.json")) module.add_client(client_file_path) assert "extra_client" in module._client_dict.keys()
def test_save_all(self, mock_communicator, mock_client): module = IqModule(mock_communicator) module.save = MagicMock() module.save.return_value = "test" num_client_entries = len(mock_client.client_entries) module.save_all(self._CLIENT_NAME) assert module.save.call_count == num_client_entries
def test_wrong_client_entry(self, mock_communicator, mock_client): module = IqModule(mock_communicator) client_name = self._CLIENT_NAME client_entry_name = "fake_client_entry" with pytest.raises(IqModuleError) as err: module.set(client_name, client_entry_name, 0) err_str = err.value.message assert ( "IQ MODULE ERROR: {0} does not support this client entry: {1}\n\n". format(client_name, client_entry_name) == err_str)
def test_update_reply(self, mock_communicator, mock_client): mock_communicator.read_bytes = MagicMock() mock_communicator.extract_message = MagicMock() mock_communicator.extract_message.side_effect = [ None, bytearray([1, 2, 3]) ] module = IqModule(mock_communicator) module.update_reply() assert mock_communicator.read_bytes.call_count == 1 assert mock_communicator.extract_message.call_count == 2
def test_get_retry_fail(self, mock_communicator, mock_client, test_input, expected): client_entry_name = test_input[0] module = IqModule(mock_communicator) module.get = MagicMock() module.get.return_value = None retries = 5 _ = module.get_retry("client_test", client_entry_name, retries=retries) assert module.get.call_count == retries
def test_get(self, mock_communicator, mock_client, test_input, expected): client_entry_name = test_input[0] valid_message = test_input[1] mock_communicator.read_bytes = MagicMock() mock_communicator.extract_message = MagicMock() mock_communicator.extract_message.side_effect = [ bytearray(valid_message), None ] module = IqModule(mock_communicator) assert module.get("client_test", client_entry_name) == expected
def test_add_client_fail(self, mock_communicator, mock_client): module = IqModule(mock_communicator) client_file_path = os.path.join(os.path.dirname(__file__), ("client_files/extra_client")) with pytest.raises(IqModuleError) as err: module.add_client(client_file_path) err_str = err.value.message assert ( err_str == f"IQ MODULE ERROR: Path does not lead to a json file: {client_file_path}\n" )
def test_clients_path(self, mock_communicator, mock_client): client_file_path = os.path.join(os.path.dirname(__file__), ("extra_client_files/")) module = IqModule(mock_communicator, clients_path=client_file_path) assert "extra_client" in module._client_dict.keys()
def test_set_retry_save(self, mock_communicator, mock_client, test_input, expected): mock_communicator.send_message = MagicMock() module = IqModule(mock_communicator) module.save = MagicMock() module.get = MagicMock() client_entry_name = test_input[0] value = test_input[1] if not value: module.get.return_value = 1 module.set_verify(self._CLIENT_NAME, client_entry_name, save=1) else: module.get.return_value = value module.set_verify(self._CLIENT_NAME, client_entry_name, value, save=1) assert module.save.call_args == call(self._CLIENT_NAME, client_entry_name)
def test_is_fresh(self, mock_communicator, mock_client, test_input): client_entry_name = test_input[0][0] valid_message = test_input[0][1] mock_communicator.read_bytes = MagicMock() mock_communicator.extract_message = MagicMock() mock_communicator.extract_message.side_effect = [ bytearray(valid_message), None ] module = IqModule(mock_communicator) assert not module.is_fresh(self._CLIENT_NAME, client_entry_name) module.update_replies() assert module.is_fresh(self._CLIENT_NAME, client_entry_name)
def test_set_retry_fail(self, mock_communicator, mock_client, test_input, expected): mock_communicator.send_message = MagicMock() module = IqModule(mock_communicator) module.get = MagicMock() client_entry_name = test_input[0] value = test_input[1] if not value: module.get.return_value = 0 success = module.set_verify(self._CLIENT_NAME, client_entry_name) else: module.get.return_value = None success = module.set_verify(self._CLIENT_NAME, client_entry_name, value) # two points of failure assert not success module.get.return_value = value - 1 success = module.set_verify(self._CLIENT_NAME, client_entry_name, value) assert not success
def test_flush_input_buffer(self, mock_communicator, mock_client): mock_communicator.flush_input_buffer = MagicMock() module = IqModule(mock_communicator) module.flush_input_com_buffer() assert mock_communicator.flush_input_buffer.called
def test_get_timeout(self, mock_communicator, mock_client, test_input): client_entry_name = test_input[0][0] module = IqModule(mock_communicator) assert module.get("client_test", client_entry_name) is None
def test_coast(self, mock_communicator, mock_client): module = IqModule(mock_communicator) module.set = MagicMock() module.coast() assert module.set.called