8000 Functions: improve existing sample by anguillanneuf · Pull Request #2220 · GoogleCloudPlatform/python-docs-samples · GitHub
[go: up one dir, main page]

Skip to content

Functions: improve existing sample #2220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Fai 8000 led to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions functions/helloworld/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google LLC
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,17 +80,23 @@ def hello_http(request):


# [START functions_helloworld_pubsub]
def hello_pubsub(data, context):
def hello_pubsub(event, context):
"""Background Cloud Function to be triggered by Pub/Sub.
Args:
data (dict): The dictionary with data specific to this type of event.
event (dict): The dictionary with data specific to this type of
event. The `data` field contains the PubsubMessage message. The
`attributes` field will contain custom attributes if there are any.
context (google.cloud.functions.Context): The Cloud Functions event
metadata.
metadata. The `event_id` field contains the Pub/Sub message ID. The
`timestamp` field contains the publish time.
"""
import base64

if 'data' in data:
name = base64.b64decode(data['data']).decode('utf-8')
print("""This Function was triggered by messageId {} published at {}
""".format(context.event_id, context.timestamp))

if 'data' in event:
name = base64.b64decode(event['data']).decode('utf-8')
else:
name = 'World'
print('Hello {}!'.format(name))
Expand Down
2 changes: 1 addition & 1 deletion functions/helloworld/main_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google LLC
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
Expand Down
16 changes: 11 additions & 5 deletions functions/helloworld/sample_pubsub_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google LLC
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
Expand All @@ -14,25 +14,31 @@

# [START functions_pubsub_unit_test]
import base64
import mock

import main


mock_context = mock.Mock()
mock_context.event_id = '617187464135194'
mock_context.timestamp = '2019-07-15T22:09:03.761Z'


def test_print_hello_world(capsys):
data = {}

# Call tested function
main.hello_pubsub(data, None)
main.hello_pubsub(data, mock_context)
out, err = capsys.readouterr()
assert out == 'Hello World!\n'
assert 'Hello World!' in out


def test_print_name(capsys):
name = 'test'
data = {'data': base64.b64encode(name.encode())}

# Call tested function
main.hello_pubsub(data, None)
main.hello_pubsub(data, mock_context)
out, err = capsys.readouterr()
assert out == 'Hello {}!\n'.format(name)
assert 'Hello {}!\n'.format(name) in out
# [END functions_pubsub_unit_test]
4 changes: 2 additions & 2 deletions functions/helloworld/sample_pubsub_test_system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Google LLC
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,5 +58,5 @@ def test_print_name(publisher_client):
start_time
], stdout=subprocess.PIPE)
logs = str(log_process.communicate()[0])
assert 'Hello, {}!'.format(name) in logs
assert 'Hello {}!'.format(name) in logs
# [END functions_pubsub_system_test]
0