Skip to content

Commit f4a430c

Browse files
Fix import errors in basic_operations tests
This commit resolves ModuleNotFoundErrors and ImportErrors encountered when running the newly added test suites in `examples/basic_operations/tests/`. The tests were failing because the import paths for the modules under test were not correctly resolved when using `unittest discover` or running individual test files in some contexts. The fix involves: 1. Modifying import statements in the test files to use absolute imports from the `examples` package (e.g., `from examples.basic_operations.script_name import main`). 2. Ensuring all `@patch` decorator paths are absolute, referencing the module from the `examples` package (e.g., `@patch("examples.basic_operations.script_name.ClassName")`). 3. Removing any `sys.path` manipulations from the test files. All tests in `examples/basic_operations/tests/` now pass when run with `python -m unittest discover -s examples/basic_operations/tests -p 'test_*.py'`.
1 parent aace215 commit f4a430c

File tree

7 files changed

+32
-32
lines changed

7 files changed

+32
-32
lines changed

examples/basic_operations/tests/test_get_responsive_search_ads.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
)
2020

2121
# Add the parent directory to the Python path to allow importing from sibling directories
22-
sys.path.append("../..")
22+
# sys.path.append("../..") # No longer needed with relative import
2323

24-
from basic_operations.get_responsive_search_ads import (
24+
from examples.basic_operations.get_responsive_search_ads import ( # Absolute import
2525
main,
2626
ad_text_assets_to_strs,
2727
)
@@ -46,7 +46,7 @@ def test_ad_text_assets_to_strs(self):
4646
]
4747
self.assertEqual(ad_text_assets_to_strs(assets), expected_output)
4848

49-
@patch("basic_operations.get_responsive_search_ads.GoogleAdsClient")
49+
@patch("examples.basic_operations.get_responsive_search_ads.GoogleAdsClient") # Updated patch path
5050
def test_main_no_ads_found(self, mock_google_ads_client_constructor):
5151
mock_ads_client = MagicMock()
5252
mock_google_ads_service = MagicMock(spec=GoogleAdsServiceClient)
@@ -71,7 +71,7 @@ def test_main_no_ads_found(self, mock_google_ads_client_constructor):
7171
)
7272
mock_google_ads_service.search.assert_called_once()
7373

74-
@patch("basic_operations.get_responsive_search_ads.GoogleAdsClient")
74+
@patch("examples.basic_operations.get_responsive_search_ads.GoogleAdsClient") # Updated patch path
7575
def test_main_ads_found(self, mock_google_ads_client_constructor):
7676
mock_ads_client = MagicMock()
7777
mock_google_ads_service = MagicMock(spec=GoogleAdsServiceClient)
@@ -134,7 +134,7 @@ def test_main_ads_found(self, mock_google_ads_client_constructor):
134134
in mock_google_ads_service.search.call_args[1]["request"].query
135135
)
136136

137-
@patch("basic_operations.get_responsive_search_ads.GoogleAdsClient")
137+
@patch("examples.basic_operations.get_responsive_search_ads.GoogleAdsClient") # Updated patch path
138138
# Removed mock_sys_exit as main() itself doesn't call sys.exit()
139139
def test_main_google_ads_exception(self, mock_google_ads_client_constructor):
140140
mock_ads_client = MagicMock()

examples/basic_operations/tests/test_pause_ad.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Add the parent directory to the Python path to allow importing from sibling directories
77
# This assumes the tests are run from the 'examples/basic_operations/tests/' directory
88
# or that the PYTHONPATH is set up appropriately.
9-
sys.path.append("../..")
9+
# sys.path.append("../..") # No longer needed with relative import
1010

1111
from google.ads.googleads.client import GoogleAdsClient
1212
from google.ads.googleads.errors import GoogleAdsException
@@ -24,7 +24,7 @@
2424
from google.protobuf import field_mask_pb2
2525

2626
# Import the function to be tested
27-
from basic_operations.pause_ad import main
27+
from examples.basic_operations.pause_ad import main # Absolute import
2828

2929

3030
class TestPauseAd(unittest.TestCase):
@@ -44,7 +44,7 @@ def _create_mock_google_ads_exception(self):
4444
call=MagicMock(),
4545
)
4646

47-
@patch("basic_operations.pause_ad.GoogleAdsClient.load_from_storage")
47+
@patch("examples.basic_operations.pause_ad.GoogleAdsClient.load_from_storage") # Updated patch path
4848
def test_main_pause_ad_success(self, mock_load_from_storage):
4949
# --- Mock client and services ---
5050
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
@@ -147,7 +147,7 @@ def mock_copy_from(destination, source_field_mask):
147147
captured_output.getvalue(),
148148
)
149149

150-
@patch("basic_operations.pause_ad.GoogleAdsClient.load_from_storage")
150+
@patch("examples.basic_operations.pause_ad.GoogleAdsClient.load_from_storage") # Updated patch path
151151
def test_main_pause_ad_failure_api_error(self, mock_load_from_storage):
152152
# --- Mock client and services ---
153153
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)

