Esempio n. 1
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')
Esempio n. 2
0
    def test_should_return_false_when_comparing_and_current_answer_is_different(
            self):
        stub_configuration1 = StubConfiguration('any_command',
                                                ['any_arg1', 'any_arg2'],
                                                'any_stdin', [], 0)
        stub_configuration2 = StubConfiguration('any_command',
                                                ['any_arg1', 'any_arg2'],
                                                'any_stdin', [], 1)

        self.assertFalse(stub_configuration1 == stub_configuration2,
                         'comparison error: attribute current_answer')
Esempio n. 3
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')
Esempio n. 4
0
    def test_should_set_stdin(self):
        stub_configuration = StubConfiguration('any_command')

        actual_return_value = stub_configuration.with_input('stdin')

        self.assertEqual(stub_configuration, actual_return_value)
        self.assertEqual('stdin', stub_configuration.command_input.stdin)
Esempio n. 5
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)
Esempio n. 6
0
    def test_should_raise_exception_when_asking_for_next_answer_when_no_answer_is_given(
            self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')

        self.assertRaises(Exception, stub_configuration.next_answer)
Esempio n. 7
0
    def test_should_send_answers_in_given_order_when_asking_for_next_answer(
            self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_answer('Hello world!', 'Hello error!', 0)
        stub_configuration.then_answer('Foo bar!', 'Foo error!', 1)
        stub_configuration.then_answer('Spam eggs!', 'Spam error!', 2)

        actual_first_answer = stub_configuration.next_answer()

        self.assertEqual('Hello world!', actual_first_answer.stdout)
        self.assertEqual('Hello error!', actual_first_answer.stderr)
        self.assertEqual(0, actual_first_answer.return_code)

        actual_second_answer = stub_configuration.next_answer()

        self.assertEqual('Foo bar!', actual_second_answer.stdout)
        self.assertEqual('Foo error!', actual_second_answer.stderr)
        self.assertEqual(1, actual_second_answer.return_code)

        actual_third_answer = stub_configuration.next_answer()

        self.assertEqual('Spam eggs!', actual_third_answer.stdout)
        self.assertEqual('Spam error!', actual_third_answer.stderr)
        self.assertEqual(2, actual_third_answer.return_code)
Esempio n. 8
0
    def test_should_have_property_answers_with_empty_list(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')

        actual_answers = stub_configuration.answers

        self.assertEqual([], actual_answers)
Esempio n. 9
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))
Esempio n. 10
0
    def test_should_set_arguments(self):
        stub_configuration = StubConfiguration('any_command')

        actual_return_value = stub_configuration.at_least_with_arguments(
            '-arg1', '-arg2', '-arg3')

        self.assertEqual(stub_configuration, actual_return_value)
        self.assertEqual(['-arg1', '-arg2', '-arg3'],
                         stub_configuration.command_input.arguments)
Esempio n. 11
0
    def test_should_return_self_when_using_method_then_write(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')

        actual_result = stub_configuration.then_write('Hello world.')

        self.assertTrue(actual_result is not None, 'Not returning anything!')
        self.assertEqual(stub_configuration, actual_result)
Esempio n. 12
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)
Esempio n. 13
0
    def calling(self, command):
        """
            creates a new StubConfiguration with the given command and appends it to
            the stub_configurations, then returns the stub_configuration for invocation
            chaining.
        """
        stub_configuration = StubConfiguration(command)
        self.stub_configurations.append(stub_configuration)

        return stub_configuration
Esempio n. 14
0
    def test_should_append_second_answer(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_answer('Hello world!', 'Hello error!', 0)
        stub_configuration.then_answer('Foo bar!', 'Foo error!', 1)

        actual_count_of_answers = len(stub_configuration.answers)

        self.assertEqual(2, actual_count_of_answers)
Esempio n. 15
0
    def test_should_set_stdout_and_stderr_with_chaining_when_answering(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_write('Hello world!', 'Hello error!')

        actual_answer = stub_configuration.next_answer()

        self.assertEqual('Hello world!', actual_answer.stdout)
        self.assertEqual('Hello error!', actual_answer.stderr)
        self.assertEqual(0, actual_answer.return_code)
Esempio n. 16
0
    def test_should_unset_waiting_time_by_default_when_answering(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_answer('Hello world!', 'Hello error!', 15)

        actual_answer = stub_configuration.next_answer()

        self.assertEqual('Hello world!', actual_answer.stdout)
        self.assertEqual('Hello error!', actual_answer.stderr)
        self.assertEqual(15, actual_answer.return_code)
        self.assertEqual(None, actual_answer.milliseconds_to_wait)
Esempio n. 17
0
    def test_should_set_stdout_and_waiting_time_when_answering(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_write('Hello world!', milliseconds_to_wait=6)

        actual_answer = stub_configuration.next_answer()

        self.assertEqual('Hello world!', actual_answer.stdout)
        self.assertEqual(None, actual_answer.stderr)
        self.assertEqual(0, actual_answer.return_code)
        self.assertEqual(6, actual_answer.milliseconds_to_wait)
Esempio n. 18
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)
Esempio n. 19
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)
Esempio n. 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)
Esempio n. 21
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)
Esempio n. 22
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)
Esempio n. 23
0
    def test_should_send_two_different_answers_when_asking_for_next_answer_twice(
            self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_answer('Hello world!', 'Hello error!', 0)
        stub_configuration.then_answer('Foo bar!', 'Foo error!', 1)

        actual_first_answer = stub_configuration.next_answer()

        self.assertEqual('Hello world!', actual_first_answer.stdout)
        self.assertEqual('Hello error!', actual_first_answer.stderr)
        self.assertEqual(0, actual_first_answer.return_code)

        actual_second_answer = stub_configuration.next_answer()

        self.assertEqual('Foo bar!', actual_second_answer.stdout)
        self.assertEqual('Foo error!', actual_second_answer.stderr)
        self.assertEqual(1, actual_second_answer.return_code)
Esempio n. 24
0
    def test_should_return_object_as_dictionary(self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_answer('Hello world.', 'Hello error!', 19)

        actual_dictionary = stub_configuration.as_dictionary()

        expected_command_input = {
            'command': 'any_command',
            'arguments': ['any_arg1', 'any_arg2'],
            'stdin': 'any_stdin'
        }
        self.assertEqual(expected_command_input,
                         actual_dictionary['command_input'])
        self.assertEqual(0, actual_dictionary['current_answer'])
        actual_answer_dictionary = actual_dictionary['answers'][0]

        self.assertEqual('Hello world.', actual_answer_dictionary['stdout'])
        self.assertEqual('Hello error!', actual_answer_dictionary['stderr'])
        self.assertEqual(19, actual_answer_dictionary['return_code'])
Esempio n. 25
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)
Esempio n. 26
0
    def test_should_set_empty_stdin_as_default(self):
        stub_configuration = StubConfiguration('any_command')

        self.assertEqual(None, stub_configuration.command_input.stdin)
Esempio n. 27
0
    def test_should_allow_with_or_and_input(self):
        stub_configuration = StubConfiguration('any_command')

        self.assertEqual(stub_configuration.with_input,
                         stub_configuration.and_input)