Skip to content

Commit

Permalink
Implement workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
rjambrecic committed Nov 21, 2024
1 parent af4fbb0 commit c630241
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 20 deletions.
41 changes: 32 additions & 9 deletions mailchimp_api/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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"
55 changes: 44 additions & 11 deletions tests/test_workflow.py
Original file line number Diff line number Diff line change
@@ -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": ["[email protected]"]}),
) 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

0 comments on commit c630241

Please sign in to comment.