8000 Functions: improve existing sample (#2220) · lwander/python-docs-samples@729efa6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 729efa6

Browse files
Functions: improve existing sample (GoogleCloudPlatform#2220)
* improved example * reformat * updated code * remove extra exclamation mark
1 parent 9f40eff commit 729efa6

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

functions/helloworld/main.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the 'License');
44
# you may not use this file except in compliance with the License.
@@ -80,17 +80,23 @@ def hello_http(request):
8080

8181

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

92-
if 'data' in data:
93-
name = base64.b64decode(data['data']).decode('utf-8')
95+
print("""This Function was triggered by messageId {} published at {}
96+
""".format(context.event_id, context.timestamp))
97+
98+
if 'data' in event:
99+
name = base64.b64decode(event['data']).decode('utf-8')
94100
else:
95101
name = 'World'
96102
print('Hello {}!'.format(name))

functions/helloworld/main_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the 'License');
44
# you may not use this file except in compliance with the License.

functions/helloworld/sample_pubsub_test.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the 'License');
44
# you may not use this file except in compliance with the License.
@@ -14,25 +14,31 @@
1414

1515
# [START functions_pubsub_unit_test]
1616
import base64
17+
import mock
1718

1819
import main
1920

2021

22+
mock_context = mock.Mock()
23+
mock_context.event_id = '617187464135194'
24+
mock_context.timestamp = '2019-07-15T22:09:03.761Z'
25+
26+
2127
def test_print_hello_world(capsys):
2228
data = {}
2329

2430
# Call tested function
25-
main.hello_pubsub(data, None)
31+
main.hello_pubsub(data, mock_context)
2632
out, err = capsys.readouterr()
27-
assert out == 'Hello World!\n'
33+
assert 'Hello World!' in out
2834

2935

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

3440
# Call tested function
35-
main.hello_pubsub(data, None)
41+
main.hello_pubsub(data, mock_context)
3642
out, err = capsys.readouterr()
37-
assert out == 'Hello {}!\n'.format(name)
43+
assert 'Hello {}!\n'.format(name) in out
3844
# [END functions_pubsub_unit_test]

functions/helloworld/sample_pubsub_test_system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the 'License');
44
# you may not use this file except in compliance with the License.
@@ -58,5 +58,5 @@ def test_print_name(publisher_client):
5858
start_time
5959
], stdout=subprocess.PIPE)
6060
logs = str(log_process.communicate()[0])
61-
assert 'Hello, {}!'.format(name) in logs
61+
assert 'Hello {}!'.format(name) in logs
6262
# [END functions_pubsub_system_test]

0 commit comments

Comments
 (0)
0