Example #1
0
def test_streams_displays(
    test_config,
    stream_name,
    endpoint,
    profiles_response,
    adgroups_response,
    targeting_response,
    product_ads_response,
):
    setup_responses(
        profiles_response=profiles_response,
        adgroups_response=adgroups_response,
        targeting_response=targeting_response,
        product_ads_response=product_ads_response,
    )

    source = SourceAmazonAds()
    streams = source.streams(test_config)
    test_stream = get_stream_by_name(streams, stream_name)

    records = get_all_stream_records(test_stream)
    assert len(records) == 4
    schema = test_stream.get_json_schema()
    for r in records:
        validate(schema=schema, instance=r)
    assert any([endpoint in call.request.url for call in responses.calls])
Example #2
0
def test_discover(config):
    setup_responses()
    source = SourceAmazonAds()
    catalog = source.discover(None, config)
    catalog = AirbyteMessage(type=Type.CATALOG,
                             catalog=catalog).dict(exclude_unset=True)
    schemas = [
        stream["json_schema"] for stream in catalog["catalog"]["streams"]
    ]
    for schema in schemas:
        Draft4Validator.check_schema(schema)
Example #3
0
def test_streams_brands_and_products(test_config, stream_name, endpoint,
                                     profiles_response):
    setup_responses(profiles_response=profiles_response,
                    generic_response=endpoint)

    source = SourceAmazonAds()
    streams = source.streams(test_config)
    test_stream = get_stream_by_name(streams, stream_name)

    records = get_all_stream_records(test_stream)
    assert records == []
    assert any([endpoint in call.request.url for call in responses.calls])
Example #4
0
def test_streams_profile(test_config, profiles_response):
    setup_responses(profiles_response=profiles_response)

    source = SourceAmazonAds()
    streams = source.streams(test_config)

    profile_stream = get_stream_by_name(streams, "profiles")
    schema = profile_stream.get_json_schema()
    records = get_all_stream_records(profile_stream)
    assert len(responses.calls) == 2
    assert len(profile_stream._profiles) == 4
    assert len(records) == 4
    expected_records = json.loads(profiles_response)
    for record, expected_record in zip(records, expected_records):
        validate(schema=schema, instance=record)
        assert record == expected_record
Example #5
0
def test_streams_campaigns_pagination_403_error(mocker, status_code,
                                                test_config, profiles_response,
                                                campaigns_response):
    setup_responses(profiles_response=profiles_response)
    responses.add(
        responses.GET,
        "https://advertising-api.amazon.com/sd/campaigns",
        json={"message": "msg"},
        status=status_code,
    )
    source = SourceAmazonAds()
    streams = source.streams(test_config)
    campaigns_stream = get_stream_by_name(streams,
                                          "sponsored_display_campaigns")

    with pytest.raises(requests.exceptions.HTTPError):
        get_all_stream_records(campaigns_stream)
Example #6
0
def test_streams_campaigns_4_vendors(test_config, profiles_response,
                                     campaigns_response):
    profiles_response = json.loads(profiles_response)
    for profile in profiles_response:
        profile["accountInfo"]["type"] = "vendor"
    profiles_response = json.dumps(profiles_response)
    setup_responses(profiles_response=profiles_response,
                    campaigns_response=campaigns_response)

    source = SourceAmazonAds()
    streams = source.streams(test_config)
    profile_stream = get_stream_by_name(streams, "profiles")
    campaigns_stream = get_stream_by_name(streams,
                                          "sponsored_display_campaigns")
    profile_records = get_all_stream_records(profile_stream)
    campaigns_records = get_all_stream_records(campaigns_stream)
    assert len(campaigns_records) == len(profile_records) * len(
        json.loads(campaigns_response))
Example #7
0
def test_streams_campaigns_pagination_403_error_expected(
        mocker, test_config, profiles_response, campaigns_response):
    setup_responses(profiles_response=profiles_response)
    responses.add(
        responses.GET,
        "https://advertising-api.amazon.com/sd/campaigns",
        json={
            "code": "403",
            "details": "details",
            "requestId": "xxx"
        },
        status=403,
    )
    source = SourceAmazonAds()
    streams = source.streams(test_config)
    campaigns_stream = get_stream_by_name(streams,
                                          "sponsored_display_campaigns")

    campaigns_records = get_all_stream_records(campaigns_stream)
    assert campaigns_records == []
Example #8
0
def test_source_streams(config):
    setup_responses()
    source = SourceAmazonAds()
    streams = source.streams(config)
    assert len(streams) == 18
    actual_stream_names = {stream.name for stream in streams}
    expected_stream_names = set([
        "profiles",
        "sponsored_display_campaigns",
        "sponsored_product_campaigns",
        "sponsored_product_ad_groups",
        "sponsored_product_keywords",
        "sponsored_product_negative_keywords",
        "sponsored_product_ads",
        "sponsored_product_targetings",
        "sponsored_products_report_stream",
        "sponsored_brands_campaigns",
        "sponsored_brands_ad_groups",
        "sponsored_brands_keywords",
        "sponsored_brands_report_stream",
    ])
    assert not expected_stream_names - actual_stream_names
Example #9
0
def test_streams_campaigns_pagination(mocker, test_config, profiles_response,
                                      campaigns_response, page_size):
    mocker.patch(
        "source_amazon_ads.streams.common.SubProfilesStream.page_size",
        page_size)
    profiles_response = json.loads(profiles_response)
    for profile in profiles_response:
        profile["accountInfo"]["type"] = "vendor"
    profiles_response = json.dumps(profiles_response)
    setup_responses(profiles_response=profiles_response)

    source = SourceAmazonAds()
    streams = source.streams(test_config)
    profile_stream = get_stream_by_name(streams, "profiles")
    campaigns_stream = get_stream_by_name(streams,
                                          "sponsored_display_campaigns")
    campaigns = json.loads(campaigns_response)

    def campaigns_paginated_response_cb(request):
        query = urlparse(request.url).query
        query = parse_qs(query)
        start_index, count = (int(query.get(f, [0])[0])
                              for f in ["startIndex", "count"])
        response_body = campaigns[start_index:start_index + count]
        return (200, {}, json.dumps(response_body))

    responses.add_callback(
        responses.GET,
        "https://advertising-api.amazon.com/sd/campaigns",
        content_type="application/json",
        callback=campaigns_paginated_response_cb,
    )
    profile_records = get_all_stream_records(profile_stream)

    campaigns_records = get_all_stream_records(campaigns_stream)
    assert len(campaigns_records) == len(profile_records) * len(
        json.loads(campaigns_response))
Example #10
0
#
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
#

import sys

from airbyte_cdk.entrypoint import launch
from source_amazon_ads import SourceAmazonAds

if __name__ == "__main__":
    source = SourceAmazonAds()
    launch(source, sys.argv[1:])
Example #11
0
def test_check(config):
    setup_responses()
    source = SourceAmazonAds()
    assert source.check(
        None, config) == AirbyteConnectionStatus(status=Status.SUCCEEDED)
    assert len(responses.calls) == 2
Example #12
0
def test_spec():
    source = SourceAmazonAds()
    spec = source.spec(None)
    assert isinstance(spec, ConnectorSpecification)