Skip to content
42 changes: 26 additions & 16 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,33 @@ def default(session, install_grpc=True, prerelease=False, install_async_rest=Fal
"python",
"-m",
"pytest",
*(
# Helpful for running a single test or testfile.
session.posargs
or [
"--quiet",
"--cov=google.api_core",
"--cov=tests.unit",
"--cov-append",
"--cov-config=.coveragerc",
"--cov-report=",
"--cov-fail-under=0",
# Running individual tests with parallelism enabled is usually not helpful.
"-n=auto",
os.path.join("tests", "unit"),
]
),
]
pytest_args.extend(["-W", "ignore::FutureWarning"])

pytest_args.extend(
[
# We use filterwarnings to ignore warnings that are out of our control,
# but we want to make sure that our own code does not generate warnings.
"-m",
"not filterwarnings",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do want to see warnings as that may require code changes. We can filter warnings here with a comment that links to a bug with more information

filterwarnings = [

*(
# Helpful for running a single test or testfile.
session.posargs
or [
"--quiet",
"--cov=google.api_core",
"--cov=tests.unit",
"--cov-append",
"--cov-config=.coveragerc",
"--cov-report=",
"--cov-fail-under=0",
# Running individual tests with parallelism enabled is usually not helpful.
"-n=auto",
os.path.join("tests", "unit"),
]
),
]
)

session.install("asyncmock", "pytest-asyncio")

Expand Down
2 changes: 1 addition & 1 deletion tests/asyncio/gapic/test_method_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async def test_wrap_method_with_overriding_timeout_as_a_number():
actual_timeout = method.call_args[1]["timeout"]
metadata = method.call_args[1]["metadata"]
assert metadata == mock.ANY
assert actual_timeout == pytest.approx(22, abs=0.01)
assert actual_timeout == pytest.approx(22, abs=0.05)


@pytest.mark.asyncio
Expand Down
32 changes: 18 additions & 14 deletions tests/asyncio/test_grpc_helpers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,12 @@ def test_create_channel_explicit_with_duplicate_credentials():
target = "example:443"

with pytest.raises(exceptions.DuplicateCredentialArgs) as excinfo:
grpc_helpers_async.create_channel(
target,
credentials_file="credentials.json",
credentials=mock.sentinel.credentials,
)
with pytest.warns(DeprecationWarning):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to clarify that credentials_file is deprecated. Applies throughout

grpc_helpers_async.create_channel(
target,
credentials_file="credentials.json",
credentials=mock.sentinel.credentials,
)

assert "mutually exclusive" in str(excinfo.value)

Expand Down Expand Up @@ -641,9 +642,10 @@ def test_create_channel_with_credentials_file(
credentials_file = "/path/to/credentials/file.json"
composite_creds = composite_creds_call.return_value

channel = grpc_helpers_async.create_channel(
target, credentials_file=credentials_file
)
with pytest.warns(DeprecationWarning):
channel = grpc_helpers_async.create_channel(
target, credentials_file=credentials_file
)

google.auth.load_credentials_from_file.assert_called_once_with(
credentials_file, scopes=None, default_scopes=None
Expand All @@ -670,9 +672,10 @@ def test_create_channel_with_credentials_file_and_scopes(
credentials_file = "/path/to/credentials/file.json"
composite_creds = composite_creds_call.return_value

channel = grpc_helpers_async.create_channel(
target, credentials_file=credentials_file, scopes=scopes
)
with pytest.warns(DeprecationWarning):
channel = grpc_helpers_async.create_channel(
target, credentials_file=credentials_file, scopes=scopes
)

google.auth.load_credentials_from_file.assert_called_once_with(
credentials_file, scopes=scopes, default_scopes=None
Expand All @@ -699,9 +702,10 @@ def test_create_channel_with_credentials_file_and_default_scopes(
credentials_file = "/path/to/credentials/file.json"
composite_creds = composite_creds_call.return_value

channel = grpc_helpers_async.create_channel(
target, credentials_file=credentials_file, default_scopes=default_scopes
)
with pytest.warns(DeprecationWarning):
channel = grpc_helpers_async.create_channel(
target, credentials_file=credentials_file, default_scopes=default_scopes
)

google.auth.load_credentials_from_file.assert_called_once_with(
credentials_file, scopes=None, default_scopes=default_scopes
Expand Down
55 changes: 38 additions & 17 deletions tests/unit/operations_v1/test_operations_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ def test_operations_client_client_options(
)

# Check the case credentials_file is provided
options = client_options.ClientOptions(credentials_file="credentials.json")
with pytest.warns(DeprecationWarning):
options = client_options.ClientOptions(credentials_file="credentials.json")
with mock.patch.object(transport_class, "__init__") as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
Expand Down Expand Up @@ -539,11 +540,22 @@ def test_operations_client_client_options_credentials_file(
client_class, transport_class, transport_name
):
# Check the case credentials file is provided.
options = client_options.ClientOptions(credentials_file="credentials.json")
with pytest.warns(DeprecationWarning):
options = client_options.ClientOptions(credentials_file="credentials.json")
if "async" in str(client_class):
# TODO(): Add support for credentials file to async REST transport.
with pytest.raises(core_exceptions.AsyncRestUnsupportedParameterError):
client_class(client_options=options, transport=transport_name)
with mock.patch.object(transport_class, "__init__") as patched:
patched.return_value = None
client = client_class(client_options=options, transport=transport_name)
patched.assert_called_once_with(
credentials=None,
credentials_file="credentials.json",
host=client.DEFAULT_ENDPOINT,
scopes=None,
client_cert_source_for_mtls=None,
quota_project_id=None,
client_info=transports.base.DEFAULT_CLIENT_INFO,
always_use_jwt_access=True,
)
else:
with mock.patch.object(transport_class, "__init__") as patched:
patched.return_value = None
Expand All @@ -570,10 +582,17 @@ def test_operations_client_client_options_credentials_file(
return_value=(mock.sentinel.credentials, mock.sentinel.project),
)
def test_list_operations_rest(google_auth_default, credentials_file):
sync_transport = transports.rest.OperationsRestTransport(
credentials_file=credentials_file,
http_options=HTTP_OPTIONS,
)
if credentials_file is not None:
with pytest.warns(DeprecationWarning):
sync_transport = transports.rest.OperationsRestTransport(
credentials_file=credentials_file,
http_options=HTTP_OPTIONS,
)
else:
sync_transport = transports.rest.OperationsRestTransport(
credentials_file=credentials_file,
http_options=HTTP_OPTIONS,
)

client = AbstractOperationsClient(transport=sync_transport)

Expand Down Expand Up @@ -1130,10 +1149,11 @@ def test_transport_adc(client_class, transport_class, credentials):
def test_operations_base_transport_error():
# Passing both a credentials object and credentials_file should raise an error
with pytest.raises(core_exceptions.DuplicateCredentialArgs):
transports.OperationsTransport(
credentials=ga_credentials.AnonymousCredentials(),
credentials_file="credentials.json",
)
with pytest.warns(DeprecationWarning):
transports.OperationsTransport(
credentials=ga_credentials.AnonymousCredentials(),
credentials_file="credentials.json",
)


def test_operations_base_transport():
Expand Down Expand Up @@ -1171,10 +1191,11 @@ def test_operations_base_transport_with_credentials_file():
) as Transport:
Transport.return_value = None
load_creds.return_value = (ga_credentials.AnonymousCredentials(), None)
transports.OperationsTransport(
credentials_file="credentials.json",
quota_project_id="octopus",
)
with pytest.warns(DeprecationWarning):
transports.OperationsTransport(
credentials_file="credentials.json",
quota_project_id="octopus",
)
load_creds.assert_called_once_with(
"credentials.json",
scopes=None,
Expand Down
35 changes: 18 additions & 17 deletions tests/unit/test_client_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ def get_client_encrypted_cert():


def test_constructor():

options = client_options.ClientOptions(
api_endpoint="foo.googleapis.com",
client_cert_source=get_client_cert,
quota_project_id="quote-proj",
credentials_file="path/to/credentials.json",
scopes=[
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/cloud-platform.read-only",
],
api_audience="foo2.googleapis.com",
universe_domain="googleapis.com",
)
with pytest.warns(DeprecationWarning):
options = client_options.ClientOptions(
api_endpoint="foo.googleapis.com",
client_cert_source=get_client_cert,
quota_project_id="quote-proj",
credentials_file="path/to/credentials.json",
scopes=[
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/cloud-platform.read-only",
],
api_audience="foo2.googleapis.com",
universe_domain="googleapis.com",
)

assert options.api_endpoint == "foo.googleapis.com"
assert options.client_cert_source() == (b"cert", b"key")
Expand Down Expand Up @@ -102,10 +102,11 @@ def test_constructor_with_api_key():

def test_constructor_with_both_api_key_and_credentials_file():
with pytest.raises(ValueError):
client_options.ClientOptions(
api_key="api-key",
credentials_file="path/to/credentials.json",
)
with pytest.warns(DeprecationWarning):
client_options.ClientOptions(
api_key="api-key",
credentials_file="path/to/credentials.json",
)


def test_from_dict():
Expand Down
28 changes: 16 additions & 12 deletions tests/unit/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,12 @@ def test_create_channel_explicit_with_duplicate_credentials():
target = "example.com:443"

with pytest.raises(exceptions.DuplicateCredentialArgs):
grpc_helpers.create_channel(
target,
credentials_file="credentials.json",
credentials=mock.sentinel.credentials,
)
with pytest.warns(DeprecationWarning):
grpc_helpers.create_channel(
target,
credentials_file="credentials.json",
credentials=mock.sentinel.credentials,
)


@mock.patch("grpc.compute_engine_channel_credentials")
Expand Down Expand Up @@ -710,7 +711,8 @@ def test_create_channel_with_credentials_file(
credentials_file = "/path/to/credentials/file.json"
composite_creds = composite_creds_call.return_value

channel = grpc_helpers.create_channel(target, credentials_file=credentials_file)
with pytest.warns(DeprecationWarning):
channel = grpc_helpers.create_channel(target, credentials_file=credentials_file)

google.auth.load_credentials_from_file.assert_called_once_with(
credentials_file, scopes=None, default_scopes=None
Expand Down Expand Up @@ -742,9 +744,10 @@ def test_create_channel_with_credentials_file_and_scopes(
credentials_file = "/path/to/credentials/file.json"
composite_creds = composite_creds_call.return_value

channel = grpc_helpers.create_channel(
target, credentials_file=credentials_file, scopes=scopes
)
with pytest.warns(DeprecationWarning):
channel = grpc_helpers.create_channel(
target, credentials_file=credentials_file, scopes=scopes
)

google.auth.load_credentials_from_file.assert_called_once_with(
credentials_file, scopes=scopes, default_scopes=None
Expand Down Expand Up @@ -776,9 +779,10 @@ def test_create_channel_with_credentials_file_and_default_scopes(
credentials_file = "/path/to/credentials/file.json"
composite_creds = composite_creds_call.return_value

channel = grpc_helpers.create_channel(
target, credentials_file=credentials_file, default_scopes=default_scopes
)
with pytest.warns(DeprecationWarning):
channel = grpc_helpers.create_channel(
target, credentials_file=credentials_file, default_scopes=default_scopes
)

load_credentials_from_file.assert_called_once_with(
credentials_file, scopes=None, default_scopes=default_scopes
Expand Down
Loading