def test_new_id_generated_if_invalid_ids_passed_in_params(self, id_generator_stub): parameters = { "other": "config", "transaction_id": 54321, } with transaction("name", parameters, transaction_id=12345) as transaction_id: assert transaction_id == id_generator_stub.last_id
def thread_with_transaction(self, thread_index): logger = self.logger if self.pass_logger else None with transaction(f"Transaction thread [{thread_index}]", logger=logger): self.logger.info( f"Transaction thread [{thread_index}], in transaction") self.logger.info(f"Thread log [{thread_index}], no transaction")
def test_specified_logger(self): logger = MagicMock() parameters = {} with transaction("name", parameters, logger=logger) as transaction_id: logger.info("A message") for i, message in enumerate(["Generated", "Enter", "A message", "Exit"]): assert logger.info.call_args_list[i].starts_with(message) assert logger.info.call_count == 4, f"Log calls incorrect {logger.info.call_args_list}"
def thread_with_transaction_exception(self, thread_index): logger = self.logger if self.pass_logger else None try: with transaction(f"Transaction thread [{thread_index}]", logger=logger): self.logger.info( f"Transaction thread in transaction [{thread_index}], in transaction" ) raise RuntimeError("An exception has occurred") except RuntimeError: pass
def test_exception_logs_transaction_id_and_command(self, recording_logger): parameters = {"other": ["config", 1, 2, 3.0]} with pytest.raises(RuntimeError): with transaction("name", parameters) as transaction_id: raise RuntimeError("Something went wrong") record_logs = get_all_record_logs(recording_logger) for log_msg in record_logs: if "RuntimeError" in log_msg and transaction_id in log_msg and "name" in log_msg: return assert 0, f"RuntimeError and transaction tag not found in exception logs: {record_logs}"
def test_name_and_id_and_params_in_context_handler(self, recording_logger): parameters = {"other": ["config", 1, 2, 3.0]} with transaction("name", parameters) as transaction_id: pass _, first_log_message = get_first_record_and_log_message(recording_logger) _, second_log_message = get_second_record_and_log_message(recording_logger) _, last_log_message = get_last_record_and_log_message(recording_logger) # __enter__ log message assert "Generated transaction ID" in first_log_message assert "Enter" in second_log_message assert "name" in second_log_message assert "other" in second_log_message assert transaction_id in second_log_message assert json.dumps(parameters) in second_log_message # __exit__ log message assert "Exit" in last_log_message assert "name" in last_log_message assert transaction_id in last_log_message
def test_new_id_generated_if_params_empty(self, id_generator_stub): parameters = {} with transaction("name", parameters) as transaction_id: assert transaction_id == id_generator_stub.last_id
def test_new_id_generated_if_id_is_not_present(self, id_generator_stub): parameters = {"other": "config"} with transaction("name", parameters) as transaction_id: assert transaction_id == id_generator_stub.last_id
def test_new_id_generated_if_id_is_not_string_type(self, id_generator_stub): parameters = {"transaction_id": 1234.5, "other": "config"} with transaction("name", parameters) as transaction_id: assert transaction_id == id_generator_stub.last_id
def test_new_id_generated_if_id_is_only_white_space(self, id_generator_stub): parameters = {"transaction_id": "\t\n \r\n", "other": "config"} with transaction("name", parameters) as transaction_id: assert transaction_id == id_generator_stub.last_id
def test_preference_order(self, input_args, expected_txn_id): with transaction(**input_args) as transaction_id: assert transaction_id == expected_txn_id
def test_error_if_params_type_is_not_mapping(self): parameters = [] with pytest.raises(TransactionParamsError): with transaction("name", parameters): pass
def test_id_provider_not_used_for_existing_valid_id(self, id_generator_stub): parameters = {"transaction_id": "abc1234"} with transaction("name", parameters): assert id_generator_stub.call_count == 0
def test_id_provider_only_used_once_for_one_new_id(self, id_generator_stub): parameters = {} with transaction("name", parameters): assert id_generator_stub.call_count == 1