def test_broker_publish_no_connection(app, mock_pika, mock_message): with app.app_context(): _, _, mock_channel, _, _ = mock_pika _, test_message = mock_message broker = Broker() with pytest.raises(AttributeError): broker.publish(test_message, "test routing key")
def send_warehouse_message(self, message: Message) -> None: """Publishes a message to the exchange as specified in configuration.""" logger.info("Attempting to publish the constructed plate event message") with Broker() as broker_channel: broker_channel.basic_publish( exchange=app.config["RMQ_EXCHANGE"], routing_key=self.routing_key, body=message.payload(), )
def test_broker_close_connection(app, mock_pika): with app.app_context(): _, _, _, mock_connection, _ = mock_pika broker = Broker() broker._connect() broker._close_connection() mock_connection.close.assert_called()
def test_broker_connect_connects(app, mock_pika): with app.app_context(): test_credentials, test_parameters, mock_channel, mock_connection, pika = mock_pika broker = Broker() broker._connect() pika.PlainCredentials.assert_called_with(app.config["RMQ_USERNAME"], app.config["RMQ_PASSWORD"]) pika.ConnectionParameters.assert_called_with( app.config["RMQ_HOST"], app.config["RMQ_PORT"], app.config["RMQ_VHOST"], test_credentials, ) pika.BlockingConnection.assert_called_with(test_parameters) mock_connection.channel.assert_called() if app.config["RMQ_DECLARE_EXCHANGE"]: mock_channel.exchange_declare.assert_called_with( app.config["RMQ_EXCHANGE"], exchange_type=app.config["RMQ_EXCHANGE_TYPE"]) assert broker._connection == mock_connection assert broker._channel == mock_channel
def test_broker_publish_no_routing_key(app, mock_pika, mock_message): with app.app_context(): _, _, mock_channel, _, _ = mock_pika _, test_message = mock_message broker = Broker() broker._connect() broker.publish(test_message, None) # type: ignore mock_channel.basic_publish.assert_not_called()
def test_broker_publish_no_message(app, mock_pika): with app.app_context(): _, _, mock_channel, _, _ = mock_pika test_routing_key = "routing key" broker = Broker() broker._connect() broker.publish(None, test_routing_key) # type: ignore mock_channel.basic_publish.assert_not_called()
def test_broker_publish(app, mock_pika, mock_message): with app.app_context(): _, _, mock_channel, _, _ = mock_pika test_payload, test_message = mock_message test_routing_key = "routing key" broker = Broker() broker._connect() broker.publish(test_message, test_routing_key) mock_channel.basic_publish.assert_called_with( exchange=app.config["RMQ_EXCHANGE"], routing_key=test_routing_key, body=test_payload, )
def fail_plate_from_barcode() -> FlaskResponse: """This endpoints attempts to publish an event to the event warehouse when a failure occurs when a destination plate is not created successfully. Note: This is the existing implementation, currently used for the v1 endpoint. Returns: FlaskResponse: If the message is published successfully return with an OK otherwise return the error messages and the corresponding HTTP status code. """ logger.info( f"Attempting to publish a '{PE_BECKMAN_DESTINATION_FAILED}' message") try: required_args = (ARG_USER_ID, ARG_BARCODE, ARG_ROBOT_SERIAL, ARG_FAILURE_TYPE) user_id, barcode, robot_serial_number, failure_type = get_required_params( request, required_args) except Exception as e: logger.error( f"{ERROR_CHERRYPICKED_FAILURE_RECORD} {ERROR_MISSING_PARAMETERS}") logger.exception(e) return bad_request(str(e)) try: if failure_type not in app.config["ROBOT_FAILURE_TYPES"]: logger.error( f"{ERROR_CHERRYPICKED_FAILURE_RECORD} unknown failure type") return bad_request( f"'{failure_type}' is not a known cherrypicked plate failure type" ) errors, message = construct_cherrypicking_plate_failed_message( barcode, user_id, robot_serial_number, failure_type) if message is None: logger.error( f"{ERROR_CHERRYPICKED_FAILURE_RECORD} error(s) constructing event message: {errors}" ) return internal_server_error(errors) routing_key = get_routing_key(PE_BECKMAN_DESTINATION_FAILED) logger.info( "Attempting to publish the destination failed event message") broker = Broker() # To use the broker as a context manager we don't need these methods to be public but we still need to refactor # these calls to use a context manager broker._connect() try: broker.publish(message, routing_key) broker._close_connection() logger.info( f"Successfully published a '{PE_BECKMAN_DESTINATION_FAILED}' message" ) return ok(errors=errors) except Exception: broker._close_connection() raise except Exception as e: msg = f"{ERROR_UNEXPECTED_CHERRYPICKING_FAILURE} ({type(e).__name__})" logger.error(msg) logger.exception(e) return internal_server_error(msg)
def test_broker_close_connection_no_connection(): broker = Broker() with pytest.raises(AttributeError): broker._close_connection()