diff --git a/.github/workflows/conformance.yml b/.github/workflows/conformance.yml index 93a55b2f..940bdf58 100644 --- a/.github/workflows/conformance.yml +++ b/.github/workflows/conformance.yml @@ -41,7 +41,7 @@ jobs: validateMapping: true cmd: "'functions-framework --source tests/conformance/main.py --target write_legacy_event --signature-type event'" - - name: Run cloudevent conformance tests + - name: Run CloudEvents conformance tests uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.1.0 with: version: 'v1.1.0' @@ -59,11 +59,11 @@ jobs: validateMapping: false cmd: "'functions-framework --source tests/conformance/main.py --target write_http_declarative'" - - name: Run cloudevent conformance tests declarative + - name: Run CloudEvents conformance tests declarative uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.1.0 with: version: 'v1.1.0' functionType: 'cloudevent' useBuildpacks: false validateMapping: true - cmd: "'functions-framework --source tests/conformance/main.py --target write_cloudevent_declarative'" + cmd: "'functions-framework --source tests/conformance/main.py --target write_cloud_event_declarative'" diff --git a/README.md b/README.md index 7d5c1e3d..029620ce 100644 --- a/README.md +++ b/README.md @@ -118,9 +118,9 @@ Create an `main.py` file with the following contents: ```python import functions_framework -@functions_framework.cloudevent -def hello_cloudevent(cloudevent): - return f"Received event with ID: {cloudevent['id']} and data {cloudevent.data}" +@functions_framework.cloud_event +def hello_cloud_event(cloud_event): + return f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}" @functions_framework.http def hello_http(request): @@ -136,13 +136,13 @@ functions-framework --target=hello_http Open http://localhost:8080/ in your browser and see *Hello world!*. -Run the following command to run `hello_cloudevent` target locally: +Run the following command to run `hello_cloud_event` target locally: ```sh -functions-framework --target=hello_cloudevent +functions-framework --target=hello_cloud_event ``` -More info on sending [CloudEvents](http://cloudevents.io) payloads, see [`examples/cloud_run_cloudevents`](examples/cloud_run_cloudevents/) instruction. +More info on sending [CloudEvents](http://cloudevents.io) payloads, see [`examples/cloud_run_cloud_events`](examples/cloud_run_cloud_events/) instruction. ### Quickstart: Error handling @@ -358,11 +358,11 @@ See the [running example](examples/cloud_run_event). ## Enable CloudEvents -The Functions framework can also unmarshall incoming [CloudEvents](http://cloudevents.io) payloads to the `cloudevent` object. This will be passed as a [cloudevent](https://github.com/cloudevents/sdk-python) to your function when it receives a request. Note that your function must use the `cloudevents`-style function signature: +The Functions framework can also unmarshall incoming [CloudEvents](http://cloudevents.io) payloads to the `cloud_event` object. This will be passed as a [CloudEvent](https://github.com/cloudevents/sdk-python) to your function when it receives a request. Note that your function must use the `CloudEvents`-style function signature: ```python -def hello(cloudevent): - print(f"Received event with ID: {cloudevent['id']}") +def hello(cloud_event): + print(f"Received event with ID: {cloud_event['id']}") ``` To enable automatic unmarshalling, set the function signature type to `cloudevent` using the `--signature-type` command-line flag or the `FUNCTION_SIGNATURE_TYPE` environment variable. By default, the HTTP signature type will be used and automatic event unmarshalling will be disabled. diff --git a/examples/README.md b/examples/README.md index dfc93131..7960a743 100644 --- a/examples/README.md +++ b/examples/README.md @@ -4,7 +4,7 @@ ### Cloud Run * [`cloud_run_http`](./cloud_run_http/) - Deploying an HTTP function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework * [`cloud_run_event`](./cloud_run_event/) - Deploying a CloudEvent function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework -* [`cloud_run_cloudevents`](./cloud_run_cloudevents/) - Deploying a [CloudEvent](https://github.com/cloudevents/sdk-python) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework +* [`cloud_run_cloud_events`](cloud_run_cloud_events/) - Deploying a [CloudEvent](https://github.com/cloudevents/sdk-python) function to [Cloud Run](http://cloud.google.com/run) with the Functions Framework ## Development Tools * [`docker-compose`](./docker-compose) - diff --git a/examples/cloud_run_cloudevents/Dockerfile b/examples/cloud_run_cloud_events/Dockerfile similarity index 100% rename from examples/cloud_run_cloudevents/Dockerfile rename to examples/cloud_run_cloud_events/Dockerfile diff --git a/examples/cloud_run_cloudevents/README.md b/examples/cloud_run_cloud_events/README.md similarity index 72% rename from examples/cloud_run_cloudevents/README.md rename to examples/cloud_run_cloud_events/README.md index 03a9931a..4bf54528 100644 --- a/examples/cloud_run_cloudevents/README.md +++ b/examples/cloud_run_cloud_events/README.md @@ -7,17 +7,17 @@ This sample uses the [CloudEvents SDK](https://github.com/cloudevents/sdk-python Build the Docker image: ```commandline -docker build -t cloudevent_example . +docker build -t cloud_event_example . ``` Run the image and bind the correct ports: ```commandline -docker run --rm -p 8080:8080 -e PORT=8080 cloudevent_example +docker run --rm -p 8080:8080 -e PORT=8080 cloud_event_example ``` Send an event to the container: ```python -docker run -t cloudevent_example send_cloudevent.py +docker run -t cloud_event_example send_cloud_event.py ``` diff --git a/examples/cloud_run_cloudevents/main.py b/examples/cloud_run_cloud_events/main.py similarity index 86% rename from examples/cloud_run_cloudevents/main.py rename to examples/cloud_run_cloud_events/main.py index da77322f..eb6d6dc0 100644 --- a/examples/cloud_run_cloudevents/main.py +++ b/examples/cloud_run_cloud_events/main.py @@ -16,5 +16,5 @@ # (https://github.com/cloudevents/sdk-python) -def hello(cloudevent): - print(f"Received event with ID: {cloudevent['id']} and data {cloudevent.data}") +def hello(cloud_event): + print(f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}") diff --git a/examples/cloud_run_cloudevents/requirements.txt b/examples/cloud_run_cloud_events/requirements.txt similarity index 100% rename from examples/cloud_run_cloudevents/requirements.txt rename to examples/cloud_run_cloud_events/requirements.txt diff --git a/examples/cloud_run_cloudevents/send_cloudevent.py b/examples/cloud_run_cloud_events/send_cloud_event.py similarity index 100% rename from examples/cloud_run_cloudevents/send_cloudevent.py rename to examples/cloud_run_cloud_events/send_cloud_event.py diff --git a/examples/cloud_run_decorator/README.md b/examples/cloud_run_decorator/README.md index 92c33b6c..ba560b6b 100644 --- a/examples/cloud_run_decorator/README.md +++ b/examples/cloud_run_decorator/README.md @@ -1,7 +1,7 @@ ## How to run this locally This guide shows how to run `hello_http` target locally. -To test with `hello_cloudevent`, change the target accordingly in Dockerfile. +To test with `hello_cloud_event`, change the target accordingly in Dockerfile. Build the Docker image: diff --git a/examples/cloud_run_decorator/main.py b/examples/cloud_run_decorator/main.py index 291280f3..19f96ee0 100644 --- a/examples/cloud_run_decorator/main.py +++ b/examples/cloud_run_decorator/main.py @@ -17,9 +17,9 @@ import functions_framework -@functions_framework.cloudevent -def hello_cloudevent(cloudevent): - return f"Received event with ID: {cloudevent['id']} and data {cloudevent.data}" +@functions_framework.cloud_event +def hello_cloud_event(cloud_event): + return f"Received event with ID: {cloud_event['id']} and data {cloud_event.data}" @functions_framework.http diff --git a/src/functions_framework/__init__.py b/src/functions_framework/__init__.py index c67cb7cd..46c8882b 100644 --- a/src/functions_framework/__init__.py +++ b/src/functions_framework/__init__.py @@ -56,7 +56,7 @@ def write(self, out): return self.stderr.write(json.dumps(payload) + "\n") -def cloudevent(func): +def cloud_event(func): """Decorator that registers cloudevent as user function signature type.""" _function_registry.REGISTRY_MAP[ func.__name__ @@ -101,13 +101,13 @@ def view_func(path): return view_func -def _run_cloudevent(function, request): +def _run_cloud_event(function, request): data = request.get_data() event = from_http(request.headers, data) function(event) -def _cloudevent_view_func_wrapper(function, request): +def _cloud_event_view_func_wrapper(function, request): def view_func(path): ce_exception = None event = None @@ -125,7 +125,7 @@ def view_func(path): # Not a CloudEvent. Try converting to a CloudEvent. try: - function(event_conversion.background_event_to_cloudevent(request)) + function(event_conversion.background_event_to_cloud_event(request)) except EventConversionException as e: flask.abort( 400, @@ -144,9 +144,9 @@ def view_func(path): def _event_view_func_wrapper(function, request): def view_func(path): - if event_conversion.is_convertable_cloudevent(request): + if event_conversion.is_convertable_cloud_event(request): # Convert this CloudEvent to the equivalent background event data and context. - data, context = event_conversion.cloudevent_to_background_event(request) + data, context = event_conversion.cloud_event_to_background_event(request) function(data, context) elif is_binary(request.headers): # Support CloudEvents in binary content mode, with data being the @@ -214,7 +214,7 @@ def _configure_app(app, function, signature_type): ) ) - app.view_functions[signature_type] = _cloudevent_view_func_wrapper( + app.view_functions[signature_type] = _cloud_event_view_func_wrapper( function, flask.request ) else: diff --git a/src/functions_framework/event_conversion.py b/src/functions_framework/event_conversion.py index 0605d940..28cf2a1b 100644 --- a/src/functions_framework/event_conversion.py +++ b/src/functions_framework/event_conversion.py @@ -23,7 +23,7 @@ from functions_framework.exceptions import EventConversionException from google.cloud.functions.context import Context -_CLOUDEVENT_SPEC_VERSION = "1.0" +_CLOUD_EVENT_SPEC_VERSION = "1.0" # Maps background/legacy event types to their equivalent CloudEvent types. # For more info on event mappings see @@ -114,7 +114,7 @@ } -def background_event_to_cloudevent(request) -> CloudEvent: +def background_event_to_cloud_event(request) -> CloudEvent: """Converts a background event represented by the given HTTP request into a CloudEvent.""" event_data = marshal_background_event_data(request) if not event_data: @@ -154,7 +154,7 @@ def background_event_to_cloudevent(request) -> CloudEvent: # Handle Firebase DB events. if service == _FIREBASE_DB_CE_SERVICE: - # The CE source of firebasedatabase cloudevents includes location information + # The CE source of firebasedatabase CloudEvents includes location information # that is inferred from the 'domain' field of legacy events. if "domain" not in event_data: raise EventConversionException( @@ -172,7 +172,7 @@ def background_event_to_cloudevent(request) -> CloudEvent: metadata = { "id": context.event_id, "time": context.timestamp, - "specversion": _CLOUDEVENT_SPEC_VERSION, + "specversion": _CLOUD_EVENT_SPEC_VERSION, "datacontenttype": "application/json", "type": new_type, "source": source, @@ -184,7 +184,7 @@ def background_event_to_cloudevent(request) -> CloudEvent: return CloudEvent(metadata, data) -def is_convertable_cloudevent(request) -> bool: +def is_convertable_cloud_event(request) -> bool: """Is the given request a known CloudEvent that can be converted to background event.""" if is_binary(request.headers): event_type = request.headers.get("ce-type") @@ -207,7 +207,7 @@ def _split_ce_source(source) -> Tuple[str, str]: return match.group(1), match.group(2) -def cloudevent_to_background_event(request) -> Tuple[Any, Context]: +def cloud_event_to_background_event(request) -> Tuple[Any, Context]: """Converts a background event represented by the given HTTP request into a CloudEvent.""" try: event = from_http(request.headers, request.get_data()) diff --git a/tests/conformance/main.py b/tests/conformance/main.py index 72fa1633..e790f626 100644 --- a/tests/conformance/main.py +++ b/tests/conformance/main.py @@ -33,8 +33,8 @@ def write_legacy_event(data, context): ) -def write_cloud_event(cloudevent): - _write_output(to_json(cloudevent).decode()) +def write_cloud_event(cloud_event): + _write_output(to_json(cloud_event).decode()) @functions_framework.http @@ -43,6 +43,6 @@ def write_http_declarative(request): return "OK", 200 -@functions_framework.cloudevent -def write_cloudevent_declarative(cloudevent): - _write_output(to_json(cloudevent).decode()) +@functions_framework.cloud_event +def write_cloud_event_declarative(cloud_event): + _write_output(to_json(cloud_event).decode()) diff --git a/tests/test_cloudevent_functions.py b/tests/test_cloud_event_functions.py similarity index 86% rename from tests/test_cloudevent_functions.py rename to tests/test_cloud_event_functions.py index 237225cd..4ad8a527 100644 --- a/tests/test_cloudevent_functions.py +++ b/tests/test_cloud_event_functions.py @@ -36,12 +36,12 @@ def data_payload(): @pytest.fixture -def cloudevent_1_0(): +def cloud_event_1_0(): attributes = { "specversion": "1.0", "id": "my-id", "source": "from-galaxy-far-far-away", - "type": "cloudevent.greet.you", + "type": "cloud_event.greet.you", "time": "2020-08-16T13:58:54.471765", } data = {"name": "john"} @@ -49,11 +49,11 @@ def cloudevent_1_0(): @pytest.fixture -def cloudevent_0_3(): +def cloud_event_0_3(): attributes = { "id": "my-id", "source": "from-galaxy-far-far-away", - "type": "cloudevent.greet.you", + "type": "cloud_event.greet.you", "specversion": "0.3", "time": "2020-08-16T13:58:54.471765", } @@ -66,7 +66,7 @@ def create_headers_binary(): return lambda specversion: { "ce-id": "my-id", "ce-source": "from-galaxy-far-far-away", - "ce-type": "cloudevent.greet.you", + "ce-type": "cloud_event.greet.you", "ce-specversion": specversion, "time": "2020-08-16T13:58:54.471765", } @@ -77,7 +77,7 @@ def create_structured_data(): return lambda specversion: { "id": "my-id", "source": "from-galaxy-far-far-away", - "type": "cloudevent.greet.you", + "type": "cloud_event.greet.you", "specversion": specversion, "time": "2020-08-16T13:58:54.471765", } @@ -91,51 +91,51 @@ def background_event(): @pytest.fixture def client(): - source = TEST_FUNCTIONS_DIR / "cloudevents" / "main.py" + source = TEST_FUNCTIONS_DIR / "cloud_events" / "main.py" target = "function" return create_app(target, source, "cloudevent").test_client() @pytest.fixture def empty_client(): - source = TEST_FUNCTIONS_DIR / "cloudevents" / "empty_data.py" + source = TEST_FUNCTIONS_DIR / "cloud_events" / "empty_data.py" target = "function" return create_app(target, source, "cloudevent").test_client() @pytest.fixture def converted_background_event_client(): - source = TEST_FUNCTIONS_DIR / "cloudevents" / "converted_background_event.py" + source = TEST_FUNCTIONS_DIR / "cloud_events" / "converted_background_event.py" target = "function" return create_app(target, source, "cloudevent").test_client() -def test_event(client, cloudevent_1_0): - headers, data = to_structured(cloudevent_1_0) +def test_event(client, cloud_event_1_0): + headers, data = to_structured(cloud_event_1_0) resp = client.post("/", headers=headers, data=data) assert resp.status_code == 200 assert resp.data == b"OK" -def test_binary_event(client, cloudevent_1_0): - headers, data = to_binary(cloudevent_1_0) +def test_binary_event(client, cloud_event_1_0): + headers, data = to_binary(cloud_event_1_0) resp = client.post("/", headers=headers, data=data) assert resp.status_code == 200 assert resp.data == b"OK" -def test_event_0_3(client, cloudevent_0_3): - headers, data = to_structured(cloudevent_0_3) +def test_event_0_3(client, cloud_event_0_3): + headers, data = to_structured(cloud_event_0_3) resp = client.post("/", headers=headers, data=data) assert resp.status_code == 200 assert resp.data == b"OK" -def test_binary_event_0_3(client, cloudevent_0_3): - headers, data = to_binary(cloudevent_0_3) +def test_binary_event_0_3(client, cloud_event_0_3): + headers, data = to_binary(cloud_event_0_3) resp = client.post("/", headers=headers, data=data) assert resp.status_code == 200 @@ -143,7 +143,7 @@ def test_binary_event_0_3(client, cloudevent_0_3): @pytest.mark.parametrize("specversion", ["0.3", "1.0"]) -def test_cloudevent_missing_required_binary_fields( +def test_cloud_event_missing_required_binary_fields( client, specversion, create_headers_binary, data_payload ): headers = create_headers_binary(specversion) @@ -160,7 +160,7 @@ def test_cloudevent_missing_required_binary_fields( @pytest.mark.parametrize("specversion", ["0.3", "1.0"]) -def test_cloudevent_missing_required_structured_fields( +def test_cloud_event_missing_required_structured_fields( client, specversion, create_structured_data ): headers = {"Content-Type": "application/cloudevents+json"} @@ -186,7 +186,7 @@ def test_invalid_fields_binary(client, create_headers_binary, data_payload): assert "InvalidRequiredFields" in resp.data.decode() -def test_unparsable_cloudevent(client): +def test_unparsable_cloud_event(client): resp = client.post("/", headers={}, data="") assert resp.status_code == 400 diff --git a/tests/test_convert.py b/tests/test_convert.py index ae227c83..9202f567 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -83,7 +83,7 @@ @pytest.fixture -def pubsub_cloudevent_output(): +def pubsub_cloud_event_output(): return from_json(json.dumps(PUBSUB_CLOUD_EVENT)) @@ -121,7 +121,7 @@ def marshalled_pubsub_request(): @pytest.fixture -def raw_pubsub_cloudevent_output(marshalled_pubsub_request): +def raw_pubsub_cloud_event_output(marshalled_pubsub_request): event = PUBSUB_CLOUD_EVENT.copy() # the data payload is more complex for the raw pubsub request data = marshalled_pubsub_request["data"] @@ -138,8 +138,8 @@ def firebase_auth_background_input(): @pytest.fixture -def firebase_auth_cloudevent_output(): - with open(TEST_DATA_DIR / "firebase-auth-cloudevent-output.json", "r") as f: +def firebase_auth_cloud_event_output(): + with open(TEST_DATA_DIR / "firebase-auth-cloud-event-output.json", "r") as f: return from_json(f.read()) @@ -150,8 +150,8 @@ def firebase_db_background_input(): @pytest.fixture -def firebase_db_cloudevent_output(): - with open(TEST_DATA_DIR / "firebase-db-cloudevent-output.json", "r") as f: +def firebase_db_cloud_event_output(): + with open(TEST_DATA_DIR / "firebase-db-cloud-event-output.json", "r") as f: return from_json(f.read()) @@ -170,89 +170,89 @@ def create_ce_headers(): @pytest.mark.parametrize( "event", [PUBSUB_BACKGROUND_EVENT, PUBSUB_BACKGROUND_EVENT_WITHOUT_CONTEXT] ) -def test_pubsub_event_to_cloudevent(event, pubsub_cloudevent_output): +def test_pubsub_event_to_cloud_event(event, pubsub_cloud_event_output): req = flask.Request.from_values(json=event) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == pubsub_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == pubsub_cloud_event_output -def test_firebase_auth_event_to_cloudevent( - firebase_auth_background_input, firebase_auth_cloudevent_output +def test_firebase_auth_event_to_cloud_event( + firebase_auth_background_input, firebase_auth_cloud_event_output ): req = flask.Request.from_values(json=firebase_auth_background_input) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == firebase_auth_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == firebase_auth_cloud_event_output -def test_firebase_auth_event_to_cloudevent_no_metadata( - firebase_auth_background_input, firebase_auth_cloudevent_output +def test_firebase_auth_event_to_cloud_event_no_metadata( + firebase_auth_background_input, firebase_auth_cloud_event_output ): # Remove metadata from the events to verify conversion still works. del firebase_auth_background_input["data"]["metadata"] - del firebase_auth_cloudevent_output.data["metadata"] + del firebase_auth_cloud_event_output.data["metadata"] req = flask.Request.from_values(json=firebase_auth_background_input) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == firebase_auth_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == firebase_auth_cloud_event_output -def test_firebase_auth_event_to_cloudevent_no_metadata_timestamps( - firebase_auth_background_input, firebase_auth_cloudevent_output +def test_firebase_auth_event_to_cloud_event_no_metadata_timestamps( + firebase_auth_background_input, firebase_auth_cloud_event_output ): # Remove metadata timestamps from the events to verify conversion still works. del firebase_auth_background_input["data"]["metadata"]["createdAt"] del firebase_auth_background_input["data"]["metadata"]["lastSignedInAt"] - del firebase_auth_cloudevent_output.data["metadata"]["createTime"] - del firebase_auth_cloudevent_output.data["metadata"]["lastSignInTime"] + del firebase_auth_cloud_event_output.data["metadata"]["createTime"] + del firebase_auth_cloud_event_output.data["metadata"]["lastSignInTime"] req = flask.Request.from_values(json=firebase_auth_background_input) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == firebase_auth_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == firebase_auth_cloud_event_output -def test_firebase_auth_event_to_cloudevent_no_uid( - firebase_auth_background_input, firebase_auth_cloudevent_output +def test_firebase_auth_event_to_cloud_event_no_uid( + firebase_auth_background_input, firebase_auth_cloud_event_output ): # Remove UIDs from the events to verify conversion still works. The UID is mapped # to the subject in the CloudEvent so remove that from the expected CloudEvent. del firebase_auth_background_input["data"]["uid"] - del firebase_auth_cloudevent_output.data["uid"] - del firebase_auth_cloudevent_output["subject"] + del firebase_auth_cloud_event_output.data["uid"] + del firebase_auth_cloud_event_output["subject"] req = flask.Request.from_values(json=firebase_auth_background_input) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == firebase_auth_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == firebase_auth_cloud_event_output -def test_firebase_db_event_to_cloudevent_default_location( - firebase_db_background_input, firebase_db_cloudevent_output +def test_firebase_db_event_to_cloud_event_default_location( + firebase_db_background_input, firebase_db_cloud_event_output ): req = flask.Request.from_values(json=firebase_db_background_input) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == firebase_db_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == firebase_db_cloud_event_output -def test_firebase_db_event_to_cloudevent_location_subdomain( - firebase_db_background_input, firebase_db_cloudevent_output +def test_firebase_db_event_to_cloud_event_location_subdomain( + firebase_db_background_input, firebase_db_cloud_event_output ): firebase_db_background_input["domain"] = "europe-west1.firebasedatabase.app" - firebase_db_cloudevent_output["source"] = firebase_db_cloudevent_output[ + firebase_db_cloud_event_output["source"] = firebase_db_cloud_event_output[ "source" ].replace("us-central1", "europe-west1") req = flask.Request.from_values(json=firebase_db_background_input) - cloudevent = event_conversion.background_event_to_cloudevent(req) - assert cloudevent == firebase_db_cloudevent_output + cloud_event = event_conversion.background_event_to_cloud_event(req) + assert cloud_event == firebase_db_cloud_event_output -def test_firebase_db_event_to_cloudevent_missing_domain( - firebase_db_background_input, firebase_db_cloudevent_output +def test_firebase_db_event_to_cloud_event_missing_domain( + firebase_db_background_input, firebase_db_cloud_event_output ): del firebase_db_background_input["domain"] req = flask.Request.from_values(json=firebase_db_background_input) with pytest.raises(EventConversionException) as exc_info: - event_conversion.background_event_to_cloudevent(req) + event_conversion.background_event_to_cloud_event(req) assert ( "Invalid FirebaseDB event payload: missing 'domain'" in exc_info.value.args[0] @@ -364,8 +364,8 @@ def test_marshal_background_event_data_with_topic_path( ("marshalled_pubsub_request", {}), ], ) -def test_pubsub_emulator_request_to_cloudevent( - raw_pubsub_cloudevent_output, request_fixture, overrides, request +def test_pubsub_emulator_request_to_cloud_event( + raw_pubsub_cloud_event_output, request_fixture, overrides, request ): request_path = overrides.get("request_path", "/") payload = request.getfixturevalue(request_fixture) @@ -373,30 +373,30 @@ def test_pubsub_emulator_request_to_cloudevent( path=request_path, json=payload, ) - cloudevent = event_conversion.background_event_to_cloudevent(req) + cloud_event = event_conversion.background_event_to_cloud_event(req) # Remove timestamps as they are generated on the fly. - del raw_pubsub_cloudevent_output["time"] - del raw_pubsub_cloudevent_output.data["message"]["publishTime"] - del cloudevent["time"] - del cloudevent.data["message"]["publishTime"] + del raw_pubsub_cloud_event_output["time"] + del raw_pubsub_cloud_event_output.data["message"]["publishTime"] + del cloud_event["time"] + del cloud_event.data["message"]["publishTime"] if "source" in overrides: # Default to the service name, when the topic is not configured subscription's pushEndpoint. - raw_pubsub_cloudevent_output["source"] = overrides["source"] + raw_pubsub_cloud_event_output["source"] = overrides["source"] - assert cloudevent == raw_pubsub_cloudevent_output + assert cloud_event == raw_pubsub_cloud_event_output def test_pubsub_emulator_request_with_invalid_message( - raw_pubsub_request, raw_pubsub_cloudevent_output + raw_pubsub_request, raw_pubsub_cloud_event_output ): # Create an invalid message payload raw_pubsub_request["message"] = None req = flask.Request.from_values(json=raw_pubsub_request, path="/") with pytest.raises(EventConversionException) as exc_info: - cloudevent = event_conversion.background_event_to_cloudevent(req) + cloud_event = event_conversion.background_event_to_cloud_event(req) assert "Failed to convert Pub/Sub payload to event" in exc_info.value.args[0] @@ -449,7 +449,7 @@ def test_pubsub_emulator_request_with_invalid_message( ), ], ) -def test_cloudevent_to_legacy_event( +def test_cloud_event_to_legacy_event( create_ce_headers, ce_event_type, ce_source, @@ -459,7 +459,7 @@ def test_cloudevent_to_legacy_event( headers = create_ce_headers(ce_event_type, ce_source) req = flask.Request.from_values(headers=headers, json={"kind": "value"}) - (res_data, res_context) = event_conversion.cloudevent_to_background_event(req) + (res_data, res_context) = event_conversion.cloud_event_to_background_event(req) assert res_context.event_id == "my-id" assert res_context.timestamp == "2020-08-16T13:58:54.471765" @@ -468,7 +468,7 @@ def test_cloudevent_to_legacy_event( assert res_data == {"kind": "value"} -def test_cloudevent_to_legacy_event_with_pubsub_message_payload( +def test_cloud_event_to_legacy_event_with_pubsub_message_payload( create_ce_headers, ): headers = create_ce_headers( @@ -484,13 +484,13 @@ def test_cloudevent_to_legacy_event_with_pubsub_message_payload( } req = flask.Request.from_values(headers=headers, json=data) - (res_data, res_context) = event_conversion.cloudevent_to_background_event(req) + (res_data, res_context) = event_conversion.cloud_event_to_background_event(req) assert res_context.event_type == "google.pubsub.topic.publish" assert res_data == {"data": "fizzbuzz"} -def test_cloudevent_to_legacy_event_with_firebase_auth_ce( +def test_cloud_event_to_legacy_event_with_firebase_auth_ce( create_ce_headers, ): headers = create_ce_headers( @@ -506,7 +506,7 @@ def test_cloudevent_to_legacy_event_with_firebase_auth_ce( } req = flask.Request.from_values(headers=headers, json=data) - (res_data, res_context) = event_conversion.cloudevent_to_background_event(req) + (res_data, res_context) = event_conversion.cloud_event_to_background_event(req) assert res_context.event_type == "providers/firebase.auth/eventTypes/user.create" assert res_data == { @@ -518,7 +518,7 @@ def test_cloudevent_to_legacy_event_with_firebase_auth_ce( } -def test_cloudevent_to_legacy_event_with_firebase_auth_ce_empty_metadata( +def test_cloud_event_to_legacy_event_with_firebase_auth_ce_empty_metadata( create_ce_headers, ): headers = create_ce_headers( @@ -528,7 +528,7 @@ def test_cloudevent_to_legacy_event_with_firebase_auth_ce_empty_metadata( data = {"metadata": {}, "uid": "my-id"} req = flask.Request.from_values(headers=headers, json=data) - (res_data, res_context) = event_conversion.cloudevent_to_background_event(req) + (res_data, res_context) = event_conversion.cloud_event_to_background_event(req) assert res_context.event_type == "providers/firebase.auth/eventTypes/user.create" assert res_data == data @@ -555,7 +555,7 @@ def test_cloudevent_to_legacy_event_with_firebase_auth_ce_empty_metadata( ), ], ) -def test_cloudevent_to_legacy_event_with_invalid_event( +def test_cloud_event_to_legacy_event_with_invalid_event( create_ce_headers, header_overrides, exception_message, @@ -573,7 +573,7 @@ def test_cloudevent_to_legacy_event_with_invalid_event( req = flask.Request.from_values(headers=headers, json={"some": "val"}) with pytest.raises(EventConversionException) as exc_info: - event_conversion.cloudevent_to_background_event(req) + event_conversion.cloud_event_to_background_event(req) assert exception_message in exc_info.value.args[0] diff --git a/tests/test_data/firebase-auth-cloudevent-output.json b/tests/test_data/firebase-auth-cloud-event-output.json similarity index 100% rename from tests/test_data/firebase-auth-cloudevent-output.json rename to tests/test_data/firebase-auth-cloud-event-output.json diff --git a/tests/test_data/firebase-db-cloudevent-output.json b/tests/test_data/firebase-db-cloud-event-output.json similarity index 100% rename from tests/test_data/firebase-db-cloudevent-output.json rename to tests/test_data/firebase-db-cloud-event-output.json diff --git a/tests/test_data/pubsub_text-cloudevent-output.json b/tests/test_data/pubsub_text-cloud-event-output.json similarity index 100% rename from tests/test_data/pubsub_text-cloudevent-output.json rename to tests/test_data/pubsub_text-cloud-event-output.json diff --git a/tests/test_decorator_functions.py b/tests/test_decorator_functions.py index b90003cf..e8c9bc70 100644 --- a/tests/test_decorator_functions.py +++ b/tests/test_decorator_functions.py @@ -29,9 +29,9 @@ @pytest.fixture -def cloudevent_decorator_client(): +def cloud_event_decorator_client(): source = TEST_FUNCTIONS_DIR / "decorators" / "decorator.py" - target = "function_cloudevent" + target = "function_cloud_event" return create_app(target, source).test_client() @@ -43,21 +43,21 @@ def http_decorator_client(): @pytest.fixture -def cloudevent_1_0(): +def cloud_event_1_0(): attributes = { "specversion": "1.0", "id": "my-id", "source": "from-galaxy-far-far-away", - "type": "cloudevent.greet.you", + "type": "cloud_event.greet.you", "time": "2020-08-16T13:58:54.471765", } data = {"name": "john"} return CloudEvent(attributes, data) -def test_cloudevent_decorator(cloudevent_decorator_client, cloudevent_1_0): - headers, data = to_structured(cloudevent_1_0) - resp = cloudevent_decorator_client.post("/", headers=headers, data=data) +def test_cloud_event_decorator(cloud_event_decorator_client, cloud_event_1_0): + headers, data = to_structured(cloud_event_1_0) + resp = cloud_event_decorator_client.post("/", headers=headers, data=data) assert resp.status_code == 200 assert resp.data == b"OK" diff --git a/tests/test_functions/cloudevents/converted_background_event.py b/tests/test_functions/cloud_events/converted_background_event.py similarity index 78% rename from tests/test_functions/cloudevents/converted_background_event.py rename to tests/test_functions/cloud_events/converted_background_event.py index 44b69556..9264251d 100644 --- a/tests/test_functions/cloudevents/converted_background_event.py +++ b/tests/test_functions/cloud_events/converted_background_event.py @@ -16,13 +16,13 @@ import flask -def function(cloudevent): +def function(cloud_event): """Test event function that checks to see if a valid CloudEvent was sent. The function returns 200 if it received the expected event, otherwise 500. Args: - cloudevent: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. + cloud_event: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. Returns: HTTP status code indicating whether valid event was sent or not. @@ -41,12 +41,12 @@ def function(cloudevent): } valid_event = ( - cloudevent["id"] == "aaaaaa-1111-bbbb-2222-cccccccccccc" - and cloudevent.data == data - and cloudevent["source"] + cloud_event["id"] == "aaaaaa-1111-bbbb-2222-cccccccccccc" + and cloud_event.data == data + and cloud_event["source"] == "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" - and cloudevent["type"] == "google.cloud.pubsub.topic.v1.messagePublished" - and cloudevent["time"] == "2020-09-29T11:32:00.000Z" + and cloud_event["type"] == "google.cloud.pubsub.topic.v1.messagePublished" + and cloud_event["time"] == "2020-09-29T11:32:00.000Z" ) if not valid_event: diff --git a/tests/test_functions/cloudevents/empty_data.py b/tests/test_functions/cloud_events/empty_data.py similarity index 78% rename from tests/test_functions/cloudevents/empty_data.py rename to tests/test_functions/cloud_events/empty_data.py index c1000265..d9209667 100644 --- a/tests/test_functions/cloudevents/empty_data.py +++ b/tests/test_functions/cloud_events/empty_data.py @@ -16,13 +16,13 @@ import flask -def function(cloudevent): +def function(cloud_event): """Test Event function that checks to see if a valid CloudEvent was sent. The function returns 200 if it received the expected event, otherwise 500. Args: - cloudevent: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. + cloud_event: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. Returns: HTTP status code indicating whether valid event was sent or not. @@ -30,9 +30,9 @@ def function(cloudevent): """ valid_event = ( - cloudevent["id"] == "my-id" - and cloudevent["source"] == "from-galaxy-far-far-away" - and cloudevent["type"] == "cloudevent.greet.you" + cloud_event["id"] == "my-id" + and cloud_event["source"] == "from-galaxy-far-far-away" + and cloud_event["type"] == "cloud_event.greet.you" ) if not valid_event: diff --git a/tests/test_functions/cloudevents/main.py b/tests/test_functions/cloud_events/main.py similarity index 71% rename from tests/test_functions/cloudevents/main.py rename to tests/test_functions/cloud_events/main.py index aa480840..739d2a9d 100644 --- a/tests/test_functions/cloudevents/main.py +++ b/tests/test_functions/cloud_events/main.py @@ -16,24 +16,24 @@ import flask -def function(cloudevent): +def function(cloud_event): """Test Event function that checks to see if a valid CloudEvent was sent. The function returns 200 if it received the expected event, otherwise 500. Args: - cloudevent: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. + cloud_event: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. Returns: HTTP status code indicating whether valid event was sent or not. """ valid_event = ( - cloudevent["id"] == "my-id" - and cloudevent.data == {"name": "john"} - and cloudevent["source"] == "from-galaxy-far-far-away" - and cloudevent["type"] == "cloudevent.greet.you" - and cloudevent["time"] == "2020-08-16T13:58:54.471765" + cloud_event["id"] == "my-id" + and cloud_event.data == {"name": "john"} + and cloud_event["source"] == "from-galaxy-far-far-away" + and cloud_event["type"] == "cloud_event.greet.you" + and cloud_event["time"] == "2020-08-16T13:58:54.471765" ) if not valid_event: diff --git a/tests/test_functions/decorators/decorator.py b/tests/test_functions/decorators/decorator.py index 0c32423c..3aae119d 100644 --- a/tests/test_functions/decorators/decorator.py +++ b/tests/test_functions/decorators/decorator.py @@ -18,25 +18,25 @@ import functions_framework -@functions_framework.cloudevent -def function_cloudevent(cloudevent): +@functions_framework.cloud_event +def function_cloud_event(cloud_event): """Test Event function that checks to see if a valid CloudEvent was sent. The function returns 200 if it received the expected event, otherwise 500. Args: - cloudevent: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. + cloud_event: A CloudEvent as defined by https://github.com/cloudevents/sdk-python. Returns: HTTP status code indicating whether valid event was sent or not. """ valid_event = ( - cloudevent["id"] == "my-id" - and cloudevent.data == {"name": "john"} - and cloudevent["source"] == "from-galaxy-far-far-away" - and cloudevent["type"] == "cloudevent.greet.you" - and cloudevent["time"] == "2020-08-16T13:58:54.471765" + cloud_event["id"] == "my-id" + and cloud_event.data == {"name": "john"} + and cloud_event["source"] == "from-galaxy-far-far-away" + and cloud_event["type"] == "cloud_event.greet.you" + and cloud_event["time"] == "2020-08-16T13:58:54.471765" ) if not valid_event: diff --git a/tests/test_view_functions.py b/tests/test_view_functions.py index 592c8300..219313f9 100644 --- a/tests/test_view_functions.py +++ b/tests/test_view_functions.py @@ -63,12 +63,12 @@ def test_event_view_func_wrapper(monkeypatch): ] -def test_run_cloudevent(): +def test_run_cloud_event(): headers = {"Content-Type": "application/cloudevents+json"} data = json.dumps( { "source": "from-galaxy-far-far-away", - "type": "cloudevent.greet.you", + "type": "cloud_event.greet.you", "specversion": "1.0", "id": "f6a65fcd-eed2-429d-9f71-ec0663d83025", "time": "2020-08-13T02:12:14.946587+00:00", @@ -77,19 +77,19 @@ def test_run_cloudevent(): ) request = pretend.stub(headers=headers, get_data=lambda: data) - function = pretend.call_recorder(lambda cloudevent: "hello") - functions_framework._run_cloudevent(function, request) - expected_cloudevent = from_http(request.headers, request.get_data()) + function = pretend.call_recorder(lambda cloud_event: "hello") + functions_framework._run_cloud_event(function, request) + expected_cloud_event = from_http(request.headers, request.get_data()) - assert function.calls == [pretend.call(expected_cloudevent)] + assert function.calls == [pretend.call(expected_cloud_event)] -def test_cloudevent_view_func_wrapper(): +def test_cloud_event_view_func_wrapper(): headers = {"Content-Type": "application/cloudevents+json"} data = json.dumps( { "source": "from-galaxy-far-far-away", - "type": "cloudevent.greet.you", + "type": "cloud_event.greet.you", "specversion": "1.0", "id": "f6a65fcd-eed2-429d-9f71-ec0663d83025", "time": "2020-08-13T02:12:14.946587+00:00", @@ -100,19 +100,19 @@ def test_cloudevent_view_func_wrapper(): request = pretend.stub(headers=headers, get_data=lambda: data) event = from_http(request.headers, request.get_data()) - function = pretend.call_recorder(lambda cloudevent: cloudevent) + function = pretend.call_recorder(lambda cloud_event: cloud_event) - view_func = functions_framework._cloudevent_view_func_wrapper(function, request) + view_func = functions_framework._cloud_event_view_func_wrapper(function, request) view_func("/some/path") assert function.calls == [pretend.call(event)] -def test_binary_cloudevent_view_func_wrapper(): +def test_binary_cloud_event_view_func_wrapper(): headers = { "ce-specversion": "1.0", "ce-source": "from-galaxy-far-far-away", - "ce-type": "cloudevent.greet.you", + "ce-type": "cloud_event.greet.you", "ce-id": "f6a65fcd-eed2-429d-9f71-ec0663d83025", "ce-time": "2020-08-13T02:12:14.946587+00:00", } @@ -121,9 +121,9 @@ def test_binary_cloudevent_view_func_wrapper(): request = pretend.stub(headers=headers, get_data=lambda: data) event = from_http(request.headers, request.get_data()) - function = pretend.call_recorder(lambda cloudevent: cloudevent) + function = pretend.call_recorder(lambda cloud_event: cloud_event) - view_func = functions_framework._cloudevent_view_func_wrapper(function, request) + view_func = functions_framework._cloud_event_view_func_wrapper(function, request) view_func("/some/path") assert function.calls == [pretend.call(event)]