Example #1
0
    def test_should_return_false_when_comparing_and_return_code_is_different(
            self):
        answer1 = Answer('Hello world.', 'Hello error!', 13)
        answer2 = Answer('Hello world.', 'Hello error!', 0)

        self.assertFalse(answer1 == answer2,
                         'comparison error: attribute return_code')
Example #2
0
    def test_should_return_answer_as_string(self):
        answer = Answer('Hello world.', 'Hello error!', 13)

        actual_string = answer.__str__()

        self.assertEqual(
            "Answer {'milliseconds_to_wait': None, 'return_code': 13, 'stderr': 'Hello error!', 'stdout': 'Hello world.'}", actual_string)
Example #3
0
    def test_should_return_answer_as_string(self):
        answer = Answer('Hello world.', 'Hello error!', 13)

        actual_string = answer.__str__()

        self.assertEqual(
            "Answer {'milliseconds_to_wait': None, 'return_code': 13, 'stderr': 'Hello error!', 'stdout': 'Hello world.'}",
            actual_string)
Example #4
0
    def test_should_return_answer_as_dictionary(self):
        answer = Answer(
            'Hello world.', 'Hello error!', 13, milliseconds_to_wait=3)

        actual_dictionary = answer.as_dictionary()

        self.assertEqual('Hello world.', actual_dictionary['stdout'])
        self.assertEqual('Hello error!', actual_dictionary['stderr'])
        self.assertEqual(13, actual_dictionary['return_code'])
        self.assertEqual(3, actual_dictionary['milliseconds_to_wait'])
Example #5
0
    def test_should_return_false_when_comparing_and_answers_are_different(
            self):
        stub_configuration1 = StubConfiguration(
            'any_command', ['any_arg1', 'any_arg2'], 'any_stdin',
            [Answer('stdout1', 'stderr1', 0)], 0)
        stub_configuration2 = StubConfiguration(
            'any_command', ['any_arg1', 'any_arg2'], 'any_stdin',
            [Answer('stdout2', 'stderr2', 1)], 0)

        self.assertFalse(stub_configuration1 == stub_configuration2,
                         'comparison error: attribute answers')
Example #6
0
    def test_should_return_true_when_comparing_and_stub_configurations_are_equal(
            self):
        stub_configuration1 = StubConfiguration(
            'any_command', ['any_arg1', 'any_arg2'], 'any_stdin',
            [Answer('stdout1', 'stderr1', 13)], 1)
        stub_configuration2 = StubConfiguration(
            'any_command', ['any_arg1', 'any_arg2'], 'any_stdin',
            [Answer('stdout1', 'stderr1', 13)], 1)

        self.assertTrue(stub_configuration1 == stub_configuration2,
                        'comparison error: objects are equal')
Example #7
0
    def test_should_return_answer_as_dictionary(self):
        answer = Answer('Hello world.',
                        'Hello error!',
                        13,
                        milliseconds_to_wait=3)

        actual_dictionary = answer.as_dictionary()

        self.assertEqual('Hello world.', actual_dictionary['stdout'])
        self.assertEqual('Hello error!', actual_dictionary['stderr'])
        self.assertEqual(13, actual_dictionary['return_code'])
        self.assertEqual(3, actual_dictionary['milliseconds_to_wait'])
Example #8
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 #9
0
    def test_should_deserialize_stub_configurations(self, mock_open, mock_json):
        fake_file = self.return_file_when_calling(mock_open)
        json_string = "[{'current_answer': 0, 'answers': [{'return_code': 15, 'stderr': 'stderr', 'stdout': 'stdout', 'milliseconds_to_wait': None}], 'command_input': {'stdin': 'stdin', 'command': 'command', 'arguments': ['-arg1', '-arg2', '-arg3']}}]"
        fake_file.read.return_value = json_string
        mock_json.return_value = [
            {'command_input': {'command': 'command',
                               'arguments': ['-arg1', '-arg2', '-arg3'],
                               'stdin': 'stdin'},
             'current_answer': 0,
             'answers': [{'stdout': 'stdout',
                          'stderr': 'stderr',
                          'return_code': 15,
                          'milliseconds_to_wait': None}]
             }]

        actual_stub_configurations = deserialize_stub_configurations(
            'stub_configuration.json')

        self.assertEqual(
            call('stub_configuration.json', mode='r'), mock_open.call_args)
        self.assertEqual(call(), fake_file.read.call_args)
        self.assertEqual(call(json_string), mock_json.call_args)

        expected_stub_configurations = [
            StubConfiguration('command', ['-arg1', '-arg2', '-arg3'], 'stdin', [Answer('stdout', 'stderr', 15)], 0)]

        self.assertEqual(
            expected_stub_configurations, actual_stub_configurations)
