Skip to content

Commit 3c7735e

Browse files
committed
test(api-core): add coverage unit tests for operations_v1 fallback and protobuf version branches
1 parent 26a19fe commit 3c7735e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

packages/google-api-core/tests/unit/operations_v1/test_operations_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,38 @@ def test_cancel_operation():
100100

101101
def test_operations_client_config():
102102
assert operations_client_config.config["interfaces"]
103+
104+
105+
def test_operations_v1_transport_base_to_dict_protobuf_versions(monkeypatch):
106+
from google.longrunning import operations_pb2
107+
108+
from google.api_core.operations_v1.transports import base
109+
110+
message = operations_pb2.Operation(name="test_op")
111+
112+
monkeypatch.setattr(base, "PROTOBUF_VERSION", "3.20.0")
113+
res3 = base.OperationsTransport._to_dict(message)
114+
assert res3.get("name") == "test_op"
115+
116+
monkeypatch.setattr(base, "PROTOBUF_VERSION", "5.26.0")
117+
res5 = base.OperationsTransport._to_dict(message)
118+
assert res5.get("name") == "test_op"
119+
120+
121+
def test_operations_v1_init_import_error_fallback(monkeypatch):
122+
import importlib
123+
124+
import google.api_core.operations_v1 as op_v1
125+
126+
orig_import = __import__
127+
128+
def mock_import(name, globals=None, locals=None, fromlist=(), level=0):
129+
if "operations_rest_client_async" in name or (
130+
fromlist and "AsyncOperationsRestClient" in fromlist
131+
):
132+
raise ImportError("Simulated async rest import error")
133+
return orig_import(name, globals, locals, fromlist, level)
134+
135+
monkeypatch.setattr("builtins.__import__", mock_import)
136+
monkeypatch.setattr(op_v1, "_has_async_rest", True)
137+
importlib.reload(op_v1)

0 commit comments

Comments
 (0)