def test_should_return_false_if_other_has_at_least_one_different_argument(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['any_arg1', 'any_arg2', 'other_argument'], 'any_stdin') self.assertFalse(command_input1.fulfills(command_input2))
def test_should_return_true_if_other_has_equal_arguments(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['any_arg2', 'any_arg1'], 'any_stdin') self.assertTrue(command_input1.fulfills(command_input2))
def test_should_return_true_when_objects_are_not_equal_and_testing_if_not_equal(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'other_command', ['other_argument1', 'other_argument2'], 'other_stdin') self.assertTrue(command_input1 != command_input2, 'comparison: stdin')
def test_should_return_true_when_other_equal_command_and_arguments(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['any_arg2', 'any_arg1'], 'any_stdin') self.assertTrue(command_input1.fulfills( command_input2), 'comparison: command')
def test_should_return_false_when_objects_arguments_are_not_equal(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['other_argument1', 'any_arg2'], 'any_stdin') self.assertFalse( command_input1 == command_input2, 'comparison of arguments')
def test_should_return_false_when_objects_are_equal_and_testing_if_not_equal(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertFalse( command_input1 != command_input2, 'no difference, but not equal returned')
def test_should_return_false_when_other_has_different_stdin(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'other_stdin') self.assertFalse( command_input1.fulfills(command_input2), 'comparison: stdin')
def test_should_return_true_when_objects_are_equal(self): command_input1 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') command_input2 = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertTrue(command_input1 == command_input2, 'no difference, but not equal returned')
def test_should_create_object_with_given_properties(self): actual = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin', expected=True) self.assertEqual( CommandInput('any_command', ['any_arg1', 'any_arg2'], 'any_stdin'), actual.command_input) self.assertEqual(True, actual.expected)
def test_should_verify_at_least_one_argument_matches_using_and(self): command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') wrapper = CommandInputVerifier(command_input) self.assertEqual( wrapper.and_at_least_one_argument_matches, wrapper.at_least_one_argument_matches)
def test_should_verify_argument_when_argument_matches_given_pattern(self): command_input = CommandInput('command', ['arg1'], 'stdin') wrapper = CommandInputVerifier(command_input) actual_value = wrapper.at_least_one_argument_matches('^arg') self.assertEqual(wrapper, actual_value)
def test_should_raise_exception_when_no_argument_matches_given_string(self): command_input = CommandInput( 'command', ['arg1', 'arg2', 'arg3'], 'stdin') wrapper = CommandInputVerifier(command_input) self.assertRaises( VerificationException, wrapper.at_least_one_argument_matches, 'spameggs')
def test_should_raise_exception_when_given_input_is_different(self): command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') wrapper = CommandInputVerifier(command_input) self.assertRaises( VerificationException, wrapper.with_input, 'hello world')
def test_should_raise_exception_when_given_argument_is_not_execution(self): command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') wrapper = CommandInputVerifier(command_input) self.assertRaises( VerificationException, wrapper.at_least_with_arguments, '-arg0')
def test_should_create_object_with_given_properties(self): actual = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertEqual('any_command', actual.command) self.assertEqual(['any_arg1', 'any_arg2'], actual.arguments) self.assertEqual('any_stdin', actual.stdin)
def test_should_return_string_with_all_properties(self): given_object = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') actual_string = str(given_object) self.assertEqual( "CommandInput {'stdin': 'any_stdin', 'command': 'any_command', 'arguments': ['any_arg1', 'any_arg2']}", actual_string)
def test_should_verify_more_complex_pattern_given(self): command_input = CommandInput( 'command', ['arg1', 'borg', 'spam', '123abc'], 'stdin') wrapper = CommandInputVerifier(command_input) actual_value = wrapper.at_least_one_argument_matches('\d{3}[a-c]{3}') self.assertEqual(wrapper, actual_value)
def __init__(self, command, arguments, stdin, expected=False): """ initializes a new execution with the given properties. If arguments is not given it will be initialized as empty list. """ self.command_input = CommandInput(command, arguments, stdin) self.expected = expected
def test_should_verify_given_input(self): command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') wrapper = CommandInputVerifier(command_input) actual_value = wrapper.with_input('stdin') self.assertEqual(wrapper, actual_value)
def test_should_load_configured_stubs(self, mock_deserialize, mock_logging_info, mock_logging_error, mock_exit, mock_serialize, mock_unlock): command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.dispatch(command_input) self.assertEqual( call('shtub/stub-configurations'), mock_deserialize.call_args)
def test_should_convert_dictionary_to_object(self): values = {'command': 'any_command', 'arguments': ['any_arguments'], 'stdin': 'any_stdin'} actual = CommandInput.from_dictionary(values) self.assertEqual( CommandInput('any_command', ['any_arguments'], 'any_stdin'), actual)
def test_should_create_new_object_with_given_properties(self): actual = StubConfiguration('any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertEqual( CommandInput('any_command', ['any_arg1', 'any_arg2'], 'any_stdin'), actual.command_input) self.assertEqual([], actual.answers) self.assertEqual(0, actual.current_answer)
def test_should_dispatch_execution(self, mock_exists, mock_mkdir, mock_logging, mock_dispatch, mock_read_stdin, mock_lock): commandstub.handle_execution() mock_dispatch.assert_called() expected_input = str( CommandInput('command', ['-arg1', '-arg2', '-arg3'], stdin=None)) actual_execution = str(mock_dispatch.call_args[0][0]) self.assertEqual(expected_input, actual_execution)
def test_should_convert_object_to_dictionary(self): command_input = CommandInput( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') actual_dictionary = command_input.as_dictionary() expected_dictionary = {'command': 'any_command', 'arguments': ['any_arg1', 'any_arg2'], 'stdin': 'any_stdin'} self.assertEqual(expected_dictionary, actual_dictionary)
def test_should_exit_with_error_code_255_when_execution_not_in_stub_configuration( self, mock_deserialize, mock_logging_info, mock_logging_error, mock_exit, mock_serialize, mock_unlock): command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.dispatch(command_input) self.assertEqual(call(255), mock_exit.call_args)
def test_should_create_new_object_with_given_properties_and_answers(self): answer1 = Answer('Abc', 'Def', 0) answer2 = Answer('Ghi', 'Jkl', 1) answer3 = Answer('Mno', 'Pqr', 2) answers = [answer1, answer2, answer3] actual = StubConfiguration('any_command', ['any_arg1', 'any_arg2'], 'any_stdin', answers, 2) self.assertEqual( CommandInput('any_command', ['any_arg1', 'any_arg2'], 'any_stdin'), actual.command_input) self.assertEqual([answer1, answer2, answer3], actual.answers) self.assertEqual(2, actual.current_answer)
def test_should_send_answer_when_execution_fulfills_stub_configurations(self, mock_deserialize, mock_logging_info, mock_answer, mock_record, mock_serialize, mock_unlock): answer = Answer('Hello world', 'Hello error', 15) stub_configuration = StubConfiguration( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') stub_configuration.then(answer) mock_deserialize.return_value = [stub_configuration] command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.dispatch(command_input) self.assertEqual(call(answer), mock_answer.call_args)
def test_should_serialize_stubs_configuration_before_sending_answer(self, mock_deserialize, mock_logging_info, mock_answer, mock_record, mock_serialize, mock_unlock): answer = Answer('Hello world', 'Hello error', 15) stub_configuration = StubConfiguration( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') stub_configuration.then(answer) stub_configurations = [stub_configuration] mock_deserialize.return_value = stub_configurations command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.dispatch(command_input) self.assertEqual( call('shtub/stub-configurations', stub_configurations), mock_serialize.call_args)
def test_should_wait_when_answer_fulfills_stub_configurations_and_needs_waiting(self, mock_deserialize, mock_logging_info, mock_answer, mock_record, mock_sleep, mock_serialize, mock_unlock): answer = Answer( 'Hello world', 'Hello error', 15, milliseconds_to_wait=5) stub_configuration = StubConfiguration( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') stub_configuration.then(answer) mock_deserialize.return_value = [stub_configuration] command_input = CommandInput( 'command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.dispatch(command_input) self.assertEqual(call(answer), mock_answer.call_args) self.assertEqual(call(5 / 1000), mock_sleep.call_args)
def test_should_convert_dictionary_to_object(self): values = { 'command_input': { 'command': 'any_command', 'arguments': ['any_arg1', 'any_arg2', 'any_argument3'], 'stdin': 'any_stdin' }, 'current_answer': 0, 'answers': [{ 'stdout': 'Hello world.', 'stderr': 'Hello error!', 'return_code': 18, 'milliseconds_to_wait': None }, { 'stdout': 'Spam eggs.', 'stderr': 'Error!', 'return_code': 21, 'milliseconds_to_wait': None }] } actual_stub_configuration = StubConfiguration.from_dictionary(values) self.assertEqual( CommandInput('any_command', ['any_arg1', 'any_arg2', 'any_argument3'], 'any_stdin'), actual_stub_configuration.command_input) self.assertEqual(0, actual_stub_configuration.current_answer) actual_count_of_answers = len(actual_stub_configuration.answers) self.assertEqual(2, actual_count_of_answers) actual_first_answer = actual_stub_configuration.next_answer() self.assertEqual('Hello world.', actual_first_answer.stdout) self.assertEqual('Hello error!', actual_first_answer.stderr) self.assertEqual(18, actual_first_answer.return_code) actual_second_answer = actual_stub_configuration.next_answer() self.assertEqual('Spam eggs.', actual_second_answer.stdout) self.assertEqual('Error!', actual_second_answer.stderr) self.assertEqual(21, actual_second_answer.return_code)