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
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the 'License');
4
4
# you may not use this file except in compliance with the License.
@@ -80,17 +80,23 @@ def hello_http(request):
80
80
81
81
82
82
# [START functions_helloworld_pubsub]
83
- def hello_pubsub (data , context ):
83
+ def hello_pubsub (event , context ):
84
84
"""Background Cloud Function to be triggered by Pub/Sub.
85
85
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.
87
89
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.
89
92
"""
90
93
import base64
91
94
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' )
94
100
else :
95
101
name = 'World'
96
102
print ('Hello {}!' .format (name ))
Original file line number Diff line number Diff line change 1
- # Copyright 2018 Google LLC
1
+ # Copyright 2019 Google LLC
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the 'License');
4
4
# 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
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the 'License');
4
4
# you may not use this file except in compliance with the License.
14
14
15
15
# [START functions_pubsub_unit_test]
16
16
import base64
17
+ import mock
17
18
18
19
import main
19
20
20
21
22
+ mock_context = mock .Mock ()
23
+ mock_context .event_id = '617187464135194'
24
+ mock_context .timestamp = '2019-07-15T22:09:03.761Z'
25
+
26
+
21
27
def test_print_hello_world (capsys ):
22
28
data = {}
23
29
24
30
# Call tested function
25
- main .hello_pubsub (data , None )
31
+ main .hello_pubsub (data , mock_context )
26
32
out , err = capsys .readouterr ()
27
- assert out == 'Hello World!\n '
33
+ assert 'Hello World!' in out
28
34
29
35
30
36
def test_print_name (capsys ):
31
37
name = 'test'
32
38
data = {'data' : base64 .b64encode (name .encode ())}
33
39
34
40
# Call tested function
35
- main .hello_pubsub (data , None )
41
+ main .hello_pubsub (data , mock_context )
36
42
out , err = capsys .readouterr ()
37
- assert out == 'Hello {}!\n ' .format (name )
43
+ assert 'Hello {}!\n ' .format (name ) in out
38
44
# [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
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the 'License');
4
4
# you may not use this file except in compliance with the License.
@@ -58,5 +58,5 @@ def test_print_name(publisher_client):
58
58
start_time
59
59
], stdout = subprocess .PIPE )
60
60
logs = str (log_process .communicate ()[0 ])
61
- assert 'Hello, {}!' .format (name ) in logs
61
+ assert 'Hello {}!' .format (name ) in logs
62
62
# [END functions_pubsub_system_test]
You can’t perform that action at this time.
0 commit comments