Skip to content

Commit

Permalink
[gaarf-py] Run api client tests only when there are default credentials
Browse files Browse the repository at this point in the history
Change-Id: I49a22e1bba2eb29cca29db65350c26a4c8b4e7ec
  • Loading branch information
AVMarkin committed Sep 26, 2024
1 parent e941489 commit aaef370
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install pytest pytest-mock
pip install -e py/[full]
pip install -e py/.[full]
- name: Test with pytest
run: |
pytest py/tests/unit/*.py
24 changes: 22 additions & 2 deletions py/tests/unit/test_api_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@

import pytest
import tenacity
from google import auth
from google.ads.googleads import client as googleads_client
from google.api_core import exceptions as google_exceptions

from gaarf import api_clients


def has_default_credentials():
try:
credentials, _ = auth.default()
return True
except auth.exceptions.DefaultCredentialsError:
return False


class TestBaseClient:
@pytest.fixture
def client(self):
Expand Down Expand Up @@ -228,7 +237,7 @@ def fake_googleads_client(self, mocker):
)

@pytest.fixture
def test_client(self, mocker, config_path):
def test_client(self, mocker, fake_googleads_client, config_path):
mocker.patch('google.ads.googleads.client.oauth2', return_value=[])
mocker.patch(
f'google.ads.googleads.{api_clients.GOOGLE_ADS_API_VERSION}'
Expand All @@ -240,8 +249,13 @@ def test_client(self, mocker, config_path):
google_exceptions.InternalServerError('test'),
],
)
return api_clients.GoogleAdsApiClient(path_to_config=config_path)
return api_clients.GoogleAdsApiClient(
path_to_config=config_path, ads_client=fake_googleads_client
)

@pytest.mark.skipif(
not has_default_credentials(), reason='Cannot found default credentials'
)
def test_get_response_raises_internal_service_error_after_3_failed_retries(
self, test_client
):
Expand All @@ -252,6 +266,9 @@ def test_get_response_raises_internal_service_error_after_3_failed_retries(
query_text='SELECT customer.id FROM customer',
)

@pytest.mark.skipif(
not has_default_credentials(), reason='Cannot found default credentials'
)
def test_from_googleads_client_returns_new_instance(
self, fake_googleads_client, caplog
):
Expand All @@ -270,6 +287,9 @@ def test_from_googleads_client_returns_new_instance(
'"use_proto_plus=False"'
) not in caplog.text

@pytest.mark.skipif(
not has_default_credentials(), reason='Cannot found default credentials'
)
def test_from_googleads_client_generates_warning_for_use_proto_plus(
self, fake_googleads_client, caplog
):
Expand Down

0 comments on commit aaef370

Please sign in to comment.