diff --git a/functions/helloworld/main.py b/functions/helloworld/main.py index da9b593d475..9c3d17190d8 100644 --- a/functions/helloworld/main.py +++ b/functions/helloworld/main.py @@ -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. @@ -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)) diff --git a/functions/helloworld/main_test.py b/functions/helloworld/main_test.py index e033276749c..bdb18682061 100644 --- a/functions/helloworld/main_test.py +++ b/functions/helloworld/main_test.py @@ -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. diff --git a/functions/helloworld/sample_pubsub_test.py b/functions/helloworld/sample_pubsub_test.py index 64fc8e1d02a..bb117853e18 100644 --- a/functions/helloworld/sample_pubsub_test.py +++ b/functions/helloworld/sample_pubsub_test.py @@ -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. @@ -14,17 +14,23 @@ # [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): @@ -32,7 +38,7 @@ def test_print_name(capsys): 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] diff --git a/functions/helloworld/sample_pubsub_test_system.py b/functions/helloworld/sample_pubsub_test_system.py index bcd644444c3..dd2d9898bc1 100644 --- a/functions/helloworld/sample_pubsub_test_system.py +++ b/functions/helloworld/sample_pubsub_test_system.py @@ -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. @@ -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]