-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathasync_search_test.py
More file actions
66 lines (52 loc) · 2.25 KB
/
async_search_test.py
File metadata and controls
66 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import unittest
import sys
from unittest.mock import MagicMock, AsyncMock, patch
# Mocking modules before import because the environment seems to lack dependencies
mock_google = MagicMock()
sys.modules["google.ads.googleads.client"] = mock_google
sys.modules["google.ads.googleads.errors"] = mock_google
sys.modules["google.ads.googleads.v23"] = mock_google
sys.modules["google.ads.googleads.v23.services"] = mock_google
sys.modules["google.ads.googleads.v23.services.services"] = mock_google
sys.modules["google.ads.googleads.v23.services.services.google_ads_service"] = (
mock_google
)
sys.modules["google.ads.googleads.v23.services.types"] = mock_google
sys.modules["google.ads.googleads.v23.services.types.google_ads_service"] = (
mock_google
)
# Import module under test AFTER mocking
from examples.asyncio import async_search
class TestAsyncSearch(unittest.IsolatedAsyncioTestCase):
async def test_main(self):
# Setup Mocks
mock_client_instance = MagicMock()
mock_googleads_service = AsyncMock()
mock_client_instance.get_service.return_value = mock_googleads_service
# Mock the search response (AsyncPager)
# search returns an object that is an async iterator
# Create a mock row
mock_row = MagicMock()
mock_row.campaign.id = 123
mock_row.campaign.name = "Test Campaign"
# Async iterator setup for the pager
async def async_gen():
yield mock_row
mock_googleads_service.search.return_value = async_gen()
customer_id = "1234567890"
# Execute
await async_search.main(mock_client_instance, customer_id)
# Verification
# Check if service was retrieved correctly
mock_client_instance.get_service.assert_called_with(
"GoogleAdsService", is_async=True
)
# Verify search called
mock_googleads_service.search.assert_called_once()
call_args = mock_googleads_service.search.call_args
self.assertEqual(call_args.kwargs["customer_id"], customer_id)
# Verify Query does NOT contain LIMIT 10
query = call_args.kwargs["query"]
self.assertNotIn("LIMIT 10", query)
self.assertIn("SELECT", query)
self.assertIn("FROM campaign", query)