diff --git a/google_sheets/data_processing/processing.py b/google_sheets/data_processing/processing.py index e0e6a1b..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(), @@ -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) diff --git a/tests/app/test_app.py b/tests/app/test_app.py index 610bdf7..5669b38 100644 --- a/tests/app/test_app.py +++ b/tests/app/test_app.py @@ -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( @@ -515,6 +535,8 @@ async def test_process_data_ads(self) -> None: "Path 1", "Path 2", "Match Type", + "Ad Group Category", + "Real Category", ], [ "EN", @@ -528,6 +550,8 @@ async def test_process_data_ads(self) -> None: "Path 1", "Path 2", "Exact", + "Bus", + "Bus", ], ] ) diff --git a/tests/data_processing/test_processing.py b/tests/data_processing/test_processing.py index 07d5753..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( @@ -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( @@ -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( @@ -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( @@ -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(