def dispatch(command_input): """ currently this will handle the given command_input by testing whether it fulfills a stub configuration. If so it will save a execution (with the expected flag set to true) and send the next answer as defined in the stub configuration object. """ stub_configurations = deserialize_stub_configurations( CONFIGURED_STUBS_FILENAME) logging.info('Got %s', command_input) execution = Execution( command_input.command, command_input.arguments, command_input.stdin) for stub_configuration in stub_configurations: if command_input.fulfills(stub_configuration.command_input): logging.info('Execution fulfills %s', stub_configuration) execution.mark_as_expected() record_execution(execution) answer = stub_configuration.next_answer() serialize_as_dictionaries( CONFIGURED_STUBS_FILENAME, stub_configurations) unlock(lock_handle) if answer.milliseconds_to_wait: time.sleep(answer.milliseconds_to_wait / 1000) send_answer(answer) return unlock(lock_handle) logging.error( 'Given command_input does not fulfill requirements of any stub configuration.') sys.exit(255)
def test_should_return_false_when_one_object_is_not_fulfilled(self): execution1 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin', expected=True) execution2 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertFalse(execution1 == execution2, 'comparison: stdin')
def dispatch(command_input): """ currently this will handle the given command_input by testing whether it fulfills a stub configuration. If so it will save a execution (with the expected flag set to true) and send the next answer as defined in the stub configuration object. """ stub_configurations = deserialize_stub_configurations( CONFIGURED_STUBS_FILENAME) logging.info('Got %s', command_input) execution = Execution(command_input.command, command_input.arguments, command_input.stdin) for stub_configuration in stub_configurations: if command_input.fulfills(stub_configuration.command_input): logging.info('Execution fulfills %s', stub_configuration) execution.mark_as_expected() record_execution(execution) answer = stub_configuration.next_answer() serialize_as_dictionaries(CONFIGURED_STUBS_FILENAME, stub_configurations) unlock(lock_handle) if answer.milliseconds_to_wait: time.sleep(answer.milliseconds_to_wait / 1000) send_answer(answer) return unlock(lock_handle) logging.error( 'Given command_input does not fulfill requirements of any stub configuration.' ) sys.exit(255)
def test_should_render_execution_chain(self, mock_deserialize, mock_exists): mock_exists.return_value = True execution1 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) execution2 = Execution( 'command', ['-arg1', '-arg3'], 'stdin', expected=True) execution3 = Execution( 'command', ['-arg2', '-arg4'], 'stdin', expected=True) execution4 = Execution( 'command', ['-arg3', '-arg4'], 'stdin', expected=True) mock_deserialize.return_value = [ execution1, execution2, execution3, execution4] verifier = VerifierLoader('/hello/world') verify = verifier.__enter__() execution_chain_stream = StringIO() verify.render_execution_chain(execution_chain_stream) self.assertEquals(execution_chain_stream.getvalue(), ''' Execution chain 1 | command -arg1 -arg2 2 | command -arg1 -arg3 3 | command -arg2 -arg4 4 | command -arg3 -arg4 ''') execution_chain_stream.close()
def test_should_return_false_when_objects_arguments_are_not_equal(self): execution1 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') execution2 = Execution( 'any_command', ['other_argument1', 'any_arg2'], 'any_stdin') self.assertFalse(execution1 == execution2, 'comparison of arguments')
def test_should_return_true_when_objects_are_not_equal_and_testing_if_not_equal(self): execution1 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') execution2 = Execution( 'other_command', ['other_argument1', 'other_argument2'], 'other_stdin') self.assertTrue(execution1 != execution2, 'comparison: stdin')
def test_should_mark_execution_as_fulfilled(self): execution = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') execution.mark_as_expected() self.assertTrue(execution.expected)
def test_should_return_true_when_both_objects_are_fulfilled(self): execution1 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin', expected=True) execution2 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin', expected=True) self.assertTrue(execution1 == execution2, 'comparison: stdin')
def test_should_return_true_when_objects_are_equal(self): execution1 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') execution2 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertTrue(execution1 == execution2, 'no difference, but not equal returned')
def test_should_return_false_when_objects_are_equal_and_testing_if_not_equal(self): execution1 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') execution2 = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') self.assertFalse( execution1 != execution2, 'no difference, but not equal returned')
def test_should_return_string_with_all_properties(self): execution = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin') actual_string = execution.__str__() self.assertEqual( "Execution {'expected': False, 'command_input': {'stdin': 'any_stdin', 'command': 'any_command', 'arguments': ['any_arg1', 'any_arg2']}}", actual_string)
def test_should_not_raise_verification_exception_when_not_verifying_anything_else(self): execution1 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) execution2 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) verifier = Verifier([execution1, execution2]) with verifier as verify: verify.finished()
def test_should_raise_exception_when_one_more_recorded_calls_available_than_verified(self): execution1 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) execution2 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) verifier = Verifier([execution1, execution2]) verifier.called('command') self.assertRaises( VerificationException, verifier.__exit__, None, None, None)
def test_should_raise_exception_when_the_second_execution_has_not_been_accepted(self, mock_deserialize, mock_exists): mock_exists.return_value = True verifier = VerifierLoader('/hello/world') execution1 = Execution( 'any_command1', ['1any_arg1', '1any_arg2'], 'any_stdin1', expected=True) execution2 = Execution( 'any_command2', ['2any_arg1', '2any_arg2'], 'any_stdin2') mock_deserialize.return_value = [execution1, execution2] self.assertRaises(VerificationException, verifier.__enter__)
def test_should_return_false_when_an_exception_occured_in_the_with_statement(self): execution1 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) execution2 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) verifier = Verifier([execution1, execution2]) verifier.called('command') actual_result = verifier.__exit__( 'exception_type', 'exception_value', 'traceback') self.assertFalse(actual_result)
def test_should_convert_object_to_dictionary(self): execution = Execution( 'any_command', ['any_arg1', 'any_arg2'], 'any_stdin', expected=True) actual_dictionary = execution.as_dictionary() expected_dictionary = {'command_input': {'command': 'any_command', 'arguments': ['any_arg1', 'any_arg2'], 'stdin': 'any_stdin'}, 'expected': True} self.assertEqual(expected_dictionary, actual_dictionary) self.assertEqual(True, actual_dictionary['expected'])
def test_should_return_verifier_with_one_matching_execution_when_filtering_two_executions_by_argument(self, mock_deserialize, mock_exists): mock_exists.return_value = True execution1 = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) execution2 = Execution( 'command', ['-arg2', '-arg3'], 'stdin', expected=True) mock_deserialize.return_value = [execution1, execution2] verifier = VerifierLoader('/hello/world') verify = verifier.__enter__() with verify.filter_by_argument('-arg1') as verify_foobar: self.assertEqual([execution1], verify_foobar.executions) verify_foobar.finished()
def test_should_raise_exception_when_expected_command_has_not_been_executed(self): stub_execution = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) verifier = Verifier([stub_execution]) self.assertRaises( VerificationException, verifier.called, 'other_command')
def test_should_not_deserialize_when_file_does_not_exist(self, mock_exists, mock_deserialize, mock_serialize, mock_lock, mock_unlock): execution = Execution('command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.record_execution(execution) self.assertEqual(None, mock_deserialize.call_args)
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_pop_verified_execution(self): stub_execution = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) verifier = Verifier([stub_execution]) verifier.called('command') self.assertEqual(0, len(verifier.executions))
def test_should_convert_dictionary_to_object(self): values = {'command_input': {'command': 'any_command', 'arguments': ['any_arguments'], 'stdin': 'any_stdin'}, 'expected': True} actual = Execution.from_dictionary(values) self.assertEqual('any_command', actual.command_input.command) self.assertEqual(['any_arguments'], actual.command_input.arguments) self.assertEqual('any_stdin', actual.command_input.stdin) self.assertEqual(True, actual.expected)
def test_should_append_execution_and_serialize(self, mock_exists, mock_deserialize, mock_serialize, mock_lock, mock_unlock): execution = Execution('command', ['-arg1', '-arg2', '-arg3'], 'stdin') commandstub.record_execution(execution) self.assertEqual( call('shtub/executions', ANY), mock_serialize.call_args) actual_recorded_calls = mock_serialize.call_args[0][1] self.assertEqual(str(execution), str(actual_recorded_calls[0]))
def test_should_verify_command_has_been_called(self, mock_deserialize, mock_exists): mock_exists.return_value = True verifier = VerifierLoader('/hello/world') stub_execution = Execution( 'command', ['-arg1', '-arg2'], 'stdin', expected=True) mock_deserialize.return_value = [stub_execution] with verifier as verify: verify.called('command') self.assertEqual(0, len(verify.executions)) self.assertEqual( call('/hello/world/shtub/executions'), mock_deserialize.call_args)
def test_should_serialize_as_dictionaries(self, mock_open, mock_json, mock_unlock, mock_lock): fake_file = self.return_file_when_calling(mock_open) mock_json.return_value = '[{"some": "json"}]' stub_configuration = [ Execution('command', ['-arg1', '-arg2', '-arg3'], 'stdin', expected=True)] serialize_as_dictionaries( 'stub_configuration.json', stub_configuration) expected_dictionary = {'command_input': {'command': 'command', 'arguments': ['-arg1', '-arg2', '-arg3'], 'stdin': 'stdin'}, 'expected': True} self.assertEqual( call([expected_dictionary], sort_keys=True, indent=4), mock_json.call_args) self.assertEqual( call('stub_configuration.json', mode='w'), mock_open.call_args) self.assertEqual(call('[{"some": "json"}]'), fake_file.write.call_args)
def test_should_deserialize_stub_configuration_with_no_answers(self, mock_open, mock_json): fake_file = self.return_file_when_calling(mock_open) json_string = "[{'expected': false, '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'}, 'expected': False}] actual_stub_configuration = deserialize_executions( '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_configuration = [ Execution('command', ['-arg1', '-arg2', '-arg3'], 'stdin')] self.assertEqual( expected_stub_configuration, actual_stub_configuration)
def deserialize_executions (filename): """ loads the given json file and returns a list of executions. """ executions = _load_json_file(filename) return list(map(lambda e: Execution.from_dictionary(e), executions))
def deserialize_executions(filename): """ loads the given json file and returns a list of executions. """ executions = _load_json_file(filename) return list(map(lambda e: Execution.from_dictionary(e), executions))