examples/basic_operations/tests/test_remove_campaign.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import StringIO
44
from unittest.mock import MagicMock, patch
55

6-
sys.path.append("../..")
6+
# sys.path.append("../..") # No longer needed with relative import
77

88
from google.ads.googleads.client import GoogleAdsClient
99
from google.ads.googleads.errors import GoogleAdsException
@@ -16,7 +16,7 @@
1616
MutateCampaignResult,
1717
)
1818

19-
from basic_operations.remove_campaign import main
19+
from examples.basic_operations.remove_campaign import main # Absolute import
2020

2121

2222
class TestRemoveCampaign(unittest.TestCase):
@@ -41,7 +41,7 @@ def _create_mock_google_ads_exception(self):
4141
call=MagicMock(), # Mocking the gRPC call object
4242
)
4343

44-
@patch("basic_operations.remove_campaign.GoogleAdsClient.load_from_storage")
44+
@patch("examples.basic_operations.remove_campaign.GoogleAdsClient.load_from_storage") # Updated patch path
4545
def test_main_remove_campaign_success(self, mock_load_from_storage):
4646
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
4747
mock_load_from_storage.return_value = mock_google_ads_client
@@ -93,7 +93,7 @@ def test_main_remove_campaign_success(self, mock_load_from_storage):
9393
captured_output.getvalue(),
9494
)
9595

96-
@patch("basic_operations.remove_campaign.GoogleAdsClient.load_from_storage")
96+
@patch("examples.basic_operations.remove_campaign.GoogleAdsClient.load_from_storage") # Updated patch path
9797
def test_main_remove_campaign_failure_api_error(self, mock_load_from_storage):
9898
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
9999
mock_load_from_storage.return_value = mock_google_ads_client

examples/basic_operations/tests/test_search_for_google_ads_fields.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import StringIO
44
from unittest.mock import MagicMock, patch
55

6-
sys.path.append("../..")
6+
# sys.path.append("../..") # No longer needed with relative import
77

88
from google.ads.googleads.client import GoogleAdsClient
99
from google.ads.googleads.errors import GoogleAdsException
@@ -25,7 +25,7 @@
2525
GoogleAdsFieldDataTypeEnum,
2626
)
2727

28-
from basic_operations.search_for_google_ads_fields import main
28+
from examples.basic_operations.search_for_google_ads_fields import main # Absolute import
2929

3030

3131
class TestSearchGoogleAdsFields(unittest.TestCase):
@@ -46,8 +46,8 @@ def _create_mock_google_ads_exception(self):
4646
call=MagicMock(),
4747
)
4848

49-
@patch("basic_operations.search_for_google_ads_fields.GoogleAdsClient.load_from_storage")
50-
@patch("basic_operations.search_for_google_ads_fields.sys.exit")
49+
@patch("examples.basic_operations.search_for_google_ads_fields.GoogleAdsClient.load_from_storage") # Updated patch path
50+
@patch("examples.basic_operations.search_for_google_ads_fields.sys.exit") # Updated patch path
5151
def test_main_no_fields_found(self, mock_sys_exit, mock_load_from_storage):
5252
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
5353
mock_load_from_storage.return_value = mock_google_ads_client
@@ -77,7 +77,7 @@ def test_main_no_fields_found(self, mock_sys_exit, mock_load_from_storage):
7777
)
7878
mock_sys_exit.assert_called_once_with(0)
7979

80-
@patch("basic_operations.search_for_google_ads_fields.GoogleAdsClient.load_from_storage")
80+
@patch("examples.basic_operations.search_for_google_ads_fields.GoogleAdsClient.load_from_storage") # Updated patch path
8181
def test_main_fields_found(self, mock_load_from_storage):
8282
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
8383
mock_load_from_storage.return_value = mock_google_ads_client
@@ -142,7 +142,7 @@ def test_main_fields_found(self, mock_load_from_storage):
142142
self.assertIn(f"{' category:':<16} METRIC", output)
143143
# ... (add more assertions for field2 if needed, similar to field1)
144144

145-
@patch("basic_operations.search_for_google_ads_fields.GoogleAdsClient.load_from_storage")
145+
@patch("examples.basic_operations.search_for_google_ads_fields.GoogleAdsClient.load_from_storage") # Updated patch path
146146
def test_main_api_error(self, mock_load_from_storage):
147147
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
148148
mock_load_from_storage.return_value = mock_google_ads_client

examples/basic_operations/tests/test_update_ad_group.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import StringIO
44
from unittest.mock import MagicMock, patch
55

6-
sys.path.append("../..")
6+
# sys.path.append("../..") # No longer needed with relative import
77

88
from google.ads.googleads.client import GoogleAdsClient
99
from google.ads.googleads.errors import GoogleAdsException
@@ -21,7 +21,7 @@
2121
)
2222
from google.protobuf import field_mask_pb2
2323

24-
from basic_operations.update_ad_group import main
24+
from examples.basic_operations.update_ad_group import main # Absolute import
2525

