Skip to content

Commit

Permalink
Don't use headlines with ticket prices if ticket price is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
rjambrecic committed Sep 11, 2024
1 parent 5a4acce commit 7775cac
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
29 changes: 20 additions & 9 deletions google_sheets/data_processing/processing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List, Literal
from typing import Any, Dict, List, Literal

import pandas as pd

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
Expand Down
71 changes: 71 additions & 0 deletions tests/data_processing/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_copy_all_with_prefixes,
_get_target_location,
_process_row,
_replace_values,
_update_campaign_name,
_use_template_row,
_validate_language_codes,
Expand Down Expand Up @@ -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)

0 comments on commit 7775cac

Please sign in to comment.