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 _mock_start_commands(self): mock = MagicMock() mock.get_all.return_value = { 'command_a': StartCommand('command_a', { 'description': 'Description of command A', 'workflow': 'wf a' }), 'command_b': StartCommand('command_b', { 'description': 'Description of command B', 'workflow': 'wf b' }), } return mock
def test_init_args(self, mock_argument): init_args = {'workflow': 'some workflow', 'args': ['arg1', 'arg2']} StartCommand('some name', init_args) mock_argument.assert_has_calls([ call('arg1'), call('arg2'), ])
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_init(self, mock_argument): init_args = { 'description': 'some description', 'workflow': 'some workflow', 'start_step': 'some start step' } start_command = StartCommand('some name', init_args) self.assertEqual(start_command.name, 'some name') for k, v in init_args.items(): self.assertEqual(v, getattr(start_command, k))
def test_init_minimal(self): expected_args = { 'name': 'some name', 'description': '', 'workflow': 'some workflow', 'start_step': None, 'args': [], } start_command = StartCommand('some name', {'workflow': 'some workflow'}) for k, v in expected_args.items(): self.assertEqual(v, getattr(start_command, k))
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_init_missing_workflow(self): with self.assertRaises(AssertionError): StartCommand('some name', {})