Skip to content

Commit

Permalink
Making Pub/Sub unit tests use mock credentials. (#4454)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes authored Nov 27, 2017
1 parent 4b10761 commit 7a9e4f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
31 changes: 21 additions & 10 deletions pubsub/tests/unit/pubsub_v1/publisher/test_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
from __future__ import absolute_import
import os

from google.auth import credentials
import mock

import pytest

from google.auth import credentials
from google.cloud.pubsub_v1.gapic import publisher_client
from google.cloud.pubsub_v1 import publisher
from google.cloud.pubsub_v1 import types


def test_init():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)

# A plain client should have an `api` (the underlying GAPIC) and a
# batch settings object, which should have the defaults.
Expand All @@ -38,6 +39,8 @@ def test_init():

def test_init_emulator(monkeypatch):
monkeypatch.setenv('PUBSUB_EMULATOR_HOST', '/foo/bar/')
# NOTE: When the emulator host is set, a custom channel will be used, so
# no credentials (mock ot otherwise) can be passed in.
client = publisher.Client()

# Establish that a gRPC request would attempt to hit the emulator host.
Expand All @@ -50,7 +53,8 @@ def test_init_emulator(monkeypatch):

def test_batch_accepting():
"""Establish that an existing batch is returned if it accepts messages."""
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
message = types.PubsubMessage(data=b'foo')

# At first, there are no batches, so this should return a new batch
Expand All @@ -67,7 +71,8 @@ def test_batch_accepting():


def test_batch_without_autocreate():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
message = types.PubsubMessage(data=b'foo')

# If `create=False` is sent, then when the batch is not found, None
Expand All @@ -79,7 +84,8 @@ def test_batch_without_autocreate():


def test_publish():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)

# Use a mock in lieu of the actual batch class; set the mock up to claim
# indiscriminately that it accepts all messages.
Expand Down Expand Up @@ -107,15 +113,17 @@ def test_publish():


def test_publish_data_not_bytestring_error():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
with pytest.raises(TypeError):
client.publish('topic_name', u'This is a text string.')
with pytest.raises(TypeError):
client.publish('topic_name', 42)


def test_publish_attrs_bytestring():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)

# Use a mock in lieu of the actual batch class; set the mock up to claim
# indiscriminately that it accepts all messages.
Expand All @@ -133,13 +141,15 @@ def test_publish_attrs_bytestring():


def test_publish_attrs_type_error():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
with pytest.raises(TypeError):
client.publish('topic_name', b'foo', answer=42)


def test_gapic_instance_method():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
with mock.patch.object(client.api, '_create_topic', autospec=True) as ct:
client.create_topic('projects/foo/topics/bar')
assert ct.call_count == 1
Expand All @@ -148,6 +158,7 @@ def test_gapic_instance_method():


def test_gapic_class_method():
client = publisher.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
answer = client.topic_path('foo', 'bar')
assert answer == 'projects/foo/topics/bar'
13 changes: 9 additions & 4 deletions pubsub/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from google.auth import credentials
import mock

from google.auth import credentials
from google.cloud.pubsub_v1 import subscriber
from google.cloud.pubsub_v1.subscriber.policy import thread


def test_init():
client = subscriber.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = subscriber.Client(credentials=creds)
assert client._policy_class is thread.Policy


def test_init_emulator(monkeypatch):
monkeypatch.setenv('PUBSUB_EMULATOR_HOST', '/baz/bacon/')
# NOTE: When the emulator host is set, a custom channel will be used, so
# no credentials (mock ot otherwise) can be passed in.
client = subscriber.Client()

# Establish that a gRPC request would attempt to hit the emulator host.
Expand All @@ -37,13 +40,15 @@ def test_init_emulator(monkeypatch):


def test_subscribe():
client = subscriber.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = subscriber.Client(credentials=creds)
subscription = client.subscribe('sub_name_a')
assert isinstance(subscription, thread.Policy)


def test_subscribe_with_callback():
client = subscriber.Client()
creds = mock.Mock(spec=credentials.Credentials)
client = subscriber.Client(credentials=creds)
callback = mock.Mock()
with mock.patch.object(thread.Policy, 'open') as open_:
subscription = client.subscribe('sub_name_b', callback)
Expand Down

0 comments on commit 7a9e4f8

Please sign in to comment.