Skip to content

Commit

Permalink
Merge pull request #116 from airtai/dev
Browse files Browse the repository at this point in the history
Update ads structure (#115)
  • Loading branch information
kumaranvpl authored Sep 11, 2024
2 parents 75ea0b4 + ece5d11 commit 72a9830
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
32 changes: 22 additions & 10 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 Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ async def test_process_data_ads(self) -> None:
"Description Line 2",
"Path 1",
"Path 2",
"Match Type",
],
[
"EN",
Expand All @@ -526,6 +527,7 @@ async def test_process_data_ads(self) -> None:
"Description Line 2",
"Path 1",
"Path 2",
"Exact",
],
]
)
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 72a9830

Please sign in to comment.