Skip to content

Commit

Permalink
Merge pull request #119 from airtai/dev
Browse files Browse the repository at this point in the history
Add new campaign structure (#118)
  • Loading branch information
kumaranvpl authored Sep 13, 2024
2 parents 72a9830 + 63c5e52 commit 3cc033e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 61 deletions.
34 changes: 21 additions & 13 deletions google_sheets/data_processing/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,6 @@ def process_campaign_data_f(
return final_df


def _use_template_row(category: Any, template_row: pd.Series) -> bool:
if not template_row["Category"]:
return True

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:
Expand All @@ -190,9 +183,6 @@ def _process_row(
final_df: pd.DataFrame,
target_resource: str,
) -> pd.DataFrame:
if not _use_template_row(new_campaign_row["Category"], template_row):
return final_df

# Positive keywords (Keyword Match Type) should be the same as Match Type (which is used as a part of Ad Group Name)
if target_resource == "keyword" and (
template_row["Negative"].lower() == "false"
Expand Down Expand Up @@ -243,7 +233,7 @@ def _process_row(
new_row["Keyword"].replace(INSERT_CATEGORY, "").strip()
)

new_row = new_row.str.replace(INSERT_CATEGORY, new_campaign_row["Category"])
new_row = new_row.str.replace(INSERT_CATEGORY, new_row["Real Category"])

final_df = pd.concat([final_df, pd.DataFrame([new_row])], ignore_index=True)

Expand All @@ -270,6 +260,15 @@ def process_data_f(
on=on,
)

template_df = template_df[
(template_df["Category"].isna())
| (template_df["Category"] == "")
| (
template_df["Real Category"].str.lower()
== template_df["Category"].str.lower()
)
]

_validate_language_codes(
new_campaign_df,
valid_language_codes=template_df["Language Code"].unique(),
Expand All @@ -279,13 +278,22 @@ def process_data_f(
final_df = pd.DataFrame(columns=template_df.columns)
for _, new_campaign_row in new_campaign_df.iterrows():
for _, template_row in template_df[
template_df["Language Code"] == new_campaign_row["Language Code"]
(template_df["Language Code"] == new_campaign_row["Language Code"])
& (template_df["Ad Group Category"] == new_campaign_row["Category"])
].iterrows():
final_df = _process_row(
new_campaign_row, template_row, final_df, target_resource
)

final_df = final_df.drop(columns=["Language Code", "Category", "Target Category"])
final_df = final_df.drop(
columns=[
"Language Code",
"Category",
"Target Category",
"Ad Group Category",
"Real Category",
]
)
if target_resource == "keyword":
final_df = final_df.drop(columns=["Keyword Match Type"])
final_df = final_df.drop_duplicates(ignore_index=True)
Expand Down
28 changes: 26 additions & 2 deletions tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,29 @@ class TestProcessData:
"Negative",
"Language Code",
"Category",
"Ad Group Category",
"Real Category",
],
[
"Keyword A",
"Exact",
None,
"False",
"EN",
"Bus",
"Bus",
"Bus",
],
[
"Keyword N",
"Broad",
"Campaign",
"True",
"EN",
"Bus",
"Bus",
"Bus",
],
["Keyword A", "Exact", None, "False", "EN", "Bus"],
["Keyword N", "Broad", "Campaign", "True", "EN", "Bus"],
]
),
GoogleSheetValues(
Expand Down Expand Up @@ -515,6 +535,8 @@ async def test_process_data_ads(self) -> None:
"Path 1",
"Path 2",
"Match Type",
"Ad Group Category",
"Real Category",
],
[
"EN",
Expand All @@ -528,6 +550,8 @@ async def test_process_data_ads(self) -> None:
"Path 1",
"Path 2",
"Exact",
"Bus",
"Bus",
],
]
)
Expand Down
56 changes: 10 additions & 46 deletions tests/data_processing/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
_process_row,
_replace_values,
_update_campaign_name,
_use_template_row,
_validate_language_codes,
_validate_output_data_campaign,
process_campaign_data_f,
Expand Down Expand Up @@ -59,58 +58,13 @@ def test_validate_input_data(df: pd.DataFrame, expected: str) -> None:
assert validate_input_data(df, mandatory_columns, "name") == expected


@pytest.mark.parametrize(
("template_row", "expected"),
[
(
pd.Series(
{
"Category": "bus",
}
),
True,
),
(
pd.Series(
{
"Category": None,
}
),
True,
),
(
pd.Series(
{
"Category": "ferry",
}
),
False,
),
(
pd.Series(
{
"Category": "",
}
),
True,
),
],
)
def test_use_template_row(template_row: pd.Series, expected: bool) -> None:
assert _use_template_row("Bus", template_row) == expected


@pytest.mark.parametrize(
("category", "expected_length"),
[
(
"Bus",
1,
),
(
"Ferry",
0,
),
],
)
def test_process_row(
Expand All @@ -130,6 +84,8 @@ def test_process_row(
"Match Type": "Exact",
"Category": "Bus",
"Target Category": "False",
"Ad Group Category": "Bus",
"Real Category": "Bus",
}
)
new_campaign_row = pd.Series(
Expand Down Expand Up @@ -164,6 +120,8 @@ def test_process_row(
"Ad Group Name": ["{INSERT_STATION_FROM} - {INSERT_STATION_TO}"],
"Match Type": ["Exact"],
"Target Category": ["False"],
"Ad Group Category": ["Bus"],
"Real Category": ["Bus"],
}
),
pd.DataFrame(
Expand Down Expand Up @@ -245,6 +203,8 @@ def test_process_row(
"Ad Group Name": ["{INSERT_STATION_FROM} - {INSERT_STATION_TO}"],
"Match Type": ["Exact"],
"Target Category": ["False"],
"Ad Group Category": ["Bus"],
"Real Category": ["Bus"],
}
),
pd.DataFrame(
Expand Down Expand Up @@ -330,6 +290,8 @@ def test_process_row(
],
"Match Type": ["Exact", "Exact"],
"Target Category": ["False", "False"],
"Ad Group Category": ["Bus", "Bus"],
"Real Category": ["Bus", "Bus"],
}
),
pd.DataFrame(
Expand Down Expand Up @@ -389,6 +351,8 @@ def test_process_row(
],
"Match Type": ["Exact", "Exact"],
"Target Category": ["False", "False"],
"Ad Group Category": ["Bus", "Bus"],
"Real Category": ["Bus", "Bus"],
}
),
pd.DataFrame(
Expand Down

0 comments on commit 3cc033e

Please sign in to comment.