From aaef3709e479cd9d4ee69ec2dc3bba377e0f63c8 Mon Sep 17 00:00:00 2001 From: Andrei Markin Date: Wed, 25 Sep 2024 16:40:45 +0400 Subject: [PATCH] [gaarf-py] Run api client tests only when there are default credentials Change-Id: I49a22e1bba2eb29cca29db65350c26a4c8b4e7ec --- .github/workflows/pytest.yaml | 2 +- py/tests/unit/test_api_clients.py | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index c5a740cc..d5188bb9 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -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 diff --git a/py/tests/unit/test_api_clients.py b/py/tests/unit/test_api_clients.py index 36b2ddf5..cead47be 100644 --- a/py/tests/unit/test_api_clients.py +++ b/py/tests/unit/test_api_clients.py @@ -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): @@ -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}' @@ -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 ): @@ -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 ): @@ -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 ):