Skip to content

Commit a68245f

Browse files
author
Ace Nassri
authored
Add Firestore reactive sample (GoogleCloudPlatform#1739)
* Add Firestore reactive sample Change-Id: I8ae1d62356927dca9cf62ff7fdf0f887550ec6ae * Fix merge lint Change-Id: Iaf6f448115f28060514016ee96a9144fabc9d824
1 parent abc419a commit a68245f

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

functions/firebase/main.py

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
# [END functions_firebase_firestore]
2525
# [END functions_firebase_auth]
2626

27+
# [START functions_firebase_reactive]
28+
from google.cloud import firestore
29+
# [END functions_firebase_reactive]
30+
2731

2832
# [START functions_firebase_rtdb]
2933
def hello_rtdb(data, context):
@@ -75,6 +79,28 @@ def hello_auth(data, context):
7579
# [END functions_firebase_auth]
7680

7781

82+
# [START functions_firebase_reactive]
83+
client = firestore.Client()
84+
85+
86+
# Converts strings added to /messages/{pushId}/original to uppercase
87+
def make_upper_case(data, context):
88+
path_parts = context.resource.split('/documents/')[1].split('/')
89+
collection_path = path_parts[0]
90+
document_path = '/'.join(path_parts[1:])
91+
92+
affected_doc = client.collection(collection_path).document(document_path)
93+
94+
cur_value = data["value"]["fields"]["original"]["stringValue"]
95+
new_value = cur_value.upper()
96+
print(f'Replacing value: {cur_value} --> {new_value}')
97+
98+
affected_doc.set({
99+
u'original': new_value
100+
})
101+
# [END functions_firebase_reactive]
102+
103+
78104
# [START functions_firebase_analytics]
79105
def hello_analytics(data, context):
80106
print(data)

functions/firebase/main_test.py

+41
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from collections import UserDict
1516
from datetime import datetime
1617
import json
18+
import uuid
19+
20+
from mock import MagicMock, patch
1721

1822
import main
1923

@@ -76,6 +80,43 @@ def test_auth(capsys):
7680
assert 'Email: [email protected]' in out
7781

7882

83+
@patch('main.client')
84+
def test_make_upper_case(firestore_mock, capsys):
85+
86+
firestore_mock.collection = MagicMock(return_value=firestore_mock)
87+
firestore_mock.document = MagicMock(return_value=firestore_mock)
88+
firestore_mock.set = MagicMock(return_value=firestore_mock)
89+
90+
user_id = str(uuid.uuid4())
91+
date_string = datetime.now().isoformat()
92+
email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4())
93+
94+
data = {
95+
'uid': user_id,
96+
'metadata': {'createdAt': date_string},
97+
'email': email_string,
98+
'value': {
99+
'fields': {
100+
'original': {
101+
'stringValue': 'foobar'
102+
}
103+
}
104+
}
105+
}
106+
107+
context = UserDict()
108+
context.resource = '/documents/some_collection/path/some/path'
109+
110+
main.make_upper_case(data, context)
111+
112+
out, _ = capsys.readouterr()
113+
114+
assert 'Replacing value: foobar --> FOOBAR' in out
115+
firestore_mock.collection.assert_called_with('some_collection')
116+
firestore_mock.document.assert_called_with('path/some/path')
117+
firestore_mock.set.assert_called_with({'original': 'FOOBAR'})
118+
119+
79120
def test_analytics(capsys):
80121
timestamp = int(datetime.utcnow().timestamp())
81122

functions/firebase/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-firestore==0.29.0

0 commit comments

Comments
 (0)