Skip to content

Commit

Permalink
Adjust method calls in samples to new client
Browse files Browse the repository at this point in the history
  • Loading branch information
plamut committed Jul 16, 2020
1 parent 5a78181 commit 2b01cc4
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 101 deletions.
16 changes: 10 additions & 6 deletions samples/snippets/iam_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def topic(publisher_client):
topic_path = publisher_client.topic_path(PROJECT, TOPIC)

try:
publisher_client.delete_topic(topic_path)
publisher_client.delete_topic(request={"topic": topic_path})
except Exception:
pass

publisher_client.create_topic(topic_path)
publisher_client.create_topic(request={"name": topic_path})

yield topic_path

publisher_client.delete_topic(topic_path)
publisher_client.delete_topic(request={"topic": topic_path})


@pytest.fixture(scope="module")
Expand All @@ -59,15 +59,19 @@ def subscription(subscriber_client, topic):
subscription_path = subscriber_client.subscription_path(PROJECT, SUBSCRIPTION)

try:
subscriber_client.delete_subscription(subscription_path)
subscriber_client.delete_subscription(
request={"subscription": subscription_path}
)
except Exception:
pass

subscriber_client.create_subscription(subscription_path, topic=topic)
subscriber_client.create_subscription(
request={"name": subscription_path, "topic": topic}
)

yield subscription_path

subscriber_client.delete_subscription(subscription_path)
subscriber_client.delete_subscription(request={"subscription": subscription_path})


def test_get_topic_policy(topic, capsys):
Expand Down
26 changes: 12 additions & 14 deletions samples/snippets/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,22 @@

TEST_CONFIG = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

"ignored_versions": ["2.7"],
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
"envs": {},
}


try:
# Ensure we can import noxfile_config in the project's directory.
sys.path.append('.')
sys.path.append(".")
from noxfile_config import TEST_CONFIG_OVERRIDE
except ImportError as e:
print("No user noxfile_config found: detail: {}".format(e))
Expand All @@ -69,12 +67,12 @@ def get_pytest_env_vars():
ret = {}

# Override the GCLOUD_PROJECT and the alias.
env_key = TEST_CONFIG['gcloud_project_env']
env_key = TEST_CONFIG["gcloud_project_env"]
# This should error out if not set.
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]

# Apply user supplied envs.
ret.update(TEST_CONFIG['envs'])
ret.update(TEST_CONFIG["envs"])
return ret


Expand All @@ -83,7 +81,7 @@ def get_pytest_env_vars():
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8"]

# Any default versions that should be ignored.
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]

TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])

Expand Down Expand Up @@ -138,7 +136,7 @@ def lint(session):
args = FLAKE8_COMMON_ARGS + [
"--application-import-names",
",".join(local_names),
"."
".",
]
session.run("flake8", *args)

Expand Down Expand Up @@ -182,9 +180,9 @@ def py(session):
if session.python in TESTED_VERSIONS:
_session_tests(session)
else:
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
session.python
))
session.skip(
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
)


#
Expand Down
26 changes: 12 additions & 14 deletions samples/snippets/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def list_topics(project_id):
# project_id = "your-project-id"

publisher = pubsub_v1.PublisherClient()
project_path = publisher.project_path(project_id)
project_path = f"projects/{project_id}"

for topic in publisher.list_topics(project_path):
for topic in publisher.list_topics(request={"project": project_path}):
print(topic)
# [END pubsub_list_topics]

Expand All @@ -53,7 +53,7 @@ def create_topic(project_id, topic_id):
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)

topic = publisher.create_topic(topic_path)
topic = publisher.create_topic(request={"name": topic_path})

print("Topic created: {}".format(topic))
# [END pubsub_quickstart_create_topic]
Expand All @@ -72,7 +72,7 @@ def delete_topic(project_id, topic_id):
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)

publisher.delete_topic(topic_path)
publisher.delete_topic(request={"topic": topic_path})

print("Topic deleted: {}".format(topic_path))
# [END pubsub_delete_topic]
Expand All @@ -94,11 +94,11 @@ def publish_messages(project_id, topic_id):
topic_path = publisher.topic_path(project_id, topic_id)

for n in range(1, 10):
data = u"Message number {}".format(n)
data = "Message number {}".format(n)
# Data must be a bytestring
data = data.encode("utf-8")
# When you publish a message, the client returns a future.
future = publisher.publish(topic_path, data=data)
future = publisher.publish(topic_path, data)
print(future.result())

print("Published messages.")
Expand All @@ -120,7 +120,7 @@ def publish_messages_with_custom_attributes(project_id, topic_id):
topic_path = publisher.topic_path(project_id, topic_id)

for n in range(1, 10):
data = u"Message number {}".format(n)
data = "Message number {}".format(n)
# Data must be a bytestring
data = data.encode("utf-8")
# Add two attributes, origin and username, to the message
Expand Down Expand Up @@ -163,9 +163,7 @@ def callback(f):
data = str(i)
futures.update({data: None})
# When you publish a message, the client returns a future.
future = publisher.publish(
topic_path, data=data.encode("utf-8") # data must be a bytestring.
)
future = publisher.publish(topic_path, data.encode("utf-8"))
futures[data] = future
# Publish failures shall be handled in the callback function.
future.add_done_callback(get_callback(future, data))
Expand Down Expand Up @@ -203,10 +201,10 @@ def callback(future):
print(message_id)

