From c6302419e108254116e0da75708b450d9583b392 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 21 Nov 2024 14:57:06 +0000 Subject: [PATCH] Implement workflow --- mailchimp_api/workflow.py | 41 ++++++++++++++++++++++------- tests/test_workflow.py | 55 +++++++++++++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/mailchimp_api/workflow.py b/mailchimp_api/workflow.py index 1264bfa..b27082b 100644 --- a/mailchimp_api/workflow.py +++ b/mailchimp_api/workflow.py @@ -27,8 +27,20 @@ def _get_config() -> Config: config = _get_config() +def _wait_for_file(timestamp: str) -> pd.DataFrame: + file_name = f"uploaded-file-{timestamp}.csv" + file_path = UPLOADED_FILES_DIR / file_name + while not file_path.exists(): + time.sleep(2) + + df = pd.read_csv(file_path) + file_path.unlink() + + return df + + @wf.register(name="mailchimp_chat", description="Mailchimp tags update chat") # type: ignore[misc] -def simple_workflow(ui: UI, params: dict[str, Any]) -> str: +def mailchimp_chat(ui: UI, params: dict[str, Any]) -> str: timestamp = time.strftime("%Y-%m-%d-%H-%M-%S") body = f"""Please upload **.csv** file with the email addresses for which you want to update the tags. @@ -40,13 +52,7 @@ def simple_workflow(ui: UI, params: dict[str, Any]) -> str: body=body, ) - file_name = f"uploaded-file-{timestamp}.csv" - file_path = UPLOADED_FILES_DIR / file_name - while not file_path.exists(): - time.sleep(2) - - df = pd.read_csv(file_path) - file_path.unlink() + df = _wait_for_file(timestamp) list_name = None while list_name is None: @@ -59,5 +65,22 @@ def simple_workflow(ui: UI, params: dict[str, Any]) -> str: add_tag_members, _ = update_tags( crm_df=df, config=config, list_name=list_name.strip() ) + if not add_tag_members: + return "No tags added" + + add_tag_members = dict(sorted(add_tag_members.items())) + updates_per_tag = "\n".join( + [f"- **{key}**: {len(value)}" for key, value in add_tag_members.items()] + ) + body = f"""Number of updates per tag: + +{updates_per_tag} - return f"Added tags\n{add_tag_members}" +(It might take some time for updates to reflect in Mailchimp) +""" + ui.text_message( + sender="Workflow", + recipient="User", + body=body, + ) + return "Task Completed" diff --git a/tests/test_workflow.py b/tests/test_workflow.py index 60bf452..2305adf 100644 --- a/tests/test_workflow.py +++ b/tests/test_workflow.py @@ -1,19 +1,52 @@ -from uuid import uuid4 +from unittest.mock import MagicMock, call, patch -import pytest -from fastagency.ui.console import ConsoleUI +import pandas as pd from mailchimp_api.workflow import wf -from tests.conftest import InputMock -@pytest.mark.skip(reason="Skipping tests for now") -def test_workflow(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr("builtins.input", InputMock([""] * 5)) +def test_workflow() -> None: + ui = MagicMock() + ui.text_message.return_value = None + ui.text_input.return_value = "test-list" - result = wf.run( - name="simple_learning", - ui=ConsoleUI().create_workflow_ui(workflow_uuid=uuid4().hex), - ) + with ( + patch( + "mailchimp_api.workflow._wait_for_file", + return_value=pd.DataFrame({"email": ["email1@gmail.com"]}), + ) as mock_wait_for_file, + patch("mailchimp_api.workflow.update_tags") as mock_update_tags, + ): + mock_update_tags.return_value = ( + { + "M4": ["a", "b"], + "M5": ["c", "d", "e"], + "M3": ["f"], + }, + {}, + ) + result = wf.run( + name="mailchimp_chat", + ui=ui, + ) + + mock_wait_for_file.assert_called_once() + mock_update_tags.assert_called_once() + + expected_body = """Number of updates per tag: + +- **M3**: 1 +- **M4**: 2 +- **M5**: 3 + +(It might take some time for updates to reflect in Mailchimp) +""" + expected_call_args = call( + sender="Workflow", + recipient="User", + body=expected_body, + ) + + assert ui.text_message.call_args_list[1] == expected_call_args assert result is not None