From 54731e2f89b2bc188f100e5e3359ece8e5bff573 Mon Sep 17 00:00:00 2001 From: Freedisch Date: Fri, 15 Mar 2024 22:11:20 +0200 Subject: [PATCH 1/5] Fix import error and reset load_message Signed-off-by: Freedisch --- fedora_messaging/cli.py | 5 +++-- tests/unit/test_cli.py | 7 +------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/fedora_messaging/cli.py b/fedora_messaging/cli.py index 36d3e07d..a6ccf83a 100644 --- a/fedora_messaging/cli.py +++ b/fedora_messaging/cli.py @@ -32,6 +32,8 @@ import requests from twisted.internet import asyncioreactor, error +from fedora_messaging import message + try: asyncioreactor.install() @@ -469,8 +471,7 @@ def replay(message_id, datagrepper_url): message_data = _get_message(message_id, datagrepper_url) if message_data: try: - for msg in message_data: - api.publish(loads(msg)) + api.publish(message.load_message(message_data)) click.echo(f"Message with ID {message_id} has been successfully replayed.") except Exception as e: raise click.ClickException(f"Failed to replay message: {e}") diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index efe0b36f..65724d58 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -18,7 +18,6 @@ import errno -import json import os import sys from unittest import mock @@ -798,11 +797,10 @@ def test_successful_message_replay(self, mock_publish, mock_get_message): message_id = "123" datagrepper_url = "http://example.com" - valid_message_structure = { + message_data = { "topic": "test.topic", "body": {"data_key": "data_value"}, } - message_data = [json.dumps(valid_message_structure)] mock_get_message.return_value = message_data result = self.runner.invoke( @@ -813,9 +811,6 @@ def test_successful_message_replay(self, mock_publish, mock_get_message): result.exit_code == 0 ), f"Command did not exit as expected. Output: {result.output}" assert "has been successfully replayed" in result.output - assert mock_publish.call_count == len( - message_data - ), "Publish was not called for each message in data." @mock.patch("fedora_messaging.cli.requests.get", side_effect=requests.HTTPError) def test_datagrepper_http_error(self, mock_get): From 71bce2356dfc85f3b14897b48254d2858ae84337 Mon Sep 17 00:00:00 2001 From: freedisch Date: Tue, 19 Mar 2024 18:09:28 +0200 Subject: [PATCH 2/5] Remove if block from from the try/exception Signed-off-by: freedisch --- fedora_messaging/cli.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fedora_messaging/cli.py b/fedora_messaging/cli.py index b2b87420..b3bdc7e9 100644 --- a/fedora_messaging/cli.py +++ b/fedora_messaging/cli.py @@ -470,12 +470,13 @@ def replay(message_id, datagrepper_url): """Replay a message from Datagrepper by its message ID""" try: message_data = _get_message(message_id, datagrepper_url) - if message_data: - api.publish(message.load_message(message_data)) - click.echo(f"Message with ID {message_id} has been successfully replayed.") except requests.HTTPError as e: raise click.ClickException(f"Failed to retrieve message from Datagrepper: {e}") + if message_data: + api.publish(message.load_message(message_data)) + click.echo(f"Message with ID {message_id} has been successfully replayed.") + def _get_message(message_id, datagrepper_url): """Fetch a message by ID from Datagreeper""" From 153003e0d4680fca9ccc99f2564150ad1ed99a05 Mon Sep 17 00:00:00 2001 From: freedisch Date: Tue, 19 Mar 2024 18:23:01 +0200 Subject: [PATCH 3/5] Fix failing test case CI Signed-off-by: freedisch --- tests/unit/test_cli.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index faf57f1e..c36cf444 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -820,13 +820,6 @@ def test_datagrepper_http_error(self, mock_get): assert "Failed to retrieve message from Datagrepper" in result.output assert result.exit_code != 0 - @mock.patch("fedora_messaging.cli._get_message", return_value=None) - def test_message_not_found(self, mock_get_message): - """Test handling when no message data is found.""" - message_id = "123" - result = self.runner.invoke(cli.replay, [message_id]) - assert result.exit_code != 0 - @mock.patch("fedora_messaging.cli._get_message", return_value={"some": "data"}) @mock.patch( "fedora_messaging.cli.api.publish", side_effect=Exception("Publish failure") From 880b2a51b37dfae0df17da9465c33521b6b7144f Mon Sep 17 00:00:00 2001 From: freedisch Date: Tue, 19 Mar 2024 18:25:32 +0200 Subject: [PATCH 4/5] Fix typo to --datagrepper-url Signed-off-by: freedisch --- fedora_messaging/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fedora_messaging/cli.py b/fedora_messaging/cli.py index b3bdc7e9..869c9662 100644 --- a/fedora_messaging/cli.py +++ b/fedora_messaging/cli.py @@ -465,7 +465,7 @@ def record(exchange, queue_name, routing_key, app_name, limit, file): @cli.command() @click.argument("message_id") -@click.option("--datagrepper_url", help=_datagrepper_help, default=URL_TEMPLATE) +@click.option("--datagrepper-url", help=_datagrepper_help, default=URL_TEMPLATE) def replay(message_id, datagrepper_url): """Replay a message from Datagrepper by its message ID""" try: From a5dc322792240030cfd85d02b4d560055e61383b Mon Sep 17 00:00:00 2001 From: freedisch Date: Tue, 19 Mar 2024 22:34:51 +0200 Subject: [PATCH 5/5] Adjust --datagrepper-url name in test_cli Signed-off-by: freedisch --- tests/unit/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index c36cf444..7b1650df 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -804,7 +804,7 @@ def test_successful_message_replay(self, mock_publish, mock_get_message): mock_get_message.return_value = message_data result = self.runner.invoke( - cli.replay, [message_id, f"--datagrepper_url={datagrepper_url}"] + cli.replay, [message_id, f"--datagrepper-url={datagrepper_url}"] ) assert (