def test_get_field_value(): field = "segment.date" date = "2001-01-01" response = GoogleAds.get_field_value(MockedDateSegment(date), field) assert response == date date = "2020" response = GoogleAds.get_field_value(MockedDateSegment(date), field) assert response is None
def test_send_request(mocker): mocker.patch("source_google_ads.google_ads.GoogleAdsClient.load_from_dict", return_value=MockGoogleAdsClient(SAMPLE_CONFIG)) mocker.patch("source_google_ads.google_ads.GoogleAdsClient.get_service", return_value=MockGoogleAdsService()) google_ads_client = GoogleAds(**SAMPLE_CONFIG) query = "Query" page_size = 1000 response = google_ads_client.send_request(query) assert response.customer_id == SAMPLE_CONFIG["customer_id"] assert response.query == query assert response.page_size == page_size
def test_send_request(mocker, customers): mocker.patch("source_google_ads.google_ads.GoogleAdsClient.load_from_dict", return_value=MockGoogleAdsClient(SAMPLE_CONFIG)) mocker.patch("source_google_ads.google_ads.GoogleAdsClient.get_service", return_value=MockGoogleAdsService()) google_ads_client = GoogleAds(**SAMPLE_CONFIG) query = "Query" page_size = 1000 customer_id = next(iter(customers)).id response = list(google_ads_client.send_request(query, customer_id=customer_id)) assert response[0].customer_id == customer_id assert response[0].query == query assert response[0].page_size == page_size
def test_stream_slices(config, customers): google_api = GoogleAds(credentials=config["credentials"]) stream = AdGroupAdReport( start_date=config["start_date"], api=google_api, conversion_window_days=config["conversion_window_days"], customers=customers, end_date="2021-02-10", ) slices = list(stream.stream_slices()) assert slices == [ { "start_date": "2020-12-19", "end_date": "2021-01-02", "customer_id": "123" }, { "start_date": "2021-01-03", "end_date": "2021-01-17", "customer_id": "123" }, { "start_date": "2021-01-18", "end_date": "2021-02-01", "customer_id": "123" }, { "start_date": "2021-02-02", "end_date": "2021-02-10", "customer_id": "123" }, ]
def test_google_ads_init(mocker): google_client_mocker = mocker.patch( "source_google_ads.google_ads.GoogleAdsClient", return_value=MockGoogleAdsClient) google_ads_client = GoogleAds(**SAMPLE_CONFIG) assert google_ads_client.customer_id == SAMPLE_CONFIG["customer_id"] assert google_client_mocker.load_from_dict.call_args[0][0] == EXPECTED_CRED
def test_convert_schema_into_query(): report_name = "ad_group_ad_report" query = "SELECT segment.date FROM ad_group_ad WHERE segments.date >= '2020-01-01' AND segments.date <= '2020-03-01' ORDER BY segments.date ASC" response = GoogleAds.convert_schema_into_query(SAMPLE_SCHEMA, report_name, "2020-01-01", "2020-03-01", "segments.date") assert response == query
def mock(latest_record): mocker.patch("source_google_ads.streams.GoogleAdsStream.read_records", Mock(return_value=[latest_record])) google_api = GoogleAds(credentials=config["credentials"]) client = AdGroupAdReport( start_date=config["start_date"], api=google_api, conversion_window_days=config["conversion_window_days"], customers=customers) return client
def test_convert_schema_into_query(): report_name = "ad_group_ad_report" query = Template(""" SELECT segment.date FROM ad_group_ad WHERE segments.date > '2020-01-01' AND segments.date < '2020-03-01' ORDER BY segments.date """) response = GoogleAds.convert_schema_into_query(SAMPLE_SCHEMA, report_name, "2020-01-01", "2020-03-01") assert response == query.substitute()
def get_instance_from_config(config, query): start_date = "2021-03-04" conversion_window_days = 14 google_api = GoogleAds(credentials=config["credentials"], customer_id=config["customer_id"]) instance = CustomQuery( api=google_api, conversion_window_days=conversion_window_days, start_date=start_date, custom_query_config={ "query": query, "table_name": "whatever_table" }, ) return instance
def test_get_updated_state(config): google_api = GoogleAds(credentials=config["credentials"], customer_id=config["customer_id"]) client = AdGroupAdReport( start_date=config["start_date"], api=google_api, conversion_window_days=config["conversion_window_days"]) current_state_stream = {} latest_record = {"segments.date": "2020-01-01"} new_stream_state = client.get_updated_state(current_state_stream, latest_record) assert new_stream_state == {"segments.date": "2020-01-01"} current_state_stream = {"segments.date": "2020-01-01"} latest_record = {"segments.date": "2020-02-01"} new_stream_state = client.get_updated_state(current_state_stream, latest_record) assert new_stream_state == {"segments.date": "2020-02-01"}
def test_read_record_error_handling(config, customers, caplog, mocked_gads_api, cls, error, failure_code, raise_expected, log_expected): error_msg = "Some unexpected error" mocked_gads_api(failure_code=failure_code, failure_msg=error_msg, error_type=error) google_api = GoogleAds(credentials=config["credentials"]) stream = cls(api=google_api, customers=customers) if raise_expected: with pytest.raises(GoogleAdsException): for _ in stream.read_records(sync_mode=Mock(), stream_slice={ "customer_id": "1234567890" }): pass else: for _ in stream.read_records( sync_mode=Mock(), stream_slice={"customer_id": "1234567890"}): pass error_in_log = error_msg in caplog.text assert error_in_log is log_expected
def test_parse_single_result(): date = "2001-01-01" response = GoogleAds.parse_single_result(SAMPLE_SCHEMA, MockedDateSegment(date)) assert response == response
def test_get_fields_from_schema(): response = GoogleAds.get_fields_from_schema(SAMPLE_SCHEMA) assert response == ["segment.date"]