Skip to content

Commit

Permalink
Functions: improve existing sample (GoogleCloudPlatform#2220)
Browse files Browse the repository at this point in the history
* improved example

* reformat

* updated code

* remove extra exclamation mark
  • Loading branch information
anguillanneuf authored Jul 19, 2019
1 parent 9f40eff commit 729efa6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
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 comments on commit 729efa6

Please sign in to comment.