File tree Expand file tree Collapse file tree 4 files changed +26
-14
lines changed
Expand file tree Collapse file tree 4 files changed +26
-14
lines changed Original file line number Diff line number Diff line change 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 ))
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.
1414
1515# [START functions_pubsub_unit_test]
1616import base64
17+ import mock
1718
1819import 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+
2127def 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
3036def 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]
Original file line number Diff line number Diff line change 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]
You can’t perform that action at this time.
0 commit comments