|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +# Assuming forecast_reach.py is structured to be importable and its main logic is in functions. |
| 5 | +# We might need to adjust this if the script's structure is different. |
| 6 | +# For example, if main() directly calls other functions, we might patch those. |
| 7 | +from examples.planning import forecast_reach |
| 8 | + |
| 9 | +class TestForecastReach(unittest.TestCase): |
| 10 | + |
| 11 | + @patch('examples.planning.forecast_reach.GoogleAdsClient.load_from_storage') |
| 12 | + def test_main_forecast_reach(self, mock_load_from_storage): |
| 13 | + # Mock the GoogleAdsClient and its services |
| 14 | + mock_google_ads_client = MagicMock() |
| 15 | + mock_load_from_storage.return_value = mock_google_ads_client |
| 16 | + |
| 17 | + # Mock the ReachPlanService |
| 18 | + mock_reach_plan_service = MagicMock() |
| 19 | + mock_google_ads_client.get_service.return_value = mock_reach_plan_service |
| 20 | + |
| 21 | + # Mock the responses from ReachPlanService |
| 22 | + # list_plannable_locations response |
| 23 | + mock_plannable_locations_response = MagicMock() |
| 24 | + mock_plannable_locations_response.plannable_locations = [ |
| 25 | + MagicMock(name="Location1", id="123", parent_country_id="321"), |
| 26 | + MagicMock(name="Location2", id="456", parent_country_id="654") |
| 27 | + ] |
| 28 | + # list_plannable_products response |
| 29 | + mock_plannable_products_response = MagicMock() |
| 30 | + mock_product_metadata = MagicMock() |
| 31 | + mock_product_metadata.plannable_product_code = "YOUTUBE_REACH" |
| 32 | + mock_product_metadata.plannable_product_name = "YouTube Reach" |
| 33 | + mock_product_metadata.plannable_targeting.age_ranges = [MagicMock(name="AGE_RANGE_18_24")] |
| 34 | + mock_product_metadata.plannable_targeting.genders = [MagicMock(type_=MagicMock(name="GENDER_FEMALE"))] |
| 35 | + mock_product_metadata.plannable_targeting.devices = [MagicMock(type_=MagicMock(name="DEVICE_MOBILE"))] |
| 36 | + mock_plannable_products_response.product_metadata = [mock_product_metadata] |
| 37 | + # generate_reach_forecast response |
| 38 | + mock_reach_forecast_response = MagicMock() |
| 39 | + mock_reach_curve = MagicMock() |
| 40 | + mock_forecast_point = MagicMock() |
| 41 | + mock_forecast_point.cost_micros = 10000000 # e.g., 10 units of currency |
| 42 | + mock_forecast_point.forecast.on_target_reach = 100000 |
| 43 | + mock_forecast_point.forecast.on_target_impressions = 150000 |
| 44 | + mock_forecast_point.forecast.total_reach = 120000 |
| 45 | + mock_forecast_point.forecast.total_impressions = 180000 |
| 46 | + mock_planned_product_reach_forecast = MagicMock() |
| 47 | + mock_planned_product_reach_forecast.plannable_product_code = "YOUTUBE_REACH" |
| 48 | + mock_planned_product_reach_forecast.cost_micros = 10000000 |
| 49 | + mock_forecast_point.planned_product_reach_forecasts = [mock_planned_product_reach_forecast] |
| 50 | + mock_reach_curve.reach_forecasts = [mock_forecast_point] |
| 51 | + mock_reach_forecast_response.reach_curve = mock_reach_curve |
| 52 | + |
| 53 | + # Configure side_effects or return_values for service calls |
| 54 | + mock_reach_plan_service.list_plannable_locations.return_value = mock_plannable_locations_response |
| 55 | + mock_reach_plan_service.list_plannable_products.return_value = mock_plannable_products_response |
| 56 | + mock_reach_plan_service.generate_reach_forecast.return_value = mock_reach_forecast_response |
| 57 | + |
| 58 | + # Mock enums (this might be more complex depending on actual usage) |
| 59 | + mock_enums = MagicMock() |
| 60 | + mock_enums.ReachPlanAgeRangeEnum.AGE_RANGE_18_65_UP = "AGE_RANGE_18_65_UP" |
| 61 | + mock_enums.GenderTypeEnum.FEMALE = "FEMALE" |
| 62 | + mock_enums.GenderTypeEnum.MALE = "MALE" |
| 63 | + mock_enums.DeviceEnum.DESKTOP = "DESKTOP" |
| 64 | + mock_enums.DeviceEnum.MOBILE = "MOBILE" |
| 65 | + mock_enums.DeviceEnum.TABLET = "TABLET" |
| 66 | + mock_google_ads_client.enums = mock_enums |
| 67 | + |
| 68 | + # Call the main function of forecast_reach.py |
| 69 | + # We need a customer_id for this test |
| 70 | + test_customer_id = "1234567890" |
| 71 | + |
| 72 | + # Capture stdout to check output |
| 73 | + with patch('sys.stdout', new_callable=MagicMock) as mock_stdout: |
| 74 | + forecast_reach.main(mock_google_ads_client, test_customer_id) |
| 75 | + |
| 76 | + # Assert that the main logic was called (e.g., services were used) |
| 77 | + # mock_load_from_storage.assert_called_once_with(version="v19") # This is removed as client is injected |
| 78 | + mock_google_ads_client.get_service.assert_any_call("ReachPlanService") |
| 79 | + |
| 80 | + # Assert that the service methods were called |
| 81 | + mock_reach_plan_service.list_plannable_locations.assert_called_once() |
| 82 | + mock_reach_plan_service.list_plannable_products.assert_called_once() |
| 83 | + # generate_reach_forecast is called by request_reach_curve, which is called by forecast_manual_mix |
| 84 | + mock_reach_plan_service.generate_reach_forecast.assert_called_once() |
| 85 | + |
| 86 | + # Example assertion on output (very basic, might need more specific checks) |
| 87 | + # This checks if "Plannable Locations" was printed by show_plannable_locations |
| 88 | + self.assertTrue(any("Plannable Locations" in call_args[0][0] for call_args in mock_stdout.write.call_args_list if call_args[0])) |
| 89 | + # This checks if "Plannable Products for Location ID" was printed by show_plannable_products |
| 90 | + self.assertTrue(any("Plannable Products for Location ID" in call_args[0][0] for call_args in mock_stdout.write.call_args_list if call_args[0])) |
| 91 | + # This checks if "Currency, Cost, On-Target Reach" header was printed by request_reach_curve |
| 92 | + self.assertTrue(any("Currency, Cost, On-Target Reach" in call_args[0][0] for call_args in mock_stdout.write.call_args_list if call_args[0])) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + unittest.main() |
0 commit comments