From ae05ad0d4a26c1d256a06148381c0a53f7d10ed5 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 5 Oct 2018 19:48:45 -0700 Subject: [PATCH 1/2] Add Firestore reactive sample Change-Id: I8ae1d62356927dca9cf62ff7fdf0f887550ec6ae --- functions/firebase/main.py | 26 ++++++++++++++++++ functions/firebase/main_test.py | 41 ++++++++++++++++++++++++++++- functions/firebase/requirements.txt | 1 + 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 functions/firebase/requirements.txt diff --git a/functions/firebase/main.py b/functions/firebase/main.py index b0f79e76881..b137ce72a82 100644 --- a/functions/firebase/main.py +++ b/functions/firebase/main.py @@ -20,6 +20,10 @@ # [END functions_firebase_firestore] # [END functions_firebase_auth] +# [START functions_firebase_reactive] +from google.cloud import firestore +# [END functions_firebase_reactive] + # [START functions_firebase_rtdb] def hello_rtdb(data, context): @@ -69,3 +73,25 @@ def hello_auth(data, context): if 'email' in data: print('Email: %s' % data["email"]) # [END functions_firebase_auth] + + +# [START functions_firebase_reactive] +client = firestore.Client() + + +# Converts strings added to /messages/{pushId}/original to uppercase +def make_upper_case(data, context): + path_parts = context.resource.split('/documents/')[1].split('/') + collection_path = path_parts[0] + document_path = '/'.join(path_parts[1:]) + + affected_doc = client.collection(collection_path).document(document_path) + + cur_value = data["value"]["fields"]["original"]["stringValue"] + new_value = cur_value.upper() + print(f'Replacing value: {cur_value} --> {new_value}') + + affected_doc.set({ + u'original': new_value + }) +# [END functions_firebase_reactive] diff --git a/functions/firebase/main_test.py b/functions/firebase/main_test.py index df30af46134..61ddc08c046 100644 --- a/functions/firebase/main_test.py +++ b/functions/firebase/main_test.py @@ -12,11 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +from collections import UserDict from datetime import datetime import json - import uuid +from mock import MagicMock, patch + import main @@ -83,3 +85,40 @@ def test_auth(capsys): assert user_id in out assert date_string in out assert email_string in out + + +@patch('main.client') +def test_make_upper_case(firestore_mock, capsys): + + firestore_mock.collection = MagicMock(return_value=firestore_mock) + firestore_mock.document = MagicMock(return_value=firestore_mock) + firestore_mock.set = MagicMock(return_value=firestore_mock) + + user_id = str(uuid.uuid4()) + date_string = datetime.now().isoformat() + email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4()) + + data = { + 'uid': user_id, + 'metadata': {'createdAt': date_string}, + 'email': email_string, + 'value': { + 'fields': { + 'original': { + 'stringValue': 'foobar' + } + } + } + } + + context = UserDict() + context.resource = '/documents/some_collection/path/some/path' + + main.make_upper_case(data, context) + + out, _ = capsys.readouterr() + + assert 'Replacing value: foobar --> FOOBAR' in out + firestore_mock.collection.assert_called_with('some_collection') + firestore_mock.document.assert_called_with('path/some/path') + firestore_mock.set.assert_called_with({'original': 'FOOBAR'}) diff --git a/functions/firebase/requirements.txt b/functions/firebase/requirements.txt new file mode 100644 index 00000000000..dac97c81fed --- /dev/null +++ b/functions/firebase/requirements.txt @@ -0,0 +1 @@ +google-cloud-firestore==0.29.0 From 730a253cc5bcdc7938cd5fbd14d685b2b52a6a85 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 15 Oct 2018 12:07:42 -0700 Subject: [PATCH 2/2] Fix merge lint Change-Id: Iaf6f448115f28060514016ee96a9144fabc9d824 --- functions/firebase/main_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/firebase/main_test.py b/functions/firebase/main_test.py index 4c1e9fd8a94..d4404834a1e 100644 --- a/functions/firebase/main_test.py +++ b/functions/firebase/main_test.py @@ -114,7 +114,7 @@ def test_make_upper_case(firestore_mock, capsys): assert 'Replacing value: foobar --> FOOBAR' in out firestore_mock.collection.assert_called_with('some_collection') firestore_mock.document.assert_called_with('path/some/path') - firestore_mock.set.assert_called_with({'original': 'FOOBAR'}) + firestore_mock.set.assert_called_with({'original': 'FOOBAR'}) def test_analytics(capsys):