Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 PRs #153

Merged
merged 2 commits into from
Oct 31, 2024
Merged

2 PRs #153

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions google_sheets/data_processing/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ def _replace_values(
return new_row


def _replace_headline_values(new_row: pd.Series, station: Dict[str, Any]) -> pd.Series:
# Filter columns that start with "Headline"
headline_columns = [col for col in new_row.index if col.startswith("Headline")]

# Perform replacements only in the headline columns
for col in headline_columns:
new_row[col] = (
new_row[col]
.replace(INSERT_STATION_FROM, station["Station From"])
.replace(INSERT_STATION_TO, station["Station To"])
)
return new_row


USE_ORIGINAL_STATION_FROM = ["Transfer", "Taxi"]


def _process_row(
new_campaign_row: pd.Series,
template_row: pd.Series,
Expand Down Expand Up @@ -214,6 +231,10 @@ def _process_row(
language_code=new_row["Language Code"],
include_locations=include_locations,
)
if new_row["Category"] in USE_ORIGINAL_STATION_FROM:
# Use the original Station From in headlines for both directions
new_row = _replace_headline_values(new_row, stations[0])

new_row = _replace_values(new_campaign_row, new_row, station)

if target_resource == "ad":
Expand Down
33 changes: 30 additions & 3 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_headline_values,
_replace_values,
_update_campaign_name,
_validate_language_codes,
Expand Down Expand Up @@ -58,13 +59,39 @@ def test_validate_input_data(df: pd.DataFrame, expected: str) -> None:
assert validate_input_data(df, mandatory_columns, "name") == expected


def test_replace_headline_values() -> None:
row = pd.Series(
{
"Name": "{INSERT_STATION_FROM}",
"Headline 1": "{INSERT_STATION_FROM} - {INSERT_STATION_TO}",
"Headline 2": "{INSERT_STATION_FROM}",
}
)
station = {
"Station From": "A",
"Station To": "B",
}

row = _replace_headline_values(row, station)
expected_row = pd.Series(
{
"Name": "{INSERT_STATION_FROM}",
"Headline 1": "A - B",
"Headline 2": "A",
}
)

assert row.equals(expected_row)


@pytest.mark.parametrize(
("category", "expected_length"),
[
(
"Bus",
1,
),
("Transfer", 1),
],
)
def test_process_row(
Expand All @@ -82,10 +109,10 @@ def test_process_row(
"Level": "",
"Keyword Match Type": "Exact",
"Match Type": "Exact",
"Category": "Bus",
"Category": category,
"Target Category": "False",
"Ad Group Category": "Bus",
"Real Category": "Bus",
"Ad Group Category": category,
"Real Category": category,
}
)
new_campaign_row = pd.Series(
Expand Down
Loading