Example #1
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)
Example #2
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)
Example #3
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)
Example #4
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)
Example #5
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)
Example #6
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)
Example #7
0
    def test_should_set_return_code_and_waiting_time_when_answering(self):
        stub_configuration = StubConfiguration("any_command", ["any_arg1", "any_arg2"], "any_stdin")
        stub_configuration.then_return(7, milliseconds_to_wait=3)

        actual_answer = stub_configuration.next_answer()

        self.assertEqual(None, actual_answer.stdout)
        self.assertEqual(None, actual_answer.stderr)
        self.assertEqual(7, actual_answer.return_code)
        self.assertEqual(3, actual_answer.milliseconds_to_wait)
Example #8
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)
Example #9
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_return(7)

        actual_answer = stub_configuration.next_answer()

        self.assertEqual(None, actual_answer.stdout)
        self.assertEqual(None, actual_answer.stderr)
        self.assertEqual(7, actual_answer.return_code)
        self.assertEqual(None, actual_answer.milliseconds_to_wait)
Example #10
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"])
Example #11
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 #12
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 #13
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 #14
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 #15
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)
Example #16
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 #17
0
    def test_should_set_return_code_and_stderr_and_stdout_and_waiting_time(
            self):
        stub_configuration = StubConfiguration('any_command',
                                               ['any_arg1', 'any_arg2'],
                                               'any_stdin')
        stub_configuration.then_answer('Hello world!',
                                       'Hello error!',
                                       15,
                                       milliseconds_to_wait=5)

        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(5, actual_answer.milliseconds_to_wait)
Example #18
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 #19
0
def deserialize_stub_configurations(filename):
    """
        loads the given json file and returns a list of stub configurations.
    """
    stub_configurations = _load_json_file(filename)
    return list(
        map(lambda e: StubConfiguration.from_dictionary(e),
            stub_configurations))
Example #20
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)
Example #21
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 #22
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 #23
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
Example #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'])
Example #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)
Example #26
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)
Example #27
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 #28
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)
Example #29
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)
Example #30
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)
Example #31
0
    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)
Example #32
0
    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)
Example #33
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)
Example #34
0
def deserialize_stub_configurations (filename):
    """
        loads the given json file and returns a list of stub configurations.
    """
    stub_configurations = _load_json_file(filename)
    return list(map(lambda e: StubConfiguration.from_dictionary(e), stub_configurations))
Example #35
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)
Example #36
0
    def test_should_set_empty_stdin_as_default(self):
        stub_configuration = StubConfiguration('any_command')

        self.assertEqual(None, stub_configuration.command_input.stdin)