From bbdf666bb4d7cf623ddccc713b03b2ec1456e68a Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 12 Sep 2024 08:48:46 +0000 Subject: [PATCH 1/2] wip --- google_sheets/data_processing/processing.py | 7 +++++-- tests/app/test_app.py | 7 +++++-- tests/data_processing/test_processing.py | 4 ++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/google_sheets/data_processing/processing.py b/google_sheets/data_processing/processing.py index e0e6a1b..d5bbd1e 100644 --- a/google_sheets/data_processing/processing.py +++ b/google_sheets/data_processing/processing.py @@ -279,13 +279,16 @@ 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"] + ) if target_resource == "keyword": final_df = final_df.drop(columns=["Keyword Match Type"]) final_df = final_df.drop_duplicates(ignore_index=True) diff --git a/tests/app/test_app.py b/tests/app/test_app.py index 610bdf7..7d7cbae 100644 --- a/tests/app/test_app.py +++ b/tests/app/test_app.py @@ -394,9 +394,10 @@ class TestProcessData: "Negative", "Language Code", "Category", + "Ad Group Category", ], - ["Keyword A", "Exact", None, "False", "EN", "Bus"], - ["Keyword N", "Broad", "Campaign", "True", "EN", "Bus"], + ["Keyword A", "Exact", None, "False", "EN", "Bus", "Bus"], + ["Keyword N", "Broad", "Campaign", "True", "EN", "Bus", "Bus"], ] ), GoogleSheetValues( @@ -515,6 +516,7 @@ async def test_process_data_ads(self) -> None: "Path 1", "Path 2", "Match Type", + "Ad Group Category", ], [ "EN", @@ -528,6 +530,7 @@ async def test_process_data_ads(self) -> None: "Path 1", "Path 2", "Exact", + "Bus", ], ] ) diff --git a/tests/data_processing/test_processing.py b/tests/data_processing/test_processing.py index 07d5753..7cee343 100644 --- a/tests/data_processing/test_processing.py +++ b/tests/data_processing/test_processing.py @@ -164,6 +164,7 @@ def test_process_row( "Ad Group Name": ["{INSERT_STATION_FROM} - {INSERT_STATION_TO}"], "Match Type": ["Exact"], "Target Category": ["False"], + "Ad Group Category": ["Bus"], } ), pd.DataFrame( @@ -245,6 +246,7 @@ def test_process_row( "Ad Group Name": ["{INSERT_STATION_FROM} - {INSERT_STATION_TO}"], "Match Type": ["Exact"], "Target Category": ["False"], + "Ad Group Category": ["Bus"], } ), pd.DataFrame( @@ -330,6 +332,7 @@ def test_process_row( ], "Match Type": ["Exact", "Exact"], "Target Category": ["False", "False"], + "Ad Group Category": ["Bus", "Bus"], } ), pd.DataFrame( @@ -389,6 +392,7 @@ def test_process_row( ], "Match Type": ["Exact", "Exact"], "Target Category": ["False", "False"], + "Ad Group Category": ["Bus", "Bus"], } ), pd.DataFrame( From aeb6a56335ddf4e31602697a6a2e528b4db5e53f Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Fri, 13 Sep 2024 08:33:25 +0000 Subject: [PATCH 2/2] New campaign type supported --- google_sheets/data_processing/processing.py | 29 +++++++----- tests/app/test_app.py | 25 +++++++++- tests/data_processing/test_processing.py | 52 +++------------------ 3 files changed, 46 insertions(+), 60 deletions(-) diff --git a/google_sheets/data_processing/processing.py b/google_sheets/data_processing/processing.py index d5bbd1e..7c07252 100644 --- a/google_sheets/data_processing/processing.py +++ b/google_sheets/data_processing/processing.py @@ -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: @@ -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" @@ -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) @@ -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(), @@ -287,7 +286,13 @@ def process_data_f( ) final_df = final_df.drop( - columns=["Language Code", "Category", "Target Category", "Ad Group Category"] + columns=[ + "Language Code", + "Category", + "Target Category", + "Ad Group Category", + "Real Category", + ] ) if target_resource == "keyword": final_df = final_df.drop(columns=["Keyword Match Type"]) diff --git a/tests/app/test_app.py b/tests/app/test_app.py index 7d7cbae..5669b38 100644 --- a/tests/app/test_app.py +++ b/tests/app/test_app.py @@ -395,9 +395,28 @@ class TestProcessData: "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", "Bus"], - ["Keyword N", "Broad", "Campaign", "True", "EN", "Bus", "Bus"], ] ), GoogleSheetValues( @@ -517,6 +536,7 @@ async def test_process_data_ads(self) -> None: "Path 2", "Match Type", "Ad Group Category", + "Real Category", ], [ "EN", @@ -531,6 +551,7 @@ async def test_process_data_ads(self) -> None: "Path 2", "Exact", "Bus", + "Bus", ], ] ) diff --git a/tests/data_processing/test_processing.py b/tests/data_processing/test_processing.py index 7cee343..f16b359 100644 --- a/tests/data_processing/test_processing.py +++ b/tests/data_processing/test_processing.py @@ -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, @@ -59,47 +58,6 @@ 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"), [ @@ -107,10 +65,6 @@ def test_use_template_row(template_row: pd.Series, expected: bool) -> None: "Bus", 1, ), - ( - "Ferry", - 0, - ), ], ) def test_process_row( @@ -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( @@ -165,6 +121,7 @@ def test_process_row( "Match Type": ["Exact"], "Target Category": ["False"], "Ad Group Category": ["Bus"], + "Real Category": ["Bus"], } ), pd.DataFrame( @@ -247,6 +204,7 @@ def test_process_row( "Match Type": ["Exact"], "Target Category": ["False"], "Ad Group Category": ["Bus"], + "Real Category": ["Bus"], } ), pd.DataFrame( @@ -333,6 +291,7 @@ def test_process_row( "Match Type": ["Exact", "Exact"], "Target Category": ["False", "False"], "Ad Group Category": ["Bus", "Bus"], + "Real Category": ["Bus", "Bus"], } ), pd.DataFrame( @@ -393,6 +352,7 @@ def test_process_row( "Match Type": ["Exact", "Exact"], "Target Category": ["False", "False"], "Ad Group Category": ["Bus", "Bus"], + "Real Category": ["Bus", "Bus"], } ), pd.DataFrame(