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_validate_arguments(self): init_args = { 'args': [{ 'name': 'arg1', }, { 'name': 'arg2', }], 'workflow': 'some workflow' } args_to_validate = { 'arg1': 'something', } start_command = StartCommand('some name', init_args) # Default case. start_command.validate_arguments(args_to_validate) start_command.args = [ StartCommandArgument({ 'name': 'arg1', 'required': True, }), StartCommandArgument({ 'name': 'arg2', 'required': True, }) ] # Missing required argument with self.assertRaisesRegex(InvalidArgumentsException, 'Argument arg2 is required'): start_command.validate_arguments(args_to_validate) args_to_validate = {'arg1': 'something', 'arg2': ''} # Present but empty required argument with self.assertRaisesRegex(InvalidArgumentsException, 'Argument arg2 is required'): start_command.validate_arguments(args_to_validate) start_command.args = [ StartCommandArgument({ 'name': 'arg1', 'choices': ['c1', 'c2'] }) ] # Invalid choice with self.assertRaisesRegex(InvalidArgumentsException, 'Argument arg1 must be one of'): start_command.validate_arguments(args_to_validate) args_to_validate = {'arg1': 'c1'} # Valid choice start_command.validate_arguments(args_to_validate)
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_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_init_minimal(self): expected = { 'name': 'some name', 'description': '', 'required': False, 'default': None, 'choices': None, } sca = StartCommandArgument({'name': 'some name'}) for k, v in expected.items(): self.assertEqual(v, getattr(sca, k))
def test_init(self): init_args = { 'name': 'some name', 'description': 'some description', 'required': True, 'default': 'the default', 'choices': ['A', 'B', 'C'], 'named': True } sca = StartCommandArgument(init_args) for k, v in init_args.items(): self.assertEqual(v, getattr(sca, k))
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_missing_name(self): with self.assertRaises(AssertionError): StartCommandArgument({})