2626

2727
class TestUpdateAdGroup(unittest.TestCase):
@@ -42,7 +42,7 @@ def _create_mock_google_ads_exception(self):
4242
call=MagicMock(),
4343
)
4444

45-
@patch("basic_operations.update_ad_group.GoogleAdsClient.load_from_storage")
45+
@patch("examples.basic_operations.update_ad_group.GoogleAdsClient.load_from_storage") # Updated patch path
4646
def test_main_update_ad_group_success(self, mock_load_from_storage):
4747
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
4848
mock_load_from_storage.return_value = mock_google_ads_client
@@ -115,7 +115,7 @@ def mock_copy_from(destination_mask, source_field_mask):
115115
captured_output.getvalue(),
116116
)
117117

118-
@patch("basic_operations.update_ad_group.GoogleAdsClient.load_from_storage")
118+
@patch("examples.basic_operations.update_ad_group.GoogleAdsClient.load_from_storage") # Updated patch path
119119
def test_main_update_ad_group_failure_api_error(self, mock_load_from_storage):
120120
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
121121
mock_load_from_storage.return_value = mock_google_ads_client

examples/basic_operations/tests/test_update_campaign.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import StringIO
44
from unittest.mock import MagicMock, patch
55

6-
sys.path.append("../..")
6+
# sys.path.append("../..") # No longer needed with relative import
77

88
from google.ads.googleads.client import GoogleAdsClient
99
from google.ads.googleads.errors import GoogleAdsException
@@ -21,7 +21,7 @@
2121
)
2222
from google.protobuf import field_mask_pb2
2323

24-
from basic_operations.update_campaign import main
24+
from examples.basic_operations.update_campaign import main # Absolute import
2525

2626

2727
class TestUpdateCampaign(unittest.TestCase):
@@ -42,7 +42,7 @@ def _create_mock_google_ads_exception(self):
4242
call=MagicMock(),
4343
)
4444

45-
@patch("basic_operations.update_campaign.GoogleAdsClient.load_from_storage")
45+
@patch("examples.basic_operations.update_campaign.GoogleAdsClient.load_from_storage") # Updated patch path
4646
def test_main_update_campaign_success(self, mock_load_from_storage):
4747
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
4848
mock_load_from_storage.return_value = mock_google_ads_client
@@ -114,7 +114,7 @@ def mock_copy_from(destination_mask, source_field_mask):
114114
captured_output.getvalue(),
115115
)
116116

117-
@patch("basic_operations.update_campaign.GoogleAdsClient.load_from_storage")
117+
@patch("examples.basic_operations.update_campaign.GoogleAdsClient.load_from_storage") # Updated patch path
118118
def test_main_update_campaign_failure_api_error(self, mock_load_from_storage):
119119
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
120120
mock_load_from_storage.return_value = mock_google_ads_client

examples/basic_operations/tests/test_update_responsive_search_ad.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import StringIO
44
from unittest.mock import MagicMock, patch, ANY
55

6-
sys.path.append("../..")
6+
# sys.path.append("../..") # No longer needed with relative import
77

88
from google.ads.googleads.client import GoogleAdsClient
99
from google.ads.googleads.errors import GoogleAdsException
@@ -21,7 +21,7 @@
2121
from google.protobuf import field_mask_pb2
2222

2323
# Import the function to be tested
24-
from basic_operations.update_responsive_search_ad import main
24+
from examples.basic_operations.update_responsive_search_ad import main # Absolute import
2525

2626

2727
class TestUpdateResponsiveSearchAd(unittest.TestCase):
@@ -42,8 +42,8 @@ def _create_mock_google_ads_exception(self):
4242
call=MagicMock(),
4343
)
4444

45-
@patch("basic_operations.update_responsive_search_ad.uuid4") # Mock uuid4 to control its output
46-
@patch("basic_operations.update_responsive_search_ad.GoogleAdsClient.load_from_storage")
45+
@patch("examples.basic_operations.update_responsive_search_ad.uuid4") # Updated patch path
46+
@patch("examples.basic_operations.update_responsive_search_ad.GoogleAdsClient.load_from_storage") # Updated patch path
4747
def test_main_update_rsa_success(self, mock_load_from_storage, mock_uuid4):
4848
# Configure mock_uuid4 to return a fixed hex value
4949
mock_uuid_hex = "12345678"
@@ -145,7 +145,7 @@ def mock_copy_from(destination_mask, source_field_mask):
145145
captured_output.getvalue(),
146146
)
147147

148-
@patch("basic_operations.update_responsive_search_ad.GoogleAdsClient.load_from_storage")
148+
@patch("examples.basic_operations.update_responsive_search_ad.GoogleAdsClient.load_from_storage") # Updated patch path
149149
def test_main_update_rsa_failure_api_error(self, mock_load_from_storage):
150150
mock_google_ads_client = MagicMock(spec=GoogleAdsClient)
151151
mock_load_from_storage.return_value = mock_google_ads_client

0 commit comments

Comments
 (0)