Skip to content

Commit

Permalink
tests: add testing w/o 'grpc' installed (#289)
Browse files Browse the repository at this point in the history
Closes #288.
  • Loading branch information
tseaver committed Oct 19, 2021
1 parent f4e776e commit 09cf285
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 12 deletions.
14 changes: 12 additions & 2 deletions noxfile.py
Expand Up @@ -33,6 +33,7 @@
nox.options.sessions = [
"unit",
"unit_grpc_gcp",
"unit_wo_grpc",
"cover",
"pytype",
"mypy",
Expand Down Expand Up @@ -78,7 +79,7 @@ def blacken(session):
session.run("black", *BLACK_EXCLUDES, *BLACK_PATHS)


def default(session):
def default(session, install_grpc=True):
"""Default unit test session.
This is intended to be run **without** an interpreter set, so
Expand All @@ -92,7 +93,10 @@ def default(session):

# Install all test dependencies, then install this package in-place.
session.install("mock", "pytest", "pytest-cov")
session.install("-e", ".[grpc]", "-c", constraints_path)
if install_grpc:
session.install("-e", ".[grpc]", "-c", constraints_path)
else:
session.install("-e", ".", "-c", constraints_path)

pytest_args = [
"python",
Expand Down Expand Up @@ -140,6 +144,12 @@ def unit_grpc_gcp(session):
default(session)


@nox.session(python=["3.6", "3.10"])
def unit_wo_grpc(session):
"""Run the unit test suite w/o grpcio installed"""
default(session, install_grpc=False)


@nox.session(python="3.6")
def lint_setup_py(session):
"""Verify that setup.py is valid (including RST check)."""
Expand Down
8 changes: 8 additions & 0 deletions tests/asyncio/gapic/test_config_async.py
Expand Up @@ -12,6 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import exceptions
from google.api_core.gapic_v1 import config_async

Expand Down
6 changes: 5 additions & 1 deletion tests/asyncio/gapic/test_method_async.py
Expand Up @@ -14,10 +14,14 @@

import datetime

from grpc import aio
import mock
import pytest

try:
from grpc import aio
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import exceptions
from google.api_core import gapic_v1
from google.api_core import grpc_helpers_async
Expand Down
10 changes: 8 additions & 2 deletions tests/asyncio/operations_v1/test_operations_async_client.py
Expand Up @@ -12,11 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from grpc import aio
import mock
import pytest

from google.api_core import grpc_helpers_async, operations_v1, page_iterator_async
try:
from grpc import aio
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import grpc_helpers_async
from google.api_core import operations_v1
from google.api_core import page_iterator_async
from google.longrunning import operations_pb2
from google.protobuf import empty_pb2

Expand Down
15 changes: 12 additions & 3 deletions tests/asyncio/test_grpc_helpers_async.py
Expand Up @@ -12,10 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import grpc
from grpc import aio
import mock
import pytest
import pytest # noqa: I202

try:
import grpc
from grpc import aio
except ImportError:
grpc = aio = None


if grpc is None:
pytest.skip("No GRPC", allow_module_level=True)


from google.api_core import exceptions
from google.api_core import grpc_helpers_async
Expand Down
5 changes: 5 additions & 0 deletions tests/asyncio/test_operation_async.py
Expand Up @@ -16,6 +16,11 @@
import mock
import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import exceptions
from google.api_core import operation_async
from google.api_core import operations_v1
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/gapic/test_client_info.py
Expand Up @@ -12,6 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)


from google.api_core.gapic_v1 import client_info

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/gapic/test_config.py
Expand Up @@ -12,6 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import exceptions
from google.api_core.gapic_v1 import config

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/gapic/test_method.py
Expand Up @@ -15,6 +15,13 @@
import datetime

import mock
import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)


from google.api_core import exceptions
from google.api_core import retry
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/gapic/test_routing_header.py
Expand Up @@ -12,6 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)


from google.api_core.gapic_v1 import routing_header

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/operations_v1/test_operations_client.py
Expand Up @@ -12,6 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import grpc_helpers
from google.api_core import operations_v1
from google.api_core import page_iterator
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/test_bidi.py
Expand Up @@ -17,10 +17,14 @@
import queue
import threading

import grpc
import mock
import pytest

try:
import grpc
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import bidi
from google.api_core import exceptions

Expand Down
12 changes: 11 additions & 1 deletion tests/unit/test_client_info.py
Expand Up @@ -13,14 +13,24 @@
# limitations under the License.


try:
import grpc
except ImportError:
grpc = None

from google.api_core import client_info


def test_constructor_defaults():
info = client_info.ClientInfo()

assert info.python_version is not None
assert info.grpc_version is not None

if grpc is not None:
assert info.grpc_version is not None
else:
assert info.grpc_version is None

assert info.api_core_version is not None
assert info.gapic_version is None
assert info.client_library_version is None
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/test_exceptions.py
Expand Up @@ -15,10 +15,15 @@
import http.client
import json

import grpc
import mock
import pytest
import requests

try:
import grpc
except ImportError:
grpc = None

from google.api_core import exceptions


Expand Down Expand Up @@ -151,6 +156,7 @@ def test_from_http_response_json_unicode_content():
assert exception.errors == ["1", "2"]


@pytest.mark.skipif(grpc is None, reason="No grpc")
def test_from_grpc_status():
message = "message"
exception = exceptions.from_grpc_status(grpc.StatusCode.OUT_OF_RANGE, message)
Expand All @@ -162,6 +168,7 @@ def test_from_grpc_status():
assert exception.errors == []


@pytest.mark.skipif(grpc is None, reason="No grpc")
def test_from_grpc_status_as_int():
message = "message"
exception = exceptions.from_grpc_status(11, message)
Expand All @@ -173,6 +180,7 @@ def test_from_grpc_status_as_int():
assert exception.errors == []


@pytest.mark.skipif(grpc is None, reason="No grpc")
def test_from_grpc_status_with_errors_and_response():
message = "message"
response = mock.sentinel.response
Expand All @@ -187,13 +195,15 @@ def test_from_grpc_status_with_errors_and_response():
assert exception.response == response


@pytest.mark.skipif(grpc is None, reason="No grpc")
def test_from_grpc_status_unknown_code():
message = "message"
exception = exceptions.from_grpc_status(grpc.StatusCode.OK, message)
assert exception.grpc_status_code == grpc.StatusCode.OK
assert exception.message == message


@pytest.mark.skipif(grpc is None, reason="No grpc")
def test_from_grpc_error():
message = "message"
error = mock.create_autospec(grpc.Call, instance=True)
Expand All @@ -211,6 +221,7 @@ def test_from_grpc_error():
assert exception.response == error


@pytest.mark.skipif(grpc is None, reason="No grpc")
def test_from_grpc_error_non_call():
message = "message"
error = mock.create_autospec(grpc.RpcError, instance=True)
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/test_grpc_helpers.py
Expand Up @@ -12,10 +12,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import grpc
import mock
import pytest

try:
import grpc
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import exceptions
from google.api_core import grpc_helpers
import google.auth.credentials
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_operation.py
Expand Up @@ -14,6 +14,12 @@


import mock
import pytest

try:
import grpc # noqa: F401
except ImportError:
pytest.skip("No GRPC", allow_module_level=True)

from google.api_core import exceptions
from google.api_core import operation
Expand Down

0 comments on commit 09cf285

Please sign in to comment.