Example #10
0
    def test_should_send_answer(self, mock_exit, mock_stderr, mock_stdout):
        answer = Answer('Hello world!', 'Hello error!', 223)

        commandstub.send_answer(answer)

        self.assertEqual('Hello world!', mock_stdout.getvalue())
        self.assertEqual('Hello error!', mock_stderr.getvalue())
        self.assertEqual(call(223), mock_exit.call_args)
Example #11
0
    def test_should_return_string_with_all_properties(self):
        stub_configuration = StubConfiguration(
            'any_command', ['any_arg1', 'any_arg2'], 'any_stdin',
            [Answer('stdout1', 'stderr1', 13)], 1)

        self.assertEqual(
            "StubConfiguration {'current_answer': 1, 'answers': [{'milliseconds_to_wait': None, 'return_code': 13, 'stderr': 'stderr1', 'stdout': 'stdout1'}], 'command_input': {'stdin': 'any_stdin', 'command': 'any_command', 'arguments': ['any_arg1', 'any_arg2']}}",
            str(stub_configuration))
Example #12
0
    def test_should_send_answer_without_writing_to_stdout_and_stderr(self, mock_exit, mock_stderr, mock_stdout):
        answer = Answer(None, None, 123)

        commandstub.send_answer(answer)

        self.assertEqual('', mock_stdout.getvalue())
        self.assertEqual('', mock_stderr.getvalue())
        self.assertEqual(call(123), mock_exit.call_args)
Example #13
0
    def test_should_return_self_when_using_method_then(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        answer = Answer('Hello world!', 'Hello error', 99)

        actual_result = stub_configuration.then(answer)

        self.assertTrue(actual_result is not None, 'Not returning anything!')
        self.assertEqual(stub_configuration, actual_result)
Example #14
0
    def test_should_create_new_instance_with_given_values(self):
        actual = Answer('Hello world!',
                        'Hello error!',
                        8,
                        milliseconds_to_wait=1337)

        self.assertEqual('Hello world!', actual.stdout)
        self.assertEqual('Hello error!', actual.stderr)
        self.assertEqual(8, actual.return_code)
        self.assertEqual(1337, actual.milliseconds_to_wait)
Example #15
0
    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))
Example #16
0
    def test_should_return_answer_from_dictionary(self):
        answer_dictionary = {'stdout': 'Hello world.',
                             'stderr': 'Hello error!',
                             'return_code': 82,
                             'milliseconds_to_wait': 2}

        actual_answer = Answer.from_dictionary(answer_dictionary)

        self.assertEqual('Hello world.', actual_answer.stdout)
        self.assertEqual('Hello error!', actual_answer.stderr)
        self.assertEqual(82, actual_answer.return_code)
        self.assertEqual(2, actual_answer.milliseconds_to_wait)
Example #17
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 #18
0
    def test_should_return_answer_from_dictionary(self):
        answer_dictionary = {
            'stdout': 'Hello world.',
            'stderr': 'Hello error!',
            'return_code': 82,
            'milliseconds_to_wait': 2
        }

        actual_answer = Answer.from_dictionary(answer_dictionary)

        self.assertEqual('Hello world.', actual_answer.stdout)
        self.assertEqual('Hello error!', actual_answer.stderr)
        self.assertEqual(82, actual_answer.return_code)
        self.assertEqual(2, actual_answer.milliseconds_to_wait)
Example #19
0
    def test_should_append_answer_when_using_method_then(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')

        stub_configuration.then(Answer('Hello world!', 'Hello error', 99))

        actual_count_of_answers = len(stub_configuration.answers)

        self.assertEqual(1, actual_count_of_answers)

        actual_answer = stub_configuration.next_answer()

        self.assertEqual('Hello world!', actual_answer.stdout)
        self.assertEqual('Hello error', actual_answer.stderr)
        self.assertEqual(99, actual_answer.return_code)
Example #20
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 #21
0
    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)
Example #22
0
    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
Example #23
0
    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
Example #24
0
    def test_should_mark_execution_as_expected_and_record_call_when_execution_fulfills_stub_configuration(
        self, mock_deserialize,
        mock_logging_info, mock_answer,
        mock_record, mock_execution_class,
        mock_serialize, mock_unlock):

        mock_execution = Mock(Execution)
        mock_execution_class.return_value = mock_execution
        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('command', ['-arg1', '-arg2', '-arg3'], 'stdin'), mock_execution_class.call_args)
        self.assertEqual(call(), mock_execution.mark_as_expected.call_args)
        self.assertEqual(call(mock_execution), mock_record.call_args)
Example #25
0
    def test_should_return_true_when_comparing_and_objects_are_equal(self):
        answer1 = Answer('Hello world.', 'Hello error!', 0)
        answer2 = Answer('Hello world.', 'Hello error!', 0)

        self.assertTrue(answer1 == answer2,
                        'comparison error: objects are equal')