Example #1
0
    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))
Example #2
0
    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))
Example #3
0
    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')
Example #4
0
    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')
Example #5
0
    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')
Example #6
0
    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')
Example #7
0
    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')
Example #8
0
    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
Example #9
0
    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')
Example #10
0
    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)
Example #11
0
    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)
Example #12
0
    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)
Example #13
0
    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)
Example #14
0
    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)
Example #15
0
    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')
Example #16
0
    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')
Example #17
0
    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')
Example #18
0
    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)
Example #19
0
    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)
Example #20
0
    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)
Example #21
0
    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
Example #22
0
    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)
Example #23
0
    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)
Example #24
0
    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)
Example #25
0
class Execution (object):

    """
        Represents the parameters of a call to the command stub.
    """

    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 as_dictionary(self):
        """
            returns a dictionary representation of the execution.
        """

        return {'command_input': self.command_input.as_dictionary(),
                'expected': self.expected}

    def mark_as_expected(self):
        """
            marks the execution as expected which means the execution fulfills a stub configuration.
        """

        self.expected = True

    def __eq__(self, other):
        return self.command_input == other.command_input \
            and self.expected == other.expected

    def __ne__(self, other):
        return not(self == other)

    def __str__(self):
        """
            returns a string representation using as_dictionary.
        """

        return 'Execution %s' % (self.as_dictionary())

    @staticmethod
    def from_dictionary(dictionary):
        """
            returns a new execution object with the properties from the given
            dictionary.
        """

        command_input_dictionary = dictionary['command_input']

        return Execution(command_input_dictionary['command'],
                         command_input_dictionary['arguments'],
                         command_input_dictionary['stdin'],
                         dictionary['expected'])
Example #26
0
    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)
Example #27
0
class Execution(object):
    """
        Represents the parameters of a call to the command stub.
    """
    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 as_dictionary(self):
        """
            returns a dictionary representation of the execution.
        """

        return {
            'command_input': self.command_input.as_dictionary(),
            'expected': self.expected
        }

    def mark_as_expected(self):
        """
            marks the execution as expected which means the execution fulfills a stub configuration.
        """

        self.expected = True

    def __eq__(self, other):
        return self.command_input == other.command_input \
            and self.expected == other.expected

    def __ne__(self, other):
        return not (self == other)

    def __str__(self):
        """
            returns a string representation using as_dictionary.
        """

        return 'Execution %s' % (self.as_dictionary())

    @staticmethod
    def from_dictionary(dictionary):
        """
            returns a new execution object with the properties from the given
            dictionary.
        """

        command_input_dictionary = dictionary['command_input']

        return Execution(command_input_dictionary['command'],
                         command_input_dictionary['arguments'],
                         command_input_dictionary['stdin'],
                         dictionary['expected'])
Example #28
0
    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)
Example #29
0
    def __init__(self,
                 command,
                 arguments=[],
                 stdin=None,
                 answers=[],
                 initial_answer=0):
        """
            will initialize a new object with the given properties.
            answers and initial_answer are not mandatory.
        """

        self.command_input = CommandInput(command, arguments, stdin)

        self.answers = []
        self.current_answer = initial_answer

        for answer in answers:
            self.answers.append(answer)

        self.and_input = self.with_input
Example #30
0
    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)
Example #31
0
    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)
Example #32
0
    def __init__(self, command, arguments=[], stdin=None, answers=[], initial_answer=0):
        """
            will initialize a new object with the given properties.
            answers and initial_answer are not mandatory.
        """

        self.command_input = CommandInput(command, arguments, stdin)

        self.answers = []
        self.current_answer = initial_answer

        for answer in answers:
            self.answers.append(answer)

        self.and_input = self.with_input
Example #33
0
    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)
