Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions functions/v2/firebase/upper-firestore/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START functions_cloudevent_firebase_reactive]
from cloudevents.http import CloudEvent
import functions_framework
from google.cloud import firestore
from google.events.cloud import firestore as firestoredata

client = firestore.Client()


# Converts strings added to /messages/{pushId}/original to uppercase
@functions_framework.cloud_event
def make_upper_case(cloud_event: CloudEvent) -> None:
firestore_payload = firestoredata.DocumentEventData()
firestore_payload._pb.ParseFromString(cloud_event.data)

path_parts = firestore_payload.value.name.split("/")
separator_idx = path_parts.index("documents")
collection_path = path_parts[separator_idx + 1]
document_path = "/".join(path_parts[(separator_idx + 2) :])

print(f"Collection path: {collection_path}")
print(f"Document path: {document_path}")

affected_doc = client.collection(collection_path).document(document_path)

cur_value = firestore_payload.value.fields["original"].string_value
new_value = cur_value.upper()

if cur_value != new_value:
print(f"Replacing value: {cur_value} --> {new_value}")
affected_doc.set({"original": new_value})
else:
# Value is already upper-case
# Don't perform a second write (which can trigger an infinite loop)
print("Value is already upper-case.")


# [END functions_cloudevent_firebase_reactive]
63 changes: 63 additions & 0 deletions functions/v2/firebase/upper-firestore/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import patch

from cloudevents.http import CloudEvent
from google.events.cloud import firestore as firestoredata

import main


def make_event(string_value, collection_id, document_path):
firestore_payload = firestoredata.DocumentEventData()
firestore_payload.value = firestoredata.Document()
firestore_payload.value.name = f"/projects/test-project/databases/(default)/documents/{collection_id}/{document_path}"
field_value = firestoredata.Value()
field_value.string_value = string_value
firestore_payload.value.fields = {"original": field_value}

attributes = {
"id": "5e9f24a",
"type": "google.cloud.firestore.document.v1.written",
"source": "/projects/test-project/databases/(default)",
}
data = firestore_payload._pb.SerializeToString()
cloud_event = CloudEvent(attributes, data)

return cloud_event


def test_make_upper_case(capsys):
with patch("main.client"):
event = make_event("foo", "test-collection", "test-doc")

main.make_upper_case(event)

out, _ = capsys.readouterr()
assert "Collection path: test-collection" in out
assert "Document path: test-doc" in out
assert "Replacing value: foo --> FOO" in out


def test_make_upper_case_skips_if_upper(capsys):
with patch("main.client"):
event = make_event("FOO", "foods", "tacos/vegan")

main.make_upper_case(event)

out, _ = capsys.readouterr()
assert "Collection path: foods" in out
assert "Document path: tacos/vegan" in out
assert "Value is already upper-case." in out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.0.1
6 changes: 6 additions & 0 deletions functions/v2/firebase/upper-firestore/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
functions-framework==3.3.0
google-events==0.4.0
google-api-core==2.11.0
protobuf==4.22.1
google-cloud-firestore==2.10.1
cloudevents==1.9.0