for n in range(1, 10):
data = u"Message number {}".format(n)
data = "Message number {}".format(n)
# Data must be a bytestring
data = data.encode("utf-8")
future = publisher.publish(topic_path, data=data)
future = publisher.publish(topic_path, data)
# Non-blocking. Allow the publisher client to batch multiple messages.
future.add_done_callback(callback)

Expand Down Expand Up @@ -263,10 +261,10 @@ def publish_messages_with_retry_settings(project_id, topic_id):
topic_path = publisher.topic_path(project_id, topic_id)

for n in range(1, 10):
data = u"Message number {}".format(n)
data = "Message number {}".format(n)
# Data must be a bytestring
data = data.encode("utf-8")
future = publisher.publish(topic_path, data=data)
future = publisher.publish(request={"topic": topic_path, "messages": data})
print(future.result())

print("Published messages with retry settings.")
Expand Down
16 changes: 8 additions & 8 deletions samples/snippets/publisher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def topic_admin(client):
topic_path = client.topic_path(PROJECT, TOPIC_ADMIN)

try:
topic = client.get_topic(topic_path)
topic = client.get_topic(request={"topic": topic_path})
except: # noqa
topic = client.create_topic(topic_path)
topic = client.create_topic(request={"name": topic_path})

yield topic.name
# Teardown of `topic_admin` is handled in `test_delete()`.
Expand All @@ -52,13 +52,13 @@ def topic_publish(client):
topic_path = client.topic_path(PROJECT, TOPIC_PUBLISH)

try:
topic = client.get_topic(topic_path)
topic = client.get_topic(request={"topic": topic_path})
except: # noqa
topic = client.create_topic(topic_path)
topic = client.create_topic(request={"name": topic_path})

yield topic.name

client.delete_topic(topic.name)
client.delete_topic(request={"topic": topic.name})


def _make_sleep_patch():
Expand Down Expand Up @@ -87,15 +87,15 @@ def eventually_consistent_test():
def test_create(client):
topic_path = client.topic_path(PROJECT, TOPIC_ADMIN)
try:
client.delete_topic(topic_path)
client.delete_topic(request={"topic": topic_path})
except Exception:
pass

publisher.create_topic(PROJECT, TOPIC_ADMIN)

@backoff.on_exception(backoff.expo, AssertionError, max_time=60)
def eventually_consistent_test():
assert client.get_topic(topic_path)
assert client.get_topic(request={"topic": topic_path})

eventually_consistent_test()

Expand All @@ -106,7 +106,7 @@ def test_delete(client, topic_admin):
@backoff.on_exception(backoff.expo, AssertionError, max_time=60)
def eventually_consistent_test():
with pytest.raises(Exception):
client.get_topic(client.topic_path(PROJECT, TOPIC_ADMIN))
client.get_topic(request={"topic": client.topic_path(PROJECT, TOPIC_ADMIN)})

eventually_consistent_test()

Expand Down
2 changes: 1 addition & 1 deletion samples/snippets/quickstart/pub.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def pub(project_id, topic_id):
ref = dict({"num_messages": 0})

# When you publish a message, the client returns a future.
api_future = client.publish(topic_path, data=data)
api_future = client.publish(topic_path, data)
api_future.add_done_callback(get_callback(api_future, data, ref))

# Keep the main thread from exiting while the message future
Expand Down
4 changes: 2 additions & 2 deletions samples/snippets/quickstart/pub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def topic(publisher_client):
topic_path = publisher_client.topic_path(PROJECT, TOPIC)

try:
publisher_client.create_topic(topic_path)
publisher_client.create_topic(request={"name": topic_path})
except AlreadyExists:
pass

yield TOPIC

publisher_client.delete_topic(topic_path)
publisher_client.delete_topic(request={"topic": topic_path})


def test_pub(publisher_client, topic, capsys):
Expand Down
10 changes: 5 additions & 5 deletions samples/snippets/quickstart/sub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def topic_path():
topic_path = publisher_client.topic_path(PROJECT, TOPIC)

try:
topic = publisher_client.create_topic(topic_path)
topic = publisher_client.create_topic(request={"name": topic_path})
yield topic.name
except AlreadyExists:
yield topic_path

publisher_client.delete_topic(topic_path)
publisher_client.delete_topic(request={"topic": topic_path})


@pytest.fixture(scope="module")
Expand All @@ -52,18 +52,18 @@ def subscription_path(topic_path):

try:
subscription = subscriber_client.create_subscription(
subscription_path, topic_path
request={"name": subscription_path, "topic": topic_path}
)
yield subscription.name
except AlreadyExists:
yield subscription_path

subscriber_client.delete_subscription(subscription_path)
subscriber_client.delete_subscription(request={"subscription": subscription_path})
subscriber_client.close()


def _publish_messages(topic_path):
publish_future = publisher_client.publish(topic_path, data=b"Hello World!")
publish_future = publisher_client.publish(topic_path, b"Hello World!")
publish_future.result()


Expand Down
Loading

0 comments on commit 2b01cc4

Please sign in to comment.