Example #34
0
    def test_should_return_true_if_other_has_exactly_one_matching_argument_and_no_others(self):
        command_input1 = CommandInput(
            'any_command', ['any_arg1', 'any_arg2'], 'any_stdin')
        command_input2 = CommandInput('any_command', ['any_arg1'], 'any_stdin')

        self.assertTrue(command_input1.fulfills(command_input2))
Example #35
0
    def test_should_return_true_when_stdin_is_none(self):
        command_input1 = CommandInput('any_command', ['argument'], 'any_stdin')
        command_input2 = CommandInput('any_command', ['argument'], None)

        self.assertTrue(command_input1.fulfills(command_input2))
Example #36
0
class StubConfiguration(object):

    """
        Represents the configuration of a command stub and contains the corresponding answers.
    """

    def __init__(self, command, arguments=[], stdin=None, answers=[], initial_answer=0):
        """
            will initialize a new object with the given properties.
            answers and initial_answer are not mandatory.
        """

        self.command_input = CommandInput(command, arguments, stdin)

        self.answers = []
        self.current_answer = initial_answer

        for answer in answers:
            self.answers.append(answer)

        self.and_input = self.with_input

    def as_dictionary(self):
        """
            returns a dictionary representation of this stub configuration.
        """

        answers_list = []

        for answer in self.answers:
            answer_dictionary = answer.as_dictionary()
            answers_list.append(answer_dictionary)

        result = {'command_input': self.command_input.as_dictionary(),
                  'answers': answers_list,
                  'current_answer': self.current_answer}

        return result

    def next_answer(self):
        """
            returns the next answer in the list of answers or if the end of the
            list is reached it will repeatedly return the last answer of the
            list.
        """

        if len(self.answers) == 0:
            raise Exception('List of answers is empty!')

        result = self.answers[self.current_answer]

        if self.current_answer < len(self.answers) - 1:
            self.current_answer += 1

        return result

    def then(self, answer):
        """
            will append the given answer to the list of answers and return
            the object itself for invocation chaining.
        """

        self.answers.append(answer)

        return self

    def then_answer(self, stdout=None, stderr=None, return_code=0, milliseconds_to_wait=None):
        """
            a convenience method to "then" which will create a new answer
            object with the given properties.
        """

        return self.then(Answer(stdout, stderr, return_code, milliseconds_to_wait))

    def then_return(self, return_code, milliseconds_to_wait=None):
        """
            a convenience method to "then" which will create a new answer
            object with the given return_code.
        """

        return self.then_answer(return_code=return_code, milliseconds_to_wait=milliseconds_to_wait)

    def then_write(self, stdout=None, stderr=None, milliseconds_to_wait=None):
        """
            a convenience method to "then" which will create a new answer
            object with the given stdout and stderr output.
        """

        return self.then_answer(stdout=stdout, stderr=stderr, milliseconds_to_wait=milliseconds_to_wait)

    def at_least_with_arguments(self, *arguments):
        """
            sets the given arguments and returns self for invocation chaining
        """

        self.command_input.arguments = list(arguments)

        return self

    def with_input(self, stdin):
        """
            sets the given arguments and returns self for invocation chaining
        """

        self.command_input.stdin = stdin

        return self

    def __eq__(self, other):
        return  self.command_input == other.command_input \
            and self.current_answer == other.current_answer \
            and self.answers == other.answers

    def __str__(self):
        """
            returns a string representation of this stub configuration using the method "as_dictionary"
        """
        return 'StubConfiguration %s' % (self.as_dictionary())

    @staticmethod
    def from_dictionary(dictionary):
        """
            returns a new stub configuration object with the properties from the given dictionary
        """
        answers = []

        for answer_dictionary in dictionary['answers']:
            answer = Answer.from_dictionary(answer_dictionary)
            answers.append(answer)

        command_input_dictionary = dictionary['command_input']
        stub_configuration = StubConfiguration(
            command_input_dictionary['command'],
            command_input_dictionary[
                'arguments'],
            command_input_dictionary[
                'stdin'],
            answers,
            dictionary['current_answer'])

        return stub_configuration