From ece5d1118179892f5f787081caa6600a6a161c2d Mon Sep 17 00:00:00 2001 From: rjambrecic <32619626+rjambrecic@users.noreply.github.com> Date: Wed, 11 Sep 2024 12:37:08 +0200 Subject: [PATCH] Update ads structure (#115) * Updated ad copy structure * Don't use headlines with ticket prices if ticket price is not defined --- google_sheets/data_processing/processing.py | 32 +++++++--- tests/app/test_app.py | 2 + tests/data_processing/test_processing.py | 71 +++++++++++++++++++++ 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/google_sheets/data_processing/processing.py b/google_sheets/data_processing/processing.py index 2fc9903..e0e6a1b 100644 --- a/google_sheets/data_processing/processing.py +++ b/google_sheets/data_processing/processing.py @@ -1,4 +1,4 @@ -from typing import Any, List, Literal +from typing import Any, Dict, List, Literal import pandas as pd @@ -166,6 +166,24 @@ def _use_template_row(category: Any, template_row: pd.Series) -> bool: return template_row["Category"].lower() == str(category).lower() # type: ignore[no-any-return] +def _replace_values( + new_campaign_row: pd.Series, new_row: pd.Series, station: Dict[str, Any] +) -> pd.Series: + new_row = new_row.str.replace(INSERT_COUNTRY, new_campaign_row["Country"]) + new_row = new_row.str.replace(INSERT_STATION_FROM, station["Station From"]) + new_row = new_row.str.replace(INSERT_STATION_TO, station["Station To"]) + new_row = new_row.str.replace(INSERT_CRITERION_TYPE, new_row["Match Type"]) + if new_campaign_row["Ticket Price"]: + new_row = new_row.str.replace( + INSERT_TICKET_PRICE, new_campaign_row["Ticket Price"] + ) + else: + # Locate all the columns with the string "{INSERT_TICKET_PRICE}" + # and replace them WHOLE column with an empty string (not only the string) + new_row = new_row.str.replace(r".*{INSERT_TICKET_PRICE}.*", "", regex=True) + return new_row + + def _process_row( new_campaign_row: pd.Series, template_row: pd.Series, @@ -206,14 +224,7 @@ def _process_row( language_code=new_row["Language Code"], include_locations=include_locations, ) - - new_row = new_row.str.replace(INSERT_COUNTRY, new_campaign_row["Country"]) - new_row = new_row.str.replace(INSERT_STATION_FROM, station["Station From"]) - new_row = new_row.str.replace(INSERT_STATION_TO, station["Station To"]) - new_row = new_row.str.replace(INSERT_CRITERION_TYPE, new_row["Match Type"]) - new_row = new_row.str.replace( - INSERT_TICKET_PRICE, new_campaign_row["Ticket Price"] - ) + new_row = _replace_values(new_campaign_row, new_row, station) if target_resource == "ad": new_row["Final URL"] = station["Final Url"] @@ -251,11 +262,12 @@ def process_data_f( ].str.upper() template_df["Language Code"] = template_df["Language Code"].str.upper() new_campaign_df["Language Code"] = new_campaign_df["Language Code"].str.upper() + on = ["Language Code", "Match Type"] if target_resource == "ad" else "Language Code" template_df = pd.merge( merged_campaigns_ad_groups_df, template_df, how="inner", - on="Language Code", + on=on, ) _validate_language_codes( diff --git a/tests/app/test_app.py b/tests/app/test_app.py index 8dd520a..610bdf7 100644 --- a/tests/app/test_app.py +++ b/tests/app/test_app.py @@ -514,6 +514,7 @@ async def test_process_data_ads(self) -> None: "Description Line 2", "Path 1", "Path 2", + "Match Type", ], [ "EN", @@ -526,6 +527,7 @@ async def test_process_data_ads(self) -> None: "Description Line 2", "Path 1", "Path 2", + "Exact", ], ] ) diff --git a/tests/data_processing/test_processing.py b/tests/data_processing/test_processing.py index f4c4320..07d5753 100644 --- a/tests/data_processing/test_processing.py +++ b/tests/data_processing/test_processing.py @@ -7,6 +7,7 @@ _copy_all_with_prefixes, _get_target_location, _process_row, + _replace_values, _update_campaign_name, _use_template_row, _validate_language_codes, @@ -766,3 +767,73 @@ def test_validate_output_data_campaign( assert result["Issues"].values[0] == expected_issues else: assert result.equals(expected) + + +@pytest.mark.parametrize( + ("new_campaign_row", "expected"), + [ + ( + pd.Series( + { + "Country": "USA", + "Language Code": "EN", + "Category": "Bus", + "Ticket Price": "100", + } + ), + pd.Series( + { + "Campaign Name": "USA - A - B", + "Ad Group Name": "A - B", + "Headline 1": "H1", + "Headline 2": "H2", + "Headline 3": "H3 100", + "Description 1": "D1", + "Match Type": "Exact", + } + ), + ), + ( + pd.Series( + { + "Country": "USA", + "Language Code": "EN", + "Category": "Bus", + "Ticket Price": "", + } + ), + pd.Series( + { + "Campaign Name": "USA - A - B", + "Ad Group Name": "A - B", + "Headline 1": "H1", + "Headline 2": "H2", + "Headline 3": "", + "Description 1": "D1", + "Match Type": "Exact", + } + ), + ), + ], +) +def test_replace_values( + new_campaign_row: pd.Series, + expected: pd.Series, +) -> None: + new_row = pd.Series( + { + "Campaign Name": "{INSERT_COUNTRY} - {INSERT_STATION_FROM} - {INSERT_STATION_TO}", + "Ad Group Name": "{INSERT_STATION_FROM} - {INSERT_STATION_TO}", + "Headline 1": "H1", + "Headline 2": "H2", + "Headline 3": "H3 {INSERT_TICKET_PRICE}", + "Description 1": "D1", + "Match Type": "Exact", + } + ) + station = { + "Station From": "A", + "Station To": "B", + } + result = _replace_values(new_campaign_row, new_row, station) + assert result.equals(expected)