Skip to content

Commit

Permalink
#12650: source google ads to GA: mock input config for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davydov-d committed May 12, 2022
1 parent f918859 commit fdac436
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
#

import json

import pytest


@pytest.fixture(scope="session", name="config")
def config_fixture():
with open("secrets/config.json", "r") as config_file:
return json.load(config_file)
def test_config():
config = {
"credentials": {
"developer_token": "test_token",
"client_id": "test_client_id",
"client_secret": "test_client_secret",
"refresh_token": "test_refresh_token",
},
"customer_id": "123",
"start_date": "2021-01-01",
"conversion_window_days": 14,
"custom_queries": [
{
"query": "SELECT campaign.accessible_bidding_strategy, segments.ad_destination_type, campaign.start_date, campaign.end_date FROM campaign",
"primary_key": None,
"cursor_field": "campaign.start_date",
"table_name": "happytable",
},
{
"query": "SELECT segments.ad_destination_type, segments.ad_network_type, segments.day_of_week, customer.auto_tagging_enabled, customer.id, metrics.conversions, campaign.start_date FROM campaign",
"primary_key": "customer.id",
"cursor_field": None,
"table_name": "unhappytable",
},
{
"query": "SELECT ad_group.targeting_setting.target_restrictions FROM ad_group",
"primary_key": "customer.id",
"cursor_field": None,
"table_name": "ad_group_custom",
},
],
}
return config


@pytest.fixture(autouse=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,10 @@
from .common import MockGoogleAdsClient as MockGoogleAdsClient


@pytest.fixture(scope="module")
def test_config():
config = {
"credentials": {
"developer_token": "test_token",
"client_id": "test_client_id",
"client_secret": "test_client_secret",
"refresh_token": "test_refresh_token",
},
"customer_id": "123",
"start_date": "2021-01-01",
"conversion_window_days": 14,
}
return config


@pytest.fixture
def mock_ads_client(mocker):
def mock_ads_client(mocker, config):
"""Mock google ads library method, so it returns mocked Client"""
mocker.patch("source_google_ads.google_ads.GoogleAdsClient.load_from_dict", return_value=MockGoogleAdsClient(test_config))
mocker.patch("source_google_ads.google_ads.GoogleAdsClient.load_from_dict", return_value=MockGoogleAdsClient(config))


# EXPIRED_PAGE_TOKEN exception will be raised when page token has expired.
Expand Down Expand Up @@ -81,7 +65,7 @@ def send_request(self, query: str, customer_id: str):
return mock_response_2()


def test_page_token_expired_retry_succeeds(mock_ads_client, test_config):
def test_page_token_expired_retry_succeeds(mock_ads_client, config):
"""
Page token expired while reading records on date 2021-01-03
The latest read record is {"segments.date": "2021-01-03", "click_view.gclid": "4"}
Expand All @@ -90,11 +74,11 @@ def test_page_token_expired_retry_succeeds(mock_ads_client, test_config):
"""
stream_slice = {"start_date": "2021-01-01", "end_date": "2021-01-15"}

google_api = MockGoogleAds(credentials=test_config["credentials"], customer_id=test_config["customer_id"])
google_api = MockGoogleAds(credentials=config["credentials"], customer_id=config["customer_id"])
incremental_stream_config = dict(
api=google_api,
conversion_window_days=test_config["conversion_window_days"],
start_date=test_config["start_date"],
conversion_window_days=config["conversion_window_days"],
start_date=config["start_date"],
time_zone="local",
end_date="2021-04-04",
)
Expand Down Expand Up @@ -139,18 +123,18 @@ def send_request(self, query: str, customer_id: str):
return mock_response_fails_2()


def test_page_token_expired_retry_fails(mock_ads_client, test_config):
def test_page_token_expired_retry_fails(mock_ads_client, config):
"""
Page token has expired while reading records within date "2021-01-03", it should raise error,
because Google Ads API doesn't allow filter by datetime.
"""
stream_slice = {"start_date": "2021-01-01", "end_date": "2021-01-15"}

google_api = MockGoogleAdsFails(credentials=test_config["credentials"], customer_id=test_config["customer_id"])
google_api = MockGoogleAdsFails(credentials=config["credentials"], customer_id=config["customer_id"])
incremental_stream_config = dict(
api=google_api,
conversion_window_days=test_config["conversion_window_days"],
start_date=test_config["start_date"],
conversion_window_days=config["conversion_window_days"],
start_date=config["start_date"],
time_zone="local",
end_date="2021-04-04",
)
Expand Down Expand Up @@ -181,19 +165,19 @@ def send_request(self, query: str, customer_id: str):
return mock_response_fails_one_date()


def test_page_token_expired_it_should_fail_date_range_1_day(mock_ads_client, test_config):
def test_page_token_expired_it_should_fail_date_range_1_day(mock_ads_client, config):
"""
Page token has expired while reading records within date "2021-01-03",
it should raise error, because Google Ads API doesn't allow filter by datetime.
Minimum date range is 1 day.
"""
stream_slice = {"start_date": "2021-01-03", "end_date": "2021-01-04"}

google_api = MockGoogleAdsFailsOneDate(credentials=test_config["credentials"], customer_id=test_config["customer_id"])
google_api = MockGoogleAdsFailsOneDate(credentials=config["credentials"], customer_id=config["customer_id"])
incremental_stream_config = dict(
api=google_api,
conversion_window_days=test_config["conversion_window_days"],
start_date=test_config["start_date"],
conversion_window_days=config["conversion_window_days"],
start_date=config["start_date"],
time_zone="local",
end_date="2021-04-04",
)
Expand Down

0 comments on commit fdac436

Please sign in to comment.