Skip to content

Commit e31919a

Browse files
google-labs-jules[bot]BenRKarl
authored andcommitted
Fix: Resolve path and mock assertions in test_campaign_report_to_csv.py
This commit addresses remaining errors in `examples/misc/tests/test_campaign_report_to_csv.py`: 1. **Success Path Tests (`test_main_success_with_headers`, `test_main_success_without_headers`):** * I corrected assertions for `builtins.open` calls. The tests now mock `os.path.abspath` to ensure a predictable absolute path is generated by the script. The `open()` call is then asserted using this controlled absolute path and matching your script's arguments (e.g., `newline=""`, no `encoding`). This resolves AssertionErrors where the expected relative path did not match the actual absolute path. * These tests were previously updated to use concrete helper classes for mock API response data and to correctly assert `search_stream` calls. 2. **Argument Parsing Tests (`test_argument_parsing`, `test_argument_parsing_no_headers_flag`):** * These tests were previously refactored to use a helper method (`_simulate_script_main_block`) that directly simulates your script's `if __name__ == "__main__":` block. This resolved TypeErrors related to `os.path.join`. 3. **`RecursionError` (from `test_main_google_ads_exception`):** * This was previously resolved by updates to `test_utils.py` which simplified the creation of mock `GoogleAdsException` instances. All known FAILURES for `test_campaign_report_to_csv.py` should now be addressed.
1 parent ecbcad6 commit e31919a

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

examples/misc/tests/test_campaign_report_to_csv.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from unittest import mock
33
import csv
44
import io # Use io.StringIO for mocking file operations in memory
5+
import os # Added import os
56

67
# Assuming the script to be tested is in the parent directory.
78
import argparse # Ensure argparse is imported
@@ -109,9 +110,11 @@ def mock_get_type_side_effect(type_name):
109110
# For now, removing it to avoid confusion with the new MockCSVRow.
110111
# def _create_mock_row(...):
111112

113+
@mock.patch("os.path.abspath") # Added
112114
@mock.patch("builtins.open", new_callable=mock.mock_open)
113-
def test_main_success_with_headers(self, mock_open_file):
115+
def test_main_success_with_headers(self, mock_open_file, mock_abspath): # Added mock_abspath
114116
"""Test a successful run of main with header writing."""
117+
mock_abspath.return_value = "/fake/examples/misc/campaign_report_to_csv.py" # Configure mock
115118
customer_id = "1234567890"
116119
output_filepath = "test_report.csv"
117120
write_headers = True
@@ -173,7 +176,8 @@ def test_main_success_with_headers(self, mock_open_file):
173176
self.mock_google_ads_service.search_stream.assert_called_once_with(self.mock_search_stream_request_obj)
174177

175178
# Check what was written to the file
176-
mock_open_file.assert_called_once_with(output_filepath, "w", newline="", encoding="utf-8")
179+
expected_full_path = os.path.join("/fake/examples/misc", output_filepath)
180+
mock_open_file.assert_called_once_with(expected_full_path, "w", newline="")
177181
# Get all write calls made to the mock file handle
178182
written_content = "".join(call.args[0] for call in mock_open_file().write.call_args_list)
179183

@@ -217,9 +221,11 @@ def test_main_success_with_headers(self, mock_open_file):
217221
self.assertEqual(row2, expected_row_2_data)
218222

219223

224+
@mock.patch("os.path.abspath") # Added
220225
@mock.patch("builtins.open", new_callable=mock.mock_open)
221-
def test_main_success_without_headers(self, mock_open_file):
226+
def test_main_success_without_headers(self, mock_open_file, mock_abspath): # Added mock_abspath
222227
"""Test a successful run of main without header writing."""
228+
mock_abspath.return_value = "/fake/examples/misc/campaign_report_to_csv.py" # Configure mock
223229
customer_id = "1234567890"
224230
output_filepath = "test_report_no_headers.csv"
225231
write_headers = False
@@ -242,7 +248,8 @@ def test_main_success_without_headers(self, mock_open_file):
242248
self.assertEqual(self.mock_search_stream_request_obj.query, campaign_report_to_csv._QUERY)
243249
self.mock_google_ads_service.search_stream.assert_called_once_with(self.mock_search_stream_request_obj)
244250

245-
mock_open_file.assert_called_once_with(output_filepath, "w", newline="", encoding="utf-8")
251+
expected_full_path = os.path.join("/fake/examples/misc", output_filepath)
252+
mock_open_file.assert_called_once_with(expected_full_path, "w", newline="")
246253
written_content = "".join(call.args[0] for call in mock_open_file().write.call_args_list)
247254

248255
expected_row_1_data = [

0 commit comments

Comments
 (0)