Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit c707f81

Browse files
authored
chore: pass explicit credentials in all unit tests creating clients (#369)
* Removed duplicated tests * Pass mock credentials. To allow unit tests to run without connecting to the google APIs. * Used a fixture to be DRY wrt test credentials. * Used a fixture to be DRY wrt test credentials. * Used a fixture to be DRY wrt test credentials. * Document the fixture.
1 parent 469ebaa commit c707f81

3 files changed

Lines changed: 62 additions & 100 deletions

File tree

tests/unit/pubsub_v1/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import google.auth.credentials
2+
import mock
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def creds():
8+
"""
9+
Provide test creds to unit tests so that they can run without
10+
GOOGLE_APPLICATION_CREDENTIALS set.
11+
"""
12+
yield mock.Mock(spec=google.auth.credentials.Credentials)

tests/unit/pubsub_v1/publisher/test_publisher_client.py

Lines changed: 30 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import inspect
1919

20-
from google.auth import credentials
2120
import grpc
2221

2322
import mock
@@ -51,8 +50,7 @@ def _assert_retries_equal(retry, retry2):
5150
assert inspect.getclosurevars(pred) == inspect.getclosurevars(pred2)
5251

5352

54-
def test_init():
55-
creds = mock.Mock(spec=credentials.Credentials)
53+
def test_init(creds):
5654
client = publisher.Client(credentials=creds)
5755

5856
# A plain client should have an `api` (the underlying GAPIC) and a
@@ -63,8 +61,7 @@ def test_init():
6361
assert client.batch_settings.max_messages == 100
6462

6563

66-
def test_init_default_client_info():
67-
creds = mock.Mock(spec=credentials.Credentials)
64+
def test_init_default_client_info(creds):
6865
client = publisher.Client(credentials=creds)
6966

7067
installed_version = publisher.client.__version__
@@ -83,8 +80,8 @@ def test_init_default_client_info():
8380
assert expected_client_info in user_agent
8481

8582

86-
def test_init_w_custom_transport():
87-
transport = PublisherGrpcTransport()
83+
def test_init_w_custom_transport(creds):
84+
transport = PublisherGrpcTransport(credentials=creds)
8885
client = publisher.Client(transport=transport)
8986

9087
# A plain client should have an `api` (the underlying GAPIC) and a
@@ -96,28 +93,18 @@ def test_init_w_custom_transport():
9693
assert client.batch_settings.max_messages == 100
9794

9895

99-
def test_init_w_api_endpoint():
96+
def test_init_w_api_endpoint(creds):
10097
client_options = {"api_endpoint": "testendpoint.google.com"}
101-
client = publisher.Client(client_options=client_options)
98+
client = publisher.Client(client_options=client_options, credentials=creds)
10299

103100
assert isinstance(client.api, publisher_client.PublisherClient)
104101
assert (client.api._transport.grpc_channel._channel.target()).decode(
105102
"utf-8"
106103
) == "testendpoint.google.com:443"
107104

108105

109-
def test_init_w_unicode_api_endpoint():
110-
client_options = {"api_endpoint": "testendpoint.google.com"}
111-
client = publisher.Client(client_options=client_options)
112-
113-
assert isinstance(client.api, publisher_client.PublisherClient)
114-
assert (client.api._transport.grpc_channel._channel.target()).decode(
115-
"utf-8"
116-
) == "testendpoint.google.com:443"
117-
118-
119-
def test_init_w_empty_client_options():
120-
client = publisher.Client(client_options={})
106+
def test_init_w_empty_client_options(creds):
107+
client = publisher.Client(client_options={}, credentials=creds)
121108

122109
assert isinstance(client.api, publisher_client.PublisherClient)
123110
assert (client.api._transport.grpc_channel._channel.target()).decode(
@@ -164,8 +151,7 @@ def test_init_emulator(monkeypatch):
164151
assert channel.target().decode("utf8") == "/foo/bar:123"
165152

166153

167-
def test_message_ordering_enabled():
168-
creds = mock.Mock(spec=credentials.Credentials)
154+
def test_message_ordering_enabled(creds):
169155
client = publisher.Client(credentials=creds)
170156
assert not client._enable_message_ordering
171157

@@ -176,8 +162,7 @@ def test_message_ordering_enabled():
176162
assert client._enable_message_ordering
177163

178164

179-
def test_publish():
180-
creds = mock.Mock(spec=credentials.Credentials)
165+
def test_publish(creds):
181166
client = publisher.Client(credentials=creds)
182167

183168
future1 = mock.sentinel.future1
@@ -212,8 +197,7 @@ def test_publish():
212197
)
213198

214199

215-
def test_publish_error_exceeding_flow_control_limits():
216-
creds = mock.Mock(spec=credentials.Credentials)
200+
def test_publish_error_exceeding_flow_control_limits(creds):
217201
publisher_options = types.PublisherOptions(
218202
flow_control=types.PublishFlowControl(
219203
message_limit=10,
@@ -235,8 +219,7 @@ def test_publish_error_exceeding_flow_control_limits():
235219
future2.result()
236220

237221

238-
def test_publish_data_not_bytestring_error():
239-
creds = mock.Mock(spec=credentials.Credentials)
222+
def test_publish_data_not_bytestring_error(creds):
240223
client = publisher.Client(credentials=creds)
241224
topic = "topic/path"
242225
with pytest.raises(TypeError):
@@ -245,16 +228,14 @@ def test_publish_data_not_bytestring_error():
245228
client.publish(topic, 42)
246229

247230

248-
def test_publish_message_ordering_not_enabled_error():
249-
creds = mock.Mock(spec=credentials.Credentials)
231+
def test_publish_message_ordering_not_enabled_error(creds):
250232
client = publisher.Client(credentials=creds)
251233
topic = "topic/path"
252234
with pytest.raises(ValueError):
253235
client.publish(topic, b"bytestring body", ordering_key="ABC")
254236

255237

256-
def test_publish_empty_ordering_key_when_message_ordering_enabled():
257-
creds = mock.Mock(spec=credentials.Credentials)
238+
def test_publish_empty_ordering_key_when_message_ordering_enabled(creds):
258239
client = publisher.Client(
259240
publisher_options=types.PublisherOptions(enable_message_ordering=True),
260241
credentials=creds,
@@ -263,8 +244,7 @@ def test_publish_empty_ordering_key_when_message_ordering_enabled():
263244
assert client.publish(topic, b"bytestring body", ordering_key="") is not None
264245

265246

266-
def test_publish_with_ordering_key_uses_extended_retry_deadline():
267-
creds = mock.Mock(spec=credentials.Credentials)
247+
def test_publish_with_ordering_key_uses_extended_retry_deadline(creds):
268248
client = publisher.Client(
269249
credentials=creds,
270250
publisher_options=types.PublisherOptions(enable_message_ordering=True),
@@ -303,8 +283,7 @@ def test_publish_with_ordering_key_uses_extended_retry_deadline():
303283
_assert_retries_equal(batch_commit_retry, expected_retry)
304284

305285

306-
def test_publish_attrs_bytestring():
307-
creds = mock.Mock(spec=credentials.Credentials)
286+
def test_publish_attrs_bytestring(creds):
308287
client = publisher.Client(credentials=creds)
309288

310289
# Use a mock in lieu of the actual batch class.
@@ -325,8 +304,7 @@ def test_publish_attrs_bytestring():
325304
)
326305

327306

328-
def test_publish_new_batch_needed():
329-
creds = mock.Mock(spec=credentials.Credentials)
307+
def test_publish_new_batch_needed(creds):
330308
client = publisher.Client(credentials=creds)
331309

332310
# Use mocks in lieu of the actual batch class.
@@ -365,16 +343,14 @@ def test_publish_new_batch_needed():
365343
batch2.publish.assert_called_once_with(message_pb)
366344

367345

368-
def test_publish_attrs_type_error():
369-
creds = mock.Mock(spec=credentials.Credentials)
346+
def test_publish_attrs_type_error(creds):
370347
client = publisher.Client(credentials=creds)
371348
topic = "topic/path"
372349
with pytest.raises(TypeError):
373350
client.publish(topic, b"foo", answer=42)
374351

375352

376-
def test_stop():
377-
creds = mock.Mock(spec=credentials.Credentials)
353+
def test_stop(creds):
378354
client = publisher.Client(credentials=creds)
379355

380356
batch1 = mock.Mock(spec=client._batch_class)
@@ -395,8 +371,7 @@ def test_stop():
395371
client.stop()
396372

397373

398-
def test_gapic_instance_method():
399-
creds = mock.Mock(spec=credentials.Credentials)
374+
def test_gapic_instance_method(creds):
400375
client = publisher.Client(credentials=creds)
401376

402377
transport_mock = mock.Mock(create_topic=mock.sentinel)
@@ -432,15 +407,13 @@ def test_class_method_factory():
432407
assert isinstance(client, publisher.Client)
433408

434409

435-
def test_gapic_class_method_on_instance():
436-
creds = mock.Mock(spec=credentials.Credentials)
410+
def test_gapic_class_method_on_instance(creds):
437411
client = publisher.Client(credentials=creds)
438412
answer = client.topic_path("foo", "bar")
439413
assert answer == "projects/foo/topics/bar"
440414

441415

442-
def test_commit_thread_created_on_publish():
443-
creds = mock.Mock(spec=credentials.Credentials)
416+
def test_commit_thread_created_on_publish(creds):
444417
# Max latency is not infinite so a commit thread is created.
445418
batch_settings = types.BatchSettings(max_latency=600)
446419
client = publisher.Client(batch_settings=batch_settings, credentials=creds)
@@ -463,8 +436,7 @@ def test_commit_thread_created_on_publish():
463436
_start_commit_thread.assert_called_once()
464437

465438

466-
def test_commit_thread_not_created_on_publish_if_max_latency_is_inf():
467-
creds = mock.Mock(spec=credentials.Credentials)
439+
def test_commit_thread_not_created_on_publish_if_max_latency_is_inf(creds):
468440
# Max latency is infinite so a commit thread is not created.
469441
batch_settings = types.BatchSettings(max_latency=float("inf"))
470442
client = publisher.Client(batch_settings=batch_settings, credentials=creds)
@@ -473,8 +445,7 @@ def test_commit_thread_not_created_on_publish_if_max_latency_is_inf():
473445
assert client._commit_thread is None
474446

475447

476-
def test_wait_and_commit_sequencers():
477-
creds = mock.Mock(spec=credentials.Credentials)
448+
def test_wait_and_commit_sequencers(creds):
478449
# Max latency is infinite so a commit thread is not created.
479450
# We don't want a commit thread to interfere with this test.
480451
batch_settings = types.BatchSettings(max_latency=float("inf"))
@@ -492,8 +463,7 @@ def test_wait_and_commit_sequencers():
492463
assert _commit_sequencers.call_count == 1
493464

494465

495-
def test_stopped_client_does_not_commit_sequencers():
496-
creds = mock.Mock(spec=credentials.Credentials)
466+
def test_stopped_client_does_not_commit_sequencers(creds):
497467
# Max latency is infinite so a commit thread is not created.
498468
# We don't want a commit thread to interfere with this test.
499469
batch_settings = types.BatchSettings(max_latency=float("inf"))
@@ -515,8 +485,7 @@ def test_stopped_client_does_not_commit_sequencers():
515485
assert _commit_sequencers.call_count == 0
516486

517487

518-
def test_publish_with_ordering_key():
519-
creds = mock.Mock(spec=credentials.Credentials)
488+
def test_publish_with_ordering_key(creds):
520489
publisher_options = types.PublisherOptions(enable_message_ordering=True)
521490
client = publisher.Client(publisher_options=publisher_options, credentials=creds)
522491

@@ -555,8 +524,7 @@ def test_publish_with_ordering_key():
555524
)
556525

557526

558-
def test_ordered_sequencer_cleaned_up():
559-
creds = mock.Mock(spec=credentials.Credentials)
527+
def test_ordered_sequencer_cleaned_up(creds):
560528
# Max latency is infinite so a commit thread is not created.
561529
# We don't want a commit thread to interfere with this test.
562530
batch_settings = types.BatchSettings(max_latency=float("inf"))
@@ -584,8 +552,7 @@ def test_ordered_sequencer_cleaned_up():
584552
assert len(client._sequencers) == 0
585553

586554

587-
def test_resume_publish():
588-
creds = mock.Mock(spec=credentials.Credentials)
555+
def test_resume_publish(creds):
589556
publisher_options = types.PublisherOptions(enable_message_ordering=True)
590557
client = publisher.Client(publisher_options=publisher_options, credentials=creds)
591558

@@ -598,8 +565,7 @@ def test_resume_publish():
598565
assert sequencer.unpause.called_once()
599566

600567

601-
def test_resume_publish_no_sequencer_found():
602-
creds = mock.Mock(spec=credentials.Credentials)
568+
def test_resume_publish_no_sequencer_found(creds):
603569
publisher_options = types.PublisherOptions(enable_message_ordering=True)
604570
client = publisher.Client(publisher_options=publisher_options, credentials=creds)
605571

@@ -608,8 +574,7 @@ def test_resume_publish_no_sequencer_found():
608574
client.resume_publish("topic", "ord_key")
609575

610576

611-
def test_resume_publish_ordering_keys_not_enabled():
612-
creds = mock.Mock(spec=credentials.Credentials)
577+
def test_resume_publish_ordering_keys_not_enabled(creds):
613578
publisher_options = types.PublisherOptions(enable_message_ordering=False)
614579
client = publisher.Client(publisher_options=publisher_options, credentials=creds)
615580

0 commit comments

Comments
 (0)