def test_parse_argument_with_user(self, mock_execute, mock_start_commands, mock_parser): wfc = WorkflowCommands() mock_parser.return_value = MockArgumentParser() MockArgumentParser.arguments['arg1'] = 'val1' MockArgumentParser.arguments['arg2'] = 'val2' MockArgumentParser.arguments['user'] = '******' wfc._extract_parser_arg_kwargs = MagicMock() start_command = StartCommand('command', {'workflow': 'theworkflow'}) start_command.args = [ StartCommandArgument({'name': 'arg1'}), StartCommandArgument({'name': 'user'}), StartCommandArgument({ 'name': 'arg2', 'named': True }) ] result = wfc._parse_arguments(start_command) self.assertEqual({ 'arg1': 'val1', 'arg2': 'val2', 'user': '******' }, result) # Expect a named argument has been added self.assertTrue(MockArgumentParser.named_argument_added)
def test_extract_parser_arg_kwargs_maximal(self, mock_execute, mock_start_commands, mock_parser): wfc = WorkflowCommands() start_command_arg = StartCommandArgument({ 'name': 'command name', 'default': 'default value', 'choices': ['a', 'b'], 'required': True, 'description': 'some description' }) expected_result = { 'type': str, 'help': 'some description', 'default': 'default value', 'choices': ['a', 'b'] } self.assertEqual(expected_result, wfc._extract_parser_arg_kwargs(start_command_arg))
def test_execute_command_without_step(self, mock_workflow, mock_start_commands, mock_parser): wfc = WorkflowCommands() wfc._parse_arguments = MagicMock() start_command = StartCommand('command', {'workflow': 'theworkflow'}) wfc.execute_command(start_command) mock_workflow.assert_called_with('theworkflow') mock_workflow.return_value.start_new.assert_called_with( wfc._parse_arguments.return_value)
def test_extract_parser_arg_kwargs_minimal(self, mock_execute, mock_start_commands, mock_parser): wfc = WorkflowCommands() start_command_arg = StartCommandArgument({'name': 'command name'}) expected_result = { 'type': str, 'help': '', 'nargs': '?', } self.assertEqual(expected_result, wfc._extract_parser_arg_kwargs(start_command_arg))
def test_extract_parser_arguments_store_true(self, mock_execute, mock_start_commandc, mock_parser): wfc = WorkflowCommands() start_command_arg = StartCommandArgument({ 'name': 'command name', 'default': 'default value', 'choices': ['a', 'b'], 'required': True, 'action': 'store_true', }) expected_result = {'help': '', 'action': 'store_true'} self.assertEqual(expected_result, wfc._extract_parser_arg_kwargs(start_command_arg))
def test_info(self, mock_start_commands, mock_argparse): mock_args = MagicMock() mock_parser = MagicMock() mock_argparse.ArgumentParser.return_value = mock_parser mock_parser.parse_args.return_value = mock_args mock_args.command = 'info' wfc = WorkflowCommands() mock_start_commands.get.assert_not_called()
def test_init_invalid_command(self, mock_start_commands, mock_parser): mock_start_commands.return_value = self._mock_start_commands() mock_start_commands.return_value.get.side_effect = NoSuchCommandException mock_parser.return_value = MockArgumentParser() MockArgumentParser.arguments['command'] = 'nonexistent' with self.assertRaises(SystemExit) as cm: WorkflowCommands() self.assertEqual(cm.exception.code, 1)
def test_init(self, mock_execute, mock_start_commands, mock_parser): mock_start_commands.return_value = self._mock_start_commands() mock_parser.return_value = MockArgumentParser() MockArgumentParser.arguments['command'] = 'command_a' WorkflowCommands() args, kwargs = mock_parser.call_args self.assertTrue( f"{'command_a':16s}Description of command A" in kwargs['usage']) self.assertTrue( f"{'command_b':16s}Description of command B" in kwargs['usage']) mock_start_commands.return_value.get.assert_called_with('command_a') mock_execute.assert_called_with( mock_start_commands.return_value.get.return_value)