From 7775cac275952e668fecfeb651cb83f9afb41d62 Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Wed, 11 Sep 2024 10:32:40 +0000 Subject: [PATCH] Don't use headlines with ticket prices if ticket price is not defined --- google_sheets/data_processing/processing.py | 29 ++++++--- tests/data_processing/test_processing.py | 71 +++++++++++++++++++++ 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/google_sheets/data_processing/processing.py b/google_sheets/data_processing/processing.py index 244ac1a..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"] 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)