Skip to content

Commit fdfd89f

Browse files
author
Kenneth Rosario
authored
chore(functions/v2/firebase/upper-firestore): Make upper firestore sample gcf gen 2 comaptible (GoogleCloudPlatform#9412)
1 parent 636a4ac commit fdfd89f

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_cloudevent_firebase_reactive]
16+
from cloudevents.http import CloudEvent
17+
import functions_framework
18+
from google.cloud import firestore
19+
from google.events.cloud import firestore as firestoredata
20+
21+
client = firestore.Client()
22+
23+
24+
# Converts strings added to /messages/{pushId}/original to uppercase
25+
@functions_framework.cloud_event
26+
def make_upper_case(cloud_event: CloudEvent) -> None:
27+
firestore_payload = firestoredata.DocumentEventData()
28+
firestore_payload._pb.ParseFromString(cloud_event.data)
29+
30+
path_parts = firestore_payload.value.name.split("/")
31+
separator_idx = path_parts.index("documents")
32+
collection_path = path_parts[separator_idx + 1]
33+
document_path = "/".join(path_parts[(separator_idx + 2) :])
34+
35+
print(f"Collection path: {collection_path}")
36+
print(f"Document path: {document_path}")
37+
38+
affected_doc = client.collection(collection_path).document(document_path)
39+
40+
cur_value = firestore_payload.value.fields["original"].string_value
41+
new_value = cur_value.upper()
42+
43+
if cur_value != new_value:
44+
print(f"Replacing value: {cur_value} --> {new_value}")
45+
affected_doc.set({"original": new_value})
46+
else:
47+
# Value is already upper-case
48+
# Don't perform a second write (which can trigger an infinite loop)
49+
print("Value is already upper-case.")
50+
51+
52+
# [END functions_cloudevent_firebase_reactive]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest.mock import patch
16+
17+
from cloudevents.http import CloudEvent
18+
from google.events.cloud import firestore as firestoredata
19+
20+
import main
21+
22+
23+
def make_event(string_value, collection_id, document_path):
24+
firestore_payload = firestoredata.DocumentEventData()
25+
firestore_payload.value = firestoredata.Document()
26+
firestore_payload.value.name = f"/projects/test-project/databases/(default)/documents/{collection_id}/{document_path}"
27+
field_value = firestoredata.Value()
28+
field_value.string_value = string_value
29+
firestore_payload.value.fields = {"original": field_value}
30+
31+
attributes = {
32+
"id": "5e9f24a",
33+
"type": "google.cloud.firestore.document.v1.written",
34+
"source": "/projects/test-project/databases/(default)",
35+
}
36+
data = firestore_payload._pb.SerializeToString()
37+
cloud_event = CloudEvent(attributes, data)
38+
39+
return cloud_event
40+
41+
42+
def test_make_upper_case(capsys):
43+
with patch("main.client"):
44+
event = make_event("foo", "test-collection", "test-doc")
45+
46+
main.make_upper_case(event)
47+
48+
out, _ = capsys.readouterr()
49+
assert "Collection path: test-collection" in out
50+
assert "Document path: test-doc" in out
51+
assert "Replacing value: foo --> FOO" in out
52+
53+
54+
def test_make_upper_case_skips_if_upper(capsys):
55+
with patch("main.client"):
56+
event = make_event("FOO", "foods", "tacos/vegan")
57+
58+
main.make_upper_case(event)
59+
60+
out, _ = capsys.readouterr()
61+
assert "Collection path: foods" in out
62+
assert "Document path: tacos/vegan" in out
63+
assert "Value is already upper-case." in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==7.0.1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
functions-framework==3.3.0
2+
google-events==0.4.0
3+
google-api-core==2.11.0
4+
protobuf==4.22.1
5+
google-cloud-firestore==2.10.1
6+
cloudevents==1.9.0

0 commit comments

Comments
 (0)